0

I have some code, here is most of it:

class Cube {
    public Cube()
    {
    ...

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        mFVertexBuffer = vbb.asFloatBuffer();
        mFVertexBuffer.put(vertices);
        mFVertexBuffer.position(0);

        mColorBuffer = ByteBuffer.allocateDirect(colors.length);
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mTfan1 = ByteBuffer.allocateDirect(tfan1.length);
        mTfan1.put(tfan1);
        mTfan1.position(0);

        mTfan2 = ByteBuffer.allocateDirect(tfan2.length);
        mTfan2.put(tfan2);
        mTfan2.position(0);
    }

    public void draw(GL10 gl)
    {
        gl.glVertexPointer(3,  GL11.GL_FLOAT, 0, mFVertexBuffer);
        gl.glColorPointer(4,  GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);

        gl.glDrawElements(GL10.GL_TRIANGLE_FAN, 6 * 3, GL10.GL_UNSIGNED_BYTE, mTfan1);
        gl.glDrawElements(GL10.GL_TRIANGLE_FAN, 6 * 3, GL10.GL_UNSIGNED_BYTE, mTfan2);
    }

    private FloatBuffer mFVertexBuffer;
    private ByteBuffer mColorBuffer;
    private ByteBuffer mTfan1;
    private ByteBuffer mTfan2;

}

And the Renderer:

public class CubeRenderer implements GLSurfaceView.Renderer 
{
    public CubeRenderer(boolean useTranslucentBackground)
    {
        mTranslucentBackground = useTranslucentBackground;
        mCube = new Cube();
    }

    public void onDrawFrame(GL10 gl)
    {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -7.0f);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        mCube.draw(gl);

        mTransY += 0.075f;
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) 
    { 
        gl.glViewport(0,0,width,height);

        float aspectRatio;
        float zNear = 0.1f;
        float zFar = 1000;
        float fieldOfView = 30.0f/57.3f;
        float size;

        gl.glEnable(GL10.GL_NORMALIZE);

        aspectRatio = (float)width/(float)height;

        gl.glMatrixMode(GL10.GL_PROJECTION);

        size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));

        gl.glFrustumf(-size, size, -size / aspectRatio, 
                                    size / aspectRatio, zNear, zFar);


        gl.glMatrixMode(GL10.GL_MODELVIEW);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    {
        gl.glDisable(GL10.GL_DITHER);

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        if (mTranslucentBackground)
        {
            gl.glClearColor(0, 0, 0, 0);
        } else {
            gl.glClearColor(1, 1, 1, 1);
        }
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnable(GL10.GL_DEPTH_TEST);
    }

    private boolean mTranslucentBackground;
    private Cube mCube;
    private float mTransY;
}

It's not showing the 3D cube object unless I refresh the screen by tilting the device sideways to force a refresh, then it works fine,

so I'm looking for a simple way to refresh the screen once and where to put the initial refresh.

jaysonpowers
  • 290
  • 2
  • 3
  • 16

1 Answers1

0

Forgot to use

gl.glLoadIdentity() in on SurfaceChanged function

// In OnSurfaceChanged put this:
aspectRatio = (float)width/(float)height;
gl.glMatrixMode(GL10.GL_PROJECTION);

gl.glLoadIdentity();
jaysonpowers
  • 290
  • 2
  • 3
  • 16