W3DTreeBuffer.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: 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 "dx8vertexbuffer.h"
  52. #include "dx8indexbuffer.h"
  53. #include "shader.h"
  54. #include "vertmaterial.h"
  55. #include "Lib/BaseType.h"
  56. #include "common/GameType.h"
  57. #include "Common/AsciiString.h"
  58. //-----------------------------------------------------------------------------
  59. // Forward References
  60. //-----------------------------------------------------------------------------
  61. class MeshClass;
  62. //-----------------------------------------------------------------------------
  63. // Type Defines
  64. //-----------------------------------------------------------------------------
  65. typedef enum {
  66. ALPINE_TREE = 0,
  67. DECIDUOUS_TREE = 1,
  68. SHRUB = 2,
  69. FENCE = 3
  70. } TTreeType;
  71. /// The individual data for a tree.
  72. typedef struct {
  73. Vector3 location; ///< Drawing location
  74. Real scale; ///< Scale at location.
  75. Real sin; ///< Sine of the rotation angle at location.
  76. Real cos; ///< Cosine of the rotation angle at location.
  77. Int panelStart; ///< Index of the "panel" lod.
  78. TTreeType treeType; ///< Type of tree. Currently only 3 supported.
  79. Bool visible; ///< Visible flag, updated each frame.
  80. Bool mirrorVisible; ///< Possibly visible in mirror.
  81. Bool rotates; ///< Trees rotate to follow the camera in single panel mode, fences don't
  82. SphereClass bounds; ///< Bounding sphere for culling to set the visible flag.
  83. Real sortKey; ///< Sort key, essentially the distance along the look at vector.
  84. } TTree;
  85. //
  86. // W3DTreeBuffer: Draw buffer for the trees.
  87. //
  88. //
  89. class W3DTreeBuffer
  90. {
  91. friend class HeightMapRenderObjClass;
  92. public:
  93. W3DTreeBuffer(void);
  94. ~W3DTreeBuffer(void);
  95. /// Add a tree at location. Name is the w3d model name.
  96. void addTree(Coord3D location, Real scale, Real angle, AsciiString name, Bool visibleInMirror);
  97. /// Empties the tree buffer.
  98. void clearAllTrees(void);
  99. /// Draws the trees. Uses camera for culling.
  100. void drawTrees(CameraClass * camera, RefRenderObjListIterator *pDynamicLightsIterator);
  101. /// Called when the view changes, and sort key needs to be recalculated.
  102. /// Normally sortKey gets calculated when a tree becomes visible.
  103. void doFullUpdate(void) {m_updateAllKeys = true;};
  104. void setIsTerrain(void) {m_isTerrainPass = true;}; ///< Terrain calls this to tell trees to draw.
  105. Bool needToDraw(void) {return m_isTerrainPass;};
  106. protected:
  107. enum { MAX_TREE_VERTEX=4000,
  108. MAX_TREE_INDEX=2*4000,
  109. MAX_TREES=2000};
  110. enum {MAX_TYPES = 4,
  111. SORT_ITERATIONS_PER_FRAME=10};
  112. DX8VertexBufferClass *m_vertexTree; ///<Tree vertex buffer.
  113. DX8IndexBufferClass *m_indexTree; ///<indices defining a triangles for the tree drawing.
  114. TextureClass *m_treeTexture; ///<Trees texture
  115. Int m_curNumTreeVertices; ///<Number of vertices used in m_vertexTree.
  116. Int m_curNumTreeIndices; ///<Number of indices used in b_indexTree;
  117. Int m_curTreeIndexOffset; ///<First index to draw at. We draw the trees backwards by filling up the index buffer backwards,
  118. // so any trees that don't fit are far away from the camera.
  119. TTree m_trees[MAX_TREES]; ///< The tree buffer. All trees are stored here.
  120. Int m_numTrees; ///< Number of trees in m_trees.
  121. Bool m_anythingChanged; ///< Set to true if visibility or sorting changed.
  122. Bool m_updateAllKeys; ///< Set to true when the view changes.
  123. Bool m_initialized; ///< True if the subsystem initialized.
  124. Bool m_isTerrainPass; ///< True if the terrain was drawn in this W3D scene render pass.
  125. SphereClass m_typeBounds[MAX_TYPES]; ///< Bounding boxes for the base tree models.
  126. MeshClass *m_typeMesh[MAX_TYPES]; ///< W3D mesh models for the trees.
  127. Vector3 m_cameraLookAtVector;
  128. void loadTreesInVertexAndIndexBuffers(RefRenderObjListIterator *pDynamicLightsIterator); ///< Fills the index and vertex buffers for drawing.
  129. void allocateTreeBuffers(void); ///< Allocates the buffers.
  130. void cull(CameraClass * camera); ///< Culls the trees.
  131. void cullMirror(CameraClass * camera); ///< Culls the trees in the mirror view.
  132. Int doLighting(Vector3 *loc, Real r, Real g, Real b, SphereClass &bounds, RefRenderObjListIterator *pDynamicLightsIterator);
  133. void freeTreeBuffers(void); ///< Frees the index and vertex buffers.
  134. void sort( Int iterations ); ///< Performs partial bubble sort.
  135. };
  136. #endif // end __W3DTREE_BUFFER_H_