A framebuffer contains a texture and a renderbuffer, and implementations of this class choose which buffers to give to each.
*/
public abstract class Framebuffer implements ResizeCallback {
private static Framebuffer active;
protected int framebuffer, renderbuffer, texture, textureTarget;
private int width, height;
private void checkStatus(int textureTarget)
{
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
throw new IllegalStateException("Framebuffer is not complete: " + this);
}
Assistant
When using OpenGL with GLFW and LWJGL, tearing or visual artifacts might occur during window resizing for a few reasons, and glClearColor combined with glClear can help mitigate those issues.
// Unbind all
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(textureTarget, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
/**
Binds this framebuffer to a particular target. Does not set this as the active framebuffer.
@param target the target to bind this framebuffer to
*/
public void bindTarget(int target)
{
glBindFramebuffer(target, framebuffer);
}
/**
Binds this framebuffer and sets it as the active framebuffer.
*/
public void bind()
{
active = this;
bindTarget(GL_FRAMEBUFFER);
}
public void resize(int width, int height)
{
// Delete old framebuffer
destroy();
this.width = width;
this.height = height;
// Create a new one
build();
}
public void renderAsQuad()
{
glBindTexture(GL_TEXTURE_2D, texture);
Mesh.renderQuad();
glBindTexture(GL_TEXTURE_2D, 0);
}
public void destroy()
{
glDeleteFramebuffers(framebuffer);
glDeleteTextures(texture);
glDeleteRenderbuffers(renderbuffer);
}
public int getFramebuffer()
{
return framebuffer;
}
public int getRenderbuffer()
{
return renderbuffer;
}
public int getTexture()
{
return texture;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
/**
@return the currently bound framebuffer
*/
public static Framebuffer active()
{
return active;
}
/**
Unbinds this framebuffer by binding 0 to GL_FRAMEBUFFER. Sets the active framebuffer to null.
*/
public static void unbind()
{
active = null;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
@Override
public final void onResize(int width, int height)
{
resize(width, height);
GLFW / LWJGL. Why does glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
become required to not show tearing? Doesn't the framebuffer get completely destroyed and nothing should be retained when resized?
Framebuffer Lifecycle and Memory: When you resize a framebuffer as you do in your resize method, you first destroy the existing framebuffer and then create a new one with the updated dimensions. However, OpenGL might not guarantee that all previous contents are fully purged before you start using the new framebuffer, especially if there’s any buffer state left in the graphics driver cache or pipeline.
Back Buffer and Front Buffer: Even if you delete and recreate your framebuffer, OpenGL still manages front and back buffers for rendering. These buffers might get partially updated during window resizing operations, leading to visual artifacts like tearing.
Synchronization Issues: OpenGL operates asynchronously with respect to the CPU. Commands issued to modify buffers (e.g., like resizing and clearing operations) might not execute immediately, causing parts of the old frame to be briefly displayed, especially if the new frame hasn’t been fully rendered yet. glClearColor and glClear ensure that once you bind the framebuffer, you start with a clean slate, effectively synchronizing the framebuffer contents with your defined color.
Partial Updates During Resize: During resizing, not all portions of the framebuffer might receive updates if the viewport/window size changes asynchronously during rendering. By clearing the buffer, you guarantee that it’s filled with a solid color, so uninitialized pixels won’t produce visual artifacts.
Use of Default Values: When binding a new framebuffer, if you haven’t explicitly cleared it, the buffer contents might be unpredictable or retain some data from previous frames. Clearing with a specific color ensures reproducible results.
While resizing, other pitfalls such as viewport mismatches, improper buffer swaps, or even driver-specific behavior might contribute to visual artifacts. Calling glClear with GL_COLOR_BUFFER_BIT and GL11.GL_DEPTH_BUFFER_BIT ensures that both color and depth information in your framebuffer start fresh, reducing the chance of these artifacts manifesting on screen.