1
\$\begingroup\$

I´ve implemented a 2d lighting system (also with support of 2d shadows). Everything was fine until today when I realised that its not working when moving the camera, as it looks like the lightmap has still some offset than the camera position (for example, if camera.x is 100, the lightmap is x=-200)

Little video showing the problem : Video

I can post any piece of code, for now Im sending you my light shader(fragment)

#version 440

in vec4 fragmentColor;
in vec2 TexCoords;

out vec4 color;

uniform float ambientStrength;

uniform vec2 resolution;
uniform sampler2D textureSampler;
uniform sampler2D lightMapTexture;

uniform bool textureON;

void main(){

    //Calc the ligthMap
    vec2 lightCoord = (gl_FragCoord.xy / resolution);
    vec4 lightMap = texture(lightMapTexture,lightCoord);

    if (textureON){

        vec4 textureColor =  texture(textureSampler, TexCoords);

        vec4 finalColor = lightMap * textureColor * fragmentColor;
        color =  vec4(finalColor.rgb,textureColor.a* fragmentColor.a);

    } else {

        color = fragmentColor;  
    }   
}

Vertex

#version 440

in vec3 vertexPosition;
in vec4 vertexColor;
in vec2 vertexUV;

out vec4 fragmentColor;
out vec2 TexCoords;

uniform mat4 Projection; 
uniform mat4 View;
uniform mat4 Model;

void main(){

    gl_Position = Projection * View * Model * vec4(vertexPosition.xy,0,1);  

    fragmentColor = vertexColor;

    TexCoords = vec2(vertexUV.x, 1.0 - vertexUV.y);
}

At first I thought that its a problem with resolution, but everything is good. Everything is working fine until I move the camera(or change its position from 0,0 to something else - all other objects have good position, also mouse coords)

\$\endgroup\$
  • \$\begingroup\$ What is the intended function? Is the light supposed to stay with the camera, or is it connected to a position on the scene? \$\endgroup\$ – Kyy13 Feb 1 '18 at 15:04
  • \$\begingroup\$ It is connected to a position in a scene . I do it like this: I render all lights(also with shadows) in a FBO and then i use this FBO texture as a lightmap (you see it in the shader). \$\endgroup\$ – Pins Feb 1 '18 at 21:57
  • \$\begingroup\$ @Pins why is the lightmap coord calculated with gl_FragCoord ? \$\endgroup\$ – Raxvan Feb 2 '18 at 10:44
  • \$\begingroup\$ @Raxvan I saw it in some article, as guy explained he is manually mapping the texture. Why? Is it causing the problem? Or is it wrong? \$\endgroup\$ – Pins Feb 2 '18 at 11:05
  • \$\begingroup\$ @Pins It's not wrong, but i don't think it's correct for what you want. I advise to use something similar to what you used for textureSampler and TexCoords. \$\endgroup\$ – Raxvan Feb 5 '18 at 11:04
0
\$\begingroup\$

Well I found a solution. The problem was , I was using the "render to texture" method for creating the lightmap. I forgot that it renders the screen including the camera position, so basically when I moved the created the lightmap with the camera offset, it was moved 2 times already. I solved it by creating a static camera (static I mean it does not move, not in programming context) and using this cameras view matrix for the render to texture part. Then i switched to normal camera. And now it works perfectly!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.