Hello,
I have create a frame buffer object with the following code. However, when I bind it, I cannot draw to the texture at all (not even change the color with glClear). Absolutely nothing happens. What am I missing?
Here is how I generate the FBO:
// Generate the fbo
int fbo = EXTFramebufferObject.glGenFramebuffersEXT();
GLErrorCheck();
// Attach a texture to render to
int texture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
// The null at the end must be cast to tell javac which overload to use,
// even though the last parameter (for the different overloads) isn't even used
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer)null);
// Unbind texture
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GLErrorCheck();
// Bind the fbo for use
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbo);
EXTFramebufferObject.glFramebufferTexture2DEXT(
EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
GL11.GL_TEXTURE_2D, texture, 0); // The 0 is for mip map levels, we aren't using any
GLErrorCheck();
// Attach a depth buffer render buffer if desired
if(depthBuffer)
{
int depthRenBuff = EXTFramebufferObject.glGenRenderbuffersEXT();
// Bind it so we can set it up
EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthRenBuff);
// Set up the depth buffer
EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, GL11.GL_DEPTH_COMPONENT, width, height);
// Attach the dpeth buffer
EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthRenBuff);
GLErrorCheck();
}
// Check
if(EXTFramebufferObject.glCheckFramebufferStatusEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT) != EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT)
throw new Error("Could not create FBO!");
// An fbo has its own viewport, so lets set it
GL11.glViewport(0, 0, width, height);
// Unbind
EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, 0);
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
GLErrorCheck();
Binding it:
// Unbind textures
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbo);
// Save view port information, since this will in almost all cases change (states are shared with main context)
GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT | GL11.GL_COLOR_BUFFER_BIT);
Unbinding:
// Finish all operations so can use texture
GL11.glFlush();
// Restore saved information for main rendering context
GL11.glPopAttrib();
// Unbind
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
After the FBO is bound I do the following:
// Create the view port for this render texture
GL11.glViewport(0, 0, m_width, m_height);
GL11.glClearColor(0.5f, 0.2f, 0.8f, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
After unbinding the FBO, I bind the texture that went with it and draw a quad, but it is plain white every time.