Need a tip for Phong shading implementation.

Hello Guys.
For flat shading implementation I used to calculate the colors in the vertex shader, and passed them to the fragment shader, which took care of the rest…

Now I want to apply the phong model. I guess I should pass the vertices values and the normals to the fragment shader to be interpolated and only then apply the lighting equation?

BTW: Is there any way I can control the interpolation using shaders?


You only need the normal and the light direction.
Remember to normalize the normal in the fragment shader otherwise you will the a linear interpolation.

You can specify to use the interpolation with the keyword
-flat: no interpolation
-noperspective: simple linear interpolation
-smooth (default): the interpolation is linear and perspective corrected.

The lighthouse3d GLSL tutorial implements the standard OpenGL light types in GLSL. For a directional light they show how to go from per-vertex to per-pixel lighting in detail.

Thanks, friends.
One more question about it:

So far I kept a list of light sources of maximum length in the vertex shader, declared as “uniform”.
Now in order to implement the Phong model I need the lights in the fragment shader…
Is there any intelligent way I can pass the lights from vertex to fragment shader withoug duplicating the same arrays in both of them?

Thanks again.

You can use the same uniforms in the fragment shader as you use in the vertex shader. They will refer to the same memory, with no copy cost needed. In your case, I suppose you would simply move the declarations as you no longer need them in the vertex shader.

Some types of light calculations depend not only on the normals, but also on the position. If so, you have to pass the vertex position data also from the vertex shader to the pixel shader.