Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Need a tip for Phong shading implementation.

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2012
    Posts
    21

    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?

    Thanks,
    Heinrich

  2. #2
    Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    352

    Re: Need a tip for Phong shading implementation.

    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.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  3. #3
    Member Regular Contributor
    Join Date
    Apr 2010
    Posts
    491

    Re: Need a tip for Phong shading implementation.

    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.

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2012
    Posts
    21

    Re: Need a tip for Phong shading implementation.

    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.
    Heinrich

  5. #5
    Junior Member Regular Contributor Kopelrativ's Avatar
    Join Date
    Apr 2011
    Posts
    212

    Re: Need a tip for Phong shading implementation.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •