Search code examples
javaopengltexturesframebuffershadow-mapping

Getting GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT with depth texturing


I have been trying to implement shadow-mapping for a long time, and I am using Lance Williams' technique (which is rendering the scene twice, once in light space and once in view space). However, when I call glCheckFramebufferStatus on GL_FRAMEBUFFER, I get GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT, after I have called glFramebufferTexture2D on it. I am confused, because that error is thrown when there isn't a texture or renderbuffer attached to the framebuffer, but I think I do have a texture attached to it.

Here is my code run in init:

    gl.glEnable(GL2.GL_TEXTURE_2D);

    // generate stuff
    IntBuffer ib = IntBuffer.allocate(1);
    gl.glGenFramebuffers(1, ib);
    frameBuffer = ib.get(0);
    gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBuffer);

    ib = IntBuffer.allocate(1);
    gl.glGenTextures(1, ib);
    depthTexture = ib.get(0);
    gl.glBindTexture(GL2.GL_TEXTURE, depthTexture);

    // prevents 'shadow acne'
    gl.glPolygonOffset(2.5f, 0);
    // prevents multiple shadows
    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP_TO_EDGE);
    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP_TO_EDGE);
    // prevents (or expects!!!) pixel-y textures
    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);
    // store one value in all four components of pixel
    gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_DEPTH_TEXTURE_MODE, GL2.GL_INTENSITY);

    gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_DEPTH_ATTACHMENT, GL2.GL_TEXTURE_2D, depthTexture, 0);
    gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_DEPTH_COMPONENT16, 1024, 1024, 0, GL2.GL_DEPTH_COMPONENT, GL2.GL_FLOAT, null);
    gl.glDrawBuffer(GL2.GL_NONE);
    gl.glReadBuffer(GL2.GL_NONE);
    int FBOStatus = gl.glCheckFramebufferStatus(GL2.GL_FRAMEBUFFER);
    // FBOStatus == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT

EDIT: I ran this method in a practically empty project too (throwing the error)


Solution

  • but I think I do have a texture attached to it.

    No, you haven't.

    Since

    gl.glBindTexture(GL2.GL_TEXTURE, depthTexture);
    

    will only generate an GL_INVALID_ENUM error because you meant GL_TEXTURE_2D here, you only have a name of a texture object, and never created the texture storage for that. Your glTexImage2D call will just affect some other texture object which happens to be bound at the time (or even texture object 0 in legacy GL).