
Jeroen wrote: >>Hi all, >> >>Who knows a good way to calculate the angle between two vectors? I want to have fully dynamic >>lighting, and in order to do that I need to know the angle between two vectors (Vertexnormal >>and lightposition relative to that vertex). Doing this requires normalization of one of the >>vectors, which in turn requires a square root. Is there a way to avoid this, and still have >>full dynamics? >> >>Jeroen
Actually you can get away using an accelerated 1/sqrt() routine, depending on the precision you need you can get this down to as low as 11 cycles on a pentium based machine, which is faster than either the division or the sqrt independantly.
I dumped a copy of some accelerated 1/sqrt() code at http://www.bloodshed.com/~harmless/dl/sqrt.zip
You can use this code to accelerate vector normalization without ill effects. I'll see if I can dig up my very low precision LUT based version as well.
Basically instead of doing:
(x,y,z) / sqrt(x*x+y*y+z*z) it becomes (x,y,z) * oosqrt(x*x+y*y+z*z)
This doesn't address your problem of finding a way to determine the angle between non normalized vectors without the sqrt, it just speeds up the process a bit, which I think is about the best you can do without changing the problem, by precalculating more stuff.
Harmless
