W3DRoadBuffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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: W3DRoadBuffer.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: W3DRoadBuffer.h
  36. //
  37. // Created: John Ahlquist, May 2001
  38. //
  39. // Desc: Draw buffer to handle all the roads in a scene.
  40. //
  41. //-----------------------------------------------------------------------------
  42. #pragma once
  43. #ifndef __W3DROAD_BUFFER_H_
  44. #define __W3DROAD_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 "common/GameFileSystem.h"
  56. #include "Common/FileSystem.h" // for LOAD_TEST_ASSETS
  57. #include "Lib/BaseType.h"
  58. #include "common/GameType.h"
  59. #include "Common/AsciiString.h"
  60. //-----------------------------------------------------------------------------
  61. // Forward References
  62. //-----------------------------------------------------------------------------
  63. //-----------------------------------------------------------------------------
  64. // Type Defines
  65. //-----------------------------------------------------------------------------
  66. enum {bottomLeft=0, bottomRight=1, topLeft=2, topRight=3, NUM_CORNERS=4};
  67. #define MAX_LINKS 6
  68. #define DEFAULT_ROAD_SCALE (8.0f)
  69. #define MIN_ROAD_SEGMENT (0.25f)
  70. struct TRoadPt
  71. {
  72. Vector2 loc;
  73. Vector2 top;
  74. Vector2 bottom;
  75. Int count;
  76. Bool last;
  77. Bool multi;
  78. Bool isAngled;
  79. Bool isJoin;
  80. };
  81. struct TRoadSegInfo
  82. {
  83. Vector2 loc;
  84. Vector2 roadNormal;
  85. Vector2 roadVector;
  86. Vector2 corners[NUM_CORNERS];
  87. Real uOffset;
  88. Real vOffset;
  89. Real scale;
  90. };
  91. // The individual data for a road segment.
  92. enum {MAX_SEG_VERTEX=500, MAX_SEG_INDEX=2000};
  93. enum TCorner
  94. {
  95. SEGMENT,
  96. CURVE,
  97. TEE,
  98. FOUR_WAY,
  99. THREE_WAY_Y,
  100. THREE_WAY_H,
  101. THREE_WAY_H_FLIP,
  102. ALPHA_JOIN,
  103. NUM_JOINS
  104. };
  105. class RoadSegment
  106. {
  107. public:
  108. TRoadPt m_pt1; ///< Drawing location pt1 to pt2.
  109. TRoadPt m_pt2; ///< Drawing location to.
  110. Real m_curveRadius; ///< Radius if it is a curve.
  111. TCorner m_type; ///< Segment, or curve, or intersection.
  112. Real m_scale; ///< Scale.
  113. Real m_widthInTexture; ///< Width of the road in the texture.
  114. Int m_uniqueID; ///< Road type.
  115. Bool m_visible;
  116. protected:
  117. Int m_numVertex;
  118. VertexFormatXYZDUV1* m_vb;
  119. Int m_numIndex;
  120. UnsignedShort* m_ib;
  121. TRoadSegInfo m_info;
  122. SphereClass m_bounds;
  123. public:
  124. RoadSegment(void);
  125. ~RoadSegment(void);
  126. public:
  127. void SetVertexBuffer(VertexFormatXYZDUV1 *vb, Int numVertex);
  128. void SetIndexBuffer(UnsignedShort *ib, Int numIndex);
  129. void SetRoadSegInfo(TRoadSegInfo *pInfo) {m_info = *pInfo;};
  130. void GetRoadSegInfo(TRoadSegInfo *pInfo) {*pInfo = m_info;};
  131. const SphereClass &getBounds(void) {return m_bounds;};
  132. Int GetNumVertex(void) {return m_numVertex;};
  133. Int GetNumIndex(void) {return m_numIndex;};
  134. Int GetVertices(VertexFormatXYZDUV1 *destination_vb, Int numToCopy);
  135. Int GetIndices(UnsignedShort *destination_ib, Int numToCopy, Int offset);
  136. void updateSegLighting(void);
  137. } ;
  138. class RoadType {
  139. public:
  140. RoadType(void);
  141. ~RoadType(void);
  142. protected:
  143. TextureClass *m_roadTexture; ///<Roads texture
  144. DX8VertexBufferClass *m_vertexRoad; ///<Road vertex buffer.
  145. DX8IndexBufferClass *m_indexRoad; ///<indices defining a triangles for the road drawing.
  146. Int m_numRoadVertices; ///<Number of vertices used in m_vertexRoad.
  147. Int m_numRoadIndices; ///<Number of indices used in b_indexRoad;
  148. Int m_uniqueID; ///< ID of the road type in INI.
  149. Bool m_isAutoLoaded;
  150. Int m_stackingOrder; ///< Order in the drawing. 0 drawn first, then 1 and so on.
  151. #ifdef LOAD_TEST_ASSETS
  152. AsciiString m_texturePath;
  153. #endif
  154. public:
  155. void loadTexture(AsciiString path, Int id);
  156. void applyTexture(void);
  157. Int getStacking(void) {return m_stackingOrder;}
  158. void setStacking(Int order) {m_stackingOrder = order;}
  159. Int getUniqueID(void) {return m_uniqueID;};
  160. DX8VertexBufferClass *getVB(void) {return m_vertexRoad;};
  161. DX8IndexBufferClass *getIB(void) {return m_indexRoad;}
  162. Int getNumVertices(void) {return m_numRoadVertices;}
  163. void setNumIndices(Int num) {m_numRoadIndices=num;}
  164. void setNumVertices(Int num) {m_numRoadVertices=num;}
  165. Int getNumIndices(void) {return m_numRoadIndices;}
  166. #ifdef LOAD_TEST_ASSETS
  167. void setAutoLoaded(void) {m_isAutoLoaded = true;};
  168. Bool isAutoLoaded(void) {return m_isAutoLoaded;};
  169. AsciiString getPath(void) {return(m_texturePath);};
  170. void loadTestTexture(void);
  171. #endif
  172. } ;
  173. class WorldHeightMap;
  174. //
  175. // W3DRoadBuffer: Draw buffer for the roads.
  176. //
  177. //
  178. class W3DRoadBuffer
  179. {
  180. friend class BaseHeightMapRenderObjClass;
  181. public:
  182. W3DRoadBuffer(void);
  183. ~W3DRoadBuffer(void);
  184. /// Loads the roads from the map objects list.
  185. void loadRoads();
  186. /// Empties the road buffer.
  187. void clearAllRoads(void);
  188. /// Draws the roads. Uses terrain bounds for culling.
  189. void drawRoads(CameraClass * camera, TextureClass *cloudTexture, TextureClass *noiseTexture, Bool wireframe,
  190. Int minX, Int maxX, Int minY, Int maxY, RefRenderObjListIterator *pDynamicLightsIterator);
  191. /// Sets the map pointer.
  192. void setMap(WorldHeightMap *pMap);
  193. /// Updates the diffuse lighting in the buffers.
  194. void updateLighting(void);
  195. /// Notifies that the camera moved.
  196. void updateCenter(void);
  197. protected:
  198. RoadType *m_roadTypes; ///<Roads texture
  199. RoadSegment *m_roads; ///< The road buffer. All roads are stored here.
  200. Int m_numRoads; ///< Number of roads used in m_roads.
  201. Bool m_initialized; ///< True if the subsystem initialized.
  202. WorldHeightMap *m_map; ///< Pointer to the height map data.
  203. RefRenderObjListIterator *m_lightsIterator; ///< Lighting iterator.
  204. Int m_curUniqueID; ///< Road type we are rendering at this pass.
  205. Int m_curRoadType;
  206. #ifdef LOAD_TEST_ASSETS
  207. Int m_maxUID; ///< Maximum UID.
  208. Int m_curOpenRoad; ///< First road type not used.
  209. #endif
  210. Int m_maxRoadSegments; ///< Size of m_roads.
  211. Int m_maxRoadVertex; ///< Size of m_vertexRoad.
  212. Int m_maxRoadIndex; ///< Size of m_indexRoad.
  213. Int m_maxRoadTypes; ///< Size of m_roadTypes.
  214. Int m_curNumRoadVertices; ///<Number of vertices used in current road type.
  215. Int m_curNumRoadIndices; ///<Number of indices used in current road type;
  216. Bool m_updateBuffers; ///< If true, update the vertex buffers.
  217. void addMapObjects(void);
  218. void addMapObject(RoadSegment *pRoad, Bool updateTheCounts);
  219. void adjustStacking(Int topUniqueID, Int bottomUniqueID);
  220. Int findCrossTypeJoinVector(Vector2 loc, Vector2 *joinVector, Int uniqueID);
  221. void flipTheRoad(RoadSegment *pRoad) {TRoadPt tmp=pRoad->m_pt1; pRoad->m_pt1 = pRoad->m_pt2; pRoad->m_pt2 = tmp;}; ///< Flips the loc1 and loc2 info.
  222. void insertCurveSegmentAt(Int ndx1, Int ndx2);
  223. void insertCrossTypeJoins(void);
  224. void insertJoinAt(Int ndx);
  225. void miter(Int ndx1, Int ndx2);
  226. void moveRoadSegTo(Int fromNdx, Int toNdx);
  227. void checkLinkAfter(Int ndx);
  228. void checkLinkBefore(Int ndx);
  229. void updateCounts(RoadSegment *pRoad);
  230. void updateCountsAndFlags(void);
  231. void insertCurveSegments(void);
  232. void insertTeeIntersections(void);
  233. void insertTee(Vector2 loc, Int index1, Real scale);
  234. Bool insertY(Vector2 loc, Int index1, Real scale);
  235. void insert4Way(Vector2 loc, Int index1, Real scale);
  236. void offset4Way(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, TRoadPt *pr3, TRoadPt *pc4, Vector2 loc, Vector2 alignVector, Real widthInTexture);
  237. void offset3Way(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc, Vector2 upVector, Vector2 teeVector, Real widthInTexture);
  238. void offsetY(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc, Vector2 upVector, Real widthInTexture);
  239. void offsetH(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc, Vector2 upVector, Vector2 teeVector, Bool flip, Bool mirror, Real widthInTexture);
  240. void preloadRoadsInVertexAndIndexBuffers(void); ///< Fills the index and vertex buffers for drawing.
  241. void preloadRoadSegment(RoadSegment *pRoad); ///< Fills the index and vertex buffers for drawing 1 segment.
  242. void loadCurve(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Real scale); ///< Fills the index and vertex buffers for drawing 1 fade.
  243. void loadTee(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Bool is4way, Real scale); ///< Fills the index and vertex buffers for drawing 1 tee intersection.
  244. void loadY(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Real scale); ///< Fills the index and vertex buffers for drawing 1 Y intersection.
  245. void loadAlphaJoin(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Real scale); ///< Fills the index and vertex buffers for drawing 1 alpha blend cap.
  246. void loadH(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Bool flip, Real scale); ///< Fills the index and vertex buffers for drawing 1 h tee intersection.
  247. void loadFloatSection(RoadSegment *pRoad, Vector2 loc,
  248. Vector2 roadVector, Real height, Real left, Real right, Real uOffset, Real vOffset, Real scale);
  249. void loadFloat4PtSection(RoadSegment *pRoad, Vector2 loc,
  250. Vector2 roadNormal, Vector2 roadVector,
  251. Vector2 *cornersP,
  252. Real uOffset, Real vOffset, Real uScale, Real vScale);
  253. void loadLit4PtSection(RoadSegment *pRoad, UnsignedShort *ib, VertexFormatXYZDUV1 *vb, RefRenderObjListIterator *pDynamicLightsIterator);
  254. void loadRoadsInVertexAndIndexBuffers(void); ///< Fills the index and vertex buffers for drawing.
  255. void loadLitRoadsInVertexAndIndexBuffers(RefRenderObjListIterator *pDynamicLightsIterator); ///< Fills the index and vertex buffers for drawing.
  256. void loadRoadSegment(UnsignedShort *ib, VertexFormatXYZDUV1 *vb, RoadSegment *pRoad); ///< Fills the index and vertex buffers for drawing 1 segment.
  257. void allocateRoadBuffers(void); ///< Allocates the buffers.
  258. void freeRoadBuffers(void); ///< Frees the index and vertex buffers.
  259. Bool visibilityChanged(const IRegion2D &bounds); ///< Returns true if some roads are now visible that weren't, or vice versa.
  260. void rotateAbout(Vector2 *ptP, Vector2 center, Real angle);
  261. };
  262. #endif // end __W3DROAD_BUFFER_H_