View Full Version : Need a tip for Phong shading implementation.
01-18-2012, 03:46 PM
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?
01-18-2012, 04:20 PM
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.
01-18-2012, 04:24 PM
The lighthouse3d GLSL tutorial (http://www.lighthouse3d.com/opengl/glsl/) 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.
01-19-2012, 02:10 AM
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?
01-19-2012, 04:40 AM
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.