• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

Reuben

Lurker
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:
public class FooRenderer implements Renderer{

    private ShortBuffer mIndexBuffer;
    private FloatBuffer mVertexBuffer;

    // Constructor
    public FooRenderer() {
        super();

        // Allocate a buffer to hold 4 vertexes
        ByteBuffer vbb = ByteBuffer.allocateDirect(4 * 3 * 4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        
        // Initialize array of vertex indexes : {0,1,2,3}
        ByteBuffer ibb = ByteBuffer.allocateDirect(4 * 2);
        ibb.order(ByteOrder.nativeOrder());
        mIndexBuffer = ibb.asShortBuffer();        
        for(int i = 0; i < 4; i++) {
            mIndexBuffer.put((short) i);
        }
        mIndexBuffer.position(0);	
    }

    static private void vertex2Dput(FloatBuffer fb, float x, float y) {
        fb.put(x);
        fb.put(y);
        fb.put(1.0f);
    }
    void setQuad(int x1, int y1, int x2, int y2) {
        mVertexBuffer.position(0);
        vertex2Dput(mVertexBuffer, x1, y1);
        vertex2Dput(mVertexBuffer, x2, y1);
        vertex2Dput(mVertexBuffer, x1, y2);
        vertex2Dput(mVertexBuffer, x2, y2);
        mVertexBuffer.position(0);
    }
	
    @Override public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glColor4f(1f, 0f,0f, 1f);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);	
    }
	
    @Override public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(0.0f, width, height, 0, 0.8f, 2.0f);
        setQuad(100,100,200,200);
    }
	
    @Override public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
    }	
}

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?
 
Back
Top Bottom