PDA

View Full Version : Simple problem with gluOrtho2D



Stuart McDonald
02-26-2008, 04:21 PM
Hi,
I have some 3D data that is a set of triangles on the XZ plane all at a fixed height and I want to draw them using gluOrtho2D.

The following test code which has a triangle on the XZ plane at a fixed depth (-500) works. It draws a triangle touching the bottom corners and the middle of the top edge


glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D(0, 400, 0, 400);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();

gluLookAt (0, -500, 0,
0.0, 0.0, 0.0,
0.0, 0.0, 1.0);

GLfloat white[] = { 1.0,1.0,1.0 };
glColor3fv(white);

glBegin(GL_TRIANGLES);
glVertex3d(0, -500, 0);
glVertex3d(400,-500, 0);
glVertex3d(200,-500,400);
glEnd();

However, if I make the triangle Y = 500 instead of -500 I can't get the triangle to appear. I assumed the -500 in gluLookAt would be 500 (and maybe change the sign of the UP vector), but I've tried lots of parameter combinations (FYI GL_CULL_FACE is disabled).

I'm missing something obvious, but I can't see it.

Thanks,

Stuart.

-NiCo-
02-26-2008, 04:59 PM
Everything is working correctly. The problem is that, when changing the sign of all -500 values, your triangle gets transformed outside of your viewing frustum.

Change the gluOrtho call to
gluOrtho2D(-400, 400, -400, 400);
to see what is happening.

N.

thinks
02-26-2008, 05:07 PM
In your example code your up-vector is (0,0,0). This is undefined. Most people in computer graphics use (0,1,0) as the up-vector. Hence, z = (0,0,1) becomes depth, and the term z-buffering starts making a lot of sense.

I also note that you are not positioning the camera/eye at the origin. I find it very useful to do so before I have wrapped my head around the world coordinate system.

Be careful of where you have the near and far clipping planes. I have pointed out in my suggested code where they are for gluOrtho2D.

May I suggest you try the following:


glMatrixMode (GL_PROJECTION);
glLoadIdentity();
// Note that by default gluOrtho2D sets near = -1 and far = 1,
// so your geometry (the triangle) has to be within these values.
//
gluOrtho2D(0, 400, 0, 400);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();

// These parameters will actually
gluLookAt (0.0, 0.0, 0.0, // Camera at origin.
0.0, 1.0, 0.0, // Y+ is up
0.0, 0.0, -1.0); // Looking down negative z-axis.

GLfloat white[] = { 1.0,1.0,1.0 };
glColor3fv(white);

glBegin(GL_TRIANGLES);
// Move triangle out by 0.5 units, which is in the range [-1..1]
//
glVertex3d(0, 0, -0.5);
glVertex3d(400, 0, -0.5);
glVertex3d(200, 400, -0.5);
glEnd();

Stuart McDonald
02-27-2008, 01:27 AM
@nico: D'oh! Thanks.

@thinks: Params 4,5,6 are the reference point and 7,8,9 is the up vector. So the vector is not undefined.