PDA

View Full Version : Strangeness in drawing lines and polygons



akrik
10-10-2008, 06:15 AM
Hi,

I'm quite new to opengl so this may be a FAQ but I didn't find the answer while searching the forum archives (searched for grid and GL_LINES) or the wiki (though the problem might be related to the Polygon Offset thingy).

I want to draw a simple filled polygon (as GL_POLYGON) with the x, y and z axis drawn as lines (as GL_LINES). The polygon lies across the three axis (I mean it is located so that the axis "goes throuh it" (see below)).

The problem I meet is that if I draw the polygon first it looks like it is "behind" the axis lines, and if I draw the axis first, then the polygon sort of completely "masks" them while there should be only parts of the axis lines that should be masked.

Here is the code (a slight extrapolation from one of the first example from the Red Book):



void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();
gluLookAt(7.0, -10.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);

glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex3f(-50.0, 0.0, 0.0);
glVertex3f(50.0, 0.0, 0.0);
glEnd();

glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex3f(0.0, -50.0, 0.0);
glVertex3f(0.0, 50.0, 0.0);
glEnd();

glColor3f(0.0,1.0,0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, -50.0);
glVertex3f(0.0, 0.0, 50.0);
glEnd();

glColor3f(1.0, 1.0, 1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_POLYGON);
glVertex3i(1, 1, 1);
glVertex3i(-1, -1, 1);
glVertex3i(-1, -1, -1);
glVertex3i(1, 1, -1);
glEnd();

glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}


I'd be really thankful if someone could give me an hint.

TIA

ZbuffeR
10-10-2008, 06:55 AM
Apparently you don't have depth testing.
To make it work, you have to request a depth buffer. Example if you use glut :
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

// to do once, after you get the GL window :
glEnable (GL_DEPTH_TEST);

Then you have to clean the depth buffer at the same time as the color buffer :
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

If all this does not solve your problem, then you have to fix your near and far projection planes.

akrik
10-10-2008, 07:37 AM
You're right, that's it !

I eventually came across it jumping ahead in the Red Book (Chapter 5 - A hidden-surface removal survival kit) but I was extrapolating on example 3.1.

And you guessed right, I then hit the frustum depth problem too. Mine was so deep the quantization was too big for such a small object.

Thank you ZbuffeR !