Approximation of vector normalization
#1
Posted 10 November 2005 - 01:51 PM
Is there any fast way to approximate the normalized vector? I think I read somewhere that you can approximate the length of a vector somehow so you don't have to do a square root.
Any usefull information on this would be appreciated :)
#2
Posted 10 November 2005 - 03:12 PM
double cVector::FastLength() const{
// The original idea for this approximation comes from one of the "Graphic Gems"-books, where I found a 2D-version of the same thing.
// This is basically the manhattan distance, with the 2 smallest factors scaled down.
double a, b, c;
a=std::fabs(x);
b=std::fabs(y);
c=std::fabs(z);
// Assigning the greatest value to a.
if((b>a)&&(b>c)){
double Temp = b;
b=a;
a=Temp;
}
else if((c>a)&&(c>b)){
double Temp = c;
c=a;
a=Temp;
}
return a+(b+c)*0.366; // I found this value optimal. There is probably no point in finetuning it further, since there still is a 20% fault... (For an int version of the same function, use a one step bitshift (a+((b+c)>>1)) instead)
}
When I tested it it was about 20% faster, but I did no optimizations whatsoever.
#3
Posted 10 November 2005 - 03:20 PM
float math::fast_invSqrt(float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x; // get bits for floating value
i = 0x5f3759df - (i>>1); // give initial guess y0
x = *(float*)&i; // convert bits back to float
x *= 1.5f - xhalf*x*x; // newton step, repeating this step
// increases accuracy
//x *= 1.5f - xhalf*x*x;
return x;
}
The whole thing is described in detail here:
http://www.geometric...InverseSqrt.pdf
#4
Posted 10 November 2005 - 06:27 PM
I'm afraid neither approximation did wonders for my framerate. Perhaps i could squeeze out 1 more FPS, which isn't really worth the artifacts it produces. Guess I'll look for other "hot spots" to optimize.
Thanks again, your posts were most helpfull
#5
Posted 10 November 2005 - 09:00 PM
weevil said:
Must have looked terrible. Care to post a screenie?
#6
Posted 11 November 2005 - 12:13 AM
Reference shot made with true normalize

Notice how round the edge of the specular highlight is
Same shot made using approzimated normalize

Notice that the specular highlight forms a triangle towards the middle of the surface.
#7
Posted 11 November 2005 - 12:32 PM
Nice screenies, though. I like the bloom effect.
#8
Posted 11 November 2005 - 02:00 PM
geon said:
Nice screenies, though. I like the bloom effect.
Yeah i have a softspot for java :)
If you're interested, the bloom effect is achieved by saving the overflow of each pixel in a buffer (that is, the part of the color that is not rendered because it is above 255 in intensity). When the scene is rendered i do a post process pass where i apply box blur to the overflow buffer (can be done in O(n) complexity) and blend it unto the rendering surface using additive blending.
#9
Posted 14 November 2005 - 04:45 AM
weevil said:
The fastest normalization I know uses SSE assembly. It has two instructions that approximate a square root and a reciprocal and execute in just one clock cycle. Their precision is 12 mantissa bits, which is plenty for the normalization in lighting functions. Assembly instructions are directly accessible from C++.
So, unless you really want a cross-platform software renderer, Java is a dead end street. Most approximations and low-level 'optimizations' just slow things down. If you want to stick with Java, forget about perfomance almost completely. With C++ and assembly you can really do some nifty stuff.
#10
Posted 14 November 2005 - 01:19 PM
However the point of my project was never to build a 3d engine that could be used for games (or anything really) but simply to prove to myself I can do it myself, without the use of hardware. So I would rather work in an environment I feel comfortable with (Java) than an environment i hate but gives me better performance (native c++).
Trying to optimize something like square root was probably just force of habbit from my C++ days, and a case of me underestimating the overhead of envoking methods etc. in Java.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











