Trying to draw solids, faces not appearing correct

Hello!
I am trying to learn openGL by following the tutorials at nehe.gamedev.net and by reading the openGL programming guide (Red book).

While running the 5th nehe tutorial I encountered a very strange problem. The code in there is supposed to draw 2 solids and rotate them. The rotation happens correctly but the solids are not drawn correctly. This is the drawing code:

glBegin(GL_TRIANGLES);
		glColor3f(1.0f,0.0f,0.0f);	    // RED
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);
		glColor3f(0.0f,1.0f,0.0f);		// GREEN
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, -1.0f);
		glColor3f(0.0f,0.0f,1.0f);	  // BLUE
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f( 1.0f,-1.0f, -1.0f);
		glVertex3f(-1.0f,-1.0f, -1.0f);
		glColor3f(1.0f,1.0f,0.0f);	// YELLOW
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
	glEnd();

    glLoadIdentity();
    glTranslatef(1.5f,0.0f,-7.0f);
	glRotatef(rquad,1.0f,1.0f,1.0f);
    glBegin(GL_QUADS);
		glColor3f(0.0f,1.0f,0.0f);		// Set The Color To Green
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glVertex3f(-1.0f, 1.0f,-1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f( 1.0f, 1.0f, 1.0f);
		glColor3f(1.0f,0.5f,0.0f);		// Set The Color To Orange
		glVertex3f( 1.0f,-1.0f, 1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);
		glVertex3f( 1.0f,-1.0f,-1.0f);
        glColor3f(1.0f,0.0f,0.0f);		// Set The Color To Red
		glVertex3f( 1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);
		glColor3f(1.0f,1.0f,0.0f);		// Set The Color To Yellow
		glVertex3f( 1.0f,-1.0f,-1.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);
		glVertex3f(-1.0f, 1.0f,-1.0f);
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glColor3f(0.0f,0.0f,1.0f);		// Set The Color To Blue
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f,-1.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
		glColor3f(1.0f,0.0f,1.0f);		// Set The Color To Violet
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glVertex3f( 1.0f, 1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f,-1.0f);
	glEnd();

The problem can be illustrated in the following picture:

Parts of some faces appear over other faces in the pyramid.
The same happens in the cube but in the cube some faces fail to appear too.

Any help would be really appreciated. I think I have understood the code and how the coordinate system would work to create the solids. But the problem is that what was supposed to happen from that code does not happen and I can not understand what the mistake is.

Thanks in advance :slight_smile:

Is the depth test enabled?

Regards,
Patrick

Hello Patrick,

Thank you for your input. I am still learning but yes the depth test seems to be enabled.

I understand that depth testing must be enabled by the windowing system the programmer uses. I use wxWidgets and I think that this is the way to do it:
(just adding arguments to a contstructor)


 int args[] = {WX_GL_RGBA, WX_GL_DOUBLEBUFFER,WX_GL_DEPTH_SIZE,16,WX_GL_BUFFER_SIZE,16,0};

    mainFrame->glWindow = new GLCanvas( mainFrame,wxDefaultSize, args);

And this is the openGL initalization function, which runs only once:


glShadeModel(GL_SMOOTH);				
glClearColor(0.0f,0.0f,0.0f,0.5f);			
glClearDepth(1.0f);					
glEnable(GL_DEPTH_TEST);				glDepthFunc(GL_LEQUAL);					glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

Unfortunately I still have not found a way to correct the problem. It has to be a depth test problem as you pointed out. I will go ask at wxWidgets forums too, to see if my depth testing is wrong.

Your GL calls for the depth test look correct. I assume you didn’t change the depth range. Are you sure that you are calling them before you do your drawing? Also, I think you should get a depth buffer by default (unless you are rendering to a FBO which you didn’t mention but I doubt it).

If you render the scene from a different view, what does it look like? Because if its not a depth test problem, your cube might just be missing a face. Does the green face of the cube ever show up?

One final possibility is if facet culling is enabled, the green face may have the wrong winding order.

Regards,
Patrick

I don’t know what an FBO is, and yes the depth test call happens once at the program’s initialization. What do you mean get a depth buffer by default?

The program I am trying is rotating these solids so I am watching them from many different views. Some times it appears as if faces are missing and you can see the faces that are placed at the back. That goes for both the pyramid and the cube.

I don’t think that I missed the drawing of a face of the cube since I triple-checked.
Unless facet culling is enabled by default I don’t think I have it. To tell the truth I don’t even know what it is, so I am going to look it up.

I found the problem and it made me feel really stupid (as it always happens when you learn something).

It seems that these depth testing calls are not just activating depth testing and are something you can call just at the initialization of your program. They are calls which need to be made in your rendering function just before the rendering happens, every time.

By doing that my problem is solved and I now got solids appearing correctly :slight_smile:

I’m glad you got your application working. This is strange though. The depth test is part of the GL state, and once you set it, the state does not change unless you explicitly change it. I suppose it is possible that you were setting the depth test state before a GL context was created.

Regards,
Patrick

Patrick thanks again for the help. You were right. All the openGL initialization were indeed running once but they were running before the GL context was created.

That explains all my problems. Thanks again. Now I properly placed the init function after the GL context is created and I am okay. I realized the problem when I tried to place textures over my solids. Now everything runs fine! Thanks again!

Glad I could help out.

Patrick