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.
Where are your artifacts? I simply don’t see anything on the 2nd picture.
You mean that, just after running you application, you see a correct thing and then, rotating the camera, you see some bands?
That’s just the 8 bit per channel precision of your framebuffer. Open the image in Photoshop/GIMP/etc. and use the color picker tool, you’ll see that there is only one bit difference between each of the “bands”.
No your depth buffer precision is good. Xmas is right, your problem is about the framebuffer color depth, not the depth buffer precision.
I don’t know if it is possible, but the best way would be reading the depth value directly in the shader that computes depth peeling, if there is one, to avoid the data lost when writing in the depth buffer. Another method consists in using a fbo and write your depth data into a floating point texture (32 bits if you don’t want any lost at all).
What you mean is that using glCopyTexImage2D is not good because it will only read from the framebuffer, and thus, there will be the problem, which is why I have a problem in the dept peeling since I use glCopyTexImage2D to get the depth buffer.
I don’t know at all FBO but if you tell me for sure this can be the solution, I am going to investigate it…
Thank you very much for your help, I have spent several days trying to understand it, unsuccessfully.
oc2k1, to fill the depth texture, I normally need to use glCopyTexImage2d, right ?
If you can’t usee framebuffer objects, yes you can do this and then give it to the depth peeling shader.
If your hardwre support fbo (GL_EXT_framebuffer_object extension), you can create a depth texture and then attach it to the framebuffer depth attachment point.
you can find a quick tutorial about how to render to texture with fbo, here: