home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Jeux / demos / crystalPPC.lha / camera.cpp < prev    next >
C/C++ Source or Header  |  1998-02-05  |  5KB  |  186 lines

  1. #include <math.h>
  2. #include <time.h>
  3.  
  4. #ifndef SYSTEM_H
  5. #include "system.h"
  6. #endif
  7.  
  8. #ifndef DEF_H
  9. #include "def.h"
  10. #endif
  11.  
  12. #ifndef CAMERA_H
  13. #include "camera.h"
  14. #endif
  15.  
  16. #ifndef POLYGON_H
  17. #include "polygon.h"
  18. #endif
  19.  
  20. #ifndef SECTOR_H
  21. #include "sector.h"
  22. #endif
  23.  
  24. #ifndef WORLD_H
  25. #include "world.h"
  26. #endif
  27.  
  28. //---------------------------------------------------------------------------
  29.  
  30. Camera::Camera ()
  31. {
  32.   m_world2cam.identity ();
  33.   v_world2cam.x = 0;
  34.   v_world2cam.y = 0;
  35.   v_world2cam.z = 0;
  36.   m_cam2world = m_world2cam;
  37.   m_cam2world.inverse ();
  38.   edit_mode = MODE_NONE;
  39.   sel_polygon = NULL;
  40.   num_sel_verts = 0;
  41.   light_lev = 200;
  42.   get_forward_position (SHIFT_VIEWPOINT, v_viewpos);
  43. }
  44.  
  45. Camera::~Camera ()
  46. {
  47. }
  48.  
  49. void Camera::save_file (char* filename)
  50. {
  51.   FILE* fp = fopen (filename, "w");
  52.   fprintf (fp, "%f %f %f\n", v_world2cam.x, v_world2cam.y, v_world2cam.z);
  53.   fprintf (fp, "%f %f %f\n", m_world2cam.m11, m_world2cam.m12, m_world2cam.m13);
  54.   fprintf (fp, "%f %f %f\n", m_world2cam.m21, m_world2cam.m22, m_world2cam.m23); 
  55.   fprintf (fp, "%f %f %f\n", m_world2cam.m31, m_world2cam.m32, m_world2cam.m33);
  56.   fprintf (fp, "%s\n", sector->get_name ());
  57.   fclose (fp);
  58. }
  59.  
  60. void Camera::load_file (World* world, char* filename)
  61. {
  62.   char buf[100];
  63.   FILE* fp = fopen (filename, "r");
  64.   fscanf (fp, "%f %f %f\n", &v_world2cam.x, &v_world2cam.y, &v_world2cam.z);
  65.   fscanf (fp, "%f %f %f\n", &m_world2cam.m11, &m_world2cam.m12, &m_world2cam.m13);
  66.   fscanf (fp, "%f %f %f\n", &m_world2cam.m21, &m_world2cam.m22, &m_world2cam.m23); 
  67.   fscanf (fp, "%f %f %f\n", &m_world2cam.m31, &m_world2cam.m32, &m_world2cam.m33);
  68.   fscanf (fp, "%s\n", buf);
  69.   fclose (fp);
  70.   m_cam2world = m_world2cam;
  71.   m_cam2world.inverse ();
  72.   sector = world->get_sector (buf);
  73.   get_forward_position (SHIFT_VIEWPOINT, v_viewpos);
  74. }
  75.  
  76. void Camera::really_move (Vector3& new_position)
  77. {
  78.   Vector3 isect;
  79.   Polygon3D* p = sector->intersect_segment (v_world2cam, new_position, isect);
  80.   if (p)
  81.   {
  82.     if (p->get_portal ()) sector = p->get_portal ();
  83.     else return; // Don't move, hit wall
  84.   }
  85.   v_world2cam = new_position;
  86.  
  87.   // Here we set our view position a small amount back. This should minimize
  88.   // the problems with portal polygons that are too close by. The reason this
  89.   // works and changing the SMALL_Z offset doesn't is as follows:
  90.   //     The real position of our hero is located in some sector. If we are
  91.   //     close to a portal polygon to another sector it is possible that
  92.   //     the portal polygon will be clipped incorrectly to the Z plane and only
  93.   //     part of the other portal will be seen. If we shift our view position
  94.   //     back WITHOUT changing the sector we are in, we will in effect take
  95.   //     a distance from the portal polygons.
  96.   get_forward_position (SHIFT_VIEWPOINT, v_viewpos);
  97. }
  98.  
  99. void Camera::get_forward_position (float dist, Vector3& where)
  100. {
  101.   Vector3 pos (0, 0, 1); pos.z = dist;
  102.   m_cam2world.transform (pos, where);
  103.   where += v_world2cam;
  104. }
  105.  
  106. Polygon3D* Camera::get_hit (Vector3& where)
  107. {
  108.   return sector->hit_beam (v_world2cam, where);
  109. }
  110.  
  111. void Camera::forward (float dist)
  112. {
  113.   Vector3 pos (0, 0, dist);
  114.   Vector3 new_position;
  115.   m_cam2world.transform (pos, new_position);
  116.   new_position += v_world2cam;
  117.   really_move (new_position);
  118. }
  119.  
  120. void Camera::up (float dist)
  121. {
  122.   Vector3 pos (0, dist, 0);
  123.   Vector3 new_position;
  124.   m_cam2world.transform (pos, new_position);
  125.   new_position += v_world2cam;
  126.   really_move (new_position);
  127. }
  128.  
  129. void Camera::right (float dist)
  130. {
  131.   Vector3 pos (dist, 0, 0);
  132.   Vector3 new_position;
  133.   m_cam2world.transform (pos, new_position);
  134.   new_position += v_world2cam;
  135.   really_move (new_position);
  136. }
  137.  
  138. void Camera::turn_around ()
  139. {
  140.   rot_right (M_PI);
  141. }
  142.  
  143. void Camera::rot_right (float angle)
  144. {
  145.   Matrix3 rot (0, 0, 0, 0, 1, 0, 0, 0, 0);
  146.   rot.m11 =  cos (angle);
  147.   rot.m13 = -sin (angle);
  148.   rot.m31 =  sin (angle);
  149.   rot.m33 =  cos (angle);
  150.   rot *= m_world2cam;
  151.   m_world2cam = rot;
  152.   m_cam2world = rot;
  153.   m_cam2world.inverse ();
  154.   get_forward_position (SHIFT_VIEWPOINT, v_viewpos);
  155. }
  156.  
  157. void Camera::rot_up (float angle)
  158. {
  159.   Matrix3 rot (1, 0, 0, 0, 0, 0, 0, 0, 0);
  160.   rot.m22 =  cos (angle);
  161.   rot.m23 = -sin (angle);
  162.   rot.m32 =  sin (angle);
  163.   rot.m33 =  cos (angle);
  164.   rot *= m_world2cam;
  165.   m_world2cam = rot;
  166.   m_cam2world = rot;
  167.   m_cam2world.inverse ();
  168.   get_forward_position (SHIFT_VIEWPOINT, v_viewpos);
  169. }
  170.  
  171. void Camera::rot_z_right (float angle)
  172. {
  173.   Matrix3 rot (0, 0, 0, 0, 0, 0, 0, 0, 1);
  174.   rot.m11 =  cos (angle);
  175.   rot.m12 = -sin (angle);
  176.   rot.m21 =  sin (angle);
  177.   rot.m22 =  cos (angle);
  178.   rot *= m_world2cam;
  179.   m_world2cam = rot;
  180.   m_cam2world = rot;
  181.   m_cam2world.inverse ();
  182.   get_forward_position (SHIFT_VIEWPOINT, v_viewpos);
  183. }
  184.  
  185. //---------------------------------------------------------------------------
  186.