OpenGL Android




CS185c

Chris Pollett

Apr. 9, 2012

Outline

OpenGL

An OpenGL 1.1 Project

More OpenGL Project rest of Activity File

Threads in OpenGL

The GLThread class

Quiz

Which of the following is true?

  1. The ShapeDrawable class has an onDraw method which is passed an Activity to draw on.
  2. Android TableRow's are only allowed to have TableCell's on themselves.
  3. In $.ajax(obj), obj is a Javascript object that specifies how one wants to open a connection back to the host the script is from.

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