meshmatdesc.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. *** 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 : ww3d *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/meshmatdesc.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 1/18/02 8:03p $*
  31. * *
  32. * $Revision:: 28 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "meshmatdesc.h"
  38. #include "texture.h"
  39. #include "vertmaterial.h"
  40. #include "realcrc.h"
  41. #include "dx8wrapper.h"
  42. #include "dx8caps.h"
  43. #include "meshmdl.h"
  44. /**************************************************************************************************
  45. **
  46. **
  47. ** MatBufferClass Implementation
  48. **
  49. **
  50. **************************************************************************************************/
  51. MatBufferClass::MatBufferClass(const MatBufferClass & that) :
  52. ShareBufferClass<VertexMaterialClass *>(that)
  53. {
  54. // add a reference for each pointer that was copied...
  55. for (int i=0; i<Count; i++) {
  56. if (Array[i]) {
  57. Array[i]->Add_Ref();
  58. }
  59. }
  60. }
  61. MatBufferClass::~MatBufferClass(void)
  62. {
  63. for (int i=0; i<Count; i++) {
  64. REF_PTR_RELEASE(Array[i]);
  65. }
  66. }
  67. void MatBufferClass::Set_Element(int index,VertexMaterialClass * mat)
  68. {
  69. REF_PTR_SET(Array[index],mat);
  70. }
  71. VertexMaterialClass * MatBufferClass::Get_Element(int index)
  72. {
  73. if (Array[index]) {
  74. Array[index]->Add_Ref();
  75. }
  76. return Array[index];
  77. }
  78. VertexMaterialClass * MatBufferClass::Peek_Element(int index)
  79. {
  80. return Array[index];
  81. }
  82. /**************************************************************************************************
  83. **
  84. **
  85. ** TexBufferClass Implementation
  86. **
  87. **
  88. **************************************************************************************************/
  89. TexBufferClass::TexBufferClass(const TexBufferClass & that) :
  90. ShareBufferClass<TextureClass *>(that)
  91. {
  92. // add a reference for each pointer that was copied...
  93. for (int i=0; i<Count; i++) {
  94. if (Array[i]) {
  95. Array[i]->Add_Ref();
  96. }
  97. }
  98. }
  99. TexBufferClass::~TexBufferClass(void)
  100. {
  101. for (int i=0;i<Count;i++) {
  102. REF_PTR_RELEASE(Array[i]);
  103. }
  104. }
  105. void TexBufferClass::Set_Element(int index,TextureClass * tex)
  106. {
  107. REF_PTR_SET(Array[index],tex);
  108. }
  109. TextureClass * TexBufferClass::Get_Element(int index)
  110. {
  111. if (Array[index]) {
  112. Array[index]->Add_Ref();
  113. }
  114. return Array[index];
  115. }
  116. TextureClass * TexBufferClass::Peek_Element(int index)
  117. {
  118. return Array[index];
  119. }
  120. /**************************************************************************************************
  121. **
  122. **
  123. ** UVBufferClass Implementation
  124. **
  125. **
  126. **************************************************************************************************/
  127. UVBufferClass::UVBufferClass(const UVBufferClass & that) :
  128. ShareBufferClass<Vector2>(that)
  129. {
  130. CRC = that.CRC;
  131. }
  132. bool UVBufferClass::operator == (const UVBufferClass & that)
  133. {
  134. // NOTE: this only works if you've properly called Update_CRC after filling the array
  135. return (CRC == that.CRC);
  136. }
  137. bool UVBufferClass::Is_Equal_To(const UVBufferClass & that)
  138. {
  139. // NOTE: this only works if you've properly called Update_CRC after filling the array
  140. return (CRC == that.CRC);
  141. }
  142. void UVBufferClass::Update_CRC(void)
  143. {
  144. CRC = CRC_Memory((unsigned char *)Get_Array(),Get_Count() * sizeof(Vector2));
  145. }
  146. /**************************************************************************************************
  147. **
  148. **
  149. ** MeshMatDescClass Implementation
  150. **
  151. **
  152. **************************************************************************************************/
  153. ShaderClass MeshMatDescClass::NullShader(0); // Used to mark no shader data
  154. MeshMatDescClass::MeshMatDescClass(void) :
  155. PassCount(1),
  156. VertexCount(0),
  157. PolyCount(0)
  158. {
  159. for (int array=0;array < MAX_COLOR_ARRAYS; array++) {
  160. ColorArray[array] = NULL;
  161. }
  162. for (int uvarray=0;uvarray<MAX_UV_ARRAYS;uvarray++) {
  163. UV[uvarray] = NULL;
  164. }
  165. for (int pass=0; pass < MAX_PASSES; pass++) {
  166. for (int stage=0; stage < MAX_TEX_STAGES; stage++) {
  167. UVSource[pass][stage] = -1;
  168. Texture[pass][stage] = NULL;
  169. TextureArray[pass][stage] = NULL;
  170. }
  171. DCGSource[pass] = VertexMaterialClass::MATERIAL;
  172. DIGSource[pass] = VertexMaterialClass::MATERIAL;
  173. Shader[pass] = 0; //ShaderClass::_PresetOpaqueSolidShader;
  174. Material[pass] = NULL;
  175. ShaderArray[pass] = NULL;
  176. MaterialArray[pass] = NULL;
  177. }
  178. }
  179. MeshMatDescClass::MeshMatDescClass(const MeshMatDescClass & that) :
  180. PassCount(1),
  181. VertexCount(0),
  182. PolyCount(0)
  183. {
  184. int pass;
  185. int stage;
  186. int array;
  187. // init everything to NULL
  188. for (array=0;array < MAX_COLOR_ARRAYS; array++) {
  189. ColorArray[array] = NULL;
  190. }
  191. for (array=0;array < MAX_UV_ARRAYS; array++) {
  192. UV[array] = NULL;
  193. }
  194. for (pass=0; pass < MAX_PASSES; pass++) {
  195. for (stage=0; stage < MAX_TEX_STAGES; stage++) {
  196. UVSource[pass][stage] = -1;
  197. Texture[pass][stage] = NULL;
  198. TextureArray[pass][stage] = NULL;
  199. }
  200. DCGSource[pass] = VertexMaterialClass::MATERIAL;
  201. DIGSource[pass] = VertexMaterialClass::MATERIAL;
  202. Shader[pass] = 0; //ShaderClass::_PresetOpaqueSolidShader;
  203. Material[pass] = NULL;
  204. ShaderArray[pass] = NULL;
  205. MaterialArray[pass] = NULL;
  206. }
  207. *this = that;
  208. }
  209. MeshMatDescClass &
  210. MeshMatDescClass::operator = (const MeshMatDescClass & that)
  211. {
  212. if (this != &that) {
  213. PassCount = that.PassCount;
  214. VertexCount = that.VertexCount;
  215. PolyCount = that.PolyCount;
  216. for (int array=0; array<MAX_COLOR_ARRAYS; array++) {
  217. REF_PTR_SET(ColorArray[array],that.ColorArray[array]);
  218. }
  219. for (int uvarray=0; uvarray<MAX_UV_ARRAYS; uvarray++) {
  220. REF_PTR_SET(UV[uvarray],that.UV[uvarray]);
  221. }
  222. for (int pass=0; pass<MAX_PASSES; pass++) {
  223. for (int stage=0; stage < MAX_TEX_STAGES; stage++) {
  224. UVSource[pass][stage] = that.UVSource[pass][stage];
  225. REF_PTR_SET(Texture[pass][stage],that.Texture[pass][stage]);
  226. // make our own array of texture pointers.
  227. REF_PTR_RELEASE(TextureArray[pass][stage]);
  228. if (that.TextureArray[pass][stage]) {
  229. TextureArray[pass][stage] = NEW_REF(TexBufferClass,(*that.TextureArray[pass][stage]));
  230. }
  231. }
  232. DCGSource[pass] = that.DCGSource[pass];
  233. DIGSource[pass] = that.DIGSource[pass];
  234. Shader[pass] = that.Shader[pass];
  235. REF_PTR_SET(Material[pass],that.Material[pass]);
  236. // make our own arrays of shaders and vertex material pointers
  237. // NOTE: We don't just add-ref these arrays, we make our own copies.
  238. // The only time we add-ref these arrays are when we make alternate material
  239. // representations within this mesh... Then we re-use the same arrays in different
  240. // passes...
  241. REF_PTR_RELEASE(MaterialArray[pass]);
  242. if (that.MaterialArray[pass]) {
  243. MaterialArray[pass] = NEW_REF(MatBufferClass,(*that.MaterialArray[pass]));
  244. }
  245. REF_PTR_RELEASE(ShaderArray[pass]);
  246. if (that.ShaderArray[pass]) {
  247. ShaderArray[pass] = NEW_REF(ShareBufferClass<ShaderClass>,(*that.ShaderArray[pass]));
  248. }
  249. }
  250. }
  251. return *this;
  252. }
  253. MeshMatDescClass::~MeshMatDescClass(void)
  254. {
  255. Reset(0,0,0);
  256. }
  257. TextureClass * MeshMatDescClass::Get_Single_Texture(int pass,int stage) const
  258. {
  259. if (Texture[pass][stage]) {
  260. Texture[pass][stage]->Add_Ref();
  261. }
  262. return Texture[pass][stage];
  263. }
  264. void MeshMatDescClass::Reset(int polycount,int vertcount,int passcount)
  265. {
  266. PolyCount = polycount;
  267. VertexCount = vertcount;
  268. PassCount = passcount;
  269. for (int array=0; array<MAX_COLOR_ARRAYS; array++) {
  270. REF_PTR_RELEASE(ColorArray[array]);
  271. }
  272. for (int uvarray=0; uvarray<MAX_UV_ARRAYS; uvarray++) {
  273. REF_PTR_RELEASE(UV[uvarray]);
  274. }
  275. for (int pass=0;pass<MAX_PASSES;pass++) {
  276. for (int stage=0; stage < MAX_TEX_STAGES; stage++) {
  277. UVSource[pass][stage] = -1;
  278. REF_PTR_RELEASE(Texture[pass][stage]);
  279. REF_PTR_RELEASE(TextureArray[pass][stage]);
  280. }
  281. DCGSource[pass] = VertexMaterialClass::MATERIAL;
  282. DIGSource[pass] = VertexMaterialClass::MATERIAL;
  283. Shader[pass] = 0;
  284. REF_PTR_RELEASE(ShaderArray[pass]);
  285. REF_PTR_RELEASE(Material[pass]);
  286. REF_PTR_RELEASE(MaterialArray[pass]);
  287. }
  288. }
  289. void MeshMatDescClass::Init_Alternate(MeshMatDescClass & default_materials,MeshMatDescClass & alternate_materials)
  290. {
  291. // just copy the counts
  292. PassCount = default_materials.PassCount;
  293. VertexCount = default_materials.VertexCount;
  294. PolyCount = default_materials.PolyCount;
  295. // Color arrays
  296. for (int array=0; array<MAX_COLOR_ARRAYS; array++) {
  297. if (alternate_materials.ColorArray[array] != NULL) {
  298. REF_PTR_SET(ColorArray[array],alternate_materials.ColorArray[array]);
  299. } else {
  300. REF_PTR_SET(ColorArray[array],default_materials.ColorArray[array]);
  301. }
  302. }
  303. // Copy the uv-arrays from the alternate materials to start. Needed uv arrays from
  304. // the default material set will be brought over as encountered below
  305. for (int i=0; i<alternate_materials.Get_UV_Array_Count(); i++) {
  306. REF_PTR_SET(UV[i],alternate_materials.UV[i]);
  307. }
  308. // add-ref the arrays in default_materials except when the same array is present in alternate_materials
  309. for (int pass = 0; pass < MAX_PASSES; pass++) {
  310. for (int stage = 0; stage < MAX_TEX_STAGES; stage++) {
  311. // UV Coorindate arrays, Each UVSource[pass][stage] which is -1 in the alternate_materials
  312. // but not -1 in the default_materials causes us to copy over a uv array from the default_materials
  313. // and set its index into our UVSource array.
  314. if (alternate_materials.UVSource[pass][stage] == -1) {
  315. if (default_materials.UVSource[pass][stage] != -1) {
  316. // Look up the uv array in default_materials that we need to bring over.
  317. int default_uv_source = default_materials.UVSource[pass][stage];
  318. UVBufferClass * uvarray = default_materials.UV[default_uv_source];
  319. int found_index = -1;
  320. // Check if we already have it.
  321. for (int i=0; i<Get_UV_Array_Count(); i++) {
  322. if (uvarray->Get_CRC() == UV[i]->Get_CRC()) {
  323. found_index = i;
  324. break;
  325. }
  326. }
  327. // If we already have it, just set the source index. Otherwise add-ref it
  328. // into a new slot in our uv array and set that index.
  329. if (found_index != -1) {
  330. UVSource[pass][stage] = found_index;
  331. } else {
  332. int new_index = Get_UV_Array_Count();
  333. REF_PTR_SET(UV[new_index],default_materials.UV[default_uv_source]);
  334. UVSource[pass][stage] = new_index;
  335. }
  336. }
  337. } else {
  338. UVSource[pass][stage] = alternate_materials.UVSource[pass][stage];
  339. }
  340. // Texture pointer(s): If alternate_materials has either a single texture or an array of textures,
  341. // then add-ref only the texture data it contains. Otherwise, add-ref the data in default_materials.
  342. if ((alternate_materials.Texture[pass][stage] != NULL) || (alternate_materials.TextureArray[pass][stage])) {
  343. REF_PTR_SET(Texture[pass][stage] , alternate_materials.Texture[pass][stage]);
  344. REF_PTR_SET(TextureArray[pass][stage] , alternate_materials.TextureArray[pass][stage]);
  345. } else {
  346. REF_PTR_SET(Texture[pass][stage] , default_materials.Texture[pass][stage]);
  347. REF_PTR_SET(TextureArray[pass][stage] , default_materials.TextureArray[pass][stage]);
  348. }
  349. }
  350. // Vertex color configuration
  351. if (alternate_materials.DCGSource[pass] == VertexMaterialClass::MATERIAL) {
  352. DCGSource[pass] = default_materials.DCGSource[pass];
  353. } else {
  354. DCGSource[pass] = alternate_materials.DCGSource[pass];
  355. }
  356. // Shaders, currently I can't tell if the alternate data has a shader... Can't override the shader for now.
  357. Shader[pass] = default_materials.Shader[pass];
  358. REF_PTR_SET(ShaderArray[pass],default_materials.ShaderArray[pass]);
  359. // Vertex Materials. If alternate_materials has either a single or array of materials, then copy them
  360. if ((alternate_materials.Material[pass] != NULL) || (alternate_materials.MaterialArray[pass] != NULL)) {
  361. REF_PTR_SET(Material[pass],alternate_materials.Material[pass]);
  362. REF_PTR_SET(MaterialArray[pass],alternate_materials.MaterialArray[pass]);
  363. } else {
  364. // Dont share vertex materials! (because the UVSources can be different!)
  365. if (default_materials.Material[pass]) {
  366. Material[pass] = NEW_REF(VertexMaterialClass,(*(default_materials.Material[pass])));
  367. } else {
  368. if (default_materials.MaterialArray[pass]) {
  369. WWDEBUG_SAY(("Unimplemented case: mesh has more than one default vertex material but no alternate vertex materials have been defined.\r\n"));
  370. }
  371. Material[pass] = NULL;
  372. }
  373. }
  374. }
  375. }
  376. bool MeshMatDescClass::Is_Empty(void)
  377. {
  378. for (int array=0; array<MAX_COLOR_ARRAYS; array++) {
  379. if (ColorArray[array] != NULL) return false;
  380. }
  381. for (int uvarray=0; uvarray<MAX_UV_ARRAYS; uvarray++) {
  382. if (UV[uvarray] != NULL) return false;
  383. }
  384. for (int pass=0; pass<MAX_PASSES; pass++) {
  385. for (int stage=0; stage<MAX_TEX_STAGES; stage++) {
  386. if (Texture[pass][stage] != NULL) return false;
  387. if (TextureArray[pass][stage] != NULL) return false;
  388. }
  389. // if (UVIndex[pass] != NULL) return false;
  390. if (Material[pass] != NULL) return false;
  391. if (MaterialArray[pass] != NULL) return false;
  392. }
  393. return true;
  394. }
  395. void MeshMatDescClass::Set_Single_Material(VertexMaterialClass * vmat,int pass)
  396. {
  397. REF_PTR_SET(Material[pass],vmat);
  398. }
  399. void MeshMatDescClass::Set_Single_Texture(TextureClass * tex,int pass,int stage)
  400. {
  401. REF_PTR_SET(Texture[pass][stage],tex);
  402. }
  403. void MeshMatDescClass::Set_Single_Shader(ShaderClass shader,int pass)
  404. {
  405. Shader[pass] = shader;
  406. }
  407. void MeshMatDescClass::Set_Material(int vidx,VertexMaterialClass * vmat,int pass)
  408. {
  409. MatBufferClass * mats = Get_Material_Array(pass,true);
  410. mats->Set_Element(vidx,vmat);
  411. }
  412. void MeshMatDescClass::Set_Shader(int pidx,ShaderClass shader,int pass)
  413. {
  414. ShaderClass * shaders = Get_Shader_Array(pass,true);
  415. shaders[pidx] = shader;
  416. }
  417. void MeshMatDescClass::Set_Texture(int pidx,TextureClass * tex,int pass,int stage)
  418. {
  419. TexBufferClass * textures = Get_Texture_Array(pass,stage,true);
  420. textures->Set_Element(pidx,tex);
  421. }
  422. VertexMaterialClass * MeshMatDescClass::Get_Material(int vidx,int pass) const
  423. {
  424. if (MaterialArray[pass]) {
  425. return MaterialArray[pass]->Get_Element(vidx);
  426. } else if (Material[pass] != NULL) {
  427. Material[pass]->Add_Ref();
  428. return Material[pass];
  429. }
  430. return NULL;
  431. }
  432. ShaderClass MeshMatDescClass::Get_Shader(int pidx,int pass) const
  433. {
  434. if (ShaderArray[pass]) {
  435. return ShaderArray[pass]->Get_Element(pidx);
  436. }
  437. return Shader[pass];
  438. }
  439. TextureClass * MeshMatDescClass::Get_Texture(int pidx,int pass,int stage) const
  440. {
  441. if (TextureArray[pass][stage]) {
  442. return TextureArray[pass][stage]->Get_Element(pidx);
  443. } else if (Texture[pass][stage] != NULL) {
  444. Texture[pass][stage]->Add_Ref();
  445. return Texture[pass][stage];
  446. }
  447. return NULL;
  448. }
  449. VertexMaterialClass * MeshMatDescClass::Peek_Material(int vidx,int pass) const
  450. {
  451. if (MaterialArray[pass]) {
  452. return MaterialArray[pass]->Peek_Element(vidx);
  453. }
  454. return Material[pass];
  455. }
  456. TextureClass * MeshMatDescClass::Peek_Texture(int pidx,int pass,int stage) const
  457. {
  458. if (TextureArray[pass][stage]) {
  459. return TextureArray[pass][stage]->Peek_Element(pidx);
  460. }
  461. return Texture[pass][stage];
  462. }
  463. TexBufferClass * MeshMatDescClass::Get_Texture_Array(int pass,int stage,bool create)
  464. {
  465. if (create && TextureArray[pass][stage] == NULL) {
  466. TextureArray[pass][stage] = NEW_REF(TexBufferClass,(PolyCount, "MeshMatDescClass::TextureArray"));
  467. }
  468. return TextureArray[pass][stage];
  469. }
  470. MatBufferClass * MeshMatDescClass::Get_Material_Array(int pass,bool create)
  471. {
  472. if (create && MaterialArray[pass] == NULL) {
  473. MaterialArray[pass] = NEW_REF(MatBufferClass,(VertexCount, "MeshMatDescClass::MaterialArray"));
  474. }
  475. return MaterialArray[pass];
  476. }
  477. ShaderClass * MeshMatDescClass::Get_Shader_Array(int pass,bool create)
  478. {
  479. if (create && ShaderArray[pass] == NULL) {
  480. ShaderArray[pass] = NEW_REF(ShareBufferClass<ShaderClass>,(PolyCount, "MeshMatDescClass::ShaderArray"));
  481. ShaderArray[pass]->Clear();
  482. }
  483. if (ShaderArray[pass]) {
  484. return ShaderArray[pass]->Get_Array();
  485. }
  486. return NULL;
  487. }
  488. void MeshMatDescClass::Make_UV_Array_Unique(int pass,int stage)
  489. {
  490. int uvindex = UVSource[pass][stage];
  491. if (UV[uvindex]->Num_Refs() > 1) {
  492. UVBufferClass * unique_uv = NEW_REF(UVBufferClass,(*UV[uvindex]));
  493. UV[uvindex]->Release_Ref();
  494. UV[uvindex] = unique_uv;
  495. }
  496. }
  497. void MeshMatDescClass::Make_Color_Array_Unique(int array)
  498. {
  499. if ((ColorArray[array] != NULL) && (ColorArray[array]->Num_Refs() > 1)) {
  500. ShareBufferClass<unsigned> * unique_color_array = NEW_REF(ShareBufferClass<unsigned>,(*ColorArray[array]));
  501. ColorArray[array]->Release_Ref();
  502. ColorArray[array] = unique_color_array;
  503. }
  504. }
  505. void MeshMatDescClass::Install_UV_Array(int pass,int stage,Vector2 * uvs,int count)
  506. {
  507. /*
  508. ** Compute the crc of this uv array
  509. */
  510. unsigned int crc = CRC_Memory((unsigned char *)uvs,count * sizeof(Vector2));
  511. /*
  512. ** See if there is an existing uv-array that matches the one just loaded
  513. */
  514. bool found = false;
  515. for (int i=0; i<Get_UV_Array_Count(); i++) {
  516. if (UV[i]->Get_CRC() == crc) {
  517. found = true;
  518. Set_UV_Source(pass,stage,i);
  519. break;
  520. }
  521. }
  522. /*
  523. ** If there was no existing uv array, install this one
  524. */
  525. if (found == false) {
  526. /*
  527. ** Find the first empty UV-array slot
  528. */
  529. int new_index = 0;
  530. while ((UV[new_index] != NULL) && (new_index < MAX_UV_ARRAYS)) {
  531. new_index++;
  532. }
  533. if (new_index < MAX_UV_ARRAYS) {
  534. WWASSERT(UV[new_index] == NULL);
  535. UV[new_index] = NEW_REF(UVBufferClass,(count, "MeshMatDescClass::UV"));
  536. memcpy(UV[new_index]->Get_Array(),uvs,count * sizeof(Vector2));
  537. UV[new_index]->Update_CRC(); // update the crc for future comparision
  538. Set_UV_Source(pass,stage,new_index);
  539. }
  540. }
  541. }
  542. void MeshMatDescClass::Post_Load_Process(bool lighting_enabled,MeshModelClass * parent)
  543. {
  544. /*
  545. ** Configure all vertex materials to source the uv coordinates and colors from the correct arrays
  546. ** Pre-multiply the vertex color arrays.
  547. */
  548. bool set_lighting_to_false=true;
  549. for (int pass=0; pass<PassCount; pass++) {
  550. /*
  551. ** If this pass doesn't have a vertex material, create one
  552. */
  553. if ((Material[pass] == NULL) && (MaterialArray[pass] == NULL)) {
  554. Material[pass] = NEW_REF(VertexMaterialClass,());
  555. }
  556. /*
  557. ** Configure the materials to source the uv coordinates and colors
  558. */
  559. if (Material[pass] != NULL) {
  560. Configure_Material(Material[pass],pass,lighting_enabled);
  561. } else {
  562. VertexMaterialClass * prev_mtl = NULL;
  563. VertexMaterialClass * mtl = Peek_Material(pass,0);
  564. for (int vidx=0; vidx<VertexCount; vidx++) {
  565. mtl = Peek_Material(vidx,pass);
  566. if ((mtl != prev_mtl) && (mtl != NULL)) {
  567. Configure_Material(mtl,pass,lighting_enabled);
  568. prev_mtl = mtl;
  569. }
  570. }
  571. }
  572. // Analyze material array types and apply hacks for supporting SR-lighting pipeline if possible.
  573. if (!ColorArray[0] && !ColorArray[1]) continue; // If no color arrays, we don't have a problem
  574. Vector3 single_diffuse(0.0f,0.0f,0.0f);
  575. Vector3 single_ambient(0.0f,0.0f,0.0f);
  576. Vector3 single_emissive(0.0f,0.0f,0.0f);
  577. float single_opacity=1.0f;
  578. bool single_diffuse_used=true;
  579. bool single_ambient_used=true;
  580. bool single_emissive_used=true;
  581. bool single_opacity_used=true;
  582. bool diffuse_used=false;
  583. bool ambient_used=false;
  584. bool emissive_used=false;
  585. bool opacity_used=false;
  586. Vector3 mtl_diffuse;
  587. Vector3 mtl_ambient;
  588. Vector3 mtl_emissive;
  589. float mtl_opacity = 1.0f;
  590. VertexMaterialClass * prev_mtl = NULL;
  591. VertexMaterialClass * mtl = Peek_Material(0, pass);
  592. if (mtl) {
  593. mtl->Get_Diffuse(&single_diffuse);
  594. single_opacity = mtl->Get_Opacity();
  595. mtl->Get_Ambient(&single_ambient);
  596. mtl->Get_Emissive(&single_emissive);
  597. if (single_diffuse.X || single_diffuse.Y || single_diffuse.Z) diffuse_used=true;
  598. if (single_ambient.X || single_ambient.Y || single_ambient.Z) ambient_used=true;
  599. if (single_emissive.X || single_emissive.Y || single_emissive.Z) emissive_used=true;
  600. if (single_opacity!=1.0f) opacity_used=true;
  601. }
  602. for (int vidx=0; vidx<VertexCount; vidx++) {
  603. mtl = Peek_Material(vidx,pass);
  604. if (mtl != prev_mtl) {
  605. prev_mtl = mtl;
  606. mtl->Get_Diffuse(&mtl_diffuse);
  607. mtl_opacity = mtl->Get_Opacity();
  608. mtl->Get_Ambient(&mtl_ambient);
  609. mtl->Get_Emissive(&mtl_emissive);
  610. }
  611. if (mtl_diffuse.X!=single_diffuse.X || mtl_diffuse.Y!=single_diffuse.Y || mtl_diffuse.Z!=single_diffuse.Z) {
  612. single_diffuse_used=false;
  613. }
  614. if (mtl_ambient.X!=single_ambient.X || mtl_ambient.Y!=single_ambient.Y || mtl_ambient.Z!=single_ambient.Z) {
  615. single_ambient_used=false;
  616. }
  617. if (mtl_emissive.X!=single_emissive.X || mtl_emissive.Y!=single_emissive.Y || mtl_emissive.Z!=single_emissive.Z) {
  618. single_emissive_used=false;
  619. }
  620. if (mtl_opacity!=single_opacity) {
  621. single_opacity_used=false;
  622. }
  623. if (mtl_diffuse.X || mtl_diffuse.Y || mtl_diffuse.Z) diffuse_used=true;
  624. if (mtl_ambient.X || mtl_ambient.Y || mtl_ambient.Z) ambient_used=true;
  625. if (mtl_emissive.X || mtl_emissive.Y || mtl_emissive.Z) emissive_used=true;
  626. if (mtl_opacity!=1.0f) opacity_used=true;
  627. }
  628. // If both DCG and DIG arrays are submitted, multiply them together to DCG channel
  629. if ((DCGSource[pass] != VertexMaterialClass::MATERIAL) && (ColorArray[0] != NULL) &&
  630. (DIGSource[pass] != VertexMaterialClass::MATERIAL) && (ColorArray[1] != NULL)) {
  631. unsigned * diffuse_array = ColorArray[0]->Get_Array();
  632. unsigned * emissive_array = ColorArray[1]->Get_Array();
  633. for (int vidx=0; vidx<VertexCount; vidx++) {
  634. Vector4 diffuse=DX8Wrapper::Convert_Color(diffuse_array[vidx]);
  635. Vector4 emissive=DX8Wrapper::Convert_Color(emissive_array[vidx]);
  636. diffuse.X *= emissive.X;
  637. diffuse.Y *= emissive.Y;
  638. diffuse.Z *= emissive.Z;
  639. diffuse_array[vidx]=DX8Wrapper::Convert_Color(diffuse);
  640. }
  641. }
  642. DIGSource[pass]=VertexMaterialClass::MATERIAL; // DIG channel no more
  643. if ((DCGSource[pass] != VertexMaterialClass::MATERIAL) && (ColorArray[0] != NULL)) {
  644. unsigned * diffuse_array = ColorArray[0]->Get_Array();
  645. Vector3 mtl_diffuse;
  646. float mtl_opacity = 1.0f;
  647. VertexMaterialClass * prev_mtl = NULL;
  648. VertexMaterialClass * mtl = Peek_Material(0,pass);
  649. for (int vidx=0; vidx<VertexCount; vidx++) {
  650. mtl = Peek_Material(vidx,pass);
  651. if (mtl != prev_mtl) {
  652. prev_mtl = mtl;
  653. mtl->Get_Diffuse(&mtl_diffuse);
  654. mtl_opacity = mtl->Get_Opacity();
  655. }
  656. // If only diffuse is used apply diffuse to color channel and set diffuse source to color 1
  657. if (diffuse_used && !ambient_used && !emissive_used) {
  658. Vector4 diffuse=DX8Wrapper::Convert_Color(diffuse_array[vidx]);
  659. diffuse.X *= mtl_diffuse.X;
  660. diffuse.Y *= mtl_diffuse.Y;
  661. diffuse.Z *= mtl_diffuse.Z;
  662. diffuse.W *= mtl_opacity;
  663. diffuse_array[vidx]=DX8Wrapper::Convert_Color(diffuse);
  664. mtl->Set_Ambient_Color_Source(VertexMaterialClass::MATERIAL);
  665. mtl->Set_Diffuse_Color_Source(VertexMaterialClass::COLOR1);
  666. mtl->Set_Emissive_Color_Source(VertexMaterialClass::MATERIAL);
  667. }
  668. // If diffuse and ambient are used, apply diffuse to color channel and set diffuse
  669. // and ambient sources to color 1. (this is not completely correct if diffuse and
  670. // ambient are different but is probably the most reasonable thing to do. Why set
  671. // diffuse and ambient differently anyway?)
  672. if (diffuse_used && ambient_used && !emissive_used) {
  673. Vector4 diffuse=DX8Wrapper::Convert_Color(diffuse_array[vidx]);
  674. diffuse.X *= mtl_diffuse.X;
  675. diffuse.Y *= mtl_diffuse.Y;
  676. diffuse.Z *= mtl_diffuse.Z;
  677. diffuse.W *= mtl_opacity;
  678. diffuse_array[vidx]=DX8Wrapper::Convert_Color(diffuse);
  679. mtl->Set_Ambient_Color_Source(VertexMaterialClass::COLOR1);
  680. mtl->Set_Diffuse_Color_Source(VertexMaterialClass::COLOR1);
  681. mtl->Set_Emissive_Color_Source(VertexMaterialClass::MATERIAL);
  682. }
  683. // If only ambient is used apply ambient to color channel and set ambient source to color 1
  684. if (!diffuse_used && ambient_used && !emissive_used) {
  685. Vector4 diffuse=DX8Wrapper::Convert_Color(diffuse_array[vidx]);
  686. diffuse.X *= mtl_ambient.X;
  687. diffuse.Y *= mtl_ambient.Y;
  688. diffuse.Z *= mtl_ambient.Z;
  689. diffuse.W *= mtl_opacity;
  690. diffuse_array[vidx]=DX8Wrapper::Convert_Color(diffuse);
  691. mtl->Set_Ambient_Color_Source(VertexMaterialClass::COLOR1);
  692. mtl->Set_Diffuse_Color_Source(VertexMaterialClass::MATERIAL);
  693. mtl->Set_Emissive_Color_Source(VertexMaterialClass::MATERIAL);
  694. }
  695. // If only emissive is used apply emissive to color channel, set diffuse source to color 1, and turn off lighting
  696. if (!diffuse_used && !ambient_used && emissive_used) {
  697. Vector4 diffuse=DX8Wrapper::Convert_Color(diffuse_array[vidx]);
  698. diffuse.X *= mtl_emissive.X;
  699. diffuse.Y *= mtl_emissive.Y;
  700. diffuse.Z *= mtl_emissive.Z;
  701. diffuse.W *= mtl_opacity;
  702. diffuse_array[vidx]=DX8Wrapper::Convert_Color(diffuse);
  703. mtl->Set_Ambient_Color_Source(VertexMaterialClass::MATERIAL);
  704. mtl->Set_Diffuse_Color_Source(VertexMaterialClass::COLOR1);
  705. mtl->Set_Emissive_Color_Source(VertexMaterialClass::MATERIAL);
  706. // mtl->Set_Lighting(false);
  707. }
  708. else {
  709. if (PassCount!=1) {
  710. set_lighting_to_false=false; // Lighting can only be set to false if ALL passes and ALL materials are requesting it
  711. }
  712. }
  713. }
  714. }
  715. }
  716. /*
  717. ** HACK: Kill BUMPENV passes on hardware that doesn't support BUMPENV
  718. ** HACK: Set lighting to false on all passes if all passes are of type NO DIFFUSE, NO AMBIENT, YES EMISSIVE
  719. */
  720. for (pass=0; pass<PassCount; pass++) {
  721. bool kill_pass = false;
  722. /*
  723. // HY: Earth and beyond uses a different fallback from Renegade with regards to bump environment maps
  724. // we keep the pass but change it to an unbumped environment
  725. if ( (Shader[pass].Get_Primary_Gradient() == ShaderClass::GRADIENT_BUMPENVMAP) &&
  726. (!DX8Wrapper::Is_Initted() || DX8Wrapper::Get_Current_Caps()->Support_Bump_Envmap() == false) )
  727. {
  728. kill_pass = true;
  729. }
  730. if ( (Shader[pass].Get_Primary_Gradient() == ShaderClass::GRADIENT_BUMPENVMAPLUMINANCE) &&
  731. (!DX8Wrapper::Is_Initted() || DX8Wrapper::Get_Current_Caps()->Support_Bump_Envmap_Luminance() == false) )
  732. {
  733. kill_pass = true;
  734. }
  735. */
  736. if (kill_pass) {
  737. if (Material[pass] != NULL) {
  738. Material[pass]->Set_Ambient(0,0,0);
  739. Material[pass]->Set_Diffuse(0,0,0);
  740. Material[pass]->Set_Emissive(0,0,0);
  741. Material[pass]->Set_Specular(0,0,0);
  742. }
  743. Shader[pass].Set_Texturing(ShaderClass::TEXTURING_DISABLE);
  744. Shader[pass].Set_Post_Detail_Color_Func(ShaderClass::DETAILCOLOR_DISABLE);
  745. Shader[pass].Set_Post_Detail_Alpha_Func(ShaderClass::DETAILALPHA_DISABLE);
  746. }
  747. // Set lighting to false if requested in all passes...
  748. else if (set_lighting_to_false) {
  749. Vector3 single_diffuse(0.0f,0.0f,0.0f);
  750. Vector3 single_ambient(0.0f,0.0f,0.0f);
  751. Vector3 single_emissive(0.0f,0.0f,0.0f);
  752. bool diffuse_used=false;
  753. bool ambient_used=false;
  754. bool emissive_used=false;
  755. Vector3 mtl_diffuse;
  756. Vector3 mtl_ambient;
  757. Vector3 mtl_emissive;
  758. VertexMaterialClass * prev_mtl = NULL;
  759. VertexMaterialClass * mtl = Peek_Material(0, pass);
  760. if (mtl) {
  761. mtl->Get_Diffuse(&single_diffuse);
  762. mtl->Get_Ambient(&single_ambient);
  763. mtl->Get_Emissive(&single_emissive);
  764. if (single_diffuse.X || single_diffuse.Y || single_diffuse.Z) diffuse_used=true;
  765. if (single_ambient.X || single_ambient.Y || single_ambient.Z) ambient_used=true;
  766. if (single_emissive.X || single_emissive.Y || single_emissive.Z) emissive_used=true;
  767. }
  768. for (int vidx=0; vidx<VertexCount; vidx++) {
  769. mtl = Peek_Material(vidx,pass);
  770. if (mtl != prev_mtl) {
  771. prev_mtl = mtl;
  772. mtl->Get_Diffuse(&mtl_diffuse);
  773. mtl->Get_Ambient(&mtl_ambient);
  774. mtl->Get_Emissive(&mtl_emissive);
  775. }
  776. if (mtl_diffuse.X || mtl_diffuse.Y || mtl_diffuse.Z) diffuse_used=true;
  777. if (mtl_ambient.X || mtl_ambient.Y || mtl_ambient.Z) ambient_used=true;
  778. if (mtl_emissive.X || mtl_emissive.Y || mtl_emissive.Z) emissive_used=true;
  779. }
  780. if ((DCGSource[pass] != VertexMaterialClass::MATERIAL) && (ColorArray[0] != NULL)) {
  781. VertexMaterialClass * prev_mtl = NULL;
  782. VertexMaterialClass * mtl = Peek_Material(0,pass);
  783. for (int vidx=0; vidx<VertexCount; vidx++) {
  784. mtl = Peek_Material(vidx,pass);
  785. if (mtl != prev_mtl) {
  786. prev_mtl = mtl;
  787. // If only emissive is used apply emissive to color channel, set diffuse source to color 1, and turn off lighting
  788. if (!diffuse_used && !ambient_used && emissive_used) {
  789. mtl->Set_Lighting(false);
  790. }
  791. }
  792. }
  793. }
  794. }
  795. }
  796. }
  797. void MeshMatDescClass::Configure_Material(VertexMaterialClass * mtl,int pass,bool lighting_enabled)
  798. {
  799. mtl->Set_Diffuse_Color_Source(DCGSource[pass]);
  800. mtl->Set_Emissive_Color_Source(DIGSource[pass]);
  801. mtl->Set_Lighting(lighting_enabled);
  802. for (int stage=0; stage<MAX_TEX_STAGES; stage++) {
  803. int src = UVSource[pass][stage];
  804. if (src == -1) {
  805. src = 0;
  806. }
  807. mtl->Set_UV_Source(stage,src);
  808. }
  809. }
  810. bool MeshMatDescClass::Do_Mappers_Need_Normals(void)
  811. {
  812. if (DX8Wrapper::Is_Initted() && DX8Wrapper::Get_Current_Caps()->Support_NPatches() && WW3D::Get_NPatches_Level()>1) return true;
  813. for (int pass=0; pass<PassCount; pass++) {
  814. /*
  815. ** Check the materials on this pass to see if any have mappers which require normals
  816. */
  817. if (Material[pass] != NULL) {
  818. if (Material[pass]->Do_Mappers_Need_Normals()) return true;
  819. } else {
  820. VertexMaterialClass * prev_mtl = NULL;
  821. VertexMaterialClass * mtl = Peek_Material(pass,0);
  822. for (int vidx=0; vidx<VertexCount; vidx++) {
  823. mtl = Peek_Material(vidx,pass);
  824. if ((mtl != prev_mtl) && (mtl != NULL)) {
  825. if (mtl->Do_Mappers_Need_Normals()) return true;
  826. prev_mtl = mtl;
  827. }
  828. }
  829. }
  830. }
  831. return false;
  832. }