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

Thread: GLSL shader problem

  1. #1
    Intern Contributor
    Join Date
    Mar 2012
    Posts
    50

    GLSL shader problem

    My shader program is clearly not computing vectors correctly, because I get these results:

    NO Shader(fixed-function):

    Shader


    This is a plain diffuse shader that is supposed to create a green diffuse on the object:
    .vert
    Code :
    varying vec3 vertex_light_position;
    varying vec3 vertex_light_half_vector;
    varying vec3 vertex_normal;
    varying vec4 color;
    varying vec3 N;
    varying vec3 v;
     
     void main(void)
       {
          v = vec3(gl_ModelViewMatrix * gl_Vertex);
          N = normalize(gl_NormalMatrix * gl_Normal);
     
          color = gl_Color;
          vertex_normal = normalize(gl_NormalMatrix * gl_Normal);
          vertex_light_position = normalize(gl_LightSource[0].position.xyz);
          vertex_light_half_vector = normalize(gl_LightSource[0].halfVector.xyz);
     
          gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
       }

    .frag
    Code :
    varying vec3 vertex_light_position;
    varying vec3 vertex_light_half_vector;
    varying vec3 vertex_normal;
    varying vec4 color;
    varying vec3 N;
    varying vec3 v;
     
    void main()
    {  
       vec3 L = normalize(gl_LightSource[0].position.xyz - v);
       vec3 E = normalize(-v);
       vec3 R = normalize(reflect(-L,N));
     
       vec4 diffuse_color = vec4(0.0,1.0,0.0,0.0);
       float diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0);
     
       if (gl_FrontFacing){
           gl_FragColor = color + ((diffuse_color-color) * (diffuse_value));
       }else{
           gl_FragColor = color;
       }
     
    }

    Im confused, because I get the correct effect on my shader designer:

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    800
    The diffuse component should be dot(N,L) i.e eye space vertex normal with eye space light vector (vector from lightpos to vertex position in eye space) So you should change this line
    Code :
    float diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0);
    to this
    Code :
    float diffuse_value = max(dot(N, L), 0.0);

    PS: There are some redundant calculations going on here. you are calculating the eye space vertex postiion and normals twice.
    Regards,
    Mobeen

  3. #3
    Intern Contributor
    Join Date
    Mar 2012
    Posts
    50
    Works perfect, thanks Mobeen! I understand the error in the dot product, now. http://oi33.tinypic.com/2je1qir.jpg

Posting Permissions

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