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

  1. #ifndef __OGL2_CAMERA__
  2. #define __OGL2_CAMERA__
  3.  
  4. #include "types.h"
  5. #include "entity.h"
  6. #include "dynamic.h"
  7. #include "switchers.h"
  8.  
  9. #ifndef PI
  10. #define PI 3.14159265358979324
  11. #endif
  12.  
  13. #include <iostream>
  14.  
  15. extern "C++" {
  16.  
  17. class Observer : public Entity {
  18. public:
  19.   virtual void GL (double time = 0.0) = 0;
  20.   virtual GLmatrix ModelviewMatrix (double time = 0.0) = 0;
  21.   virtual ~Observer () {}
  22. };
  23.  
  24. template class EntityList<Observer>;
  25. typedef EntityList<Observer> Observers;
  26.  
  27. class Camera : public Observer
  28. {
  29. protected:
  30.   GLfloat horizontal_fov, vertical_fov; 
  31.   GLfloat near_clipplane;
  32.   GLfloat far_clipplane;
  33.  
  34. public:
  35.   Camera () : horizontal_fov(PI/2), vertical_fov(PI/2),
  36.               near_clipplane(1.0), far_clipplane(1000.0) {}
  37.  
  38.   void HorizontalFOV (GLfloat hf) {
  39.     horizontal_fov = hf;
  40.   }
  41.   void VerticalFOV (GLfloat vf) {
  42.     vertical_fov = vf;
  43.   }
  44.   void NearClipplane (GLfloat n) {
  45.     near_clipplane = n;
  46.   }
  47.   void FarClipplane (GLfloat f) {
  48.     far_clipplane = f;
  49.   }
  50.   void DepthRange (GLfloat n, GLfloat f) {
  51.     near_clipplane = n;
  52.     far_clipplane = f;
  53.   }
  54.  
  55.   GLfloat HorizontalFOV () {
  56.     return horizontal_fov;
  57.   }
  58.   GLfloat VerticalFOV () {
  59.     return vertical_fov;
  60.   }
  61.   GLfloat NearClipplane () {
  62.     return near_clipplane;
  63.   }
  64.   GLfloat FarClipplane () {
  65.     return far_clipplane;
  66.   }
  67. };
  68.  
  69. template class EntityList<Camera>;
  70. typedef EntityList<Camera> Cameras;
  71.  
  72. class CameraTargetGL : public Camera {
  73.   DynamicSwitch<Vector3f, GLfloat> origin, target;
  74.   DynamicSwitch<GLfloat, GLfloat> roll;
  75.  
  76.   GLfloat M[16];
  77. public:
  78.   CameraTargetGL () {
  79.     origin(Vector3f(0.0F, 0.0F, 0.0F));
  80.     target(Vector3f(0.0F, 0.0F, 1.0F));
  81.     roll(0.0F);
  82.   }
  83.  
  84.   void GL (double time = 0.0);
  85.   GLmatrix ModelviewMatrix (double time = 0.0) {
  86.     return M;
  87.   }
  88.  
  89.   void Origin (GLfloat x, GLfloat y, GLfloat z),
  90.        Origin (const Vector3f& V),
  91.        Origin (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false);
  92.   void Target (GLfloat x, GLfloat y, GLfloat z),
  93.        Target (const Vector3f& V),
  94.        Target (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false);
  95.   void Roll (GLfloat r),
  96.        Roll (Dynamic<GLfloat, GLfloat> *ptr, bool strong = false);
  97.  
  98.   Vector3f Origin (double time = 0.0),
  99.            Target (double time = 0.0);
  100. };
  101.  
  102. class CameraTarget3DS : public Camera {
  103.   DynamicSwitch<Vector3f, GLfloat> origin, target;
  104.   DynamicSwitch<GLfloat, GLfloat> roll;
  105.  
  106.   GLfloat M[16];
  107. public:
  108.   CameraTarget3DS () {
  109.     origin(Vector3f(0.0F, 0.0F, 0.0F));
  110.     target(Vector3f(0.0F, 0.0F, 1.0F));
  111.     roll(0.0F);
  112.   }
  113.  
  114.   void GL (double time = 0.0);
  115.   GLmatrix ModelviewMatrix (double time = 0.0) {
  116.     return M;
  117.   }
  118.  
  119.   void Origin (GLfloat x, GLfloat y, GLfloat z),
  120.        Origin (const Vector3f& V),
  121.        Origin (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false);
  122.   void Target (GLfloat x, GLfloat y, GLfloat z),
  123.        Target (const Vector3f& V),
  124.        Target (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false);
  125.   void Roll (GLfloat r),
  126.        Roll (Dynamic<GLfloat, GLfloat> *ptr, bool strong = false);
  127.  
  128.   Vector3f Origin (double time = 0.0),
  129.            Target (double time = 0.0);
  130. };
  131.  
  132. class CameraDescent3DS : public Camera
  133. {
  134.  
  135.   Vector3f O;
  136.   Vector3f I, J, K; // coordinate system for descent alike camera
  137.                         // I - to the right
  138.                         // J - to the top
  139.                         // K - to the front
  140.   GLfloat Vroll,Uroll,Nroll; // rolls for descent camera
  141.  
  142.   void rotate(Vector3f& U, GLfloat Nroll, GLfloat Vroll, GLfloat Uroll);
  143.   void uprav(Vector3f& U, Vector3f& I, Vector3f& J, Vector3f& K);
  144.  
  145.   GLfloat M[16];
  146. public:
  147.   CameraDescent3DS() {
  148.     O.x = 0.0; O.y = 0.0; O.z = 0.0;
  149.     J.x = 1.0; J.y = 0.0; J.z = 0.0;
  150.     I.x = 0.0; I.y = 1.0; I.z = 0.0;
  151.     K.x = 0.0; K.y = 0.0; K.z = 1.0;
  152.     Nroll = 0.0; Uroll = 0.0; Vroll = 0.0;
  153.   }
  154.   void GL (double time = 0.0);
  155.   void GLmirror (double a, double b, double c, double d, double time = 0.0);
  156.   GLmatrix ModelviewMatrix (double time = 0.0) {
  157.     return M;
  158.   }
  159.  
  160.   GLmatrix infoGL(double time = 0.0);
  161.   void Info()
  162.    {
  163.    cout << "Descent origin       " << O << endl;
  164.    cout << "Descent right        " << I << endl;
  165.    cout << "Descent up           " << J << endl;
  166.    cout << "Descent forward      " << K << endl;
  167.    }
  168.  
  169.   void I3f(GLfloat x, GLfloat y, GLfloat z) {
  170.     I.x = x; I.y = y; I.z = z;
  171.   }
  172.   void I3fv(GLfloat* v) {
  173.     I.x = *v; I.y = *(v+1); I.z = *(v+2);
  174.   }
  175.   void J3f(GLfloat x, GLfloat y, GLfloat z) {
  176.     J.x = x; J.y = y; J.z = z;
  177.   }
  178.   void J3fv(GLfloat* v) {
  179.     J.x = *v; J.y = *(v+1); J.z = *(v+2);
  180.   }
  181.   void K3f(GLfloat x, GLfloat y, GLfloat z) {
  182.     K.x = x; K.y = y; K.z = z;
  183.   }
  184.   void K3fv(GLfloat* v) {
  185.     K.x = *v; K.y = *(v+1); K.z = *(v+2);
  186.   }
  187.   void IJKmove(GLfloat a, GLfloat b, GLfloat c) {
  188.     O.x+= a*I.x + b*J.x + c*K.x;
  189.     O.y+= a*I.y + b*J.y + c*K.y;
  190.     O.z+= a*I.z + b*J.z + c*K.z;
  191.   }
  192.  
  193.   void URoll(GLfloat ur) { Uroll = ur; }
  194.   void VRoll(GLfloat vr) { Vroll = vr; }
  195.   void NRoll(GLfloat nr) { Nroll = nr; }
  196.  
  197.   void SetAll(const Vector3f& o,const Vector3f& i,const Vector3f& j,const Vector3f& k) {
  198.     O=o;
  199.     I=i;
  200.     J=j;
  201.     K=k;
  202.   }
  203.  
  204.   void Origin (const Vector3f& o) {
  205.    O=o;
  206.   }
  207.  
  208.  
  209.   Vector3f Origin () {
  210.     return O;
  211.   }
  212.   Vector3f IVector () {
  213.     return I;
  214.   }
  215.   Vector3f JVector () {
  216.     return J;
  217.   }
  218.   Vector3f KVector () {
  219.     return K;
  220.   }
  221.  
  222.  
  223. };
  224.  
  225. } // extern "C++"
  226.  
  227. #endif
  228.