Original Post
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. Now the main render loop ges like this : 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]
[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
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