Skip to main content
gamedev.net
🔒 Locked 🎯 Unreal

Depth buffer precision

Started by nickben 3 replies 650+ views
Original Post
nickben
nickben
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]
MARS_999
MARS_999
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?
Promethium
Promethium
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.
MARS_999
MARS_999
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...
sprite_hound
sprite_hound
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 shaderuniform 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 shadervarying 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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.