renegadeterrainmaterialpass.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. ** Command & Conquer Renegade(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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : wwphys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/renegadeterrainmaterialpass.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 3/05/02 10:56a $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "renegadeterrainmaterialpass.h"
  36. #include "terrainmaterial.h"
  37. #include "vector2.h"
  38. #include "dx8vertexbuffer.h"
  39. #include "dx8indexbuffer.h"
  40. #include "chunkio.h"
  41. ////////////////////////////////////////////////////////////////
  42. // Local constants
  43. ////////////////////////////////////////////////////////////////
  44. enum
  45. {
  46. CHUNKID_VARIABLES = 0x03040552,
  47. CHUNKID_VERTEX_ALPHAS,
  48. CHUNKID_VERTEX_UVS,
  49. CHUNKID_MATERIAL,
  50. CHUNKID_QUAD_LIST,
  51. CHUNKID_VERTEX_RENDER_LIST,
  52. CHUNKID_VERTEX_INDEX_MAP
  53. };
  54. enum
  55. {
  56. VARID_VERTEX_COUNT = 0x01,
  57. };
  58. ////////////////////////////////////////////////////////////////
  59. //
  60. // RenegadeTerrainMaterialPassClass
  61. //
  62. ////////////////////////////////////////////////////////////////
  63. RenegadeTerrainMaterialPassClass::RenegadeTerrainMaterialPassClass (void) :
  64. VertexAlpha (NULL),
  65. GridUVs (NULL),
  66. Material (NULL),
  67. VertexCount (0)
  68. {
  69. ::memset (VertexIndexMap, 0, sizeof (VertexIndexMap));
  70. ::memset (IndexBuffers, 0, sizeof (IndexBuffers));
  71. ::memset (VertexBuffers, 0, sizeof (VertexBuffers));
  72. //
  73. // Prep the lists
  74. //
  75. for (int index = 0; index < PASS_COUNT; index ++) {
  76. QuadList[index].Set_Growth_Step (1000);
  77. VertexRenderList[index].Set_Growth_Step (1000);
  78. }
  79. return ;
  80. }
  81. ////////////////////////////////////////////////////////////////
  82. //
  83. // ~RenegadeTerrainMaterialPassClass
  84. //
  85. ////////////////////////////////////////////////////////////////
  86. RenegadeTerrainMaterialPassClass::~RenegadeTerrainMaterialPassClass (void)
  87. {
  88. REF_PTR_RELEASE (Material);
  89. //
  90. // Free the UV array
  91. //
  92. if (GridUVs != NULL) {
  93. delete [] GridUVs;
  94. GridUVs = NULL;
  95. }
  96. //
  97. // Free the vertex alpha array
  98. //
  99. if (VertexAlpha != NULL) {
  100. delete [] VertexAlpha;
  101. VertexAlpha = NULL;
  102. }
  103. //
  104. // Free the vertex index arrays
  105. //
  106. for (int index = 0; index < PASS_COUNT; index ++) {
  107. if (VertexIndexMap[index] != NULL) {
  108. delete [] VertexIndexMap[index];
  109. VertexIndexMap[index] = NULL;
  110. }
  111. //
  112. // Release our hold on the index and vertex buffers for this pass
  113. //
  114. REF_PTR_RELEASE (IndexBuffers[index]);
  115. REF_PTR_RELEASE (VertexBuffers[index]);
  116. }
  117. return ;
  118. }
  119. ////////////////////////////////////////////////////////////////
  120. //
  121. // Allocate
  122. //
  123. ////////////////////////////////////////////////////////////////
  124. void
  125. RenegadeTerrainMaterialPassClass::Allocate (int vertex_count)
  126. {
  127. //
  128. // Allocate the vertex alpha and UV arrays
  129. //
  130. VertexCount = vertex_count;
  131. VertexAlpha = new float[vertex_count];
  132. GridUVs = new Vector2[vertex_count];
  133. //
  134. // Initialize the arrays
  135. //
  136. for (int index = 0; index < vertex_count; index ++) {
  137. VertexAlpha[index] = 1.0F;
  138. GridUVs[index].Set (0.0F, 0.0F);
  139. }
  140. //
  141. // Allocate the vertex index map
  142. //
  143. for (index = 0; index < PASS_COUNT; index ++) {
  144. VertexIndexMap[index] = new int[vertex_count];
  145. ::memset (VertexIndexMap[index], -1, sizeof (int) * vertex_count);
  146. }
  147. return ;
  148. }
  149. ////////////////////////////////////////////////////////////////
  150. //
  151. // Reset
  152. //
  153. ////////////////////////////////////////////////////////////////
  154. void
  155. RenegadeTerrainMaterialPassClass::Reset (void)
  156. {
  157. //
  158. // Allocate the vertex index map
  159. //
  160. for (int index = 0; index < PASS_COUNT; index ++) {
  161. ::memset (VertexIndexMap[index], -1, sizeof (int) * VertexCount);
  162. QuadList[index].Reset_Active ();
  163. VertexRenderList[index].Reset_Active ();
  164. }
  165. return ;
  166. }
  167. //////////////////////////////////////////////////////////////////////
  168. //
  169. // Save
  170. //
  171. //////////////////////////////////////////////////////////////////////
  172. bool
  173. RenegadeTerrainMaterialPassClass::Save (ChunkSaveClass &csave)
  174. {
  175. //
  176. // Write the variables
  177. //
  178. csave.Begin_Chunk (CHUNKID_VARIABLES);
  179. WRITE_MICRO_CHUNK (csave, VARID_VERTEX_COUNT, VertexCount);
  180. csave.End_Chunk ();
  181. //
  182. // Save the vertex alpha array
  183. //
  184. csave.Begin_Chunk (CHUNKID_VERTEX_ALPHAS);
  185. csave.Write (VertexAlpha, sizeof (float) * VertexCount);
  186. csave.End_Chunk ();
  187. //
  188. // Save the grid UVs array
  189. //
  190. csave.Begin_Chunk (CHUNKID_VERTEX_UVS);
  191. csave.Write (GridUVs, sizeof (Vector2) * VertexCount);
  192. csave.End_Chunk ();
  193. //
  194. // Save the material
  195. //
  196. csave.Begin_Chunk (CHUNKID_MATERIAL);
  197. Material->Save (csave);
  198. csave.End_Chunk ();
  199. //
  200. // Save the per-pass lists
  201. //
  202. int count = 0;
  203. for (int index = 0; index < PASS_COUNT; index ++) {
  204. //
  205. // Save the quad list
  206. //
  207. csave.Begin_Chunk (CHUNKID_QUAD_LIST);
  208. count = QuadList[index].Count ();
  209. csave.Write (&count, sizeof (int));
  210. csave.Write (&QuadList[index][0], sizeof (int) * QuadList[index].Count ());
  211. csave.End_Chunk ();
  212. //
  213. // Save the vertex render list
  214. //
  215. csave.Begin_Chunk (CHUNKID_VERTEX_RENDER_LIST);
  216. count = VertexRenderList[index].Count ();
  217. csave.Write (&count, sizeof (int));
  218. csave.Write (&VertexRenderList[index][0], sizeof (int) * VertexRenderList[index].Count ());
  219. csave.End_Chunk ();
  220. //
  221. // Save the vertex index map
  222. //
  223. csave.Begin_Chunk (CHUNKID_VERTEX_INDEX_MAP);
  224. csave.Write (VertexIndexMap[index], sizeof (int) * VertexCount);
  225. csave.End_Chunk ();
  226. }
  227. return true;
  228. }
  229. //////////////////////////////////////////////////////////////////////
  230. //
  231. // Load
  232. //
  233. //////////////////////////////////////////////////////////////////////
  234. bool
  235. RenegadeTerrainMaterialPassClass::Load (ChunkLoadClass &cload)
  236. {
  237. int pass = -1;
  238. while (cload.Open_Chunk ()) {
  239. switch (cload.Cur_Chunk_ID ()) {
  240. //
  241. // Load all the variables from this chunk
  242. //
  243. case CHUNKID_VARIABLES:
  244. Load_Variables (cload);
  245. break;
  246. case CHUNKID_VERTEX_ALPHAS:
  247. cload.Read (VertexAlpha, sizeof (float) * VertexCount);
  248. break;
  249. case CHUNKID_VERTEX_UVS:
  250. cload.Read (GridUVs, sizeof (Vector2) * VertexCount);
  251. break;
  252. case CHUNKID_MATERIAL:
  253. {
  254. //
  255. // Load the material
  256. //
  257. Material = new TerrainMaterialClass;
  258. Material->Load (cload);
  259. break;
  260. }
  261. case CHUNKID_QUAD_LIST:
  262. {
  263. pass ++;
  264. //
  265. // Read the number of quads
  266. //
  267. int count = 0;
  268. cload.Read (&count, sizeof (int));
  269. //
  270. // Allocate enough room for this many quads and read them in...
  271. //
  272. QuadList[pass].Resize (count);
  273. if (count > 0) {
  274. cload.Read (&QuadList[pass][0], sizeof (int) * count);
  275. QuadList[pass].Set_Active (count);
  276. }
  277. break;
  278. }
  279. case CHUNKID_VERTEX_RENDER_LIST:
  280. {
  281. //
  282. // Read the number of vertices
  283. //
  284. int count = 0;
  285. cload.Read (&count, sizeof (int));
  286. //
  287. // Allocate enough room for this many vertices and read them in...
  288. //
  289. VertexRenderList[pass].Resize (count);
  290. if (count > 0) {
  291. cload.Read (&VertexRenderList[pass][0], sizeof (int) * count);
  292. VertexRenderList[pass].Set_Active (count);
  293. }
  294. break;
  295. }
  296. case CHUNKID_VERTEX_INDEX_MAP:
  297. cload.Read (VertexIndexMap[pass], sizeof (int) * VertexCount);
  298. break;
  299. }
  300. cload.Close_Chunk ();
  301. }
  302. return true;
  303. }
  304. ////////////////////////////////////////////////////////////////
  305. //
  306. // Load_Variables
  307. //
  308. ////////////////////////////////////////////////////////////////
  309. void
  310. RenegadeTerrainMaterialPassClass::Load_Variables (ChunkLoadClass &cload)
  311. {
  312. int vertex_count = 0;
  313. while (cload.Open_Micro_Chunk ()) {
  314. switch (cload.Cur_Micro_Chunk_ID ()) {
  315. //
  316. // Read each of the microchunks
  317. //
  318. READ_MICRO_CHUNK (cload, VARID_VERTEX_COUNT, vertex_count);
  319. }
  320. cload.Close_Micro_Chunk ();
  321. }
  322. //
  323. // Allocate the necessary data structures
  324. //
  325. Allocate (vertex_count);
  326. return ;
  327. }