Pages

Showing posts with label OpenGL. Show all posts
Showing posts with label OpenGL. Show all posts

Sunday, October 3, 2010

OpenGL For Android - GLSurfaceView

One of the fascinating features of Android platform is its support for OpenGL. OpenGL is a cross-platform 2D and 3D rendering graphics library. An industry consortium, called Khronos Group maintains the OpenGL specification. http://www.khronos.org/opengl/

OpenGL uses C-based flat APIs and hence we need to have a Java language binding to be able to use it with Android Application framework. Java ME has already defined this binding with JSR 231. (Java binding for OpenGL 1.5). What that means for you is - 
import javax.microedition.khronos.opengles.GL10;


Let's get started with the code. 
The main class we use is GLSurfaceView which extends from SurfaceView class. GLSurfaceView
  • Provides a Surface onto which we can render OpenGL scenes
  • Binds with customized Renderer which performs the actual drawing
  • Manages EGL display 
  • Uses a dedicated GUI thread, decouples the main application thread for user interactions
We need to call one method on its object i.e setRenderer() which registers a user specified Renderer object that knows how to draw the desired shapes. 
Activity's onCreate() will look like this - 
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
   GLSurfaceView view = new GLSurfaceView(this);
     view.setRenderer(new SimpleRenderer());
     setContentView(view);
    }
The next thing we need is SimpleRenderer which will be responsible for actual OpenGL rendering. It implements GLSurfaceView.Renderer interface and defines its three methods.



public abstract void onSurfaceCreated (GL10 gl, EGLConfig config)

This method is called at the very beginning e.g when a Surface is created or recreated ( when phone awakes after sleep), it's a good place for all initialization code. 

A typical initialization sequence - 
@Override
 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  // Set the background color to black (RGBA).
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
                // enable smooth shading
  gl.glShadeModel(GL10.GL_SMOOTH);
         // set up depth buffer and enable depth testing
  gl.glClearDepthf(1.0f);
  gl.glEnable(GL10.GL_DEPTH_TEST);
  gl.glDepthFunc(GL10.GL_LEQUAL);
  // use nice perspective calculations.
  gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
  
 }


public abstract void onSurfaceChanged (GL10 gl, int width, int height)

This method is called when the Surface size changes. The size (width, height) affects the aspect ratio so this function should contain the code that depends upon aspect ratio such as GLU.gluPerspective() . A sample code - 
@Override
 public void onSurfaceChanged(GL10 gl, int width, int height) {  
  gl.glViewport(0, 0, width, height);
  gl.glMatrixMode(GL10.GL_PROJECTION);
  gl.glLoadIdentity();
  GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
    100.0f);
  gl.glMatrixMode(GL10.GL_MODELVIEW);
  gl.glLoadIdentity();
  
 }





public abstract void onDrawFrame (GL10 gl)

This method is called when the current frame is being drawn on the Surface and thus actual drawing code goes here. e.g 


@Override
 public void onDrawFrame(GL10 gl) {
  // Clears the color and depth buffer. -- clears the framebuffer
  gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
  gl.glLoadIdentity();
                gl.glTranslatef(0, 0, -5); 
  // Draw our square.
  /* square.draw() */
  
 }
This code will create a blank frame with black color. We will see the actual drawing code in the part 2. So stay tuned!