Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Having problem with depth test

This topic is 3736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys, i am having problem with the depth test. it seems like opengl is not checking whether the object is in front or behind the other, basically it just draw according to the order of my code. can anyone help to resolve the issue?
void RenderScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glLoadIdentity(); //Reset modelview matrix for new frame. //set projection matrix and view volumn glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho (0,1024,0,768, -2, 0); glMultMatrixd(proj_homograph); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(Rotate, 0, 0, 1); if(locateShape) { generateTerrain(); } else { renderCalibration(); } renderTerrain(); glFlush(); glutSwapBuffers(); } static void reshape( int width, int height ) { glViewport(0, 0, (GLint)width, (GLint)height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void initialiseGraphics() { glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(0,0); glutInitWindowSize(1024,768); projNo = glutCreateWindow("Sandbox Alive!"); glutFullScreen(); glutDisplayFunc(RenderScene); glutIdleFunc( idle ); glutReshapeFunc( reshape ); ShowCursor(false); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LESS); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glDepthMask(GL_TRUE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); }
this code is located in another file
void renderTerrain() { if(!locateShape) { renderTerrainG(); renderNormal(); showAllGrids(); } //grass texture if(showTerrain) { glBindTexture(GL_TEXTURE_2D, texture[GRASS]); for(int y = 0; y < terrainSizeZ - 1; y++) { glBegin(GL_TRIANGLE_STRIP); glColor3f(1.0f, 1.0f, 1.0f); for(int x = 0; x < terrainSizeX - 1; x++) { glNormal3dv(terrainNormal[x][y]); glTexCoord2f(0.0f, 0.0f); glVertex3d(terrain[x][y][0], terrain[x][y][1], terrain[x][y][2]); glTexCoord2f(1.0f, 0.0f); glVertex3d(terrain[x][y+1][0], terrain[x][y+1][1], terrain[x][y+1][2]); glTexCoord2d(0.0f, 1.0f); glVertex3d(terrain[x+1][y][0], terrain[x+1][y][1], terrain[x+1][y][2]); glTexCoord2f(1.0f, 1.0f); glVertex3d(terrain[x+1][y+1][0], terrain[x+1][y+1][1], terrain[x+1][y+1][2]); } glEnd(); } //water texture glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glEnable(GL_BLEND); glDepthMask(GL_FALSE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, texture[WATER]); glBegin(GL_QUADS); glColor4f(0.0f, 0.5f, 1.0f, 0.3f); glVertex3d(terrain[0][0][0], waterHeight, terrain[0][0][2]); glVertex3d(terrain[0][terrainSizeZ-1][0], waterHeight, terrain[0][terrainSizeZ-1][2]); glVertex3d(terrain[terrainSizeX-1][terrainSizeZ-1][0], waterHeight, terrain[terrainSizeX-1][terrainSizeZ-1][2]); glVertex3d(terrain[terrainSizeX-1][0][0], waterHeight, terrain[terrainSizeX-1][0][2]); glEnd(); glEnable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); } }
intended view Free Image Hosting at www.ImageShack.us frontview Free Image Hosting at www.ImageShack.us actually, the green surface has a Y value range between 8 to 14, while the blue plane just at Y = 10. but from the image, you can see that its appearing below the the green surface instead of slicing thru. would gladly appreciate any help thanks in advance

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!