OpenGL: a simple render works on emulator, fails on hw?


Last Updated:

  1. Reuben

    Reuben New Member This Topic's Starter

    Joined:
    Oct 31, 2009
    Messages:
    1
    Likes Received:
    0
    I am migrating the graphics code of a 2D game from android.graphics.* to OpenGL ES.

    So far so good... it looks okay in the emulator, but fails to draw anything at all on hardware. I have boiled the Renderer code down to the below, which simply draws a red square on the screen... it works great in the emulator, fails to render on hardware.

    Code (Text):
    1.  
    2. public class FooRenderer implements Renderer{
    3.  
    4.     private ShortBuffer mIndexBuffer;
    5.     private FloatBuffer mVertexBuffer;
    6.  
    7.     // Constructor
    8.     public FooRenderer() {
    9.         super();
    10.  
    11.         // Allocate a buffer to hold 4 vertexes
    12.         ByteBuffer vbb = ByteBuffer.allocateDirect(4 * 3 * 4);
    13.         vbb.order(ByteOrder.nativeOrder());
    14.         mVertexBuffer = vbb.asFloatBuffer();
    15.        
    16.         // Initialize array of vertex indexes : {0,1,2,3}
    17.         ByteBuffer ibb = ByteBuffer.allocateDirect(4 * 2);
    18.         ibb.order(ByteOrder.nativeOrder());
    19.         mIndexBuffer = ibb.asShortBuffer();        
    20.         for(int i = 0; i < 4; i++) {
    21.             mIndexBuffer.put((short) i);
    22.         }
    23.         mIndexBuffer.position(0);  
    24.     }
    25.  
    26.     static private void vertex2Dput(FloatBuffer fb, float x, float y) {
    27.         fb.put(x);
    28.         fb.put(y);
    29.         fb.put(1.0f);
    30.     }
    31.     void setQuad(int x1, int y1, int x2, int y2) {
    32.         mVertexBuffer.position(0);
    33.         vertex2Dput(mVertexBuffer, x1, y1);
    34.         vertex2Dput(mVertexBuffer, x2, y1);
    35.         vertex2Dput(mVertexBuffer, x1, y2);
    36.         vertex2Dput(mVertexBuffer, x2, y2);
    37.         mVertexBuffer.position(0);
    38.     }
    39.    
    40.     @Override public void onDrawFrame(GL10 gl) {
    41.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    42.         gl.glMatrixMode(GL10.GL_MODELVIEW);
    43.         gl.glLoadIdentity();
    44.         gl.glColor4f(1f, 0f,0f, 1f);
    45.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
    46.         gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
    47.     }
    48.    
    49.     @Override public void onSurfaceChanged(GL10 gl, int width, int height) {
    50.         gl.glViewport(0, 0, width, height);
    51.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    52.         gl.glMatrixMode(GL10.GL_PROJECTION);
    53.         gl.glLoadIdentity();
    54.         gl.glOrthof(0.0f, width, height, 0, 0.8f, 2.0f);
    55.         setQuad(100,100,200,200);
    56.     }
    57.    
    58.     @Override public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
    59.     }  
    60. }
    61.  
    Comparing it with SDK sample code which *does* work on hardware, it seems the only thing I do differently is use an orthographic projection rather than a perspective projection.

    Can anyone familiar with OpenGL on Android advise as to what I've done wrong?
     

    Advertisement

Share This Page

Loading...