FBObject: Difference between revisions

From JogampWiki
Jump to navigation Jump to search
No edit summary
(Link unit test to JogAmp gitweb; Distinction of MSAA and non MSAA FBO; Note for "glEnable(GL2.GL_TEXTURE_2D)")
Line 3: Line 3:
     package com.jogamp.opengl;
     package com.jogamp.opengl;


===Creating FBO===
=== Without MSAA ===
====Creating FBO====
     final FBObject fbo = new FBObject(); // Create FrameBuffer
     final FBObject fbo = new FBObject(); // Create FrameBuffer
     final GL2ES2 gl = GLContext.getCurrentGL().getGL2ES2();
     final GL2ES2 gl = GLContext.getCurrentGL().getGL2ES2();
Line 12: Line 13:
     fbo.unbind(gl); // Unbind FrameBuffer (You will probably want to make bind() - render - unbind() loop later on)
     fbo.unbind(gl); // Unbind FrameBuffer (You will probably want to make bind() - render - unbind() loop later on)


===Render to FBO===
====Render to FBO====
     fbo.bind(gl);
     fbo.bind(gl);
     // Render scene as usual
     // Render scene as usual
     fbo.unbind(gl);
     fbo.unbind(gl);
Note: calls to FBObject.use()/FBObject.unuse() will unbind current buffer
 
* 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);
 
* Note-1: One could also call FBObject.use(gl, ta) w/ TextureAttachment ta instead of FBObject.syncSamplingSink(gl)


===Render from FBO to another FBO===
===Render from FBO to another FBO===
Line 27: Line 47:
     target.bind(gl); // Starting rendering to target FBO
     target.bind(gl); // Starting rendering to target FBO
     gl.glActiveTexture(GL2.GL_TEXTURE0);
     gl.glActiveTexture(GL2.GL_TEXTURE0);
     gl.glEnable(GL2.GL_TEXTURE_2D);
     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
     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
     // Here you can render something using source texture as GL_TEXTURE0
Line 36: Line 56:
     final TextureAttachment tex0 = (TextureAttachment) source.getColorbuffer(0);
     final TextureAttachment tex0 = (TextureAttachment) source.getColorbuffer(0);
     gl.glActiveTexture(GL2.GL_TEXTURE0);
     gl.glActiveTexture(GL2.GL_TEXTURE0);
     gl.glEnable(GL2.GL_TEXTURE_2D);
     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
     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
     // Here you can just render a fullscreen quad to show FBO content, or do something more exciting
Line 43: Line 63:
===Demos/Tests===
===Demos/Tests===


* [https://github.com/JogAmp/jogl/blob/master/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestFBOMix2DemosES2NEWT.java TestFBOMix2DemosES2NEWT.java]
* [{{SERVER}}/git/?p=jogl.git;a=blob;f=src/test/com/jogamp/opengl/test/junit/jogl/acore/TestFBOMix2DemosES2NEWT.java;hb=HEAD TestFBOMix2DemosES2NEWT.java]

Revision as of 03:01, 11 January 2013

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);
  • 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


Demos/Tests