FBObject
Jump to navigation
Jump to search
FrameBuffer Object (FBO)
package com.jogamp.opengl;
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: calls to FBObject.use()/FBObject.unuse() will unbind current buffer
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) getSourceFBO().getColorbuffer(0); target.bind(gl); // Starting rendering to target FBO gl.glActiveTexture(GL2.GL_TEXTURE0); gl.glEnable(GL2.GL_TEXTURE_2D); 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) getSourceFBO().getColorbuffer(0); gl.glActiveTexture(GL2.GL_TEXTURE0); gl.glEnable(GL2.GL_TEXTURE_2D); 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