home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / camera.cpp < prev    next >
C/C++ Source or Header  |  2000-08-17  |  9KB  |  382 lines

  1. #include "camera.h"
  2. #include "macros.h"
  3.  
  4. template <class __type> inline
  5. void normalize (Vector3<__type>& V) {
  6.   __type len = 1.0/sqrt(V.x*V.x + V.y*V.y + V.z*V.z);
  7.   V.x *= len;
  8.   V.y *= len;
  9.   V.z *= len;
  10. }
  11.  
  12. void CameraTargetGL::Origin (GLfloat x, GLfloat y, GLfloat z) {
  13.   origin(Vector3f(x, y, z));
  14. }
  15. void CameraTargetGL::Origin (const Vector3f& V) {
  16.   origin(V);
  17. }
  18. void CameraTargetGL::Origin (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false) {
  19.   origin(ptr, strong);
  20. }
  21. void CameraTargetGL::Target (GLfloat x, GLfloat y, GLfloat z) {
  22.   target(Vector3f(x, y, z));
  23. }
  24. void CameraTargetGL::Target (const Vector3f& V) {
  25.   target(V);
  26. }
  27. void CameraTargetGL::Target (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false) {
  28.   target(ptr, strong);
  29. }
  30. void CameraTargetGL::Roll (GLfloat r) {
  31.   roll(r);
  32. }
  33. void CameraTargetGL::Roll (Dynamic<GLfloat, GLfloat> *ptr, bool strong = false) {
  34.   roll(ptr, strong);
  35. }
  36. Vector3f CameraTargetGL::Origin (double time = 0.0) {
  37.   return origin.Val(time);
  38. }
  39. Vector3f CameraTargetGL::Target (double time = 0.0) {
  40.   return target.Val(time);
  41. }
  42.  
  43. void CameraTargetGL::GL (double time = 0.0)
  44. {
  45.   Vector3f U, V, N, A, B;
  46.   GLfloat sr, cr, h, v;
  47.  
  48.   Vector3f O = origin.Val(time);
  49.   Vector3f T = target.Val(time);
  50.   N = T - O;
  51.  
  52.   U.x = N.z;
  53.   U.y = 0;
  54.   U.z = -N.x;
  55.   V.x = -N.x*N.y;
  56.   V.y = N.z*N.z + N.x*N.x;
  57.   V.z = -N.y*N.z;
  58.   normalize(N);
  59.   normalize(U);
  60.   normalize(V);
  61.   sr = sin(roll.Val(time));
  62.   cr = cos(roll.Val(time));
  63.   A.x = cr*U.x + sr*V.x;
  64.   A.y = cr*U.y + sr*V.y;
  65.   A.z = cr*U.z + sr*V.z;
  66.   B.x = -sr*U.x + cr*V.x;
  67.   B.y = -sr*U.y + cr*V.y;
  68.   B.z = -sr*U.z + cr*V.z;
  69.   U = A;
  70.   V = B;
  71.  
  72.   h = sin(horizontal_fov/2) / cos(horizontal_fov/2); // tan(hfov/2)
  73.   v = sin(vertical_fov/2) / cos(vertical_fov/2); // tan(vfov/2)
  74.  
  75.   glMatrixMode(GL_PROJECTION);
  76.  
  77.   glLoadIdentity();
  78.   glFrustum(-near_clipplane*h, near_clipplane*h,
  79.             -near_clipplane*v, near_clipplane*v,
  80.              near_clipplane, far_clipplane);
  81.  
  82.   glMatrixMode(GL_MODELVIEW);
  83.  
  84.   M[0] = U.x; M[4] = U.y; M[8] =  U.z; M[12] = -(O.x*U.x + O.y*U.y + O.z*U.z);
  85.   M[1] = V.x; M[5] = V.y; M[9] =  V.z; M[13] = -(O.x*V.x + O.y*V.y + O.z*V.z);
  86.   M[2] = -N.x; M[6] = -N.y; M[10] = -N.z; M[14] = (O.x*N.x + O.y*N.y + O.z*N.z);
  87.   M[3] = 0.0; M[7] = 0.0; M[11] = 0.0; M[15] = 1.0;
  88.  
  89.   glLoadMatrixf(M);
  90. }
  91.  
  92. void CameraTarget3DS::Origin (GLfloat x, GLfloat y, GLfloat z) {
  93.   origin(Vector3f(x, y, z));
  94. }
  95. void CameraTarget3DS::Origin (const Vector3f& V) {
  96.   origin(V);
  97. }
  98. void CameraTarget3DS::Origin (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false) {
  99.   origin(ptr, strong);
  100. }
  101. void CameraTarget3DS::Target (GLfloat x, GLfloat y, GLfloat z) {
  102.   target(Vector3f(x, y, z));
  103. }
  104. void CameraTarget3DS::Target (const Vector3f& V) {
  105.   target(V);
  106. }
  107. void CameraTarget3DS::Target (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false) {
  108.   target(ptr, strong);
  109. }
  110. void CameraTarget3DS::Roll (GLfloat r) {
  111.   roll(r);
  112. }
  113. void CameraTarget3DS::Roll (Dynamic<GLfloat, GLfloat> *ptr, bool strong = false) {
  114.   roll(ptr, strong);
  115. }
  116. Vector3f CameraTarget3DS::Origin (double time = 0.0) {
  117.   return origin.Val(time);
  118. }
  119. Vector3f CameraTarget3DS::Target (double time = 0.0) {
  120.   return target.Val(time);
  121. }
  122.  
  123.  
  124. void CameraTarget3DS::GL (double time = 0.0)
  125. {
  126.   Vector3f U, V, N, A, B;
  127.   GLfloat sr, cr, h, v;
  128.  
  129.   Vector3f O = origin.Val(time);
  130.   Vector3f T = target.Val(time);
  131.   N = T - O;
  132.  
  133.   U.x = N.y;
  134.   U.y = -N.x;
  135.   U.z = 0;
  136.   V.x = -N.x*N.z;
  137.   V.y = -N.y*N.z;
  138.   V.z = N.x*N.x+N.y*N.y;
  139.  
  140.   normalize(N);
  141.   normalize(U);
  142.   normalize(V);
  143.  
  144.   GLfloat __roll = -PI*(roll.Val(time))/180.0F;
  145.   sr = sin(__roll);
  146.   cr = cos(__roll);
  147.   A.x = cr*U.x + sr*V.x;
  148.   A.y = cr*U.y + sr*V.y;
  149.   A.z = cr*U.z + sr*V.z;
  150.   B.x = -sr*U.x + cr*V.x;
  151.   B.y = -sr*U.y + cr*V.y;
  152.   B.z = -sr*U.z + cr*V.z;
  153.   U = A;
  154.   V = B;
  155.  
  156.   h = sin(horizontal_fov/2) / cos(horizontal_fov/2); // tan(hfov/2)
  157.   v = sin(vertical_fov/2) / cos(vertical_fov/2); // tan(vfov/2)
  158.  
  159.   glMatrixMode(GL_PROJECTION);
  160.  
  161.   glLoadIdentity();
  162.   glFrustum(-near_clipplane*h, near_clipplane*h,
  163.             -near_clipplane*v, near_clipplane*v,
  164.              near_clipplane, far_clipplane);
  165.  
  166.   glMatrixMode(GL_MODELVIEW);
  167.  
  168.   M[0] = U.x; M[4] = U.y; M[8] =  U.z; M[12] = -(O.x*U.x + O.y*U.y + O.z*U.z);
  169.   M[1] = V.x; M[5] = V.y; M[9] =  V.z; M[13] = -(O.x*V.x + O.y*V.y + O.z*V.z);
  170.   M[2] = -N.x; M[6] = -N.y; M[10] = -N.z; M[14] = (O.x*N.x + O.y*N.y + O.z*N.z);
  171.   M[3] = 0.0; M[7] = 0.0; M[11] = 0.0; M[15] = 1.0;
  172.  
  173.   glLoadMatrixf(M);
  174. }
  175.  
  176. void CameraDescent3DS::rotate(Vector3f& U,GLfloat Nroll,GLfloat Vroll,GLfloat Uroll)
  177. {
  178.   GLfloat sp = sin(Nroll);
  179.   GLfloat cp = cos(Nroll);
  180.  
  181.   GLfloat sf = sin(Vroll);
  182.   GLfloat cf = cos(Vroll);
  183.  
  184.   GLfloat sg = sin(Uroll);
  185.   GLfloat cg = cos(Uroll);
  186.  
  187.   GLfloat x,y,z;
  188.  
  189.   x=U.x;
  190.   y=U.y;
  191.   z=U.z;
  192.   U.x=x;
  193.   U.y=cp*y+sp*z;
  194.   U.z=-sp*y+cp*z;
  195.  
  196.   x=U.x;
  197.   y=U.y;
  198.   z=U.z;
  199.   U.x=cf*x-sf*z;
  200.   U.y=y;
  201.   U.z=sf*x+cf*z;
  202.  
  203.   x=U.x;
  204.   y=U.y;
  205.   z=U.z;
  206.   U.x=cg*x+sg*y;
  207.   U.y=-sg*x+cg*y;
  208.   U.z=z;
  209. }
  210.  
  211. void CameraDescent3DS::uprav(Vector3f& U,Vector3f& I,Vector3f& J,Vector3f& K)
  212. {
  213.   GLfloat x=U.x;
  214.   GLfloat y=U.y;
  215.   GLfloat z=U.z;
  216.  
  217.   U.x= x*I.x+ y*J.x+ z*K.x;
  218.   U.y= x*I.y+ y*J.y+ z*K.y;
  219.   U.z= x*I.z+ y*J.z+ z*K.z;
  220. }
  221.  
  222. void CameraDescent3DS::GL(double time = 0.0)
  223. {
  224.   Vector3f U, V, N;
  225.   GLfloat h, v;
  226.  
  227.   normalize(I);
  228.   normalize(J);
  229.   normalize(K);
  230.  
  231.   U.x=1;U.y=0;U.z=0;
  232.   V.x=0;V.y=1;V.z=0;
  233.   N.x=0;N.y=0;N.z=1;
  234.  
  235.   rotate(U,Uroll,Vroll,Nroll);
  236.   rotate(V,Uroll,Vroll,Nroll);
  237.   rotate(N,Uroll,Vroll,Nroll);
  238.  
  239.   uprav(U,I,J,K);
  240.   uprav(V,I,J,K);
  241.   uprav(N,I,J,K);
  242.  
  243.   I=U;
  244.   J=V;
  245.   K=N;
  246.  
  247.   h = sin(horizontal_fov/2) / cos(horizontal_fov/2); // tan(hfov/2)
  248.   v = sin(vertical_fov/2) / cos(vertical_fov/2); // tan(vfov/2)
  249.  
  250.   glMatrixMode(GL_PROJECTION);
  251.  
  252.   glLoadIdentity();
  253.   glFrustum(-near_clipplane*h, near_clipplane*h,
  254.             -near_clipplane*v, near_clipplane*v,
  255.              near_clipplane, far_clipplane);
  256.  
  257.   glMatrixMode(GL_MODELVIEW);
  258.  
  259.   M[0] = U.x; M[4] = U.y; M[8] =  U.z; M[12] = -(O.x*U.x + O.y*U.y + O.z*U.z);
  260.   M[1] = V.x; M[5] = V.y; M[9] =  V.z; M[13] = -(O.x*V.x + O.y*V.y + O.z*V.z);
  261.   M[2] = -N.x; M[6] = -N.y; M[10] = -N.z; M[14] = (O.x*N.x + O.y*N.y + O.z*N.z);
  262.   M[3] = 0.0; M[7] = 0.0; M[11] = 0.0; M[15] = 1.0;
  263.   glLoadMatrixf(M);
  264. }
  265.  
  266. GLmatrix CameraDescent3DS::infoGL(double time = 0.0)
  267. {
  268.   Vector3f U, V, N;
  269.   GLfloat h, v;
  270.  
  271.   normalize(I);
  272.   normalize(J);
  273.   normalize(K);
  274.  
  275.   U.x=1;U.y=0;U.z=0;
  276.   V.x=0;V.y=1;V.z=0;
  277.   N.x=0;N.y=0;N.z=1;
  278.  
  279.   rotate(U,Uroll,Vroll,Nroll);
  280.   rotate(V,Uroll,Vroll,Nroll);
  281.   rotate(N,Uroll,Vroll,Nroll);
  282.  
  283.   uprav(U,I,J,K);
  284.   uprav(V,I,J,K);
  285.   uprav(N,I,J,K);
  286.  
  287.   I=U;
  288.   J=V;
  289.   K=N;
  290.  
  291.   h = sin(horizontal_fov/2) / cos(horizontal_fov/2); // tan(hfov/2)
  292.   v = sin(vertical_fov/2) / cos(vertical_fov/2); // tan(vfov/2)
  293.  
  294.   glMatrixMode(GL_PROJECTION);
  295.  
  296.   glLoadIdentity();
  297.   glFrustum(-near_clipplane*h, near_clipplane*h,
  298.             -near_clipplane*v, near_clipplane*v,
  299.              near_clipplane, far_clipplane);
  300.  
  301.   glMatrixMode(GL_MODELVIEW);
  302.  
  303.   M[0] = U.x; M[4] = U.y; M[8] =  U.z; M[12] = -(O.x*U.x + O.y*U.y + O.z*U.z);
  304.   M[1] = V.x; M[5] = V.y; M[9] =  V.z; M[13] = -(O.x*V.x + O.y*V.y + O.z*V.z);
  305.   M[2] = -N.x; M[6] = -N.y; M[10] = -N.z; M[14] = (O.x*N.x + O.y*N.y + O.z*N.z);
  306.   M[3] = 0.0; M[7] = 0.0; M[11] = 0.0; M[15] = 1.0;
  307.  
  308.   return M;
  309. }
  310.  
  311.  
  312.  
  313. void CameraDescent3DS::GLmirror (double a, double b, double c, double d, double time = 0.0)
  314. {
  315.   Vector3f U, V, N;
  316.   GLfloat h, v;
  317.  
  318.   normalize(I);
  319.   normalize(J);
  320.   normalize(K);
  321.  
  322.   U.x=1;U.y=0;U.z=0;
  323.   V.x=0;V.y=1;V.z=0;
  324.   N.x=0;N.y=0;N.z=1;
  325.  
  326.   rotate(U,Uroll,Vroll,Nroll);
  327.   rotate(V,Uroll,Vroll,Nroll);
  328.   rotate(N,Uroll,Vroll,Nroll);
  329.  
  330.   uprav(U,I,J,K);
  331.   uprav(V,I,J,K);
  332.   uprav(N,I,J,K);
  333.  
  334.   I=U;
  335.   J=V;
  336.   K=N;
  337.  
  338.   h = sin(horizontal_fov/2) / cos(horizontal_fov/2); // tan(hfov/2)
  339.   v = sin(vertical_fov/2) / cos(vertical_fov/2); // tan(vfov/2)
  340.  
  341.   glMatrixMode(GL_PROJECTION);
  342.  
  343.   glLoadIdentity();
  344.   glFrustum(-near_clipplane*h, near_clipplane*h,
  345.             -near_clipplane*v, near_clipplane*v,
  346.              near_clipplane, far_clipplane);
  347.  
  348.   glMatrixMode(GL_MODELVIEW);
  349.  
  350.  
  351.   Vector3f nrm(a,b,c);
  352.   float l;
  353.   Vector3f OB=O;
  354.  
  355.   V=O+V;
  356.   l = V.x*a + V.y*b + V.z*c + d;
  357.   V-=2*l*nrm;
  358.  
  359.   U=O+U;
  360.   l = U.x*a + U.y*b + U.z*c + d;
  361.   U-=2*l*nrm;
  362.  
  363.   N=O+N;
  364.   l = N.x*a + N.y*b + N.z*c + d;
  365.   N-=2*l*nrm;
  366.  
  367.   l = O.x*a + O.y*b + O.z*c + d;
  368.   O-=2*l*nrm;
  369.  
  370.   V=V-O;
  371.   U=U-O;
  372.   N=N-O;
  373.  
  374.   M[0] = U.x; M[4] = U.y; M[8] =  U.z; M[12] = -(O.x*U.x + O.y*U.y + O.z*U.z);
  375.   M[1] = V.x; M[5] = V.y; M[9] =  V.z; M[13] = -(O.x*V.x + O.y*V.y + O.z*V.z);
  376.   M[2] = -N.x; M[6] = -N.y; M[10] = -N.z; M[14] = (O.x*N.x + O.y*N.y + O.z*N.z);
  377.   M[3] = 0.0; M[7] = 0.0; M[11] = 0.0; M[15] = 1.0;
  378.  
  379.   O=OB;
  380.  
  381.   glLoadMatrixf(M);
  382. }