PDA

View Full Version : How to do a simple point omnidirectional light.



Alberto García
09-24-2014, 05:59 AM
Hi, I'm trying to do a simple point omniderectional light shader.

Here my code for vertex shader:



vec3 lightPos, vertexPos, normal, s;
varying float i;

void main()
{
normal = normalize( gl_NormalMatrix * gl_Normal );
vertexPos = vec3( gl_Vertex * gl_ModelViewMatrix );
lightPos = vec3( 0, 0, 0 );

s = normalize( lightPos - vertexPos );
// The diffuse shading equation
i = dot( s, normal );

gl_Position = ftransform();
}



and this is my fragment shader:



varying float i;

void main (void)
{
gl_FragColor = vec4( i, i, i, 1.0);
}



It doesn't work fine. I see all my sphere black, and it is positionate in 0, 3, 0 coords.

Any help?

GClements
09-24-2014, 10:17 PM
vertexPos = vec3( gl_Vertex * gl_ModelViewMatrix );


This should be reversed:


vertexPos = vec3( gl_ModelViewMatrix * gl_Vertex );

The original version effectively transposes the matrix.

Alberto García
09-25-2014, 02:00 AM
Lot of thanks for your response, it no works fin, but the problem now is that I moved the ball, and always recive the same lighting.