Smoove

07-19-2011, 04:02 AM

Good morning,

I am doing my first steps in shader coding using GLSL. For a start I wanted to create a lighting algorithm, so I browsed the internet and found some fairly simple examples. To suit my need I only changed the lights by adding my own custom ones, as uniform variables. Here the code:

// The shader is creating the light effects on a sphere

// from several light sources.

uniform vec3 Sphere_Position;

uniform int Light_Counter;

uniform vec3 Light_Position[20];

uniform vec3 Light_Color [20];

uniform float Light_Factor [20];

// Main function

gl_TexCoord[0] = gl_MultiTexCoord0;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

gl_FrontColor = gl_Color;

for(int i = 0; i < Light_Counter; i ++)

{

vec3 Light_Direction = -normalize(gl_Position.xyz - Light_Position[i].xyz);

vec3 Vertex_Normal = normalize(gl_NormalMatrix * gl_Normal);

float Dot_Product = max(dot(Vertex_Normal, Light_Direction), 0.0);

float Light_Distance = length(gl_Position.xyz - Light_Position[i].xyz);

float Final_Factor = (Dot_Product * Light_Factor[i]) / (Light_Distance * Light_Distance);

gl_FrontColor.xyz = gl_FrontColor.xyz * Light_Color[i].xyz * Final_Factor;

}

Now as the title suggest, when rendering, this produces a nice light/shadow effect, except for a small black circle zone in the middle of the sphere... Sounds to me like a very basic geometry principle, but I can't figure it out!

Any help would be very much appreciated!

Thank you,

Philippe

EDIT: I forgot to mention that to first keep it simple, I am testing with a single light. So the 'for' loop can be neglect for the moment!

I am doing my first steps in shader coding using GLSL. For a start I wanted to create a lighting algorithm, so I browsed the internet and found some fairly simple examples. To suit my need I only changed the lights by adding my own custom ones, as uniform variables. Here the code:

// The shader is creating the light effects on a sphere

// from several light sources.

uniform vec3 Sphere_Position;

uniform int Light_Counter;

uniform vec3 Light_Position[20];

uniform vec3 Light_Color [20];

uniform float Light_Factor [20];

// Main function

gl_TexCoord[0] = gl_MultiTexCoord0;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

gl_FrontColor = gl_Color;

for(int i = 0; i < Light_Counter; i ++)

{

vec3 Light_Direction = -normalize(gl_Position.xyz - Light_Position[i].xyz);

vec3 Vertex_Normal = normalize(gl_NormalMatrix * gl_Normal);

float Dot_Product = max(dot(Vertex_Normal, Light_Direction), 0.0);

float Light_Distance = length(gl_Position.xyz - Light_Position[i].xyz);

float Final_Factor = (Dot_Product * Light_Factor[i]) / (Light_Distance * Light_Distance);

gl_FrontColor.xyz = gl_FrontColor.xyz * Light_Color[i].xyz * Final_Factor;

}

Now as the title suggest, when rendering, this produces a nice light/shadow effect, except for a small black circle zone in the middle of the sphere... Sounds to me like a very basic geometry principle, but I can't figure it out!

Any help would be very much appreciated!

Thank you,

Philippe

EDIT: I forgot to mention that to first keep it simple, I am testing with a single light. So the 'for' loop can be neglect for the moment!