Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

lights

Started by _gl_coder_one_ Jan 24, 2006 at 9:52 AM 3 replies 700+ views
Original Post
_gl_coder_one_
_gl_coder_one_
when we access the state.light[0].position in a vertex program , what coordinate space is the light position in ? I wrote the following program assuming the light position has not been transformed into eye space.
[source ] 
!!ARBvp1.0

#Vertex program for one point light loaded into GL_LIGHT0

#vertex attributes
ATTRIB	iPos	=	vertex.position;
ATTRIB	iNormal	=	vertex.normal;
ATTRIB	iColor	=	vertex.color;

#program parameters
PARAM	lightpos	=	state.light[0].position;
PARAM	lightdir	=	state.light[0].spot.direction;
PARAM	mvp[4]		=	{ state.matrix.mvp };
PARAM   mvinvt[4]	=	{ state.matrix.modelview.invtrans };
PARAM	mv[4]		=	{ state.matrix.modelview };
PARAM	d		=	state.material.diffuse;
PARAM	a		=	state.material.ambient;
PARAM	la		=	state.light[0].ambient;
PARAM	ld		=	state.light[0].diffuse;

#temps
TEMP	N,V,H,L,eV,temp1,temp2;
TEMP	dots;	

#result registers
OUTPUT	oColor		=	result.color;
OUTPUT	oPos		=	result.position;

#LIGHTING COMPUTATIONS

#transform vertex into eye space
DP3	eV.x	, mv[0] , iPos;
DP3	eV.y	, mv[1] , iPos;
DP3	eV.z	, mv[2] , iPos;

#transform lightpos to eye space
DP3	L.x	, mv[0] , lightpos;
DP3	L.y	, mv[1] , lightpos;
DP3	L.z	, mv[2] , lightpos;

#find the L vector : vector from vertex to light
SUB	L , L , eV;
#normalize L
DP3	temp1.x , L , L;	#temp1.x contains the magnitude
RSQ	temp2 , temp1.x;	#temp1.y contains the inv sq root
MUL	L , L , temp2;

#transform normal into eye space
DP3	N.x , mvinvt[0] , iNormal;
DP3	N.y , mvinvt[1] , iNormal;
DP3	N.z , mvinvt[2] , iNormal;

DP3	temp1.x , N , N;
RSQ	temp2 , temp1.x;
MUL	N,N,temp2;

#now we have N , L , H in eye space
#we compute the dot products

DP3	dots	,	N,L;
MAX	dots ,  	dots , {0,0,0,0};

#transform the vertex into homogenous clip space
DP4 oPos.x, mvp[0], iPos;
DP4 oPos.y, mvp[1], iPos;
DP4 oPos.z, mvp[2], iPos;
DP4 oPos.w, mvp[3], iPos;

#move original color to output
MUL	temp2	, d , ld;
MUL	temp1 , a , la;
MAD	oColor	, temp2 , dots.xxxx ,temp1;

END
	

Now the main render loop ges like this :

     vertex_shader->loadShader(); 
	
     static float rot ;
     float* pos;
     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     glColor3f(1.0,1.0,1.0 );
     glLoadIdentity();

     setupLights(0);

     glTranslatef( 0,0,-camera_z);
     glRotatef( rot ++ , 1,1,1);  
     
     //now render stuff

This code runs fine when the vertex program is enabled .i.e it renders the scene lit with a light positioned at 0,10,0 in world coordinates. But it does not render the samething when VERTEX_PROGRAM Is disabled and i switch back to conventional T&L. In conventional T&L the correct lighting is got with the setupLights(0) function being called after the translate and rotate funcs I feel my vertex program is correct or atleast am not able to spot the error. [Edited by - _gl_coder_one_ on January 24, 2006 11:36:31 PM]
frob
frob
See the fourth entry.

Like everything else, it's the base values. Transformations (and several other actions) are bypassed completely in vertex programs. Use state.matrix.* for transformation matrix values.

[Edited by - frob on January 24, 2006 11:51:06 AM]
_gl_coder_one_
_gl_coder_one_
Phantom sorry for reposting , will take care from now on .. but can you [help] me out with this problem
_gl_coder_one_
_gl_coder_one_
okay i found the problem

the state.light[0].position is actually the position in eye space
it seems to have been transformed by the modelviuew matrix when used
in the vertex program
_the_phantom_
_the_phantom_
yes, all in built light positions are transformed by the Modelview matrix when the data is submitted (call to glLight()), as stated in the specs and the OpenGL red book.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.