Question

i'm having difficulties understanding the math between the different shader stages.

in the fragment shader from the lights perspective i basically write out the fragDepth to rgb color

#version 330

out vec4 shader_fragmentColor;

void main()
{
    shader_fragmentColor = vec4(gl_FragCoord.z, gl_FragCoord.z, gl_FragCoord.z, 1);
    //shader_fragmentColor = vec4(1, 0.5, 0.5, 1);
}

when rendering the scene using the above shader it displays the scene in an all white color. i suppose thats because gl_FragCoord.z is bigger than 1. hopefully its not maxed out at 1. but we can leave that question alone for now.

in the geometry shader from the cameras perspective i basicly turn all points into quads and write out the probably "incorrect" texture position to lookup in the lightTexture. the math here is the question. im also a bit unsure about if the interpolation value will be correct in the next shader stage.

#version 330
#extension GL_EXT_geometry_shader4 : enable

uniform mat4 p1_modelM;
uniform mat4 p1_cameraPV;
uniform mat4 p1_lightPV;

out vec4 shader_lightTexturePosition;

void main()
{
    float s = 10.00;

    vec4 llCorner = vec4(-s, -s, 0.0, 0.0);
    vec4 llWorldPosition = ((p1_modelM * llCorner) + gl_in[0].gl_Position);
    gl_Position = p1_cameraPV * llWorldPosition;
    shader_lightTexturePosition = p1_lightPV * llWorldPosition;
    EmitVertex();

    vec4 rlCorner = vec4(+s, -s, 0.0, 0.0);
    vec4 rlWorldPosition = ((p1_modelM * rlCorner) + gl_in[0].gl_Position);
    gl_Position = p1_cameraPV * rlWorldPosition;
    shader_lightTexturePosition = p1_lightPV * rlWorldPosition;
    EmitVertex();

    vec4 luCorner = vec4(-s, +s, 0.0, 0.0);
    vec4 luWorldPosition = ((p1_modelM * luCorner) + gl_in[0].gl_Position);
    gl_Position = p1_cameraPV * luWorldPosition;
    shader_lightTexturePosition = p1_lightPV * luWorldPosition;
    EmitVertex();

    vec4 ruCorner = vec4(+s, +s, 0.0, 0.0);
    vec4 ruWorldPosition = ((p1_modelM * ruCorner) + gl_in[0].gl_Position);
    gl_Position = p1_cameraPV * ruWorldPosition;
    shader_lightTexturePosition = p1_lightPV * ruWorldPosition;
    EmitVertex();

    EndPrimitive();
}

in the fragment shader from the cameras perspective i basicly lookup in the lightTexture what color would be shown from the lights perspecive and write out the same color.

#version 330

uniform sampler2D p1_lightTexture;

in vec4 shader_lightTexturePosition;

out vec4 shader_fragmentColor;

void main()
{
    vec4 lightTexel = texture2D(p1_lightTexture, shader_lightTexturePosition.xy);
    shader_fragmentColor = lightTexel;
    /*
    if(lightTexel.x < shader_lightTexturePosition.z)
        shader_fragmentColor = vec4(1, 0, 0, 1);
    else
        shader_fragmentColor = vec4(0, 1, 0, 1);
    */
    //shader_fragmentColor = vec4(1, 1, 1, 1);
}

when rendering from the cameras perspective i see the scene drawn as it should but with the incorrect texture coordinates applied on them that repeats. repeating texture is probably caused by the texture-coordinate being outside the bounds of 0 to 1.

I've tried several things but still fail to understand what the math should be. some of out commented code and one example im unsure of is:

shader_lightTexturePosition = normalize(p1_lightPV * llWorldPosition) / 2 + vec4(0.5, 0.5, 0.5, 0.5);

for the lower-left corner. similair code to the other corners

from the solution i expect the scene to be rendered from the cameras perspective with exactly the same color as from the lights perspective. with perhaps some precision error.

Was it helpful?

Solution

i figured out the texture mapping bit myself. the depth value bit is still a bit strange. convert the screenProjectedCoords to normalizedDeviceCoords then add 1 divide by 2.

vec4 textureNormalizedCoords(vec4 screenProjected)
{
    vec3 normalizedDeviceCoords = (screenProjected.xyz / screenProjected.w);
    return vec4( (normalizedDeviceCoords.xy + 1.0) / 2.0, screenProjected.z * 0.005, 1/screenProjected.w);
}

void main()
{
    float s = 10.00;

    vec4 llCorner = vec4(-s, -s, 0.0, 0.0);
    vec4 llWorldPosition = ((p1_modelM * llCorner) + gl_in[0].gl_Position);
    gl_Position = p1_cameraPV * llWorldPosition;
    shader_lightTextureCoords = textureNormalizedCoords(p1_lightPV * llWorldPosition);
    EmitVertex();a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top