Jump to content

  • Log In with Google      Sign In   
  • Create Account


Cube showing then disappearing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 mathacka   Members   -  Reputation: 123

Like
0Likes
Like

Posted 15 April 2013 - 04:08 PM

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

Edited by mathacka, 15 April 2013 - 05:39 PM.


Sponsor:

#2 mathacka   Members   -  Reputation: 123

Like
0Likes
Like

Posted 15 April 2013 - 04:42 PM

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?


Edited by mathacka, 15 April 2013 - 05:38 PM.


#3 L. Spiro   Crossbones+   -  Reputation: 12231

Like
1Likes
Like

Posted 15 April 2013 - 05:42 PM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS