PDA

View Full Version : Parametric Shapes, and Tangent Space



laeuchli
02-11-2003, 02:10 AM
Dear All,
I have a vertex program that generates a sphere using a parametric equation. The equation is:
x=Sin(u)*Sin(v)
y=Cos(u)*Sin(v)
z=Cos(v)
I want to generate the tangent space by taking the derviate with respect to u,v, then cross the result. I have this:
//Generate sphere
sincos(In.pos.x,fxsin,fxcos);
sincos(In.pos.y,fysin,fycos);
Sphere.x=fysin*fxsin;
Sphere.y=fysin*fxcos;
Sphere.z=fycos;

//
float3 UTangent;
float3 VTangent;
float3 CrossProduct;
UTangent.x=fxcos*fysin;
UTangent.y=-fxsin*fysin;
UTangent.z=0;

//
VTangent.x=fycos*fxsin;
VTangent.y=fycos*fxcos;
VTangent.z=-fysin;
//
CrossProduct=cross(UTangent,VTangent);

These are the resultant partial derivitaves. Then I use it to transform the light vector as below:
CrossProduct=cross(UTangent,VTangent);
//
float3x3 TangentMatrix;
TangentMatrix[0]=normalize(UTangent);
TangentMatrix[1]=normalize(VTangent);
TangentMatrix[2]=normalize(CrossProduct);
float3 testl;
testl.x=1;//test light
testl.y=1;
testl.z=1;
float3 tlight=0.5 *mul(TangentMatrix,testl)+.5;
This is how it's done in the example programs, but it's not working in my program. In the first place, what lighting there is looks quite off, and when I move/rotate the sphere around it's as though I'm moving the light, and not the sphere. Do I need to change the tangent matrix somehow bassed on my current view matrix? If not, anyone have any other ideas?
Thanks,
Jesse www.laeuchli.com/jesse/ (http://www.laeuchli.com/jesse/)