I followed the tutorial scratt gave. There is one part that says to check
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
{
cout << gluErrorString(status) << endl;
}
It is returning “no error”, but I guess that is different than “GL_FRAMEBUFFER_COMPLETE_EXT” ?
here is my complete attempt (almost every line is from the tutorial)
//Create handle to FBO
GLuint fbo;
glGenFramebuffersEXT(1, &fbo);
//bind FBO (what does this mean?)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
//create depth buffer for the FBO
GLuint depthbuffer;
glGenRenderbuffersEXT(1, &depthbuffer);
//bind the renderbuffer
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer);
//specify size of the depth buffer
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
//attach the depth buffer to the frame buffer
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer);
//attach a texture to the FBO
//------------
//create a texture
GLuint img;
glGenTextures(1, &img);
glBindTexture(GL_TEXTURE_2D, img);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
//attach the texture to the framebuffer
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, img, 0);
//check that everything went ok
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
{
cout << gluErrorString(status) << endl;
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0,0,width, height);
// Render as normal here
// output goes to the FBO and it’s attached buffers
LookAt();
DrawCube();
ReadDepthBuffer();
glPopAttrib();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
//clean up
glDeleteFramebuffersEXT(1, &fbo);
glDeleteRenderbuffersEXT(1, &depthbuffer);
LookAt() is simply
void LookAt()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, 1, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(2, 2, 10, 2, 0, 0, 0, 1, 0);
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}
And my read and write buffer are
void ReadDepthBuffer()
{
float depths[width*height];
glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, depths);
WriteDepthBuffer("DepthBuffer.txt", depths);
}
void WriteDepthBuffer(const string &Filename, float* depths)
{
ofstream fout(Filename.c_str());
for(unsigned int im_x = 0; im_x < width; im_x++)
{
for(unsigned int im_y = 0; im_y < height; im_y++)
{
double CurrentDepth = depths[width*im_y + im_x];
fout << im_x << " " << im_y << " " << CurrentDepth << endl;
}
}
fout.close();
cout << "Done Writing." << endl;
}
The output looks nothing like the depth buffer of the cube. Any thoughts?
Thanks,
Dave