Jump to content
  • Advertisement
Sign in to follow this  
cippyboy

Standard Lighting in a Vertex Program

This topic is 4949 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

For some unkown reason, the lighting changes if I move the camera and that seems really weird, plus it`s not even well computed,here`s exactly what I`m doing, please corect me if I`m wrong->
!!ARBvp1.0
#	
ATTRIB	vPos	= vertex.position;
ATTRIB	vNorm	= vertex.normal;
ATTRIB  vCol	= vertex.color;
ATTRIB  vTex	= vertex.texcoord[0];
#
OUTPUT oPos	= result.position;
OUTPUT oCol	= result.color;
OUTPUT oTex	= result.texcoord[0];

PARAM	M[4]	= { state.matrix.mvp };
PARAM  IM[4]	= { state.matrix.modelview.invtrans };

PARAM LightPosition =state.light[0].position;
TEMP  tPos,fLight,fNorm,lighted;
MOV fNorm,vNorm;

#
DP4  tPos.x, M[0], vPos;	
DP4  tPos.y, M[1], vPos;	
DP4  tPos.z, M[2], vPos;	
DP4  tPos.w, M[3], vPos;	
#
DP3  fNorm.x,IM[0], vNorm;	
DP3  fNorm.y,IM[1], vNorm;	
DP3  fNorm.z,IM[2], vNorm;



MOV oPos,tPos;
MOV oTex, vTex;

DP3 lighted, fNorm, LightPosition;
MUL lighted,lighted,vCol;

MOV oCol,lighted;

END


I`m completely confused with the matrices, dunno If I`m supposed to multiplicate the invtrans with the normal and should I multiplicate the lightposition with something ?

Share this post


Link to post
Share on other sites
Advertisement
After a few headaches I discovered some pretty interesting things. First of all If I pass the light position through a program parameter everything looks pretty good(I get my diffuse lighting after all),that is without making any Matrix Multiplication or anything,just normal and lightdirection normalized.

So... my light position differse of what is written in state.light[0].position, why ? I thought it might be multiplicated through some OpenGL matrix, so I tryed all of the following:
mvp
mvp.transpose
mvp.inverse
mvp.invtrans
modelview
modelview.transpose
modelview.inverse
modelview.invtrans
projection
projection.transpose
projection.inverse
projection.invtrans

And still it doesn`t look right... I computed all that using something like

DP3 fLightVec.x, M[0], LightVec;
DP3 fLightVec.y, M[1], LightVec;
DP3 fLightVec.z, M[2], LightVec;



My other guess would be that the normal should be multiplicated with one matrix and the lightvector with other... but theyre too many combinations to just try them all and I`m already bored with it... also DP4/DPH would make any difference ?

Any thought is greatly appreciated

Share this post


Link to post
Share on other sites
state.light.position comes already transformed by current modelview-matrix to the shader.
In the shader you just need to calculate
light_vec = light_pos - vertex_pos;
And then do
light_affect = dotproduct ( vertex_normal, light_vec );

Some boost could be gained, if you'd inverse-rotate the lightpos by modelview matrix in actual program (not the shader), then use "OPTION ARB_position_invariant" in the shader, this would just copy the transformed vertex-position to output register automatically, and you don't need to do that yourself. And cause the light is now sitting at the model-space (through inverse modelview-matrix), you don't need to rotate the normal either :)

[edit]
Oh, forgot to say, is you also bring the transformed light-pos via local parameters, and disable lighting in gl-states.
[/edit]

ch.

Share this post


Link to post
Share on other sites
Thanks for the tip, So if I had the light in Clip Space(BTW, does someone know where I can find documentation regarding Clip Space/Eye Space/etc ? It`s getting me dizzy sometimes with all those matrices). So if lightpos was already transformed as I`ve seen, I`ve multiplicatd with the inverse modelview and... it worked as if I passed the coords manually(now I don`t have to do things manually any more->cool). Now... How Do I make specular lighting and how is that computed (oh and... why is it computed so :D)?
So far, So good->

!!ARBvp1.0
#
ATTRIB vPos = vertex.position;
ATTRIB vNorm = vertex.normal;
ATTRIB vCol = vertex.color;
ATTRIB vTex = vertex.texcoord[0];
ATTRIB vTex1 = vertex.texcoord[1];
ATTRIB vTex2 = vertex.texcoord[2];
#
OUTPUT oPos = result.position;
OUTPUT oCol = result.color;
OUTPUT oTex = result.texcoord[0];
OUTPUT oTex1 = result.texcoord[1];
OUTPUT oTex2 = result.texcoord[2];

PARAM M[4] = { state.matrix.mvp };
PARAM IVM[4] = { state.matrix.modelview.inverse };
#Trash
PARAM M2[4] = { state.matrix.modelview };
PARAM PM[4] = { state.matrix.projection };
PARAM IM[4] = { state.matrix.mvp.inverse};

PARAM LP =state.light[0].position;
#PARAM LP =state.light[0].half;
#PARAM LP =program.env[0];#{0,35,0,0};
TEMP tPos,fNorm,lighted,LightVec,fLightVec,LightPosition,FinalColor;

#
DP4 tPos.x, M[0], vPos;
DP4 tPos.y, M[1], vPos;
DP4 tPos.z, M[2], vPos;
DP4 tPos.w, M[3], vPos;

#
DP4 LightPosition.x, IVM[0], LP;
DP4 LightPosition.y, IVM[1], LP;
DP4 LightPosition.z, IVM[2], LP;
DP4 LightPosition.w, IVM[3], LP;

#Get Light Vector
SUB LightVec,LightPosition,vPos;

#Normalize
DP3 LightVec.w,LightVec,LightVec;
RSQ LightVec.w,LightVec.w;
MUL LightVec,LightVec,LightVec.w;


#N Dot Light
DP3 lighted,LightVec,vNorm;
MOV FinalColor,lighted;

MOV oPos,tPos;
MOV oTex, vTex;
MOV oTex1,vTex;
MOV oTex2,LightVec;
MOV oCol,FinalColor;

END

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!