Question

I have a shader which calculates diffuse lighting values. It consists of a vertex and a fragment shader that calculate the lighting intensity on a per-vertex basis. However, as expected, if I have a large GL_TRIANGLE with a light position, say, just above the center of the triangle, the light that should illuminate it does not appear because the light values are smoothly interpolated across the surface of the triangle based on the vertex calculations.

So my question is this- how can a primitive be lit by a light source at a position other than at one of its vertices?

Was it helpful?

Solution

When you do lighting at vertices, you're doing Phong lighting, which is computing a color at the vertices, and then merely interpolating the colors computed at the vertices (i.e., Gouraud shading) across all the pixels in the primitive. If you were to compute the lighting color at each pixel, you'd get Phong shading. Given your scenario, if you move the lighting computations from the vertex shader to the fragment shader, and interpolate the normal across the primitive you should get much better results.

If you make the vertex normal a varying variable and normalize it in the fragment shader, and then do your lighting computations, you should get much better results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top