Camera.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /******************************************************************************
  2. Use 'Camera' to handle setting the viewing camera,
  3. where you can use the default 'Cam' camera, or create your own 'Camera' objects.
  4. Use 'ActiveCam' to access properties of currently active camera.
  5. Use helper functions to convert shapes between 3D world space and 2D screen space.
  6. /******************************************************************************/
  7. enum CAMH_FLAG // Camera Handle flags
  8. {
  9. CAMH_ZOOM =0x01, // zoom with mouse wheel
  10. CAMH_ROT_X =0x02, // rotate.x on mouse move (yaw)
  11. CAMH_ROT_Y =0x04, // rotate.y on mouse move (pitch)
  12. CAMH_MOVE =0x08, // move on mouse move
  13. CAMH_MOVE_XZ =0x10, // move.xz on mouse move
  14. CAMH_NO_VEL_UPDATE=0x20, // don't automatically update the velocities
  15. CAMH_NO_SET =0x40, // don't automatically activate the camera
  16. CAMH_ROT =CAMH_ROT_X|CAMH_ROT_Y, // rotate on mouse move
  17. };
  18. enum CAM_ATTACHMENT // information about camera attachment
  19. {
  20. CAM_ATTACH_FREE , // no attachment
  21. CAM_ATTACH_ACTOR, // camera is attached to a physical actor
  22. CAM_ATTACH_CTRL , // camera is attached to a character controller
  23. };
  24. /******************************************************************************/
  25. struct Camera
  26. {
  27. Flt yaw , // yaw angle , default=0
  28. pitch , // pitch angle , default=0
  29. roll , // roll angle , default=0
  30. dist ; // distance between camera position and 'at' point, default=1
  31. VecD at ; // point where camera is looking at , default=Vec(0,0,0)
  32. MatrixM matrix; // camera object matrix , default=MatrixIdentity-Vec(0,0,1)
  33. Vec vel, // camera velocity (this gets modified when calling 'updateVelocities')
  34. ang_vel; // camera angular velocity (this gets modified when calling 'updateVelocities')
  35. Camera& operator+=(C VecD &offset) {at+=offset; matrix.pos+=offset; return T;} // move camera by 'offset'
  36. Camera& operator-=(C VecD &offset) {at-=offset; matrix.pos-=offset; return T;} // move camera by 'offset'
  37. // set
  38. Camera& set (C MatrixM &matrix ); // set from matrix
  39. Camera& setAngle (C VecD &pos , Flt yaw, Flt pitch, Flt roll=0 ); // set from position and angles
  40. Camera& setSpherical(C VecD &at , Flt yaw, Flt pitch, Flt roll, Flt dist ); // set spherical from 'look at' position, angles and distance
  41. Camera& setSpherical( ); // set 'matrix' member as a spherical camera from current 'at dist yaw pitch roll' member values
  42. Camera& setFromAt (C VecD &from, C VecD &at , Flt roll=0 ); // set from "look from" position, "look at" position and 'roll' angle
  43. Camera& setPosDir (C VecD &pos , C Vec &dir=Vec(0,0,1), C Vec &up=Vec(0,1,0)); // set from look directions
  44. // operations
  45. #if EE_PRIVATE
  46. Camera& teleported();
  47. #endif
  48. Camera& updateVelocities(CAM_ATTACHMENT attachment=CAM_ATTACH_CTRL) ; // update camera velocities 'vel ang_vel' according to current 'matrix' and the one from previous frame, this needs to be called exactly once per frame, 'attachment'=specifies to what the camera is attached to (this affects only Motion Blur effect)
  49. void set ( )C; // set as active camera - sets rendering matrixes, sets frustum, copies self to 'ActiveCam'
  50. Camera& transformByMouse(Flt dist_min, Flt dist_max, UInt flag); // this is a helper method that transforms the camera basing on mouse input, 'dist_min'=minimum zoom distance, 'dist_max'=maximum zoom distance, 'flag'=CAMH_FLAG
  51. // io
  52. Bool save(File &f)C; // save to file, false on fail
  53. Bool load(File &f) ; // load from file, false on fail
  54. Camera();
  55. #if !EE_PRIVATE
  56. private:
  57. #endif
  58. MatrixM _matrix_prev;
  59. };
  60. extern Camera Cam; // default camera, you can use it to manipulate the camera
  61. extern C Camera ActiveCam; // active camera in read-only mode, this object is always changed when you activate any 'Camera' using its 'set' method, when rendering mirror/reflections, its 'matrix' temporarily gets adjusted
  62. /******************************************************************************/
  63. //
  64. // Following functions work on active viewport and 'ActiveCam' camera:
  65. //
  66. Vec2 PosToScreen (C Vec &pos ); // convert 3D position to 2D screen position
  67. Vec2 PosToScreen (C VecD &pos ); // convert 3D position to 2D screen position
  68. Vec2 PosToScreenM(C Vec &pos ); // convert 3D position to 2D screen position (transformed by current object matrix)
  69. Vec2 PosToScreenM(C VecD &pos ); // convert 3D position to 2D screen position (transformed by current object matrix)
  70. Bool PosToScreen (C Vec &pos, Vec2 &screen); // convert 3D position to 2D screen position , false on fail (point is behind the camera)
  71. Bool PosToScreen (C VecD &pos, Vec2 &screen); // convert 3D position to 2D screen position , false on fail (point is behind the camera)
  72. Bool PosToScreenM(C Vec &pos, Vec2 &screen); // convert 3D position to 2D screen position (transformed by current object matrix), false on fail (point is behind the camera)
  73. Bool PosToScreenM(C VecD &pos, Vec2 &screen); // convert 3D position to 2D screen position (transformed by current object matrix), false on fail (point is behind the camera)
  74. Vec ScreenToPosD (C Vec2 &screen_d, Flt z=ActiveCam.dist); // convert 2D screen delta to 3D world-space delta (affected by camera)
  75. VecD ScreenToPos (C Vec2 &screen , Flt z=ActiveCam.dist); // convert 2D screen position to 3D world-space position (affected by camera)
  76. Vec ScreenToViewPos(C Vec2 &screen , Flt z=ActiveCam.dist); // convert 2D screen position to 3D view-space position (in camera space)
  77. Vec ScreenToPosDM (C Vec2 &screen_d, Flt z=ActiveCam.dist); // convert 2D screen delta to 3D world-space delta (affected by camera, transformed by current object drawing matrix which is set by 'SetMatrix')
  78. VecD ScreenToPosM (C Vec2 &screen , Flt z=ActiveCam.dist); // convert 2D screen position to 3D world-space position (affected by camera, transformed by current object drawing matrix which is set by 'SetMatrix')
  79. Vec ScreenToDir (C Vec2 &screen ); // convert 2D screen position to world space 3D direction
  80. void ScreenToPosDir(C Vec2 &screen, Vec &pos, Vec &dir); // convert 2D screen position to world space 3D position start and 3D direction
  81. void ScreenToPosDir(C Vec2 &screen, VecD &pos, Vec &dir); // convert 2D screen position to world space 3D position start and 3D direction
  82. Bool ToScreenRect(C Box &box , Rect &rect); // project 3D box to 2D on-screen rectangle, false on fail (shape is behind the camera)
  83. Bool ToScreenRect(C OBox &obox , Rect &rect); // project 3D obox to 2D on-screen rectangle, false on fail (shape is behind the camera)
  84. Bool ToScreenRect(C Ball &ball , Rect &rect); // project 3D ball to 2D on-screen rectangle, false on fail (shape is behind the camera)
  85. Bool ToScreenRect(C BallM &ball , Rect &rect); // project 3D ball to 2D on-screen rectangle, false on fail (shape is behind the camera)
  86. Bool ToScreenRect(C Capsule &capsule, Rect &rect); // project 3D capsule to 2D on-screen rectangle, false on fail (shape is behind the camera)
  87. Bool ToScreenRect(C CapsuleM &capsule, Rect &rect); // project 3D capsule to 2D on-screen rectangle, false on fail (shape is behind the camera)
  88. Bool ToScreenRect(C Pyramid &pyramid, Rect &rect); // project 3D pyramid to 2D on-screen rectangle, false on fail (shape is behind the camera)
  89. Bool ToScreenRect(C Shape &shape , Rect &rect); // project 3D shape to 2D on-screen rectangle, false on fail (shape is behind the camera)
  90. Bool ToScreenRect(C Shape *shape , Int shapes, Rect &rect); // project 3D shapes to 2D on-screen rectangle, false on fail (shape is behind the camera)
  91. Int CompareTransparencyOrderDepth(C Vec &pos_a, C Vec &pos_b); // return comparing value -1/0/+1 determining the order of transparent objects rendering according to their depth (linear distance along camera look direction)
  92. Int CompareTransparencyOrderDepth(C VecD &pos_a, C VecD &pos_b); // return comparing value -1/0/+1 determining the order of transparent objects rendering according to their depth (linear distance along camera look direction)
  93. Int CompareTransparencyOrderDist (C Vec &pos_a, C Vec &pos_b); // return comparing value -1/0/+1 determining the order of transparent objects rendering according to their distance (spherical distance from camera position)
  94. Int CompareTransparencyOrderDist (C VecD &pos_a, C VecD &pos_b); // return comparing value -1/0/+1 determining the order of transparent objects rendering according to their distance (spherical distance from camera position)
  95. #if EE_PRIVATE
  96. Vec2 PosToFullScreen(C Vec &pos ); // convert 3D position to 2D (full and not gui) screen position
  97. Vec2 PosToFullScreen(C VecD &pos ); // convert 3D position to 2D (full and not gui) screen position
  98. Bool PosToFullScreen(C Vec &pos, Vec2 &screen); // convert 3D position to 2D (full and not gui) screen position, false on fail (point is behind the camera)
  99. Bool PosToFullScreen(C VecD &pos, Vec2 &screen); // convert 3D position to 2D (full and not gui) screen position, false on fail (point is behind the camera)
  100. VecD FullScreenToPos (C Vec2 &screen, Flt z=ActiveCam.dist); // convert 2D (full and not gui) screen position to 3D world-space position (affected by camera)
  101. Vec FullScreenToViewPos(C Vec2 &screen, Flt z=ActiveCam.dist); // convert 2D (full and not gui) screen position to 3D view-space position (in camera space)
  102. Bool ToFullScreenRect(C Ball &ball , Rect &rect); // project 3D ball to 2D (full and not gui) on-screen rectangle, false on fail (shape is behind the camera)
  103. Bool ToFullScreenRect(C BallM &ball , Rect &rect); // project 3D ball to 2D (full and not gui) on-screen rectangle, false on fail (shape is behind the camera)
  104. Bool ToFullScreenRect(C Pyramid &pyramid, Rect &rect); // project 3D pyramid to 2D (full and not gui) on-screen rectangle, false on fail (shape is behind the camera)
  105. Bool ToFullScreenRect(C PyramidM &pyramid, Rect &rect); // project 3D pyramid to 2D (full and not gui) on-screen rectangle, false on fail (shape is behind the camera)
  106. #endif
  107. /******************************************************************************/
  108. #if EE_PRIVATE
  109. void InitCamera();
  110. void SetCam (C MatrixM &matrix, Bool set_frustum=true);
  111. void MotionScaleChanged();
  112. #endif
  113. /******************************************************************************/