Here's my code, nothing fancy, I haven't wrapped up textures, render buffers and frame buffer objects inside actual C++ objects:
FBO::FBO(int width, int height)
: _width(width),
_height(height)
{
// create a depth texture object
glGenTextures(1, &_depthTex); glBindTexture(GL_TEXTURE_2D, _depthTex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, _width, _height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
// create a color texture object
glGenTextures(1, &_colorTex);
glBindTexture(GL_TEXTURE_2D, _colorTex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
// create a framebuffer object
glGenFramebuffersEXT(1, &_fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo);
// create a renderbuffer object to store depth info
glGenRenderbuffersEXT(1, &_depthBuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _depthBuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, _width, _height);
// create a renderbuffer object to store color info
glGenRenderbuffersEXT(1, &_colorBuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _colorBuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, _width, _height);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
// attach a texture to FBO depth attachement point
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, _depthTex, 0);
// attach a texture to FBO color attachement point
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, _colorTex, 0);
glReadBuffer(GL_NONE);
// check FBO status
checkFramebufferStatus();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
No comments:
Post a Comment