OpenGL Depth Problem

I have a problem with rendering depth in OpenGL

The following code is a simple code example in which a problem occurs. It displays 2 trapeziums in one place, and one of them rotates. But the spinner is always displayed from above, although it should rotate around the first trapezoid.

I assume I messed up something with the initialization of OpenGL. Also, while searching through stackoverflow, I found a message in which someone suggested executing the following code and see the result.

int depth; glGetIntegerv(GL_DEPTH_BITS, &depth); printf("%i bits depth", depth); 

well, the output is 0 bits of depth, which is bad, I think :(

I am developing on Mac in xcode with glfw library

 #include <GL/glfw.h> #include <stdlib.h> int main( void ) { int running = GL_TRUE; if( !glfwInit() ) { exit( EXIT_FAILURE ); } // Open an OpenGL window if( !glfwOpenWindow( 640,480, 0,0,0,0,0,0, GLFW_WINDOW )) { glfwTerminate(); exit( EXIT_FAILURE ); } glEnable(GL_DEPTH_TEST); float angle = 0; // Main loop while( running ) { double elapsedTime = glfwGetTime(); glfwSetTime(0); angle += 90 * elapsedTime; // OpenGL rendering goes here... glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); //Save the transformations performed thus far glColor3f(1.0f, 1.0, 0.0); glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid //glRotatef(angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis glBegin(GL_QUADS); //Trapezoid glVertex3f(-0.7f, -0.5f, 0.0f); glVertex3f(0.7f, -0.5f, 0.0f); glVertex3f(0.4f, 0.5f, 0.0f); glVertex3f(-0.4f, 0.5f, 0.0f); glEnd(); glPopMatrix(); glPushMatrix(); //Save the transformations performed thus far glColor3f(1.0f, 0.0, 0.0); glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid glRotatef(angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis glBegin(GL_QUADS); //Trapezoid glVertex3f(-0.7f, -0.5f, 0.0f); glVertex3f(0.7f, -0.5f, 0.0f); glVertex3f(0.4f, 0.5f, 0.0f); glVertex3f(-0.4f, 0.5f, 0.0f); glEnd(); glPopMatrix(); //Undo the move to the center of the trapezoid glfwSwapBuffers(); // Check if ESC key was pressed or window was closed running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED ); } // Close window and terminate GLFW glfwTerminate(); // Exit program exit( EXIT_SUCCESS ); } 
+4
source share
3 answers

if (! glfwOpenWindow (640,480, 0,0,0,0,0,0,0, GLFW_WINDOW))

Why are you passing all these zeros? These options are important. The GLFW reference documentation (pdf) shows that these options describe how many bits you want for colors, depth and stencil. The last zero is the number of bits of depth. You requested 0 bits of depth for the framebuffer, so what you got.

You cannot blame the API for what you requested;)

A more reasonable team would be:

 if( !glfwOpenWindow( 640,480, 8, 8, 8, 8, 24, 8, GLFW_WINDOW )) 

This gives you 8 bits for RGBA, 24 for depth and 8-bit stencil.

+5
source

If you draw polygons on top of each other, you are not guaranteed the order in which you draw them, there will be an order that they will be rendered. It can work alone on one driver, and another on the next. Same thing with drivers from different manufacturers.

You need to either add a tiny depth difference in the glVertex commands, or play with glPolygonOffset (). Or do glFlush () after the first polygon, which is extremely inefficient.

Using the depth buffer, you tell the graphics card that it can display triangles in whatever order it selects, because the depth buffer will parse what was done in front. This is why in some games you will see how polygons flicker above each other (especially at a distance) when they display the same depth value.

+2
source

glGetIntegerv (GL_DEPTH_BITS, ..) deprecated in OpenGL 3

use instead:

 int GetDepthBits() { HDC hdc = wglGetCurrentDC(); int iPixelFormat = GetPixelFormat( hdc); int iAttr[] = {WGL_DEPTH_BITS_ARB}; int iDepthBits; wglGetPixelFormatAttribivARB( hdc, iPixelFormat, 0, 1, iAttr, & iDepthBits); return iDepthBits; } 
0
source

All Articles