PDA

View Full Version : help drawing cube :(



HumanMan
11-17-2011, 08:57 AM
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 :)
i am using the LWJGL but the code is all opengl :)

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

Lefteris
11-17-2011, 09:42 AM
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.

HumanMan
11-17-2011, 11:25 AM
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

BionicBytes
11-17-2011, 12:24 PM
You need glLoadIdentity before gluPerspective

HumanMan
11-17-2011, 01:14 PM
i have now added the glLoadIdentity(); call before the perspective one and still get a blank window :(
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);

trinitrotoluene
11-17-2011, 04:05 PM
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.

HumanMan
11-18-2011, 01:07 AM
erm, I think its 50. So it should be half way through the scene

-James

Lefteris
11-18-2011, 01:40 AM
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.

HumanMan
11-18-2011, 09:09 AM
still get the same problem :( 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) :P

-James

HumanMan
11-20-2011, 07:00 AM
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