Xref: utzoo rec.games.programmer:3404 comp.os.msdos.programmer:4613 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!uflorida!reef.cis.ufl.edu!jdb From: jdb@reef.cis.ufl.edu (Brian K. W. Hook) Newsgroups: rec.games.programmer,comp.os.msdos.programmer Subject: 3D int/float optimizations stuff Message-ID: <28002@uflorida.cis.ufl.EDU> Date: 12 Apr 91 23:14:07 GMT Sender: news@uflorida.cis.ufl.EDU Organization: UF CIS Dept. Lines: 68 Thanks to everyone who helped with the optimizations. For those interested, I am posting the results of each optimization followed by the final source code. Summary: WOW! First pass: 21.86 seconds Optimizations: none (this means all doubles/floats) Second pass: 21.48 seconds Optimizations: converted functions to pass pointers to objects instead of copies of objects Third pass: 13.40 seconds(!) Optimizations: converted most of the calculations to fixed point integer calculations versus floating point Fourth pass: 13.35 seconds Optimizations: converted "tmp" variable from long to int Fifth pass: 12.46 seconds Optimizations: converted global variable from double to int Last pass: 12.20 seconds Optimizations: converted _yaw, _roll, and _pitch to ints from doubles I am sure that a couple of optimizations can still be done, most obviously those bit shifts (although I really doubt they matter much). I am not sure how accurate these calculations are, but I do know that they don't distort when displaying wireframe images which is all I care about. I chose to shift by 10 completely arbitrarily. If you have any suggestions, please post or mail. This is the actual function. It is based on pseudo code from Lee Adam's " Hi Performance Interactive Graphics in C" but is a pretty crappy book otherwise. /* WX, WY, and WY: World coordinates of object (relative to object's center) MX, MY, and MZ: Distance object is from viewpoint *DX, *DY: Final display coordinates */ void Calc3D ( int WX, int WY, int WZ, int MX, int MY, int MZ, int *DX, int *DY) { int tmp; long xa, ya, za; // Temp variables WX=-WX; xa=(_yawCosFactor*WX-_yawSinFactor*WZ)>>10; za=(_yawSinFactor*WX-_yawCosFactor*WZ)>>10; WX=(_rollCosFactor*xa+_rollSinFactor*WY)>>10; ya=(_rollCosFactor*WY-_rollSinFactor*xa)>>10; WZ=(_pitchCosFactor*za-_pitchSinFactor*ya)>>10; WY=(_pitchSinFactor*za+_pitchCosFactor*ya)>>10; WX+=MX; WY+=MY; WZ+=MZ; if (WZ==0) WZ=-1; tmp=AngularPerspFactor/WZ; *DX=tmp*WX+400; *DY=tmp*WY+300; }