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 6 of 6

Thread: Calculating Specular and GLFlat/Smooth

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    3

    Calculating Specular and GLFlat/Smooth

    update: The problem seems to be the G and B values. When I change them, nothing happens. Its weird because by default the values are 1.0,1.0,1.0,1.0 for specular lighting.

    Hi guys,

    first off, thanks for your help. I have been having difficulty with my glsl shader. I am suppose to implement the phoung model in the vertex shader. It seems to work for the most part, except my specular value is wrong I believe. When I run the program, my model appears red instead of white. I have two windows running. One is the opengl implementation which shows the correct output. I am trying to get my shader to match its output. Here is my code. I have gone through the red book, and can't find anything wrong in my calculation

    //calculate specular term
    //for the specular term, we need to get the halfway vector between the viewer and light.
    //If L n is less than or equal to zero, there is no specular component at the vertex

    if(dot(L,normal) <= 0.0)
    specularTerm = specularTerm * vec4(0.0,0.0,0.0,0.0);
    else{
    //calculate the halfway vector and normalize it
    halfwayVector =normalize(gl_LightSource[0].halfVector.xyz);

    //this gives us the "s" value from opengl pg 244
    //so now calculate the specular term
    //(max {s n, 0})^shininess * specularlight * specularmaterial

    float maxV = max(dot(halfwayVector,normal),0.0);

    //we dont want 0^0 possibility in opengl
    if(maxV > 0.0)
    specularTerm = pow(maxV,shine) * gl_LightSource[0].specular * specularMaterial;
    else//since 0 raised to anything is 1
    specularTerm = gl_LightSource[0].specular * specularMaterial;
    }

    Another problem I am having is with the shading type. My shader doesn't seem to recognize when glEnable(Flat) and glEnable(smooth) are on, yet it recognizes when lights are on/off.

    Here is a picture


    Thanks for your help!

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,165
    I think you should post the entire shader so we can see the all the terms defined, eg Light vector L

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    3
    Quote Originally Posted by BionicBytes View Post
    I think you should post the entire shader so we can see the all the terms defined, eg Light vector L
    ok, here you go.

    Also, everything seems to be working except the specular. if I set my specular values all to 0 then my shader matches my opengl image.

    http://pastebin.com/yvAWUbiV

    then my fragment shader is

    varying vec4 vColor;
    void main(){
    gl_FragColor = vColor;
    }

    thanks!

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,126
    Quote Originally Posted by Greenkirby21 View Post
    Another problem ... My shader doesn't seem to recognize when glEnable(Flat) and glEnable(smooth) are on, yet it recognizes when lights are on/off.
    See this post:
    * Emulating glShadeModel( GL_FLAT )?

    and this to tweak its behavior
    * ARB_provoking_vertex

  5. #5
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,126
    Quote Originally Posted by Greenkirby21 View Post
    When I run the program, my model appears red instead of white.
    Change subexpressions in your shader to null out terms (e.g. multiply one term by 0), and should be pretty simple to figure out where the red mask is coming from.

    Also, print out the uniform values you think you're feeding the shader. Betting one of those is set to a decidedly reddish value.

  6. #6
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    3
    Quote Originally Posted by Dark Photon View Post
    Change subexpressions in your shader to null out terms (e.g. multiply one term by 0), and should be pretty simple to figure out where the red mask is coming from.

    Also, print out the uniform values you think you're feeding the shader. Betting one of those is set to a decidedly reddish value.
    wow thanks for your help! I wasn't passing the shinyness value correctly! I fixed it and now it works! Thanks!!

Posting Permissions

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