PDA

View Full Version : Trying to draw solids, faces not appearing correct



Lefteris
01-02-2010, 05:54 AM
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 (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=05) 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:
http://img710.imageshack.us/img710/2581/solids.png

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 :)

pjcozzi
01-02-2010, 07:37 AM
Is the depth test enabled?

Regards,
Patrick

Lefteris
01-02-2010, 09:51 AM
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_BUFFE R_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.

pjcozzi
01-02-2010, 10:03 AM
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

Lefteris
01-02-2010, 10:13 AM
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.

Lefteris
01-03-2010, 04:05 AM
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 :)

pjcozzi
01-03-2010, 07:07 AM
They are calls which need to be made in your rendering function just before the rendering happens, every time.
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

Lefteris
01-04-2010, 04:09 AM
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!

pjcozzi
01-04-2010, 07:25 AM
Glad I could help out.

Patrick