W3DTreeBuffer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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: W3DTreeBuffer.h //////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: W3DTreeBuffer.h
  36. //
  37. // Created: John Ahlquist, May 2001
  38. //
  39. // Desc: Draw buffer to handle all the trees in a scene.
  40. //
  41. //-----------------------------------------------------------------------------
  42. #pragma once
  43. #ifndef __W3DTREE_BUFFER_H_
  44. #define __W3DTREE_BUFFER_H_
  45. //-----------------------------------------------------------------------------
  46. // Includes
  47. //-----------------------------------------------------------------------------
  48. #include "always.h"
  49. #include "rendobj.h"
  50. #include "w3d_file.h"
  51. #include "Texture.h"
  52. #include "dx8vertexbuffer.h"
  53. #include "dx8indexbuffer.h"
  54. #include "shader.h"
  55. #include "vertmaterial.h"
  56. #include "Lib/BaseType.h"
  57. #include "common/GameType.h"
  58. #include "Common/AsciiString.h"
  59. #include "common/GlobalData.h"
  60. //-----------------------------------------------------------------------------
  61. // Forward References
  62. //-----------------------------------------------------------------------------
  63. class MeshClass;
  64. class W3DTreeBuffer;
  65. class TileData;
  66. class W3DTreeDrawModuleData;
  67. struct BreezeInfo;
  68. class GeometryInfo;
  69. class W3DProjectedShadow;
  70. //-----------------------------------------------------------------------------
  71. // Type Defines
  72. //-----------------------------------------------------------------------------
  73. enum W3DToppleState
  74. {
  75. TOPPLE_UPRIGHT = 0,
  76. TOPPLE_FALLING,
  77. TOPPLE_FOGGED,
  78. TOPPPLE_SHROUDED,
  79. TOPPLE_DOWN
  80. };
  81. /// The individual data for a tree.
  82. typedef struct {
  83. Vector3 location; ///< Drawing location
  84. Real scale; ///< Scale at location.
  85. Real sin; ///< Sine of the rotation angle at location.
  86. Real cos; ///< Cosine of the rotation angle at location.
  87. Int treeType; ///< Type of tree.
  88. Bool visible; ///< Visible flag, updated each frame.
  89. SphereClass bounds; ///< Bounding sphere for culling to set the visible flag.
  90. Real sortKey; ///< Sort key, essentially the distance along the look at vector.
  91. DrawableID drawableID; ///< Drawable this tree corresponds to.
  92. Real pushAside;
  93. Real pushAsideDelta;
  94. Real pushAsideSin; ///< Sine of the rotation angle at location.
  95. Real pushAsideCos; ///< Cosine of the rotation angle at location.
  96. ObjectID pushAsideSource; ///< Unit that is pushing us aside.
  97. UnsignedInt lastFrameUpdated; ///< Last frame push aside was updated.
  98. Int nextInPartition;
  99. Int swayType; ///< Which sway array entry we are using.
  100. Int firstIndex; ///< First index in the vertex buffer for this tree.
  101. Int bufferNdx; ///< Which vertex buffer this is in.
  102. // Topple parameters. [7/7/2003]
  103. Real m_angularVelocity; ///< Velocity in degrees per frame (or is it radians per frame?)
  104. Real m_angularAcceleration; ///< Acceleration angularVelocity is increasing
  105. Coord3D m_toppleDirection; ///< Z-less direction we are toppling
  106. W3DToppleState m_toppleState; ///< Stage this module is in.
  107. Real m_angularAccumulation; ///< How much have I rotated so I know when to bounce.
  108. UnsignedInt m_options; ///< topple options
  109. Matrix3D m_mtx;
  110. UnsignedInt m_sinkFramesLeft; ///< Toppled trees sink into the terrain & disappear, how many frames left.
  111. } TTree;
  112. /// The individual data for a tree type.
  113. typedef struct {
  114. MeshClass * m_mesh; ///< Mesh for this kind of tree.
  115. SphereClass m_bounds; ///< Bounding boxes for the base tree models.
  116. const W3DTreeDrawModuleData *m_data;
  117. ICoord2D m_textureOrigin; ///< Texture origin in the mega texture.
  118. Int m_numTiles; ///< Number of tex tiles.
  119. Int m_firstTile;///< First texture tile.
  120. Int m_tileWidth;///< Width in tiles of texture;
  121. Bool m_halfTile; ///< Tiles are 64x64 pixels, half tile supports a 32x32 bit texture. Have to adjust the uv values.
  122. Vector3 m_offset;
  123. Real m_shadowSize; ///< Shadow radius.
  124. Bool m_doShadow; ///< Draw shadow.
  125. } TTreeType;
  126. //
  127. // W3DTreeBuffer: Draw buffer for the trees.
  128. //
  129. //
  130. class W3DTreeBuffer : public Snapshot
  131. {
  132. //friend class BaseHeightMapRenderObjClass;
  133. //-----------------------------------------------------------------------------
  134. // W3DTreeTextureClass
  135. //-----------------------------------------------------------------------------
  136. class W3DTreeTextureClass : public TextureClass
  137. {
  138. W3DMPO_GLUE(W3DTreeTextureClass)
  139. protected:
  140. virtual void Apply(unsigned int stage);
  141. public:
  142. /// Create texture.
  143. W3DTreeTextureClass(unsigned width, unsigned height);
  144. // just use default destructor. ~TerrainTextureClass(void);
  145. public:
  146. int update(W3DTreeBuffer *buffer); ///< Sets the pixels, and returns the actual height of the texture.
  147. void setLOD(Int LOD) const;
  148. };
  149. public:
  150. W3DTreeBuffer(void);
  151. ~W3DTreeBuffer(void);
  152. /// Add a tree at location. Name is the w3d model name.
  153. void addTree(DrawableID id, Coord3D location, Real scale, Real angle,
  154. Real randomScaleAmount, const W3DTreeDrawModuleData *data);
  155. /// Notify that an object moved, so check for collisions.
  156. void unitMoved(Object *unit);
  157. /// Add a type of tree. Name is the w3d model name.
  158. Int addTreeType(const W3DTreeDrawModuleData *data);
  159. /// Updates a tree's location.
  160. Bool updateTreePosition(DrawableID id, Coord3D location, Real angle);
  161. void pushAsideTree( DrawableID id, const Coord3D *pusherPos,
  162. const Coord3D *pusherDirection, ObjectID pusherID );
  163. /// Remove a tree.
  164. void removeTree(DrawableID id);
  165. /// Remove trees that would be under a building.
  166. void removeTreesForConstruction(
  167. const Coord3D* pos,
  168. const GeometryInfo& geom,
  169. Real angle
  170. );
  171. void setTextureLOD(Int lod); ///<used to adjust maximum mip level sent to hardware.
  172. /// Empties the tree buffer.
  173. void clearAllTrees(void);
  174. /// Empties the tree buffer.
  175. void setBounds(const Region2D &bounds) {m_bounds = bounds;}
  176. /// Draws the trees. Uses camera for culling.
  177. void drawTrees(CameraClass * camera, RefRenderObjListIterator *pDynamicLightsIterator);
  178. /// Called when the view changes, and sort key needs to be recalculated.
  179. /// Normally sortKey gets calculated when a tree becomes visible.
  180. void doFullUpdate(void) {m_updateAllKeys = true;};
  181. void setIsTerrain(void) {m_isTerrainPass = true;}; ///< Terrain calls this to tell trees to draw.
  182. Bool needToDraw(void) {return m_isTerrainPass;};
  183. Int getNumTiles(void) {return m_numTiles;}
  184. TileData *getSourceTile(Int ndx) {return m_sourceTiles[ndx];}
  185. void allocateTreeBuffers(void); ///< Allocates the buffers.
  186. void freeTreeBuffers(void); ///< Frees the index and vertex buffers.
  187. private:
  188. enum { MAX_TREE_VERTEX=30000,
  189. MAX_TREE_INDEX=60000,
  190. MAX_TREES=4000};
  191. enum {MAX_TYPES = 64,
  192. MAX_TILES = 512,
  193. NUM_SWAY_ENTRIES = 100,
  194. MAX_SWAY_TYPES = 10,
  195. MAX_BUFFERS = 1,
  196. SORT_ITERATIONS_PER_FRAME=10};
  197. enum {PARTITION_WIDTH_HEIGHT = 100};
  198. DX8VertexBufferClass *m_vertexTree[MAX_BUFFERS]; ///<Tree vertex buffer.
  199. DX8IndexBufferClass *m_indexTree[MAX_BUFFERS]; ///<indices defining a triangles for the tree drawing.
  200. DWORD m_dwTreePixelShader; ///<handle to D3D pixel shader
  201. DWORD m_dwTreeVertexShader; ///<handle to D3D vertex shader
  202. Short m_areaPartition[PARTITION_WIDTH_HEIGHT*PARTITION_WIDTH_HEIGHT];
  203. Region2D m_bounds;
  204. TextureClass *m_treeTexture; ///<Trees texture
  205. Int m_textureWidth; ///<Width in pixels m_treeTexture;
  206. Int m_textureHeight; ///<Width in pixels m_treeTexture;
  207. Int m_curNumTreeVertices[MAX_BUFFERS]; ///<Number of vertices used in m_vertexTree.
  208. Int m_curNumTreeIndices[MAX_BUFFERS]; ///<Number of indices used in b_indexTree;
  209. TTree m_trees[MAX_TREES]; ///< The tree buffer. All trees are stored here.
  210. Int m_numTrees; ///< Number of trees in m_trees.
  211. Bool m_anythingChanged; ///< Set to true if visibility or sorting changed.
  212. Bool m_anyPushChanged; ///< Set to true if push aside is active.
  213. Bool m_updateAllKeys; ///< Set to true when the view changes.
  214. Bool m_initialized; ///< True if the subsystem initialized.
  215. Bool m_isTerrainPass; ///< True if the terrain was drawn in this W3D scene render pass.
  216. Bool m_needToUpdateTexture; ///< True if we need to update the texture.
  217. TTreeType m_treeTypes[MAX_TYPES]; ///< Info about a kind of tree.
  218. Int m_numTreeTypes; ///< Number of entries in m_treeTypes.
  219. Int m_numTiles;
  220. TileData *m_sourceTiles[MAX_TILES]; ///< Tiles for m_textureClasses
  221. Vector3 m_cameraLookAtVector;
  222. Vector3 m_swayOffsets[NUM_SWAY_ENTRIES];
  223. Int m_curSwayVersion;
  224. Real m_curSwayOffset[MAX_SWAY_TYPES];
  225. Real m_curSwayStep[MAX_SWAY_TYPES];
  226. Real m_curSwayFactor[MAX_SWAY_TYPES];
  227. W3DProjectedShadow *m_shadow;
  228. protected:
  229. // snapshot methods
  230. virtual void crc( Xfer *xfer );
  231. virtual void xfer( Xfer *xfer );
  232. virtual void loadPostProcess( void );
  233. protected:
  234. /// Updates the sway offsets.
  235. void updateSway(const BreezeInfo& info);
  236. void loadTreesInVertexAndIndexBuffers(RefRenderObjListIterator *pDynamicLightsIterator); ///< Fills the index and vertex buffers for drawing.
  237. void updateVertexBuffer(void); ///< Fills the index and vertex buffers for drawing.
  238. void cull(const CameraClass * camera); ///< Culls the trees.
  239. UnsignedInt doLighting(const Vector3 *normal,
  240. const GlobalData::TerrainLighting *objectLighting,
  241. const Vector3 *emissive, UnsignedInt vertexDiffuse, Real scale) const;
  242. #if 0 // sort is no longer used and messes up the order. jba [6/6/2003]
  243. void sort( Int iterations ); ///< Performs partial bubble sort.
  244. #endif
  245. void updateTexture(void);
  246. Int getPartitionBucket(const Coord3D &pos) const;
  247. void updateTopplingTree(TTree *tree);
  248. void applyTopplingForce( TTree *tree, const Coord3D* toppleDirection, Real toppleSpeed,
  249. UnsignedInt options );
  250. };
  251. #endif // end __W3DTREE_BUFFER_H_