Hi all,
I have posted this morning for a depth buffer problem and it seems to be now resolved using FBO.
I want now to perform some very simple depth peeling. I am just drawing a cube, and I would like to have the second slice (second depth).
The code to perform this is the next one:
// First draw of the scene into the framebuffer object
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glBindTexture(GL_TEXTURE_2D, screenTexture[0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 512, 512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, screenTexture[0], 0);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
exit(1);
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);
drawObjects(1, -1);
drawObjects(-1.5, -3.5);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
// Now create the second depth
glEnable(GL_TEXTURE_2D);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glBindTexture(GL_TEXTURE_2D, sliceTextures[1]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, 512, 512, 0, GL_RGBA, GL_FLOAT, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, sliceTextures[1], 0);
glBindTexture(GL_TEXTURE_2D, screenTexture[1]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 512, 512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, screenTexture[1], 0);
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
exit(1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, screenTexture[0]);
glUseProgram(program);
glColor4f(1, 1, 1, 1);
drawObjects(1, -1);
drawObjects(-1.5, -3.5);
glUseProgram(0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
// Prepare the matrices to draw a simple texture
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, m_iWidth, 0, m_iHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glColor4f(1, 1, 1, 0.5f);
// Draw the texture
glBindTexture(GL_TEXTURE_2D, sliceTextures[1]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 1);
glVertex2f(0, m_iHeight);
glTexCoord2f(1, 1);
glVertex2f(m_iWidth, m_iHeight);
glTexCoord2f(1, 0);
glVertex2f(m_iWidth, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glutSwapBuffers();
You can see in the code, I use a shader in order to test fragment depth with the previous one comparing these depth with the previous depth buffer stored in a texture.
The vertex shader is:
varying vec4 coord;
void main()
{
gl_Position = ftransform();
coord = gl_Position / gl_Position.w;
coord = (coord + 1) / 2;
gl_FrontColor = gl_Color;
}
Thus, coord is now between 0 and 1, and the fragment shader is:
uniform sampler2D ShadowMap;
varying vec4 coord;
void main()
{
float depth = texture2D(ShadowMap, coord.xy).x;
if (coord.z <= depth)
discard;
gl_FragColor = gl_Color;
}
The result, when I zoom is the next one:
Can you explain me this problem ?
Thank you very much.