PDA

View Full Version : FBO render depth to texture, cause depth texture to equal diffuse.



RobertBerg
04-04-2015, 04:28 AM
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:

http://i.imgur.com/hvqNmCX.png

What have I done wrong ?

Alfonse Reinheart
04-04-2015, 09:26 AM
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 (https://www.opengl.org/wiki/Framebuffer#Draw_color_buffers). The depth buffer always gets its value from gl_FragDepth in the fragment shader (https://www.opengl.org/wiki/Fragment_Shader#Other_outputs). That call should have given you an OpenGL error.

RobertBerg
04-04-2015, 11:01 AM
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...

Alfonse Reinheart
04-04-2015, 12:18 PM
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.


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?

GClements
04-04-2015, 12:34 PM
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.

Alfonse Reinheart
04-04-2015, 01:01 PM
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.

RobertBerg
04-04-2015, 01:18 PM
Ok, Thanks ! But now I get a white texture....

Alfonse Reinheart
04-04-2015, 02:25 PM
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?

RobertBerg
04-05-2015, 06:48 AM
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 ! :D