Cube showing then disappearing

Started by
1 comment, last by L. Spiro 11 years ago

UPDATE: This is working once I tilt the device to do a refresh, now I need to see how to initially refresh the screen.

How can I programatically tell it to refresh the screen?

I commented out just about everything I could, and the only thing that did was make the cube not appear once, however with it all uncommented it's just "blipping" a cube to the screen then disappearing.


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;
}


I'm not sure what's wrong with this code, it is showing a cube, very briefly, then disappearing, any suggestions as to what's wrong?

Here's the cube class:


class Cube
{
    public Cube()
    {
        float vertices[] = 
        {
                -1.0f,  1.0f, 1.0f,
                 1.0f,  1.0f, 1.0f,
                 1.0f, -1.0f, 1.0f,
                -1.0f, -1.0f, 1.0f,
                 
                -1.0f,  1.0f,-1.0f,
                 1.0f,  1.0f,-1.0f,
                 1.0f, -1.0f,-1.0f,
                -1.0f, -1.0f,-1.0f
        }; 


        byte maxColor=(byte)255;
        
        byte colors[] = 
        {
         maxColor,maxColor,       0,maxColor,
                0,       maxColor,maxColor,maxColor,
                0,              0,       0,maxColor,
                maxColor,       0,maxColor,maxColor,
                
         maxColor, 0,       0,maxColor,
                0,       maxColor,  0,maxColor,
                0,              0,maxColor,maxColor,
                0,        0,  0,maxColor
        }; 




        byte tfan1[] = 
         {
         1,0,3,
         1,3,2,
         1,2,6,
         1,6,5,
         1,5,4,
         1,4,0
         
         
         };


        byte tfan2[] = 
         {
         7,4,5,
         7,5,6,
         7,6,2,
         7,2,3,
         7,3,0,
         7,0,4
         };


         
        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 here's the Activity class:


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		GLSurfaceView view = new GLSurfaceView(this);
		view.setRenderer(new CubeRenderer(true));
		setContentView(view);
	}
}
Advertisement

Update: It is showing the cube when I refresh the screen by tilting the device till it refreshes sideways, then it's showing both sideways and normal wize. How can I update the screen to get it to refresh when it first loads?

Games aren’t event-based. You don’t draw only when events such as screen rotation etc. have been issued.

You draw every frame from inside your game loop.

In order not to starve Android of its events you may be forced to do this on another thread. I don’t know the details of Android in this regard but it can be done because we do it at work.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement