Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: FBO render depth to texture, cause depth texture to equal diffuse.

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    23

    Question FBO render depth to texture, cause depth texture to equal diffuse.

    I have 1 fbo with 3 textures(diffuse, normal, depth) and 1 render buffer(depth). The problem is that when I set the final fragment color in the shader to depth texture: `FragColor = texture(depthtex, uvs);` The it looks like diffuse texture...

    This is how I setup my fbo:



    What have I done wrong ?

  2. #2
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    5,932
    This is how I setup my fbo:
    Was it really easier for you to take a screenshot of your code than to just copy-and-paste it into the text box? It certainly doesn't make it easier for us. For example, I'd like to copy-and-paste out which lines are causing the problem, but I can't. Since it's a screenshot.

    In any case, your code seems a bit confused. While you can have multiple color attachments to an FBO, FBOs only have a single depth attachment. Yet your code tries to use two of them. Only not really, since your call to glFramebufferTexture for the depth attachment doesn't actually set the depth attachment to that texture. The call passes 0 for the texture object, even though the code right before it set up a depth texture.

    So do you want to render depth to a renderbuffer or to the texture?

    Also, glDrawBuffers can only take color targets. The depth buffer always gets its value from gl_FragDepth in the fragment shader. That call should have given you an OpenGL error.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    23
    Sorry, I thought it would be easier to read the code with the syntax highlights that IntelliJ provide.
    So do you want to render depth to a renderbuffer or to the texture?
    I want to render depth to texture so I can use it for SSAO and lightcalculations.
    The call passes 0 for the texture object
    Should the last argument be the depth texture id, not the 4th one ?

    This is the code I currently have:

    GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo);

    for (int i = 0; i < textures.length; i++) {
    glBindTexture(GL_TEXTURE_2D, textures[i]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA,
    GL_UNSIGNED_BYTE, (ByteBuffer) null);
    GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0 + i, GL11.GL_TEXTURE_2D, textures[i], 0);

    }

    //depth texture

    glBindTexture(GL_TEXTURE_2D, depth);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (ByteBuffer) null);
    GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, depth, 0);

    IntBuffer db = Utils.createIntBuffer(2);

    db.put(GL30.GL_COLOR_ATTACHMENT0);
    db.put(GL30.GL_COLOR_ATTACHMENT1);
    db.flip();

    GL20.glDrawBuffers(db);

    int Status = GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) ;

    if (Status != GL30.GL_FRAMEBUFFER_COMPLETE) {
    System.out.println("ERROR: FrameBuffer: " + fbo + " din't complete !" + Utils.glError(Status));
    }

    // restore default FBO
    GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);

    I get a black texture instead of depth and no depth testing and error GL_INVALID_OPERATION​, 0x0502 shows up (I think 0x0502 shows up because of my mesh code) and I get error GL_INVALID_VALUE 0x0501 too...
    Last edited by RobertBerg; 04-04-2015 at 11:44 AM.

  4. #4
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    5,932
    Quote Originally Posted by RobertBerg View Post
    Should the last argument be the depth texture id, not the 4th one ?
    Actually, your code was right on that (I forgot that the last parameter is the image's level, not the texture object itself). The problem was that attaching the renderbuffer also unattaches the texture, since you can only have one depth attachment.

    Quote Originally Posted by RobertBerg View Post
    error GL_INVALID_OPERATION​, 0x0502 shows up (I think 0x0502 shows up because of my mesh code) and I get error GL_INVALID_VALUE 0x0501 too...
    And where do you get these errors?

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,515
    Quote Originally Posted by RobertBerg View Post
    Code :
            GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, depth, 0);
    GL_DEPTH_COMPONENT isn't valid here. The third parameter should be GL_TEXTURE_2D.

  6. #6
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    5,932
    Quote Originally Posted by GClements View Post
    GL_DEPTH_COMPONENT isn't valid here. The third parameter should be GL_TEXTURE_2D.
    Yet another reason to only use glFramebufferTexture and glFramebufferTextureLayer, where redundant information like that isn't part of the API.

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    23
    Ok, Thanks ! But now I get a white texture....

  8. #8
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    5,932
    Quote Originally Posted by RobertBerg View Post
    Ok, Thanks ! But now I get a white texture....
    So... what exactly is the problem? Is this a color texture that's coming out all white, or are you visualizing the depth buffer? And if it's the latter, are you visualizing it correctly, or are you just reading the top 8 bits?

    What do you expect to see and how is that different from what you're actually seeing?

  9. #9
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    23
    I expect to visualize the depth buffer from the depth texture, I resolved it, the problem whas that I din't linearize the depth value before I showed it. Thanks all for the help !
    Last edited by RobertBerg; 04-05-2015 at 07:06 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •