OpenGL Android




CS185c

Chris Pollett

Nov. 4, 2009

Outline

OpenGL

An OpenGL Project

More OpenGL Project rest of Activity File

Threads in OpenGL

The GLThread class

Drawing a Frame

Initializing OpenGL options

Draw a Frame

Exciting Screenshot

Screenshot of OpenGL code running -- just a blank screen in this example

A Cube Example

Code for GLCube Class

class GLCube 
{
   private final IntBuffer mVertexBuffer;
	
   GLCube()
   {
      int one = 65536;
      int half = one/2;
		
      int vertices[] = 
      {
         //Front
         -half, -half, half, half, -half, half,
         -half, half, half, half, half, half,
         //Back
         -half, -half, -half, -half, half, -half,
         half, -half, -half, half, half, -half,
         //Left
         -half, -half, half, -half, half, half,
         -half, -half, -half, -half, half, -half,
         //Right
         half, -half, -half, half, half, -half,
         half, -half, half, half, half, half,
         //Top
         -half, half, half, half, half, half,
         -half, half, -half, half, half, -half,
         //Bottom
         -half, -half, half, -half, -half, -half,
         half, -half, half, half, -half, -half
      };
		
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
      vbb.order(ByteOrder.nativeOrder());
      mVertexBuffer = vbb.asIntBuffer();
      mVertexBuffer.put(vertices);
      mVertexBuffer.position(0);
  }

   void draw(GL10 gl)
   {
      gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
		
      gl.glColor4f(1, 1, 1, 1);
      gl.glNormal3f(0, 0, 1);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
      gl.glNormal3f(0, 0, -1);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
		
      gl.glColor4f(1, 1, 1, 1);
      gl.glNormal3f(-1, 0, 0);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
      gl.glNormal3f(1, 0, 0);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
      
      gl.glColor4f(1, 1, 1, 1);
      gl.glNormal3f(0, 1, 0);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
      gl.glNormal3f(0, -1, 0);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);

   }
}

Putting it together with the previous code

Exciting Screenshot II

Screenshot of OpenGL code running -- can see one face of a cube