Jump to content
  • Advertisement
Sign in to follow this  

cg shader dot product

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

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
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!