GLSL passes texture coordinates from the vertex shader

What I'm trying to accomplish: Draw a depth map of my scene on top of my scene (so that objects closer are darker, and then easier)

Problem: I don't seem to understand how to pass the correct texture coordinates from my vertex shader to my fragment shader.

So, I created my FBO and the texture on which the depth map is drawn ... not that I'm completely sure what I'm doing, but whatever it is, it works. I tested drawing a texture using a fixed functionality pipeline and it looks as intended (depth map).

But trying to use it in my shaders just doesn't work ...

Here is the part from my render method that binds the texture:

glActiveTexture(GL_TEXTURE7); glBindTexture(GL_TEXTURE_2D, depthTextureId); glUniform1i(depthMapUniform, 7); glUseProgram(shaderProgram); look(); //updates my viewing matrix box.render(); //renders box VBO 

So ... I think this is correct? May be? I don’t know why texture 7, it was what was in the textbook that I checked ...

And here is the important thing from my vertex shader:

 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. } 

Aaand, fragment shader:

 in vec4 ShadowCoord; in vec3 Color; //passed from vertex shader, didn't include the code for it though. Just the vertex color. out vec4 FragColor; void main( FragColor = vec4(texture2D(ShadowMap,shadowCoord.st).x * vec3(Color), 1.0); 

Now the problem is that the coordinate that the fragment shader receives for the texture is always (0,0) or the lower left corner. I tried changing it to ShadowCoord = gl_MultiTexCoord7, because I thought maybe this is something due to the fact that I put the texture in slot number 7 ... but, alas, the problem persists. When the color (0, 0) changes, the color of the whole scene, not the color change, is only for the corresponding pixel / fragment.

And this is what I hope to get some idea of ​​how to convey the correct coordinates (I would like the corners of the texture to be the same coordinates as the corners of my screen). And yes, this is a newbie question ... but I looked in the Orange Book, and the problem with it is that it is excellent in GLSL, but the OpenGL side is very missing in the examples that I could really use ...

+4
source share
1 answer

The input variable gl_MultiTexCoord0 (or 7) is the built-in texture coordinate for the vertex for the 0th (or 7th) texture coordinate set by gl(Multi)TexCoord (when using immediate mode) or glTexCoordPointer (when using arrays / O).

But since the depth buffer is already in the screen space, what you want is not an ordinary texture superimposed on an object, but simply a value in the texture for a specific pixel / fragment. Thus, the vertex shader does not participate in any way. Instead, you simply use the current position of the screen as the texture coordinate, which can be read in the fragment shader using gl_FragCoord . But keep in mind that this coordinate is at [0, w] x [0, h], and the textures are available at the normalized coordinates of the texture in [0,1]. Thus, you should divide the fragment coordinate by the screen size:

 uniform vec2 screenSize; ... ... texture2D(ShadowMap, gl_FragCoord.st/screenSize) ... 

But you generally do not need two passes for this effect, since you can simply directly use the depth of the fragment without writing it to the texture. Instead

 texture2D(ShadowMap, gl_FragCoord.st/screenSize).x 

you can just use

 gl_FragCoord.z 

which is nothing more than the value of the depth of the fragment, which would be recorded in the texture in the first pass. Thus, you completely free the first pass of depth and access to the text on the second pass.

+4
source

All Articles