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

Thread: diffuse shader problem

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

    diffuse shader problem

    I have got a problem with diffuse light shader. Moving or rotating camera changes color on objects(light position is the same as camera position or something like that). I want to make stationary source of light.
    This is the shader I'm using:

    -VertexShader

    out vec4 outColor;
    void main()
    {
    vec3 LightPosi = vec3(0, 0, 0);
    vec4 DiffuseColor = vec4(1, 1, 1, 1);
    vec3 EyeNormal = gl_NormalMatrix * gl_Normal;
    vec4 VertPosi = gl_ModelViewMatrix * gl_Vertex;
    vec3 VertexPosition = VertPosi.xyz/VertPosi.w;
    vec3 LightDir = normalize(LightPosi - VertexPosition);
    float diff = max(0, dot(EyeNormal, LightDir));
    outColor.rgb = gl_Color.rgb + diff * DiffuseColor.rgb;
    outColor.a = 1;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    }

    -Fragment Shader

    in vec4 outColor;
    out vec4 FragColor;
    void main()
    {
    FragColor = outColor;
    }

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,169

    Re: diffuse shader problem

    This is wrong
    vec3 VertexPosition = VertPosi.xyz/VertPosi.w;

    The vertexposition should be in eye space just like your light position is supposed to be.
    So, don't divide by W

    Now back to your light position. This variable should be multiplied by the camera matrix (the view part of the ModelView matrix). This is why mixing legacy OGL matricies with shaders is a bit painful and your better off supplying your own model and camera matricies instead of using ModelView.

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

    Re: diffuse shader problem

    Is there any other way to do it besides making my own matrices? What do you think about using built-in gl_LightSource[n].position or loading ModelView matrix after gluLookAt?

  4. #4
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,169

    Re: diffuse shader problem

    Honestly, if you are going to the trouble of making a shader then you'll need to pass parameters (uniforms) at some point. When that happens it's time to let go of the fixed function pipeline as much as possible. Sure, it's really handy sometimes because you don't have to create and track your own uniform variables. But, long term, the fixed function just gets in the way and doesn't do what you want it to do.

    Forget gl_LightSource[n].position and friends. These rely upon performing gllightv commands which inturn rely upon the current value of the ModelView matrix. Trust me, it's better to start thinking of a strategy to roll your own matricies and uniforms sooner rather than later. When you have your own model matrix, camera matrix, etc you realise just how much freedom you have (especially when dealing with lighing and shadowing).

Posting Permissions

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