help drawing cube :(

hi,
i have a very basic problem . iโ€™m trying to draw a simple 3d cube to the screen. however whenever i use the gluPerspective() there is no output; i have only drawn the one side at the moment but will add the others later :slight_smile:
i am using the LWJGL but the code is all opengl :slight_smile:

here is my init code


          glEnable(GL_DEPTH_TEST);

	  /* Setup the view of the cube. */
	  glMatrixMode(GL_PROJECTION);
	  gluPerspective(50, (float)WIDTH/(float)HEIGHT, 0, 100);
	  glMatrixMode(GL_MODELVIEW);

and here is my loop code


                glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
		glClear(GL_COLOR_BUFFER_BIT);
		glLoadIdentity();

		glColor3f(0f, 1f, 1f);
		glBegin(GL_QUADS);
		{
			glVertex3i(10, 10, z);
			glVertex3i(10+10, 10, z);
			glVertex3i(10+10, 10+10, z);
			glVertex3i(10, 10+10, z);
		}
		glEnd();

		glFlush(); 

does anyone know where im going wrong?

-James

I can definitely say that in gluPerspective() the 3rd parameter (zNear) should always be positive and never zero. For further reference seek here:
http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml

So why donโ€™t you put something like 0.1? Also how come you are using 50 degrees for your field of view? Most starting tutorials have this set at a standard of 45 degrees.

Another mistake is that you are using glClearColor inside your main loop. That function sets the color you want your screen to be clear with every time glClear() is called. So it does not need to be in the main loop.

thanks for the reply,
i have changed the znear to 1 and moved the clearcolor declaration to the init but i still see nothing in the output

-James

You need glLoadIdentity before gluPerspective

i have now added the glLoadIdentity(); call before the perspective one and still get a blank window :frowning:
the init code now looks like this


          glEnable(GL_DEPTH_TEST);

	  glMatrixMode(GL_PROJECTION);
	  glLoadIdentity();
	  gluPerspective(45, (float)WIDTH/(float)HEIGHT, 1, 100);
	  glMatrixMode(GL_MODELVIEW);


glBegin(GL_QUADS);
{
glVertex3i(10, 10, z);
glVertex3i(10+10, 10, z);
glVertex3i(10+10, 10+10, z);
glVertex3i(10, 10+10, z);
}

What are the value of z? The quad you are drawing can be behind the camera.

erm, I think its 50. So it should be half way through the scene

-James

There is your problem then, trinitrotoluene pointed it out.

z is too big and positive. Positive means towards the camera, towards you and so it puts the square behind the camera. Try a negative value, say -50.

still get the same problem :frowning: even with -50;

here is the current complete code : http://pastebin.com/bifVwTuG

wow opengl is harder than i first thought, iโ€™m only trying to draw a cube! (well one side) :stuck_out_tongue:

-James

ok
ive fixed the problem now i ended up using only 1 and 0โ€™s to draw the plane and then scaled before the calles to Vector3f

-James