Sign in to follow this  

cg shader dot product

Recommended Posts

Hi, in my shader i'm trying to compute the angle between two vectors, but i can't seem to figure out what i'm doing wrong. I was hoping that since this is supposed to be a fairly straightforward procedure someone could point out where i've gone wrong. I'm not too sure what the Cg functions throw out their results in (degrees? radians?) which further adds complication.
 float3 angle;
 angle = dot(oNormal, up);
 float modX; modX = length(oNormal);
 float modY; modY = length(up);
 oAngle = acos(angle / (modX * modY));
 oAngle *= 57.2957795;  // degrees
 oAngle /= 180.0f;	//should in theory produce a float between 0.0f and 1.0f

Share this post

Link to post
Share on other sites
I'm not sure if this'll help, but here's what I've got. Please note that I'm copying it and modifying it for display here, so this may or may not compile perfectly...

Without optimizations:
double magnitude(double v[3])
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);

void normalize(double v[3])
double m = magnitude(v);

v[0] /= m;
v[1] /= m;
v[2] /= m;

double dot(double v1[3], double v2[3])
return (v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]);

So, I guess the version using the code above would be (note that this modifies the vectors passed in; probably not what you want!!):
double angle;

// in radians
angle = acosf(dot(vector1, vector2));
// in degrees
angle = acosf(dot(vector1, vector2)) * (180 / PI);

The only thing I see is that I do the order a bit differently in normalizing the vectors before figuring the angle. As for the Cg functions, not a clue, so I can't help you there. :/

I know it's not much, but I hope it helps!


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this