Opengl depth buffer problem

Started by
2 comments, last by zedzeek 19 years ago
I have a small program using sdl and opengl, it displays 2 triangles on the screen using the following code... float z = -10.0f, x = 0.0f, y = 0.0f; void Draw() { glTranslatef(x,0,z); glColor3f(0.5f,0.5f,1.0f); // blue glBegin(GL_TRIANGLES); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glEnd(); glColor3f(1.0f,0.5f,0.0f); // orange glBegin(GL_TRIANGLES); glVertex3f(-1.0f, 1.0f, -2.0f); glVertex3f( 1.0f, 1.0f, -2.0f); glVertex3f( 1.0f,-1.0f, -2.0f); glEnd(); } What I expect is 2 triangles one orange one blue with the orange one behind the blue one. (and indeed being obscured by it.). This isnt what happens though and even when i cange the signs on the z for the second triangle it remains visible. So i looked in the blue book and it told me to use glEnable(GL_DEPTH_TEST); and to clear my depth buffer. //glEnable(GL_DEPTH_TEST); glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(0.0); while( g_Done == false ) { SDL_Event event; while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) g_Done = 1; } glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity( ); Update(); Draw(); SDL_GL_SwapBuffers(); } this is what I tried. with glEnable() uncommented. I get a blank screen. I am very new to Opengl and 3d in general. some pointers to help me through this would be appreciated. As long as glEnable() is commented i see my triangles. I have googled, searched gamedev, and read upto chapter 7 in nehe's and quite a bit of blue book(which is a bit over my head but still quite useful).
Advertisement
So a positive number for z is deeper? clearing my depth buffer to 20 worked. Thanks GD for helping me think this out myself.
What do you mean by clear to 20? The glClearDepth() takes floats clamped between 0.0 and 1.0...
as chad saiz use
glClearDepth(1.0)
glEnable( GL_DEPTH_TEST );
glDEpthFunc( GL_LEQUAL );
glDepthMask( GL_TRUE );

This topic is closed to new replies.

Advertisement