Jump to content
  • Advertisement
Sign in to follow this  
nickben

Depth buffer precision

This topic is 3631 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 all, I have a big problem I can't understand. I am rendering a simple cube using this function : glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, (float)m_iWidth / (float)m_iHeight, 1.0f, 100); glMatrixMode(GL_MODELVIEW); glLoadIdentity (); glTranslatef(0.0f,0.0f,-z); // Translate Into/Out Of The Screen By z glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis By xrot glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis By yrot glEnable(GL_DEPTH_TEST); glUseProgram(program); drawCube(1, -1); glUseProgram(0); .... I use a shader to change the fragment colors by its depth. The vertex shader is: void main() { gl_FrontColor = gl_Color; gl_Position = ftransform(); } and the fragment shader: void main() { gl_FragColor = vec4(gl_FragCoord.z, gl_FragCoord.z, gl_FragCoord.z, 1); } The problem is when I zoom in direction of the cube I obtain big artifacts (little bands). The normal display (where you an also see the problem even if not easy) is shown here: Finally, the problem is here: FYI, this only happens when I rotate the camera. I hope you'll be able to help me. Thank you. [Edited by - nickben on July 16, 2008 7:06:03 AM]

Share this post


Link to post
Share on other sites
Advertisement
Are you setting up a 24bit depth buffer or 16bit? Try moving the near plane out to like 3 vs. 1 and see if it gets better. I am assuming that the drawCube() is just immediate mode calls and nothing else?

Share this post


Link to post
Share on other sites
To me that looks more like color banding. Banding happens when you try to create a smooth gradient using to few colors, for example when rendering in an 16 bit color depth.

Try double checking that your render target/front buffer/whatever-it's-called-in-OpenGL is 32 bit.

Share this post


Link to post
Share on other sites
IIRC on windows you need a 32bit color buffer to get a 24bit depth buffer if you want. So with OSX I am assuming it would be the same...

Share this post


Link to post
Share on other sites
I think you really want the depth from the near plane to the vertex, so something like this: (taken / adapted from the Orange Book)


// vertex shader
uniform vec3 CameraPos, CameraDir;
uniform float DepthNear, DepthFar;

varying float CameraDepth;

...
vec3 offset = (gl_Vertex.xyz / gl_Vertex.w) - CameraPos;
float z = -dot(offset, CameraDir);
CameraDepth = (z - DepthNear) / (DepthFar - DepthNear);
...

// fragment shader
varying float CameraDepth;

...
gl_FragColor.rgb = vec3(CameraDepth);
gl_FragColor.a = 1.0;
...




Bear in mind that this is normalized with the far plane at 1.0.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!