W3DRoadBuffer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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: 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. protected:
  116. Int m_numVertex;
  117. VertexFormatXYZDUV1* m_vb;
  118. Int m_numIndex;
  119. UnsignedShort* m_ib;
  120. TRoadSegInfo m_info;
  121. SphereClass m_bounds;
  122. public:
  123. RoadSegment(void);
  124. ~RoadSegment(void);
  125. public:
  126. void SetVertexBuffer(VertexFormatXYZDUV1 *vb, Int numVertex);
  127. void SetIndexBuffer(UnsignedShort *ib, Int numIndex);
  128. void SetRoadSegInfo(TRoadSegInfo *pInfo) {m_info = *pInfo;};
  129. void GetRoadSegInfo(TRoadSegInfo *pInfo) {*pInfo = m_info;};
  130. const SphereClass &getBounds(void) {return m_bounds;};
  131. Int GetNumVertex(void) {return m_numVertex;};
  132. Int GetNumIndex(void) {return m_numIndex;};
  133. Int GetVertices(VertexFormatXYZDUV1 *destination_vb, Int numToCopy);
  134. Int GetIndices(UnsignedShort *destination_ib, Int numToCopy, Int offset);
  135. void updateSegLighting(void);
  136. } ;
  137. class RoadType {
  138. public:
  139. RoadType(void);
  140. ~RoadType(void);
  141. protected:
  142. TextureClass *m_roadTexture; ///<Roads texture
  143. DX8VertexBufferClass *m_vertexRoad; ///<Road vertex buffer.
  144. DX8IndexBufferClass *m_indexRoad; ///<indices defining a triangles for the road drawing.
  145. Int m_numRoadVertices; ///<Number of vertices used in m_vertexRoad.
  146. Int m_numRoadIndices; ///<Number of indices used in b_indexRoad;
  147. Int m_uniqueID; ///< ID of the road type in INI.
  148. Bool m_isAutoLoaded;
  149. Int m_stackingOrder; ///< Order in the drawing. 0 drawn first, then 1 and so on.
  150. #ifdef LOAD_TEST_ASSETS
  151. AsciiString m_texturePath;
  152. #endif
  153. public:
  154. void loadTexture(AsciiString path, Int id);
  155. void applyTexture(void);
  156. Int getStacking(void) {return m_stackingOrder;}
  157. void setStacking(Int order) {m_stackingOrder = order;}
  158. Int getUniqueID(void) {return m_uniqueID;};
  159. DX8VertexBufferClass *getVB(void) {return m_vertexRoad;};
  160. DX8IndexBufferClass *getIB(void) {return m_indexRoad;}
  161. Int getNumVertices(void) {return m_numRoadVertices;}
  162. void setNumIndices(Int num) {m_numRoadIndices=num;}
  163. void setNumVertices(Int num) {m_numRoadVertices=num;}
  164. Int getNumIndices(void) {return m_numRoadIndices;}
  165. #ifdef LOAD_TEST_ASSETS
  166. void setAutoLoaded(void) {m_isAutoLoaded = true;};
  167. Bool isAutoLoaded(void) {return m_isAutoLoaded;};
  168. AsciiString getPath(void) {return(m_texturePath);};
  169. void loadTestTexture(void);
  170. #endif
  171. } ;
  172. class WorldHeightMap;
  173. //
  174. // W3DRoadBuffer: Draw buffer for the roads.
  175. //
  176. //
  177. class W3DRoadBuffer
  178. {
  179. friend class HeightMapRenderObjClass;
  180. public:
  181. W3DRoadBuffer(void);
  182. ~W3DRoadBuffer(void);
  183. /// Loads the roads from the map objects list.
  184. void loadRoads();
  185. /// Empties the road buffer.
  186. void clearAllRoads(void);
  187. /// Draws the roads. Uses terrain bounds for culling.
  188. void drawRoads(CameraClass * camera, TextureClass *cloudTexture, TextureClass *noiseTexture, Bool wireframe,
  189. Int minX, Int maxX, Int minY, Int maxY, RefRenderObjListIterator *pDynamicLightsIterator);
  190. /// Sets the map pointer.
  191. void setMap(WorldHeightMap *pMap);
  192. /// Updates the diffuse lighting in the buffers.
  193. void updateLighting(void);
  194. protected:
  195. RoadType *m_roadTypes; ///<Roads texture
  196. RoadSegment *m_roads; ///< The road buffer. All roads are stored here.
  197. Int m_numRoads; ///< Number of roads used in m_roads.
  198. Bool m_initialized; ///< True if the subsystem initialized.
  199. WorldHeightMap *m_map; ///< Pointer to the height map data.
  200. RefRenderObjListIterator *m_lightsIterator; ///< Lighting iterator.
  201. Int m_minX, m_maxX, m_minY, m_maxY; ///< Bounds on the terrain to be rendered.
  202. Int m_curUniqueID; ///< Road type we are rendering at this pass.
  203. Int m_curRoadType;
  204. #ifdef LOAD_TEST_ASSETS
  205. Int m_maxUID; ///< Maximum UID.
  206. Int m_curOpenRoad; ///< First road type not used.
  207. #endif
  208. Int m_maxRoadSegments; ///< Size of m_roads.
  209. Int m_maxRoadVertex; ///< Size of m_vertexRoad.
  210. Int m_maxRoadIndex; ///< Size of m_indexRoad.
  211. Int m_maxRoadTypes; ///< Size of m_roadTypes.
  212. Int m_curNumRoadVertices; ///<Number of vertices used in current road type.
  213. Int m_curNumRoadIndices; ///<Number of indices used in current road type;
  214. void addMapObjects(void);
  215. void addMapObject(RoadSegment *pRoad, Bool updateTheCounts);
  216. void adjustStacking(Int topUniqueID, Int bottomUniqueID);
  217. Int findCrossTypeJoinVector(Vector2 loc, Vector2 *joinVector, Int uniqueID);
  218. 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.
  219. void insertCurveSegmentAt(Int ndx1, Int ndx2);
  220. void insertCrossTypeJoins(void);
  221. void insertJoinAt(Int ndx);
  222. void miter(Int ndx1, Int ndx2);
  223. void moveRoadSegTo(Int fromNdx, Int toNdx);
  224. void checkLinkAfter(Int ndx);
  225. void checkLinkBefore(Int ndx);
  226. void updateCounts(RoadSegment *pRoad);
  227. void updateCountsAndFlags(void);
  228. void insertCurveSegments(void);
  229. void insertTeeIntersections(void);
  230. void insertTee(Vector2 loc, Int index1, Real scale);
  231. Bool insertY(Vector2 loc, Int index1, Real scale);
  232. void insert4Way(Vector2 loc, Int index1, Real scale);
  233. void offset4Way(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, TRoadPt *pr3, TRoadPt *pc4, Vector2 loc, Vector2 alignVector, Real widthInTexture);
  234. void offset3Way(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc, Vector2 upVector, Vector2 teeVector, Real widthInTexture);
  235. void offsetY(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc, Vector2 upVector, Real widthInTexture);
  236. void offsetH(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc, Vector2 upVector, Vector2 teeVector, Bool flip, Bool mirror, Real widthInTexture);
  237. void preloadRoadsInVertexAndIndexBuffers(void); ///< Fills the index and vertex buffers for drawing.
  238. void preloadRoadSegment(RoadSegment *pRoad); ///< Fills the index and vertex buffers for drawing 1 segment.
  239. void loadCurve(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Real scale); ///< Fills the index and vertex buffers for drawing 1 fade.
  240. void loadTee(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Bool is4way, Real scale); ///< Fills the index and vertex buffers for drawing 1 tee intersection.
  241. void loadY(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Real scale); ///< Fills the index and vertex buffers for drawing 1 Y intersection.
  242. void loadAlphaJoin(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Real scale); ///< Fills the index and vertex buffers for drawing 1 alpha blend cap.
  243. void loadH(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Bool flip, Real scale); ///< Fills the index and vertex buffers for drawing 1 h tee intersection.
  244. void loadFloatSection(RoadSegment *pRoad, Vector2 loc,
  245. Vector2 roadVector, Real height, Real left, Real right, Real uOffset, Real vOffset, Real scale);
  246. void loadFloat4PtSection(RoadSegment *pRoad, Vector2 loc,
  247. Vector2 roadNormal, Vector2 roadVector,
  248. Vector2 *cornersP,
  249. Real uOffset, Real vOffset, Real uScale, Real vScale);
  250. void loadLit4PtSection(RoadSegment *pRoad, UnsignedShort *ib, VertexFormatXYZDUV1 *vb, RefRenderObjListIterator *pDynamicLightsIterator);
  251. void loadRoadsInVertexAndIndexBuffers(void); ///< Fills the index and vertex buffers for drawing.
  252. void loadLitRoadsInVertexAndIndexBuffers(RefRenderObjListIterator *pDynamicLightsIterator); ///< Fills the index and vertex buffers for drawing.
  253. void loadRoadSegment(UnsignedShort *ib, VertexFormatXYZDUV1 *vb, RoadSegment *pRoad); ///< Fills the index and vertex buffers for drawing 1 segment.
  254. void allocateRoadBuffers(void); ///< Allocates the buffers.
  255. void freeRoadBuffers(void); ///< Frees the index and vertex buffers.
  256. void rotateAbout(Vector2 *ptP, Vector2 center, Real angle);
  257. };
  258. #endif // end __W3DROAD_BUFFER_H_