W3DView.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DView.h ////////////////////////////////////////////////////////////////////////////////
  24. //
  25. // W3D implementation of the game view window. View windows are literally
  26. // a window into the game world that have width, height, and camera
  27. // controls for what to look at
  28. //
  29. // Author: Colin Day, April 2001
  30. //
  31. ///////////////////////////////////////////////////////////////////////////////////////////////////
  32. #pragma once
  33. #ifndef __W3DVIEW_H_
  34. #define __W3DVIEW_H_
  35. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////////////////////////
  36. // USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
  37. #include "Common/STLTypedefs.h"
  38. #include "GameClient/View.h"
  39. #include "WW3D2/Camera.h"
  40. // FORWARD REFERENCES /////////////////////////////////////////////////////////////////////////////
  41. class Drawable;
  42. ///////////////////////////////////////////////////////////////////////////////////////////////////
  43. enum {MAX_WAYPOINTS=25};
  44. // ------------------------------------------------------------------------------------------------
  45. // ------------------------------------------------------------------------------------------------
  46. typedef struct
  47. {
  48. Int numWaypoints;
  49. Coord3D waypoints[MAX_WAYPOINTS+2]; // We pad first & last for interpolation.
  50. Real waySegLength[MAX_WAYPOINTS+2]; // Length of each segment;
  51. Real cameraAngle[MAX_WAYPOINTS+2]; // Camera Angle;
  52. Real cameraFXPitch[MAX_WAYPOINTS+2]; // Camera Pitch;
  53. Real cameraZoom[MAX_WAYPOINTS+2]; // Camera Zoom;
  54. Int timeMultiplier[MAX_WAYPOINTS+2]; // Time speedup factor.
  55. Real groundHeight[MAX_WAYPOINTS+1]; // Ground height.
  56. Int totalTimeMilliseconds; // Num of ms to do this movement.
  57. Int elapsedTimeMilliseconds; // Time since start.
  58. Real totalDistance; // Total length of paths.
  59. Real curSegDistance; // How far we are along the current seg.
  60. Int shutter;
  61. Int curSegment; // The current segment.
  62. Int curShutter; // The current shutter.
  63. Int rollingAverageFrames; // Number of frames to roll.
  64. } TMoveAlongWaypointPathInfo;
  65. // ------------------------------------------------------------------------------------------------
  66. // ------------------------------------------------------------------------------------------------
  67. typedef struct
  68. {
  69. Int numFrames; ///< Number of frames to rotate.
  70. Int curFrame; ///< Current frame.
  71. Real angle;
  72. Real startZoom;
  73. Real endZoom;
  74. Real startPitch;
  75. Real endPitch;
  76. Int startTimeMultiplier;
  77. Int endTimeMultiplier;
  78. ObjectID targetObjectID; ///< Target if we are tracking an object instead of just rotating
  79. Coord3D targetObjectPos; ///< Target's position (so we can stay looking at that spot if he dies)
  80. Bool trackObject; ///< Are we tracking an object or just rotating?
  81. Int numHoldFrames; ///< Number of frames to hold the camera before finishing the movement
  82. } TRotateCameraInfo;
  83. // ------------------------------------------------------------------------------------------------
  84. // ------------------------------------------------------------------------------------------------
  85. typedef struct
  86. {
  87. Int numFrames; ///< Number of frames to pitch.
  88. Int curFrame; ///< Current frame.
  89. Real angle;
  90. Real startPitch;
  91. Real endPitch;
  92. Int startTimeMultiplier;
  93. Int endTimeMultiplier;
  94. } TPitchCameraInfo;
  95. // ------------------------------------------------------------------------------------------------
  96. // ------------------------------------------------------------------------------------------------
  97. typedef struct
  98. {
  99. Int numFrames; ///< Number of frames to zoom.
  100. Int curFrame; ///< Current frame.
  101. Real startZoom;
  102. Real endZoom;
  103. Int startTimeMultiplier;
  104. Int endTimeMultiplier;
  105. } TZoomCameraInfo;
  106. // ------------------------------------------------------------------------------------------------
  107. /** W3DView implementation of the game view class. This allows us to create
  108. * a "window" into the game world that can be sized and contains controls
  109. * for manipulating the camera */
  110. // ------------------------------------------------------------------------------------------------
  111. class W3DView : public View, public SubsystemInterface
  112. {
  113. public:
  114. W3DView();
  115. ~W3DView();
  116. virtual void init( void ); ///< init/re-init the W3DView
  117. virtual void reset( void );
  118. virtual void drawView( void ); ///< draw this view
  119. virtual void updateView(void); ///<called once per frame to determine the final camera and object transforms
  120. virtual void draw( ); ///< draw this view
  121. virtual void update( ); ///<called once per frame to determine the final camera and object transforms
  122. virtual Drawable *pickDrawable( const ICoord2D *screen, Bool forceAttack, PickType pickType ); ///< pick drawable given the screen pixel coords. If force attack, picks bridges.
  123. /// all drawables in the 2D screen region will call the 'callback'
  124. virtual Int iterateDrawablesInRegion( IRegion2D *screenRegion,
  125. Bool (*callback)( Drawable *draw, void *userData ),
  126. void *userData );
  127. virtual void setWidth( Int width );
  128. virtual void setHeight( Int height );
  129. virtual void setOrigin( Int x, Int y); ///< Sets location of top-left view corner on display
  130. virtual void scrollBy( Coord2D *delta ); ///< Shift the view by the given delta
  131. virtual void forceRedraw();
  132. virtual void setAngle( Real angle ); ///< Rotate the view around the up axis by the given angle
  133. virtual void setPitch( Real angle ); ///< Rotate the view around the horizontal axis to the given angle
  134. virtual void setAngleAndPitchToDefault( void ); ///< Set the view angle back to default
  135. virtual void lookAt( const Coord3D *o ); ///< Center the view on the given coordinate
  136. virtual void initHeightForMap( void ); ///< Init the camera height for the map at the current position.
  137. virtual void moveCameraTo(const Coord3D *o, Int miliseconds, Int shutter, Bool orient);
  138. virtual void moveCameraAlongWaypointPath(Waypoint *pWay, Int frames, Int shutter, Bool orient);
  139. virtual Bool isCameraMovementFinished(void);
  140. virtual void resetCamera(const Coord3D *location, Int frames); ///< Move camera to location, and reset to default angle & zoom.
  141. virtual void rotateCamera(Real rotations, Int frames); ///< Rotate camera about current viewpoint.
  142. virtual void rotateCameraTowardObject(ObjectID id, Int milliseconds, Int holdMilliseconds); ///< Rotate camera to face an object, and hold on it
  143. virtual void rotateCameraTowardPosition(const Coord3D *pLoc, Int milliseconds); ///< Rotate camera to face a location.
  144. virtual void cameraModFreezeTime(void){ m_freezeTimeForCameraMovement = true;} ///< Freezes time during the next camera movement.
  145. virtual void cameraModFreezeAngle(void); ///< Freezes time during the next camera movement.
  146. virtual Bool isTimeFrozen(void){ return m_freezeTimeForCameraMovement;} ///< Freezes time during the next camera movement.
  147. virtual void cameraModFinalZoom(Real finalZoom); ///< Final zoom for current camera movement.
  148. virtual void cameraModRollingAverage(Int framesToAverage); ///< Number of frames to average movement for current camera movement.
  149. virtual void cameraModFinalTimeMultiplier(Int finalMultiplier); ///< Final time multiplier for current camera movement.
  150. virtual void cameraModFinalPitch(Real finalPitch); ///< Final pitch for current camera movement.
  151. virtual void cameraModLookToward(Coord3D *pLoc); ///< Sets a look at point during camera movement.
  152. virtual void cameraModFinalLookToward(Coord3D *pLoc); ///< Sets a look at point during camera movement.
  153. virtual void cameraModFinalMoveTo(Coord3D *pLoc); ///< Sets a final move to.
  154. virtual Int getTimeMultiplier(void) {return m_timeMultiplier;};///< Get the time multiplier.
  155. virtual void setTimeMultiplier(Int multiple) {m_timeMultiplier = multiple;}; ///< Set the time multiplier.
  156. virtual void setDefaultView(Real pitch, Real angle, Real maxHeight);
  157. virtual void zoomCamera( Real finalZoom, Int milliseconds );
  158. virtual void pitchCamera( Real finalPitch, Int milliseconds );
  159. virtual void setHeightAboveGround(Real z);
  160. virtual void setZoom(Real z);
  161. virtual void setZoomToDefault( void ); ///< Set zoom to default value
  162. virtual void setFieldOfView( Real angle ); ///< Set the horizontal field of view angle
  163. virtual Bool worldToScreen( const Coord3D *w, ICoord2D *s ); ///< Transform world coordinate "w" into screen coordinate "s"
  164. virtual void screenToWorld( const ICoord2D *s, Coord3D *w ); ///< Transform screen coordinate "s" into world coordinate "w"
  165. virtual void screenToTerrain( const ICoord2D *screen, Coord3D *world ); ///< transform screen coord to a point on the 3D terrain
  166. virtual void screenToWorldAtZ( const ICoord2D *s, Coord3D *w, Real z ); ///< transform screen point to world point at the specified world Z value
  167. CameraClass *get3DCamera() const { return m_3DCamera; }
  168. virtual const Coord3D& get3DCameraPosition() const;
  169. virtual void setCameraLock(ObjectID id);
  170. virtual void setSnapMode( CameraLockType lockType, Real lockDist );
  171. /// Add an impulse force to shake the camera
  172. virtual void shake( const Coord3D *epicenter, CameraShakeType shakeType );
  173. virtual Real getFXPitch( void ) const { return m_FXPitch; } ///< returns the FX pitch angle
  174. virtual Bool setViewFilterMode(enum FilterModes filterMode); ///< Turns on viewport special effect (black & white mode)
  175. virtual Bool setViewFilter(enum FilterTypes filter); ///< Turns on viewport special effect (black & white mode)
  176. virtual void setViewFilterPos(const Coord3D *pos); ///< Passes a position to the special effect filter.
  177. virtual enum FilterModes getViewFilterMode(void) {return m_viewFilterMode;} ///< Turns on viewport special effect (black & white mode)
  178. virtual enum FilterTypes getViewFilterType(void) {return m_viewFilter;} ///< Turns on viewport special effect (black & white mode)
  179. virtual void setFadeParameters(Int fadeFrames, Int direction);
  180. virtual void set3DWireFrameMode(Bool enable); ///<enables custom wireframe rendering of 3D viewport
  181. Bool updateCameraMovements(void);
  182. virtual void forceCameraConstraintRecalc(void) { calcCameraConstraints(); }
  183. virtual void setGuardBandBias( const Coord2D *gb ) { m_guardBandBias.x = gb->x; m_guardBandBias.y = gb->y; }
  184. private:
  185. CameraClass *m_3DCamera; ///< camera representation for 3D scene
  186. CameraClass *m_2DCamera; ///< camera for UI overlayed on top of 3D scene
  187. enum FilterModes m_viewFilterMode;
  188. enum FilterTypes m_viewFilter;
  189. Bool m_isWireFrameEnabled;
  190. Bool m_nextWireFrameEnabled; ///< used to delay wireframe changes by 1 frame (needed for transitions).
  191. Coord2D m_shakeOffset; ///< the offset to add to the camera position
  192. Real m_shakeAngleCos; ///< the cosine of the orientation of the oscillation
  193. Real m_shakeAngleSin; ///< the sine of the orientation of the oscillation
  194. Real m_shakeIntensity; ///< the intensity of the oscillation
  195. TRotateCameraInfo m_rcInfo;
  196. Bool m_doingRotateCamera; ///< True if we are doing a camera rotate.
  197. TPitchCameraInfo m_pcInfo;
  198. Bool m_doingPitchCamera;
  199. TZoomCameraInfo m_zcInfo;
  200. Bool m_doingZoomCamera;
  201. Bool m_doingScriptedCameraLock; ///< True if we are following a unit via script
  202. Real m_FXPitch; ///< Camera effects pitch. 0 = flat, infinite = look down, 1 = normal.
  203. TMoveAlongWaypointPathInfo m_mcwpInfo; ///< Move camera along waypoint path info.
  204. Bool m_doingMoveCameraOnWaypointPath; ///< If true, moving camera along waypoint path.
  205. Bool m_freezeTimeForCameraMovement;
  206. Int m_timeMultiplier; ///< Time speedup multiplier.
  207. Bool m_cameraHasMovedSinceRequest; ///< If true, throw out all saved locations
  208. VecPosRequests m_locationRequests; ///< These are cached. New requests are added here
  209. Coord3D m_cameraOffset; ///< offset for camera from view center
  210. Coord3D m_previousLookAtPosition; ///< offset for camera from view center
  211. Coord2D m_scrollAmount; ///< scroll speed
  212. Real m_scrollAmountCutoff; ///< scroll speed at which we do not adjust height
  213. Real m_groundLevel; ///< height of ground.
  214. Region2D m_cameraConstraint; ///< m_pos should be constrained to be within this area
  215. Bool m_cameraConstraintValid; ///< if f, recalc cam constraints
  216. void setCameraTransform( void ); ///< set the transform matrix of m_3DCamera, based on m_pos & m_angle
  217. void buildCameraTransform( Matrix3D *transform ) ; ///< calculate (but do not set) the transform matrix of m_3DCamera, based on m_pos & m_angle
  218. void calcCameraConstraints() ; ///< recalc m_cameraConstraint
  219. void moveAlongWaypointPath(Int milliseconds); ///< Move camera along path.
  220. void getPickRay(const ICoord2D *screen, Vector3 *rayStart, Vector3 *rayEnd); ///<returns a line segment (ray) originating at the given screen position
  221. void setupWaypointPath(Bool orient); ///< Calculates distances & angles for moving along a waypoint path.
  222. void rotateCameraOneFrame(void); ///< Do one frame of a rotate camera movement.
  223. void zoomCameraOneFrame(void); ///< Do one frame of a zoom camera movement.
  224. void pitchCameraOneFrame(void); ///< Do one frame of a pitch camera movement.
  225. void getAxisAlignedViewRegion(Region3D &axisAlignedRegion); ///< Find 3D Region enclosing all possible drawables.
  226. void calcDeltaScroll(Coord2D &screenDelta);
  227. }; // end class W3DView
  228. // EXTERNALS //////////////////////////////////////////////////////////////////////////////////////
  229. extern Int TheW3DFrameLengthInMsec; // default is 33msec/frame == 30fps. but we may change it depending on sys config.
  230. #endif // end __W3DVIEW_H_