FBObject
Jump to navigation
Jump to search
FrameBuffer Object (FBO)
package com.jogamp.opengl;
Without MSAA
Creating FBO
final FBObject fbo = new FBObject(); // Create FrameBuffer final GL2ES2 gl = GLContext.getCurrentGL().getGL2ES2(); fbo.reset(gl, width, height); // int width, height - size of FBO, can be resized with the same call fbo.attachTexture2D(gl, 0, true); // Create GL_TEXTURE_2D texture which can be used as sampler2D at the shader code // Created texture will be bound to FBO's GL_COLOR_ATTACHMENT0 (see the second parameter 0) fbo.attachRenderbuffer(gl, Attachment.Type.DEPTH, 32); // Create depth buffer (if required) fbo.unbind(gl); // Unbind FrameBuffer (You will probably want to make bind() - render - unbind() loop later on)
Render to FBO
fbo.bind(gl); // Render scene as usual fbo.unbind(gl);
- Note-1: One could also call FBObject.use(gl, ta) w/ TextureAttachment ta instead of FBObject.unbind(gl)
- Note-2: calls to FBObject.use(..)/FBObject.unuse() will unbind current buffer
With MSAA
Creating FBO
final FBObject fbo = new FBObject(); // Create FrameBuffer final GL2ES2 gl = GLContext.getCurrentGL().getGL2ES2(); fbo.reset(gl, width, height, sampleNum, true); // int width, height, sampleNum - size/sampleNum of FBO, can be resized with the same call fbo.attachTexture2D(gl, 0, true); // Create GL_TEXTURE_2D texture which can be used as sampler2D at the shader code // Created texture will be bound to FBO's GL_COLOR_ATTACHMENT0 (see the second parameter 0) fbo.attachRenderbuffer(gl, Attachment.Type.DEPTH, 32); // Create depth buffer (if required) fbo.unbind(gl); // Unbind FrameBuffer (You will probably want to make bind() - render - unbind() loop later on)
Render to FBO
fbo.bind(gl); // Render scene as usual fbo.syncSamplingSink(gl); // This call ensures MSAA buffers are blitted to the FBO sink, can be called by non MSAA as well (NOP). Will always unbind FBO.
- Note-1: One could also call FBObject.use(gl, ta) w/ TextureAttachment ta instead of FBObject.syncSamplingSink(gl)
Render from FBO to another FBO
Imagine we have 2 FBO, source and target. Source FBO must have TextureAttachment (created with attachTexture2D(), for example)
FBObject source; FBObject target;
Using low-level texture binding:
final TextureAttachment tex0 = (TextureAttachment) source.getColorbuffer(0); target.bind(gl); // Starting rendering to target FBO gl.glActiveTexture(GL2.GL_TEXTURE0); gl.glEnable(GL2.GL_TEXTURE_2D); // Should not call this for CORE profiles, i.e. ES2, GL3, .. gl.glBindTexture(GL2.GL_TEXTURE_2D, tex0.getName()); // Binding source FBO's TextureAttachment as GL_TEXTURE_2D // Here you can render something using source texture as GL_TEXTURE0 // For example, you can enable shader with some filter, which makes use of sampler2D and render a fullscreen quad. target.unbind(gl); // Unbind FBO
Render from FBO to screen
final TextureAttachment tex0 = (TextureAttachment) source.getColorbuffer(0); gl.glActiveTexture(GL2.GL_TEXTURE0); gl.glEnable(GL2.GL_TEXTURE_2D); // Should not call this for CORE profiles, i.e. ES2, GL3, .. gl.glBindTexture(GL2.GL_TEXTURE_2D, tex0.getName()); // Binding source FBO's TextureAttachment as GL_TEXTURE_2D // Here you can just render a fullscreen quad to show FBO content, or do something more exciting