Pages

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!

6 comments:

  1. Can you explain the things under the hood - meaning how the SurfaceFlinger will take care of these gl calls?

    ReplyDelete
  2. @Kinjal, ideally you should answer this question! But if I understand correctly, SurfaceFlinger creates a new Surface backed by two buffers in PMEM so that gl can directly work on this memory.

    ReplyDelete
  3. Is supporting OpenGL or OpenGL ES? Also any plans in pipeline for supporting 2.0?

    ReplyDelete
  4. @Zaphod It specifically supports OpenGL ES. This example was to show it's possible to have graphics without going through EGL which is required for ES

    Android supports hardware binding for 2.0 since donut release and there are some efforts to support software renderer too. They already have a class http://developer.android.com/reference/android/opengl/GLES20.html
    but I haven't tried yet and definitely not programmable shader capability!

    ReplyDelete
  5. Cool just look through the reference, so they are expecting/implementing shader compiler and optimizer on phone hardware interesting! Bit different from D3D approach and also from IPhone approach as Cupertino has control over HW hence can always compile shaders offline.

    ReplyDelete
  6. Interesting ... thanks for the insight :)

    ReplyDelete