light position transformations in shader

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 :slight_smile:

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();
}

You want to transform vec4(5, 5, 5, 1) to eye space?
Multiply it by the modelview matrix.

I did - the transformation is done incorrectly. shouldn’t be there MV matrix passed from opengl program ? Cause this gl_ModelViewMatrix generates wrong result :stuck_out_tongue:

If you use the modelview matrix that transforms vertices, it transforms the light position too.

I know - it should, but it’s wrong - i tried such transformation. Does the ModelViewMatrix change during shader lifetime ? is it different at the begining and for example in the middle of the pipeline ?

No during the shader execution until you change the modelview matrix yourself in the application

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.