Search code examples
androidopengl-esrenderingfixed-point

Ortho Projection issues with OpenGL ES


I am working in Android using Open GL ES. I have been trying to move our UI from Android Views into 3D. I am seeing some odd behavior with the vertex placements.

I have two different Quads. One is -0.5,-0.5 -> 0.5,0.5 and the other is 0,0 -> 1,1 yet when I render them they both appear on top of each other. As if they were both 0,0 -> 1,1 (the Ortho projection is setup from 0,0 to 1,1). They are both being sent through the same pipeline so I am not sure how this can happen.

Another odd and possibly related issue. Giving one of then quads a translation of 0.5 moves it entirely across the screen, rather than just half way as I would expect with a projection of 0,0 -> 1,1.

Here are some snippets from the Java...

//Fixed Point Class public static final int FIXED_POINT = 16;

public static final int ONE = (1 << FIXED_POINT);
public static final int HALF = (ONE >> 1);

//Vertex Setup mNumVerts = 6;

    ByteBuffer vbb = ByteBuffer.allocateDirect( mNumVerts * 2 * 4 );
    vbb.order( ByteOrder.nativeOrder() );
    mVtxBuf = vbb.asIntBuffer();

    vbb = ByteBuffer.allocateDirect( mNumVerts * 2 * 4 );
    vbb.order( ByteOrder.nativeOrder() );
    mTexBuf = vbb.asIntBuffer();

    mVtxBuf.put( -Fixed32.HALF ); mVtxBuf.put( -Fixed32.HALF ); mTexBuf.put( 0 ); mTexBuf.put( Fixed32.ONE );
    mVtxBuf.put(  Fixed32.HALF ); mVtxBuf.put( -Fixed32.HALF ); mTexBuf.put( Fixed32.ONE ); mTexBuf.put( Fixed32.ONE );
    mVtxBuf.put(  Fixed32.HALF ); mVtxBuf.put(  Fixed32.HALF ); mTexBuf.put( Fixed32.ONE ); mTexBuf.put( 0 );

    mVtxBuf.put( -Fixed32.HALF ); mVtxBuf.put( -Fixed32.HALF ); mTexBuf.put( 0 ); mTexBuf.put( Fixed32.ONE );
    mVtxBuf.put(  Fixed32.HALF ); mVtxBuf.put(  Fixed32.HALF ); mTexBuf.put( Fixed32.ONE ); mTexBuf.put( 0 );        
    mVtxBuf.put( -Fixed32.HALF ); mVtxBuf.put(  Fixed32.HALF ); mTexBuf.put( 0 ); mTexBuf.put( 0 );

    mVtxBuf.position( 0 );        
    mTexBuf.position( 0 );

//and....

    mVtxBuf.put( 0 ); mVtxBuf.put( 0 ); mTexBuf.put( 0 ); mTexBuf.put( Fixed32.ONE );
    mVtxBuf.put( Fixed32.ONE ); mVtxBuf.put( 0 ); mTexBuf.put( Fixed32.ONE ); mTexBuf.put( Fixed32.ONE );
    mVtxBuf.put( Fixed32.ONE ); mVtxBuf.put( Fixed32.ONE ); mTexBuf.put( Fixed32.ONE ); mTexBuf.put( 0 );

    mVtxBuf.put( 0 ); mVtxBuf.put( 0 ); mTexBuf.put( 0 ); mTexBuf.put( Fixed32.ONE );
    mVtxBuf.put( Fixed32.ONE ); mVtxBuf.put( Fixed32.ONE ); mTexBuf.put( Fixed32.ONE ); mTexBuf.put( 0 );        
    mVtxBuf.put( 0 ); mVtxBuf.put( Fixed32.ONE ); mTexBuf.put( 0 ); mTexBuf.put( 0 );

    mVtxBuf.position( 0 );        
    mTexBuf.position( 0 );

//Projection Setup

                gl.glMatrixMode( GL10.GL_PROJECTION );
                gl.glLoadIdentity();
                gl.glOrthox( 0, Fixed32.ONE, 0, Fixed32.ONE, -Fixed32.ONE, Fixed32.ONE );

//Render

                {
                    gl.glDisableClientState( GL10.GL_NORMAL_ARRAY );

                    gl.glScalef( thing.getXScale(), thing.getYScale(), thing.getZScale() );
                    gl.glTranslatef( thing.getX(), thing.getY(), thing.getZ() );

                    gl.glFrontFace( GL10.GL_CW );
                    gl.glVertexPointer( 2, GL10.GL_FIXED, 0, mdl.getVtxBuf() );
                }


                gl.glTexCoordPointer( 2, GL10.GL_FIXED, 0, mdl.getTexBuf() );
                // gl.glColorPointer( 4, GL10.GL_FLOAT, 0, mColorBuffer );
                gl.glDrawArrays( GL10.GL_TRIANGLES, 0, mdl.getVtxCount() );    

Solution

  • As many of you may have guessed, I must have missed something. The vertices were being edited in my objects super class on a per-frame basis to handle an old issue with aspect ratio. Thus the vertices were not really -.5,-.5 - .5,.5 they were all reset to be 0,0-1,1. Thanks for the time.