- This project is 2D, but uses depth test.
- I've been trying so many things to make this work that the code is a mess of comments and unnecessary calls to OpenGL functions like gl.glEnable(GL_TEXTURE_2D);
-
public static GL begin(PApplet app) {
pgl = ((PGraphicsOpenGL) app.g).beginPGL();
gl = pgl.gl.getGL().getGL2();
Verbose.debug("This system uses OpenGL:"+gl.glGetString(GL.GL_VERSION));
return gl;
}
- public static int createFBO(){
- int[] array = new int[1];
- gl.glGenFramebuffers(1, array, 0);
- return array[0];
- }
- /**
- * Bind fbo.
- *
- * @param fbo the fbo
- */
- public static void bindFBO(int fbo){
- gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, fbo);
- }
- /**
- * Inits the fbo.
- *
- * @param w the w
- * @param h the h
- * @return the object[]
- * @throws Exception the exception
- */
- public static Object[] initFBO(int w, int h) throws Exception{
- //Now create a texture
- int[] array = new int[1];
- gl.glGenTextures(1,array,0);
- int tex = array[0];
- gl.glEnable(GL.GL_TEXTURE_2D);
- gl.glBindTexture(GL.GL_TEXTURE_2D, tex);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
- GL.GL_NEAREST);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
- GL.GL_NEAREST);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S,
- GL.GL_CLAMP_TO_EDGE);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T,
- GL.GL_CLAMP_TO_EDGE);
- gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, w, h,
- 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, null);
- initTexture(GL.GL_TEXTURE_2D, GL.GL_RGBA, w, h); //same code as in Processing PGL class
- gl.glBindTexture(GL.GL_TEXTURE_2D, 0);
- //and attach it to the FBO
- //first create a Frame Buffer Object
- int fbo = createFBO();
- bindFBO(fbo);
- //gl.glFramebufferTexture2DEXT(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, tex, 0);
- gl.glFramebufferTexture2D(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, GL.GL_TEXTURE_2D, tex, 0);
- //then create a render buffer
- //gl.glGenRenderbuffersEXT(1, array,0);
- gl.glGenRenderbuffers(1, array,0);
- int rb = array[0];
- gl.glBindRenderbuffer(GL.GL_RENDERBUFFER, rb);
- //ask for a depth buffer and sets the size
- gl.glRenderbufferStorage(GL.GL_RENDERBUFFER, GL2.GL_DEPTH_COMPONENT24, w, h);
- //attach it
- gl.glFramebufferRenderbuffer(GL2.GL_DRAW_FRAMEBUFFER, GL.GL_DEPTH_ATTACHMENT, GL.GL_RENDERBUFFER, rb);
- //check for completeness
- if(!checkBufferCompletness()){
- throw new Exception("Init FBO: rendering to texture could not be initialised.");
- }
- gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0);
- gl.glBindRenderbuffer(GL.GL_RENDERBUFFER, 0);
- disableTexture(0);
- return new Object[]{fbo,rb,new Texture(tex)};
- }
However, when I bind this FBO to render to its attached texture, the completeness test fails and report a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT, which seems to indicate that there is no texture attached to the FBO.
This is how I'm binding the FBO inside the sketch draw() method:
- gl.glEnable(GL.GL_TEXTURE_2D);
- // w and h are the dimension of the FBO, fbo is the id of fbo object created by the code above
- gl.glBindFramebuffer(GL.GL_FRAMEBUFFER,fbo);
- gl.glPushAttrib(GL2.GL_VIEWPORT_BIT); //save the current viewport info
- gl.glViewport(0,0,w, h); //make sure we draw in a big enough area
I checked the PGL class source code and I can see that a few FBOs are also created there. Could there be some sort of conflict when using multiple FBOs?
I've tried more involved debugging technics without much luck either.
Even if I get this working, I'm not sure this is the most effective solution (see here). Any thoughts?
I'm far from being an OpenGL expert and I have very little understanding of how it is setup by Processing, so I'm open to any suggestion regarding a different way of achieving the same thing (take snapshots and save them to files, render at different scales in realtime, do high resolution rendering).
Thanks in advance and have a merry easter.