OpenGL Android




CS185c

Chris Pollett

Nov. 3, 2010

Outline

A Second Android Animation example

package org.pollett;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class AnimationTest3 extends Activity {

	protected static final int GUIUPDATEID = 0x1FF;
	AnimRunnable animRunnable;
	GraphicsTestView myGraphicsView;
	
	Handler myGUIUpdateHandler = new Handler() {
		public void handleMessage(Message msg) 
		{			
			switch(msg.what) 
			{
				case AnimationTest3.GUIUPDATEID:
					myGraphicsView.invalidate();
				break;
			}
		}
	};
 
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        myGraphicsView = new GraphicsTestView(this);
        setContentView(myGraphicsView);
        
        new Thread(new AnimRunnable(this)).start();
    }
    
    private static class AnimRunnable implements Runnable
    {
    	AnimationTest3 myAnimation;
    	public AnimRunnable(AnimationTest3 myAnim)
    	{
    		myAnimation = myAnim;
    	}
		public void run() 
		{
                   while(!Thread.currentThread().isInterrupted()) 
                   {
                      Message message = new Message();
                      message.what = AnimationTest3.GUIUPDATEID;
                      myAnimation.myGUIUpdateHandler.sendMessage(message);

                      try
                      {
                         Thread.sleep(100);
                    
                      } 
                      catch(InterruptedException e)
                      {
                         Thread.currentThread().interrupt();
                      }
                   }
		}
    	
    }
    private static class GraphicsTestView extends View
    {
    	private ShapeDrawable myDrawable;
    	int ovalX;
    	int ovalY;
    	int direction;
    	
    	public GraphicsTestView (Context context) 
    	{
    	   super(context);
    	   setFocusable(true);

	       OvalShape oval = new OvalShape();
	       myDrawable = new ShapeDrawable(oval);
    	   myDrawable.getPaint().setColor(0xFFFF0000);
    	   ovalX = 10;
    	   ovalY = 10;
    	   direction = 5;
    	}
    
	    @Override
	    protected void onDraw(Canvas canvas)
	    // the onDraw method is where a view draws itself
	    {
	    	int width = 50;
	    	int height = 20;
	    	myDrawable.setBounds(ovalX, ovalY, ovalX+ width, ovalY + height);
	
	    	myDrawable.draw(canvas);
	    	if(ovalX < 10 || ovalX > 250) 
	    	{
	    		direction = -direction;
	    	}
	    	ovalX +=direction;
	    }
    }
    
}

OpenGL

An OpenGL 1.1 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