PDA

View Full Version : light position transformations in shader



Warzywo
06-08-2008, 02:06 AM
Hi, i'm trying to write point light shader, as listed.
When i use the light vector:

L = vec3(gl_LightSource[0].position - V4);

the position comes from GL_LIGHT and it's already in eye-space.
And thats fine - it works correctly, but what i need is the
light position passed as vec4 vector, for example vec4(5,5,5,1).

What exactly transformations do I need to use to make vec4 behave
like GL_LIGHT position ?

please help me :)


void main(void)
{
vec3 camPos = vec3(100,20,100);
vec3 L,R,H,C,Cx,S,V,N;
vec4 diff,spec,V4,L4;
vec4 color = gl_FrontLightModelProduct.sceneColor;
float diffuse, specular, d ,att;

C = vec3(0.0,0.0,1.0);
vec4 parameter[2];
parameter[0] = vec4(5,5,5,1);
parameter[1] = vec4(1,1,1,0);

gl_FrontColor = vec4(0.0);


//V4 = gl_Vertex;
N = normalize(gl_NormalMatrix * gl_Normal);
//N = normalize(gl_Normal);
N = N * gl_NormalScale;
//V4 = gl_ModelViewMatrix * gl_Vertex;
V4 = gl_ModelViewMatrix * gl_Vertex;


//N = normalize(gl_Normal);
vec4 position = vec4(parameter[0]);



specular = 0.0;

//L = normalize(vec3(gl_ModelViewMatrix * vec4(parameter[i].xyz,1.0)));

L = vec3(gl_LightSource[0].position - V4);

//L = vec3(position - V4);

//Cx = -normalize(V);
L = normalize(L);
diffuse = max(0.0,dot(N,L));


H = normalize(L + C);



if(diffuse > 0.0)
specular = pow(max(0.0,dot(N,H)),gl_FrontMaterial.shininess);

diff = vec4(parameter[1].xyz,0.0) * diffuse;
spec = vec4(parameter[1].xyz,0.0) * specular;

color += diff * gl_FrontMaterial.diffuse + spec * gl_FrontMaterial.specular;





gl_FrontColor = color;
gl_Position = ftransform();
}

dletozeun
06-08-2008, 03:09 AM
This post would be better in the shading language section.

Using a uniform variable to give the light position, you will need to transform it in eye space. So you need to pass the modelview matrix that transform your light in eye space, separately in an other uniform and then using it in your shader to transform the light position given in the vec4 uniform.

This is what I would do, I don't know if it is the best way. Why do you need to pass the light position as an uniform while opengl give exactly the same data in glsl built-in variables?

Warzywo
06-08-2008, 07:47 AM
Yes, You're right - I should put this post in SL ;)
The reason I want to pass light position (and other light params) is that I'm creating shader which computes many light sources (more than 8) :)