home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / dynamic.h < prev    next >
C/C++ Source or Header  |  2000-08-07  |  7KB  |  280 lines

  1. #ifndef __OGL2_DYNAMIC__
  2. #define __OGL2_DYNAMIC__
  3.  
  4. #include "types.h"
  5. #include "miscmath.h"
  6.  
  7. extern "C++" {
  8.  
  9. template <class __dtype, class __type>
  10. class Dynamic {
  11. public:
  12.   virtual ~Dynamic () {}
  13.   virtual __dtype operator () (__type t=0) = 0;
  14. };
  15.  
  16. template class Dynamic<Vector3f, GLfloat>;
  17. typedef Dynamic<Vector3f, GLfloat> Dvector3f;
  18.  
  19. template <class __vtype, class __type>
  20. class DvectorStatic : public Dynamic<__vtype, __type> {
  21.   __vtype v;
  22. public:
  23.   void Set (const __vtype& a) {
  24.     v = a;
  25.   }
  26.   __vtype operator () (__type t=0) {
  27.     return v;
  28.   }
  29. };
  30.  
  31. template <class __vtype, class __type>
  32. class DvectorLinear : public Dynamic<__vtype, __type> {
  33.   __vtype a0, a1;
  34. public:
  35.   void From (const __vtype& a) {
  36.     a0 = a;
  37.   }
  38.   void To (const __vtype& a) {
  39.     a1 = a;
  40.   }
  41.   __vtype operator () (__type t=0) {
  42.     return (a0 + t*(a1-a0));
  43.   }
  44. };
  45.  
  46. template <class __vtype, class __type>
  47. class DvectorLinearFramed : public Dynamic<__vtype, __type> {
  48.   __vtype *a;
  49.   int frames; 
  50.   int allframes; 
  51.   int fbegin; 
  52.   int fend; 
  53. public:
  54.   DvectorLinearFramed () {}
  55.   ~DvectorLinearFramed () {
  56.     delete[] a;
  57.   }
  58.   void Frames (int all, int n, int first, int last, __vtype* p) {
  59.     allframes = all;
  60.     frames = n;
  61.     fbegin = first;
  62.     fend = last;
  63.     a = p;
  64.   }
  65.   __vtype operator () (__type t=0) {
  66.     __type frame = allframes*t;
  67.     int frame_i = (int)frame;
  68.     __type fract = frame - frame_i;
  69.     if(frame_i<fbegin)
  70.       return a[0];
  71.     if(frame_i>=fend)
  72.       return a[frames-1];
  73.     frame_i -= fbegin;
  74.     return (a[frame_i] + fract*(a[frame_i+1]-a[frame_i]));
  75.   }
  76. };
  77.  
  78.  
  79. template <class __mtype, class __type>
  80. class DmatrixBasis : public Dynamic<__mtype, __type> {
  81.   __mtype M;
  82. public:
  83.   DmatrixBasis() {
  84.     M.Identity();
  85.   }
  86.   void Load(__mtype &m) {
  87.     M=m;
  88.   }
  89.   void Load(__type *m) {
  90.     M.Load(m);
  91.   }
  92.   void X(__type x, __type y, __type z) {
  93.     M[0] = x; M[1] = y; M[2] = z;
  94.   }
  95.   void Y(__type x, __type y, __type z) {
  96.     M[4] = x; M[5] = y; M[6] = z;
  97.   }
  98.   void Z(__type x, __type y, __type z) {
  99.     M[8] = x; M[9] = y; M[10] = z;
  100.   }
  101.   void Translation(__type x, __type y, __type z) {
  102.     M[12] = x; M[13] = y; M[14] = z;
  103.   }
  104.   __mtype operator () (__type t=0) {
  105.     return M;
  106.   }
  107. };
  108.  
  109. template <class __mtype, class __type>
  110. inline void quat2matrix(quaternion<__type>& q, __mtype& M)
  111. {
  112.   __type w = re(q);
  113.   __type x = im1(q);
  114.   __type y = im2(q);
  115.   __type z = im3(q);
  116.  
  117.   __type wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
  118.  
  119.   x2 = x + x; y2 = y + y; 
  120.   z2 = z + z;
  121.   xx = x * x2;   xy = x * y2;   xz = x * z2;
  122.   yy = y * y2;   yz = y * z2;   zz = z * z2;
  123.   wx = w * x2;   wy = w * y2;   wz = w * z2;
  124.  
  125.   M[0] = 1.0 - (yy + zz); M[4] = xy - wz;         M[8] = xz + wy;
  126.   M[1] = xy + wz;         M[5] = 1.0 - (xx + zz); M[9] = yz - wx;
  127.   M[2] = xz - wy;         M[6] = yz + wx;         M[10] = 1.0 - (xx + yy);
  128. }
  129.  
  130. template <class __mtype, class __type>
  131. class DmatrixOrientation : public Dynamic<__mtype, __type>
  132. {
  133.   quaternion<__type> q;
  134.   Vector3<__type> a;
  135.   Vector3<__type> pivot;
  136.   __mtype M;
  137.  
  138. public:
  139.   DmatrixOrientation () {
  140.     M.Identity();
  141.     q = quaternion<__type> (0,0,0,0);
  142.   }
  143.   void Orientation (__type angle, __type x, __type y, __type z) {
  144.     q = AngleAxis2Quaternion(angle, x, y, z);
  145.   }
  146.   void Translation (__type x, __type y, __type z) {
  147.     a = Vector3<__type> (x, y, z);
  148.   }
  149.   void Translation (const Vector3<__type>& T) {
  150.     a = t;
  151.   }
  152.   void Pivot (__type x, __type y, __type z) {
  153.     pivot = Vector3<__type> (x, y, z);
  154.   }
  155.   void Pivot (const Vector3<__type>& T) {
  156.     pivot = T;
  157.   }
  158.   __mtype operator () (__type t=0) {
  159.     __mtype mat;
  160.     mat.Identity();
  161.     mat.Translate(pivot.x, pivot.y, pivot.z);
  162.     quat2matrix(q, M);
  163.     M[12] = a.x; M[13] = a.y; M[14] = a.z;
  164.     mat = mat*M;
  165.     mat.Translate(-pivot.x, -pivot.y, -pivot.z);
  166.     return mat;
  167.   }
  168. };
  169.  
  170. template <class __mtype, class __type>
  171. class DmatrixOrientationLinear : public Dynamic<__mtype, __type> {
  172.   quaternion<__type> q0, q1;
  173.   Vector3<__type> a0, a1, pivot;
  174.   __mtype M;
  175.  
  176. public:
  177.   DmatrixOrientationLinear () {
  178.     M.Identity();
  179.     q0 = quaternion<__type> (0,0,0,0);
  180.     q1 = quaternion<__type> (0,0,0,0);
  181.   }
  182.   void StartRot(__type angle, __type x, __type y, __type z) {
  183.     q0 = AngleAxis2Quaternion(angle, x, y, z);
  184.   }
  185.   void EndRot(__type angle, __type x, __type y, __type z) {
  186.     q1 = AngleAxis2Quaternion(angle, x, y, z);
  187.   }
  188.   void StartTranslation(__type x, __type y, __type z) {
  189.     a0 = Vector3<__type> (x, y, z);
  190.   }
  191.   void EndTranslation(__type x, __type y, __type z) {
  192.     a1 = Vector3<__type> (x, y, z);
  193.   }
  194.   void Pivot (__type x, __type y, __type z) {
  195.     pivot = Vector3<__type> (x, y, z);
  196.   }
  197.   void Pivot (const Vector3<__type>& T) {
  198.     pivot = T;
  199.   }
  200.   __mtype operator () (__type t=0) {
  201.     __mtype mat;
  202.     mat.Identity();
  203.     mat.Translate(pivot.x, pivot.y, pivot.z);
  204.     quaternion<__type> q = slerp(t, q0, q1);
  205.     quat2matrix(q, M);
  206.     M[12] = a0.x + (a1.x-a0.x)*t;
  207.     M[13] = a0.y + (a1.y-a0.y)*t;
  208.     M[14] = a0.z + (a1.z-a0.z)*t;
  209.     mat = mat*M;
  210.     mat.Translate(-pivot.x, -pivot.y, -pivot.z);
  211.     return mat;
  212.   }
  213. };
  214.  
  215. /*
  216.  * this one is still questionable
  217.  */
  218.  
  219. template <class __mtype, class __type>
  220. class DmatrixAnimation : public Dynamic<__mtype, __type> {
  221.   int allframes;
  222.  
  223.   int pframes, pfbegin, pfend;
  224.   Vector3<__type>* positions;
  225.  
  226.   int rframes, rfbegin, rfend;
  227.   quaternion<__type>* rotations;
  228.  
  229.   __mtype M;
  230. public:
  231.   DmatrixAnimation () :
  232.     positions(0), rotations(0) {}
  233.   ~DmatrixAnimation () {
  234.     delete[] positions;
  235.     delete[] rotations;
  236.   }
  237.   void Positions (int all, int n, int first, int last, Vector3<__type>* p) {
  238.     allframes = all;
  239.     pframes = n;
  240.     pfbegin = first;
  241.     pfend = last;
  242.     positions = p;
  243.   }
  244.   void Rotations (int all, int n, int first, int last, quaternion<__type>* p) {
  245.     allframes = all;
  246.     rframes = n;
  247.     rfbegin = first;
  248.     rfend = last;
  249.     rotations = p;
  250.   }
  251.  
  252.   __mtype operator () (__type t=0) {
  253.     M.LoadIdentity();
  254.     if(rotations) {
  255.       __type f = rframes*t;
  256.       int frame = (int)f;
  257.       __type loc_t = f-frame;
  258.       quaternion<__type> q = slerp(loc_t, rotations[frame], rotations[frame+1]);
  259.       quat2matrix(q, M);
  260.     }
  261.     if(positions) {
  262.       __type f = pframes*t;
  263.       int frame = (int)f;
  264.       __type loc_t = f-frame;
  265.       Vector3<__type>& a0 = positions[frame];
  266.       Vector3<__type>& a1 = positions[frame+1];
  267.       Vector3<__type> v = a0 + loc_t*(a1-a0);
  268.       M[12] = v.x;
  269.       M[13] = v.y;
  270.       M[14] = v.z;
  271.     }
  272.     return M;
  273.   }
  274. };
  275.  
  276.  
  277. } // extern "C++"
  278.  
  279. #endif
  280.