meshmatdesc.cpp 30 KB

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