PDA

View Full Version : Draw light with fragment shaders

CaptainSnugglebottom
07-13-2016, 04:27 PM
Hey there, I am trying to draw a source of light in the shading stage (using differed shading), basically I thought of using the light, converting it into projection space, then I would "project" its xy position onto my plane where I draw the final output (by taking pixel of the plane and converting it to the same [-1, -1]->[1, 1] xy range used in the projection space.

This is the portion of the code that is responsible for drawing the light on top of my differed shading output.

vec4 light_projspace = inverse(inv_projection) * inverse(inv_cameramat) * test_light_pos;
vec4 frag_projspace = gl_FragCoord;
frag_projspace.x = (frag_projspace.x - 0.5)/949;
frag_projspace.y = (frag_projspace.y - 0.5)/499;
frag_projspace.x = frag_projspace.x * 2.0 - 1.0;
frag_projspace.y = -(frag_projspace.y * 2.0 - 1.0);

float distance = length(light_projspace.xy - frag_projspace.xy);
if (distance < 0.05) {
lightParticle = vec3(1.0, 0.0, 0.0);
}

Right now I am simply trying to draw a red circle where the light would be in space, the procedure is:
- take light position, convert it to projection space (by the way, I know I am inverting inversion matrices which is dumb, this is just test code)
- getting coordinate of the fragment when I draw the differed shading output plane
- convert it to [-1, -1]->[1, 1] range
- get difference between

The issue is with the general light position, that makes the red circle float all over the screen. The plane is located at [-0.5, 0.0, 0.5] and the light is located at [-0.5, 0.0, 0.6], so the red circle is supposed to be right above the plane so that all shading makes sense (shading isn't the problem either). The screenies are included:

https://s31.postimg.org/5fsj0mwq3/lighterror1.png

https://s31.postimg.org/a2yl2ek2z/lighterror2.png

https://s31.postimg.org/3qjfskh0r/lighterror3.png

I verified the fragment position and it's working, so I assume the issue comes from the light, but it's a straight forward operation and I use matrices I've used so many times without issues.

Where could the issue come from?