4
\$\begingroup\$

What I'm trying to do is add my depth buffers values to my scene, ie. I'm trying to make objects closer to the camera darker and objects further away lighter. Which should be easy: just render the depth buffer to a texture, and then render the scene, multiplying each pixels colour by the colour value at the same coordinates in my depth-buffer texture...

Yet I don't know how to use coordinates for textures.

Or more like it's just failing. What's happening is that the vertex shader is only using the (0,0) coordinate of my texture, so the entire scene changes colour depending on what's there.

What I'm doing is this (the critical stuff from my render function):

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthTextureId);   //Binds the texture    
box.render();    //Sets the viewing matrix and renders a test VBO

I guess that's the right way to bind a texture? Anyhoo, here's the vertex shader's important stuff:

out vec4 ShadowCoord;

void main() {
    gl_Position = PMatrix * (VMatrix * MMatrix) * gl_Vertex;  //Projection view and model matrices
    ShadowCoord = gl_MultiTexCoord0;    //something I kept seeing in examples, was hoping it would work.
}

It seems gl_MultiTexCoord0 is the problem? Maybe?

The frag shader sets the colour using this function:

vec4(texture2D(ShadowMap, ShadowCoord.st).x * vec3(Color), 1.0);

Where the ShadowMap is the sampler2D for the texture, and Color is the vertex's color...

So what am I missing? How come the coordinates are not changing from 0,0?

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

You don't even have to write the depth buffer to another texture, just convert your current fragment depth to linear space like shown here and multiply your fragment color with the inverse of the depth value obtained.

\$\endgroup\$
3
  • \$\begingroup\$ I'm definitely going to read through this and work it into my code, thanks. Still, I'd be interested to know why my implementation is stuck on (0,0)... in case I do need to map a texture across the entire screen at some point. But again, thank you, this'll come in handy. \$\endgroup\$
    – Fault
    Oct 15, 2011 at 22:48
  • 3
    \$\begingroup\$ Because you aren't specifying any texture coordinates. For your approach to work, you would need to draw a single quad with an orthographic projection that covered the entire screen, with the two textures applied (color and depth), and performed the blending in the shader. You can't just mix 3D and 2D without projecting one dimension in the other. \$\endgroup\$
    – r2d2rigo
    Oct 15, 2011 at 22:56
  • \$\begingroup\$ Ah, okay, I think i get it now. I guess I had things a little mixed up in terms of how the coordinate system works. \$\endgroup\$
    – Fault
    Oct 15, 2011 at 23:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .