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: Ugly per Vertex/Fragment Lighting

  1. #1
    Intern Newbie
    Join Date
    Feb 2010
    Location
    France
    Posts
    46

    Ugly per Vertex/Fragment Lighting

    Hi,
    Having touched Glsl for the first time recently, I thought I could use it to make my lighting more realistic, so I tried to use a per vertex or a per fragment lighting shader, but in both cases, my lighting results in a really ugly ripple effect on the plane surface I use for testing.
    Here is my fragment shader source, and an image of the result.
    If anyone has an idea of the problem, Thank you for answering (and sorry for my poor english )

    --- Vertex shader ---
    Code :
    varying vec3 Position;
    varying vec3 Normal;
     
    void main()
    {
    	gl_Position = ftransform();
     
    	gl_FrontColor = gl_Color;
     
    	Position = vec3(gl_ModelViewMatrix * gl_Vertex);
    	Normal = normalize(gl_NormalMatrix * gl_Normal);
    }

    --- Fragment shader ---
    Code :
    varying vec3 Position;
    varying vec3 Normal;
     
    float Shininess = 10.0;
     
    void main()
    {
     
    	vec3 ToLightVector = normalize(gl_LightSource[0].position.xyz - Position);
    	vec3 ToEyeVector = normalize(- Position);
    	vec3 LightReflectVector = reflect(-ToLightVector, Normal);
     
    	vec3 Color = gl_Color.rgb;
     
    	vec3 AmbientColor = Color * gl_LightSource[0].ambient.rgb;
    	vec3 DiffuseColor = Color * gl_LightSource[0].diffuse.rgb * abs(dot(ToLightVector, Normal));
    	vec3 SpecularColor = Color * gl_LightSource[0].specular.rgb * pow(max(dot(LightReflectVector, ToEyeVector), 0.0), Shininess);
     
    	gl_FragColor.rgb = AmbientColor + DiffuseColor + SpecularColor;
    	gl_FragColor.a = gl_Color.a;
    }

    --- Image ---
    Well, the quality is bad, and it's a little hard to see, but in fullscreen mode, it hurts the eyes, really
    Lighting.png

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Ugly per Vertex/Fragment Lighting

    Sorry but this picture look perfectly fine, unless I miss something ?

  3. #3
    Member Regular Contributor
    Join Date
    Oct 2006
    Posts
    349

    Re: Ugly per Vertex/Fragment Lighting

    Indeed, the picture looks perfectly fine. The effect is only visible upon zooming 10-15x on my CRT monitor. Of course, if you are using a cheap, 18bit TN monitor the artifact will be much more visible.

    An amount of banding is normal. 24bit color means 8bits per component or 256 distinct shades per component. The eye can easily distinguish between them (you'd need at least 1024 shades for smooth gradients). Even worse, cheap monitors can only display 18bit colors (6bits per component or 64 shades) which makes the issue much more pronounced.

    It's not the end of the world, however. Just add some textures and the effect will all but disappear.

  4. #4
    Intern Newbie
    Join Date
    Feb 2010
    Location
    France
    Posts
    46

    Re: Ugly per Vertex/Fragment Lighting

    Well, maybe I was wrong, indeed the problem isn't visible anymore when I use meshes instead of a large flat surface (I don't want to use textures).
    Sorry for having doubted of GLSL's power , and thank you again.

Posting Permissions

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