home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / lib / vecmat.h < prev   
Encoding:
C/C++ Source or Header  |  2000-12-18  |  6.5 KB  |  358 lines

  1. #define FRAND (((rand()%10000)-5000)/5000.0f)
  2. #define FABSRAND ((rand()%10000)/10000.0f)
  3.  
  4. #define vec_dot(v1,v2) ((v1).x*(v2).x+(v1).y*(v2).y+(v1).z*(v2).z)
  5.  
  6. #define SMALL 1.0e-4f
  7. #define BIG   1.0e+10f
  8. #define ISZERO(a) ((a)>-SMALL && (a)<SMALL)
  9.  
  10. #define M_Pi2            1.570796326795f
  11. #define M_Pi            3.14159265359f
  12. #define M_2Pi            6.28318530718f
  13. #define PiOver180        1.74532925199433E-002f
  14. #define PiUnder180        5.72957795130823E+001f
  15. #define PiOver180        1.74532925199433E-002f
  16.  
  17. #define FP_BITS(fp) (*(DWORD *)&(fp))
  18. #define FP_ABS_BITS(fp) (FP_BITS(fp)&0x7FFFFFFF)
  19. #define FP_SIGN_BIT(fp) (FP_BITS(fp)&0x80000000)
  20. #define FP_ONE_BITS 0x3F800000
  21.  
  22. //#define P3SIMD
  23.  
  24. class FLY_API vector;        // 4 floats
  25. class FLY_API quaternion;    // 4 floats
  26. class FLY_API mat4x4;        // 16 floats
  27.  
  28. class FLY_API vector
  29. {
  30.     public:
  31.         float x,y,z,w;
  32.  
  33.     inline vector() 
  34.     { ; };
  35.     inline vector(float x0,float y0,float z0)
  36.     { x=x0; y=y0; z=z0; };
  37.     inline vector(float x0,float y0,float z0,float w0)
  38.     { x=x0; y=y0; z=z0; w=w0; };
  39.     inline vector(vector &v) 
  40.     { *this=v; };
  41.     inline void null(void)
  42.     { x=y=z=0; };
  43.     inline float length(void)
  44.     { return (float)sqrt(x*x+y*y+z*z); };
  45.     inline void  vec(float x0,float y0,float z0)
  46.     { x=x0; y=y0; z=z0; };
  47.     inline void  vec(float x0,float y0,float z0,float w0)
  48.     { x=x0; y=y0; z=z0; w=w0; };
  49.     inline void  negate(void)
  50.     { x=-x; y=-y; z=-z; };
  51.     inline void cross(vector& v1,vector& v2)
  52.     {
  53. #ifdef P3SIMD
  54.         __asm
  55.         {
  56.         mov esi, v1
  57.         mov edi, v2
  58.  
  59.         movups xmm0, [esi]
  60.         movups xmm1, [edi]
  61.         movups xmm2, xmm0
  62.         movups xmm3, xmm1
  63.  
  64.         shufps xmm0, xmm0, 0xc9
  65.         shufps xmm1, xmm1, 0xd2
  66.         mulps xmm0, xmm1
  67.  
  68.         shufps xmm2, xmm2, 0xd2
  69.         shufps xmm3, xmm3, 0xc9
  70.         mulps xmm2, xmm3
  71.  
  72.         subps xmm0, xmm2
  73.  
  74.         mov esi, this
  75.         movups [esi],xmm0
  76.         }
  77. #else
  78.         x=v1.y*v2.z-v1.z*v2.y;
  79.         y=v1.z*v2.x-v1.x*v2.z;
  80.         z=v1.x*v2.y-v1.y*v2.x;
  81. #endif
  82.     }
  83.     inline void normalize(void)
  84.     {
  85.      float len=(float)sqrt(x*x+y*y+z*z);
  86.      if (FP_BITS(len)==0) return;
  87.      len=1.0f/len;
  88.      x*=len; y*=len; z*=len;
  89.     }
  90.     inline float& operator[](int i) { return (&x)[i]; };
  91. };
  92.  
  93. class FLY_API mat4x4
  94. {
  95. public:
  96.     float m[4][4];
  97.  
  98.     inline void null(void)
  99.     {
  100.         memset(&m,0,sizeof(m));
  101.     }
  102.     inline void load_identity(void)
  103.     {
  104.          memset(m,0,sizeof(m));
  105.          m[0][0]=m[1][1]=m[2][2]=m[3][3]=1.0;
  106.     }
  107.  
  108.     void set_rotation( float ang,vector& dir );
  109.     void rotate( float ang,vector& dir );
  110.     void lerp(mat4x4& m1,mat4x4& m2,float t);
  111.  
  112.     inline mat4x4 operator*(mat4x4& m1)
  113.     {
  114.       mat4x4 m2;
  115. #ifdef P3SIMD
  116.       __asm
  117.       {
  118.         mov edi,m1
  119.         movups xmm4,[edi]
  120.         movups xmm5,[edi+16]
  121.         movups xmm6,[edi+32]
  122.         movups xmm7,[edi+48]
  123.  
  124.         mov esi,this
  125.         mov    eax,0
  126.  
  127.         LOOP:
  128.         movups xmm0,[esi+eax]
  129.         movups xmm1,xmm0
  130.         movups xmm2,xmm0
  131.         movups xmm3,xmm0
  132.  
  133.         shufps xmm0,xmm2,0x00
  134.         shufps xmm1,xmm2,0x55
  135.         shufps xmm2,xmm2,0xAA
  136.         shufps xmm3,xmm3,0xFF
  137.         
  138.         mulps xmm0,[edi]
  139.         mulps xmm1,[edi+16]
  140.         mulps xmm2,[edi+32]
  141.         mulps xmm3,[edi+48]
  142.  
  143.         addps xmm0,xmm1
  144.         addps xmm0,xmm2
  145.         addps xmm0,xmm3
  146.  
  147.         movups m2+eax,xmm0
  148.  
  149.         add eax, 16
  150.         cmp    eax, 48
  151.         jle LOOP
  152.       }
  153. #else
  154.       int i,j,k;
  155.       float ab;
  156.  
  157.       for(i=0; i<4; i++)
  158.         for(j=0; j<4; j++)
  159.         {
  160.           ab=0;
  161.           for(k=0; k<4; k++)
  162.                ab+=m[i][k]*m1.m[k][j];
  163.           m2.m[i][j]=ab;
  164.         }
  165. #endif
  166.       return m2;
  167.     };
  168. };
  169.  
  170. class FLY_API quaternion : public vector
  171. {
  172. public:
  173.     quaternion() {};
  174.     quaternion(mat4x4 &mat);
  175.     quaternion(float angle, vector &axis)
  176.     {
  177.         float f=(float)sin(angle*PiOver180*0.5f);
  178.         x=axis.x*f;
  179.         y=axis.y*f;
  180.         z=axis.z*f;
  181.         w=(float)cos(angle*PiOver180*0.5f);
  182.     };
  183.  
  184.     void normalize();
  185.     void get_mat(mat4x4 &mat);
  186.     void get_rotate(float &angle, vector &axis);
  187.     void lerp(quaternion& q1,quaternion& q2,float t);
  188.  
  189.     quaternion operator *(quaternion &q);
  190.     quaternion operator +(quaternion &q)
  191.     {
  192.         quaternion res;
  193.         res.x=x+q.x;
  194.         res.y=y+q.y;
  195.         res.z=z+q.z;
  196.         res.w=w+q.w;
  197.         return res;
  198.     };
  199. };
  200.  
  201. inline void operator*=(vector& v,float f)
  202. {
  203.   v.x*=f; v.y*=f; v.z*=f;
  204. }
  205. inline void operator/=(vector& v,float f)
  206. {
  207.   v.x/=f; v.y/=f; v.z/=f;
  208. }
  209. inline void operator-=(vector& v1,vector& v2)
  210. {
  211.   v1.x-=v2.x; v1.y-=v2.y; v1.z-=v2.z;
  212. }
  213. inline void operator+=(vector& v1,vector& v2)
  214. {
  215.   v1.x+=v2.x; v1.y+=v2.y; v1.z+=v2.z;
  216. }
  217. inline void operator*=(vector& v1,vector& v2)
  218. {
  219.   v1.x*=v2.x; v1.y*=v2.y; v1.z*=v2.z;
  220. }
  221. inline void operator/=(vector& v1,vector& v2)
  222. {
  223.   v1.x/=v2.x; v1.y/=v2.y; v1.z/=v2.z;
  224. }
  225. inline vector operator-(vector v1,vector& v2)
  226. {
  227.    v1.x-=v2.x; v1.y-=v2.y; v1.z-=v2.z;
  228.    return v1;
  229. }
  230. inline vector operator+(vector v1,vector& v2)
  231. {
  232.    v1.x+=v2.x; v1.y+=v2.y; v1.z+=v2.z;
  233.    return v1;
  234. }
  235. inline vector operator*(vector v1,vector& v2)
  236. {
  237.    v1.x*=v2.x; v1.y*=v2.y; v1.z*=v2.z;
  238.    return v1;
  239. }
  240. inline vector operator/(vector v1,vector& v2)
  241. {
  242.    v1.x/=v2.x; v1.y/=v2.y; v1.z/=v2.z;
  243.    return v1;
  244. }
  245. inline vector operator*(float f,vector v)
  246. {
  247.    v.x*=f; v.y*=f; v.z*=f;
  248.    return v;
  249. }
  250. inline vector operator*(vector v,float f)
  251. {
  252.    v.x*=f; v.y*=f; v.z*=f;
  253.    return v;
  254. }
  255. inline vector operator/(vector v,float f)
  256. {
  257.    v.x/=f; v.y/=f; v.z/=f;
  258.    return v;
  259. }
  260.  
  261. inline vector operator*(vector& v,mat4x4& m)
  262. {
  263.   vector a;
  264. #ifdef P3SIMD
  265.     v.w=1.0f;
  266.     __asm
  267.     {
  268.         mov esi, v
  269.         mov edi, m
  270.  
  271.         movups xmm0,[esi]
  272.         movups xmm1,xmm0
  273.         movups xmm2,xmm0
  274.         movups xmm3,xmm0
  275.  
  276.         shufps xmm0,xmm2,0x00
  277.         shufps xmm1,xmm2,0x55
  278.         shufps xmm2,xmm2,0xAA
  279.         shufps xmm3,xmm3,0xFF
  280.         
  281.         movups xmm4,[edi]
  282.         movups xmm5,[edi+16]
  283.         movups xmm6,[edi+32]
  284.         movups xmm7,[edi+48]
  285.  
  286.         mulps xmm0,xmm4
  287.         mulps xmm1,xmm5
  288.         mulps xmm2,xmm6
  289.         mulps xmm3,xmm7
  290.  
  291.         addps xmm0,xmm1
  292.         addps xmm0,xmm2
  293.         addps xmm0,xmm3
  294.  
  295.         movups a,xmm0
  296.     }
  297. #else
  298.     float *f=&a.x;
  299.     for(int j=0; j<3; j++,f++ )
  300.     {
  301.         *f=0;
  302.         for(int i=0; i<3; i++)
  303.             *f+=*(&v.x+i)*m.m[i][j];
  304.         *f+=m.m[i][j];
  305.     }
  306. #endif
  307.   return a;
  308. }
  309. inline vector operator*(mat4x4& m,vector& v)
  310. {
  311.   vector a;
  312. #ifdef P3SIMD
  313.     v.w=1.0f;
  314.     __asm
  315.     {
  316.         mov esi, v
  317.         mov edi, m
  318.  
  319.         movups xmm0,[esi]
  320.         movups xmm1,xmm0
  321.         movups xmm2,xmm0
  322.         movups xmm3,xmm0
  323.  
  324.         shufps xmm0,xmm2,0x00
  325.         shufps xmm1,xmm2,0x55
  326.         shufps xmm2,xmm2,0xAA
  327.         shufps xmm3,xmm3,0xFF
  328.         
  329.         movups xmm4,[edi]
  330.         movups xmm5,[edi+16]
  331.         movups xmm6,[edi+32]
  332.         movups xmm7,[edi+48]
  333.  
  334.         mulps xmm0,xmm4
  335.         mulps xmm1,xmm5
  336.         mulps xmm2,xmm6
  337.         mulps xmm3,xmm7
  338.  
  339.         addps xmm0,xmm1
  340.         addps xmm0,xmm2
  341.         addps xmm0,xmm3
  342.  
  343.         movups a,xmm0
  344.     }
  345. #else
  346.     float *f=&a.x;
  347.     for(int j=0; j<3; j++,f++ )
  348.     {
  349.         *f=0;
  350.         for(int i=0; i<3; i++)
  351.             *f+=*(&v.x+i)*m.m[i][j];
  352.         *f+=m.m[i][j];
  353.     }
  354. #endif
  355.   return a;
  356. }
  357.  
  358.