I tried using your code to set an FBO but I did not get any framebuffer completeness errors, everything worked fine. I did have to change your code a little to make it work. I passed null as the last argument to glTexImage2D(), and had to make fboId and depthTextureId int arrays. I think that was a typo on your part when posting, though, since it shouldn’t let you compile if fboId was just a simple int.
Anyway, here is my complete code that I used that ran everything correctly for me. If you run this and get errors, then I think there are problems with your graphics card and/or drivers and you’ll want to try updating them because your code looks and behaves fine on my computer.
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.FPSAnimator;
public class FBOTest implements GLEventListener {
public static void main(String[] args) {
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener(new FBOTest());
JFrame window = new JFrame();
window.setSize(800, 500);
window.add(canvas);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
Animator anim = new FPSAnimator(canvas, 60);
anim.start();
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
int shadowMapWidth = 1024;
int shadowMapHeight = 1024;
int fboStatus;
// Try to use a texture depth component
int[] depthTextureId = new int[1];
gl.glGenTextures(1, depthTextureId, 0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, depthTextureId[0]);
// GL_LINEAR does not make sense for depth texture. However, next tutorial shows usage of GL_LINEAR and PCF. Using GL_NEAREST
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
// Remove artefact on the edges of the shadowmap
gl.glTexParameterf( GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP );
gl.glTexParameterf( GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP );
// This is to allow usage of shadow2DProj function in the shader
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_COMPARE_R_TO_TEXTURE);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_COMPARE_FUNC, GL2.GL_LEQUAL);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_DEPTH_TEXTURE_MODE, GL2.GL_INTENSITY);
// No need to force GL_DEPTH_COMPONENT24, drivers usually give you the max precision if available
gl.glTexImage2D( GL2.GL_TEXTURE_2D, 0, GL2.GL_DEPTH_COMPONENT, shadowMapWidth, shadowMapHeight, 0, GL2.GL_DEPTH_COMPONENT, GL2.GL_UNSIGNED_BYTE, null);
gl.glBindTexture(GL2.GL_TEXTURE_2D, 0);
// create a framebuffer object
int[] fboId = new int[1];
gl.glGenFramebuffers(1, fboId, 0);
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, fboId[0]);
// Instruct openGL that we won't bind a color texture with the currently binded FBO
gl.glDrawBuffer(GL2.GL_NONE);
gl.glReadBuffer(GL2.GL_NONE);
// attach the texture to FBO depth attachment point
gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_DEPTH_ATTACHMENT,GL2.GL_TEXTURE_2D, depthTextureId[0], 0);
// check FBO status
fboStatus = gl.glCheckFramebufferStatus(GL2.GL_FRAMEBUFFER);
if(fboStatus != GL2.GL_FRAMEBUFFER_COMPLETE)
System.out.println("GL_FRAMEBUFFER_COMPLETE_EXT failed, CANNOT use FBO: " + fboStatus);
else
System.out.println("Framebuffer created successfully");
// switch back to window-system-provided framebuffer
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);
}
@Override
public void dispose(GLAutoDrawable drawable) {
// do nothing
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0f, 0f, 0f, 1f);
gl.glClear(GL2.GL_DEPTH_BUFFER_BIT | GL2.GL_COLOR_BUFFER_BIT);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
// do nothing
}
}