|

OpenGL/ FPC - Chapter 11 - by Delax
Again, take a look at the example from the last chapter. And sure enough: there is something to notice. I mean the jagged edges of our pyramid. It looks like the back of the surface is shining through our edge. Ugly.
The solution: get rid of the surfaces that are behind other surfaces. They shouldn't be visible anyway and nothing can shine through edges.
But alas! If you think about it for some time, you'll notice something cool about this. Think about a scene that is composed of 100.000 triangles. But from these 100.000 only 40.000 are visible at a time - the other 60.000 are hidden behind the other ones. So drawing all 100.000 the whole time would simply be a big waste of CPU/ GPU cycles. So if we would eliminate the triangles that are hidden behind other ones, we would save 60% CPU/ GPU power! And our program would actually look better. Cool!
Best of all: it's not that hard to tell OpenGL it should use the hidden surface removal. It's just a call in OpenGL_Init.
PROCEDURE OpenGL_Init;
begin
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(45.0,width/height,0.1,100.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glClearDepth(1.0); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
end;
The first innovation is glEnable(GL_CULL_FACE);. This is activating the hidden surface removal. But how does OpenGL now when something is hidden behind another surface? Nope, not the Depth Buffer, but the way we draw our surfaces.
Think about it - if we draw something counter clockwise, we can see it's front, if we draw it clockwise, we see it's back. Since we don't want to see the back of our surfaces, we simply tell OpenGL to eliminate everything that's facing it's back on us.
We do this by calling glCullFace();. The parameter is either GL_BACK or GL_FRONT (guess what is what ;).
Since this can be a pain sometimes, you can set the direction you want to draw the vertices. With glFrontFace(GL_CCW) you would draw everything counter clockwise and with glFrontFace(GL_CW) everything would have a clockwise front.
But anyway: from now on you should look after your surface direction. Oh, and get the source code of the example and play around with it.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|