Results 1 to 2 of 2

Thread: convert Phong shading to Gouraud shading

  1. #1
    Newbie Newbie
    Join Date
    May 2015
    Posts
    1

    Unhappy convert Phong shading to Gouraud shading

    Code :
    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?

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674
    Quote Originally Posted by vinceeema View Post
    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 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.

    Quote Originally Posted by vinceeema View Post
    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.

Similar Threads

  1. Replies: 1
    Last Post: 12-14-2018, 08:16 PM
  2. Help with Gouraud/Phong Shading in Shaders
    By Primus in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-21-2015, 03:22 AM
  3. Difference between Gouraud and Phong Shading
    By explorer101 in forum OpenGL: General
    Replies: 8
    Last Post: 01-30-2015, 11:03 PM
  4. Gouraud and phong shading model
    By debinair in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 10-24-2013, 05:28 AM
  5. Phong-Shading vs. PerPixel-Lightning vs. Gouraud-Shading
    By DJSnow in forum OpenGL: Advanced Coding
    Replies: 15
    Last Post: 02-15-2000, 11:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean