Advertisement Jump to content
Sign in to follow this  

problem with multiple-light shader

This topic is 4376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a way of using multiple pp lights in a shader to render scene objects - but I seem to be having a problem with the light vector being messed up with the projection matrix. As I have a limit on the amount of variables I can pass between the vs and the fs, and therefore how many light vectors in tangent space I can pass over; i'm trying to do the ts stuff per light in the fs. Does anyone know what i'm doing wrong? vert:

attribute vec3 tangent;
attribute vec3 binormal;

varying vec3 T, B, N;
varying vec3 V;

uniform mat4 projMatrix;

void main()
	T	=	gl_NormalMatrix * tangent;
	B	=	gl_NormalMatrix * binormal;
	N	=	gl_NormalMatrix * gl_Normal;
	V	=	-vec3(gl_ModelViewMatrix * gl_Vertex);
	gl_TexCoord[1] = gl_MultiTexCoord0;
	gl_Position = ftransform();

light stuff in frag:
void	_computeLighting( int nLight, vec3 addSpecular, vec3 addLight, vec3 map_normal, vec3 map_specular ){
	//	per light colors
	vec3 	l_ambient;
	vec3 	l_diffuse;
	vec3 	l_specular;
	//	diffuse contribution
	float	diffuse;
	//	specular contribution
	float	specular;
	//	distance from light to vertex in TS
	float 	dist;	
	//	complete attenuation
	float 	atten;

	//	per light
	vec3 thisLight;
	vec3 thisSpecular;
	//	for each light
	vec3 L 		= vec3(gl_LightSource[nLight].position);
	vec3 LtoV 	= (L - V);
	//	TS vertex, and light
	Lts 		=  	(gl_NormalMatrix *LtoV) * TBN_Matrix; 	
	Vts 		=  	V*TBN_Matrix; 	
	// distance from light to pixel
	dist = abs(distance(Lts, Vts));
	// attenuation from light to pixel
	atten = 1.0 / (gl_LightSource[nLight].constantAttenuation +
          		 gl_LightSource[nLight].linearAttenuation * dist +
          		 gl_LightSource[nLight].quadraticAttenuation * dist * dist);
	l_ambient 	= 	gl_LightSource[nLight].ambient.rgb;
	l_diffuse 	= 	gl_LightSource[nLight].diffuse.rgb;
	l_specular 	= 	gl_LightSource[nLight].specular.rgb;

	diffuse = atten*clamp(dot(Lts, map_normal), 0.0, 1.0);

	thisLight		=	l_ambient+(l_diffuse*diffuse*atten);//+
	LightingComponent	+=	thisLight;

	thisSpecular		=	pow(clamp(dot(reflect(-Vts, map_normal), Lts), 0.0,1.0),16.0)*map_specular;
	SpecularComponent	+=	thisSpecular;


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!