PDA

View Full Version : convert Phong shading to Gouraud shading

vinceeema
05-04-2015, 03:16 AM
vec4 light_ambient= vec4(0.2, 0.2, 0.2, 1.);
vec4 light_diffuse= vec4(1., 1., 1., 1.);
vec4 light_specular= vec4 (1., 1., 1., 1.);

vec4 material_ambient = vec4(.3, .6, .3, 1.);
vec4 material_diffuse = vec4(0.3, .6, 0.3, 1.);
vec4 material_specular = vec4(1., 1., 1., 1.);
float Shininess = 100.0;

vec4 AmbientProduct = light_ambient * material_ambient;
vec4 DiffuseProduct = light_diffuse * material_diffuse;
vec4 SpecularProduct = light_specular * material_specular;

vec3 N = normalize (fN);
vec3 E = normalize (fE);
vec3 L = normalize (fL);

vec3 H = normalize(L + E);

// Compute terms in the illumination equation
/*
vec4 ambient = AmbientProduct;
float Kd = max(dot(L, N), 0.0);
vec4 diffuse = Kd*DiffuseProduct;
float Ks = pow(max(dot(N, H), 0.0), Shininess);
vec4 specular = Ks * SpecularProduct;
if(dot(L, N) < 0.0)
specular = vec4(0.0, 0.0, 0.0, 1.0);
*/

vec4 ambient = AmbientProduct;
vec4 diffuse = DiffuseProduct;
vec4 specular = SpecularProduct;

gl_FragColor = ambient + diffuse + specular;
gl_FragColor.a = 1.0;
I know both shading method have to determine the normal at each vertex. But, the linear interpolation and illumination orders are different. As you can see, this is the phong shading. I did not get the part after calculating normal. How does it do the linear interpolation and illunimation? I stuck on this for a long time. If I want to change it to Gouraud shading, what should I change?

Alfonse Reinheart
05-04-2015, 06:59 AM
I did not get the part after calculating normal. How does it do the linear interpolation and illunimation?

It doesn't "calculate" the normal. The normal is passed in from the vertex shader, and interpolated just like any per-vertex value passed from the vertex shader. These values are linearly interpolated in accord with the various interpolation qualifiers (https://www.opengl.org/wiki/Type_Qualifier_%28GLSL%29#Interpolation_qualifiers ) used on the input variables in the fragment shader.

Normals are usually part of the mesh's data, just like positions, texture coordinates, colors, etc.

Illumination (aka: lighting)... is what this code does. It explicitly spells out exactly how it does it. Or at least it did until you commented out the actual lighting part of the lighting equation.

If I want to change it to Gouraud shading, what should I change?

You move the code from the fragment shader to the vertex shader. That is basically the only difference between Gouraud shading and Phong shading.

Well, if you want to apply textures, you probably don't want to have the material color multiplied into the lighting parameters. So you'd remove the "*Product" variables and compute the rest without the colors, then pass those three values to the fragment shader.