meshmatdesc.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 : 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));
  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));
  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));
  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));
  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. if ( (Shader[pass].Get_Primary_Gradient() == ShaderClass::GRADIENT_BUMPENVMAP) &&
  723. (!DX8Wrapper::Is_Initted() || DX8Wrapper::Get_Current_Caps()->Support_Bump_Envmap() == false) )
  724. {
  725. kill_pass = true;
  726. }
  727. if ( (Shader[pass].Get_Primary_Gradient() == ShaderClass::GRADIENT_BUMPENVMAPLUMINANCE) &&
  728. (!DX8Wrapper::Is_Initted() || DX8Wrapper::Get_Current_Caps()->Support_Bump_Envmap_Luminance() == false) )
  729. {
  730. kill_pass = true;
  731. }
  732. if (kill_pass) {
  733. if (Material[pass] != NULL) {
  734. Material[pass]->Set_Ambient(0,0,0);
  735. Material[pass]->Set_Diffuse(0,0,0);
  736. Material[pass]->Set_Emissive(0,0,0);
  737. Material[pass]->Set_Specular(0,0,0);
  738. }
  739. Shader[pass].Set_Texturing(ShaderClass::TEXTURING_DISABLE);
  740. Shader[pass].Set_Post_Detail_Color_Func(ShaderClass::DETAILCOLOR_DISABLE);
  741. Shader[pass].Set_Post_Detail_Alpha_Func(ShaderClass::DETAILALPHA_DISABLE);
  742. }
  743. // Set lighting to false if requested in all passes...
  744. else if (set_lighting_to_false) {
  745. Vector3 single_diffuse(0.0f,0.0f,0.0f);
  746. Vector3 single_ambient(0.0f,0.0f,0.0f);
  747. Vector3 single_emissive(0.0f,0.0f,0.0f);
  748. bool diffuse_used=false;
  749. bool ambient_used=false;
  750. bool emissive_used=false;
  751. Vector3 mtl_diffuse;
  752. Vector3 mtl_ambient;
  753. Vector3 mtl_emissive;
  754. VertexMaterialClass * prev_mtl = NULL;
  755. VertexMaterialClass * mtl = Peek_Material(0, pass);
  756. if (mtl) {
  757. mtl->Get_Diffuse(&single_diffuse);
  758. mtl->Get_Ambient(&single_ambient);
  759. mtl->Get_Emissive(&single_emissive);
  760. if (single_diffuse.X || single_diffuse.Y || single_diffuse.Z) diffuse_used=true;
  761. if (single_ambient.X || single_ambient.Y || single_ambient.Z) ambient_used=true;
  762. if (single_emissive.X || single_emissive.Y || single_emissive.Z) emissive_used=true;
  763. }
  764. for (int vidx=0; vidx<VertexCount; vidx++) {
  765. mtl = Peek_Material(vidx,pass);
  766. if (mtl != prev_mtl) {
  767. prev_mtl = mtl;
  768. mtl->Get_Diffuse(&mtl_diffuse);
  769. mtl->Get_Ambient(&mtl_ambient);
  770. mtl->Get_Emissive(&mtl_emissive);
  771. }
  772. if (mtl_diffuse.X || mtl_diffuse.Y || mtl_diffuse.Z) diffuse_used=true;
  773. if (mtl_ambient.X || mtl_ambient.Y || mtl_ambient.Z) ambient_used=true;
  774. if (mtl_emissive.X || mtl_emissive.Y || mtl_emissive.Z) emissive_used=true;
  775. }
  776. if ((DCGSource[pass] != VertexMaterialClass::MATERIAL) && (ColorArray[0] != NULL)) {
  777. VertexMaterialClass * prev_mtl = NULL;
  778. VertexMaterialClass * mtl = Peek_Material(0,pass);
  779. for (int vidx=0; vidx<VertexCount; vidx++) {
  780. mtl = Peek_Material(vidx,pass);
  781. if (mtl != prev_mtl) {
  782. prev_mtl = mtl;
  783. // If only emissive is used apply emissive to color channel, set diffuse source to color 1, and turn off lighting
  784. if (!diffuse_used && !ambient_used && emissive_used) {
  785. mtl->Set_Lighting(false);
  786. }
  787. }
  788. }
  789. }
  790. }
  791. }
  792. // HACK: force meshes which are named b_wire and using texture razorw.tga to use alpha-test
  793. // These meshes will follow the given pattern:
  794. // - there will be a single vertex material, shader, and texture for the first pass
  795. // - the texture will be named razorw.tga
  796. // - the mesh name will contain b_wire
  797. #pragma message("(gth) Renegade-specific hack, forcing b_wire mesh to use alpha-test...")
  798. if ( (parent != NULL) &&
  799. (PassCount == 1) &&
  800. (Has_Shader_Array(0) == false) &&
  801. (Has_Texture_Array(0,0) == false) &&
  802. (Has_Material_Array(0) == false))
  803. {
  804. if (strstr(parent->Get_Name(),"B_WIRE")) {
  805. TextureClass * tex = Peek_Single_Texture(0,0);
  806. if (tex && stricmp(tex->Get_Texture_Name(),"razorw.tga") == 0) {
  807. ShaderClass shader = Get_Single_Shader(0);
  808. shader.Set_Alpha_Test(ShaderClass::ALPHATEST_ENABLE);
  809. shader.Set_Dst_Blend_Func(ShaderClass::DSTBLEND_ZERO);
  810. shader.Set_Src_Blend_Func(ShaderClass::SRCBLEND_ONE);
  811. shader.Set_Depth_Mask(ShaderClass::DEPTH_WRITE_ENABLE);
  812. Set_Single_Shader(shader,0);
  813. parent->Set_Flag(MeshGeometryClass::SORT,false);
  814. }
  815. }
  816. }
  817. }
  818. void MeshMatDescClass::Configure_Material(VertexMaterialClass * mtl,int pass,bool lighting_enabled)
  819. {
  820. mtl->Set_Diffuse_Color_Source(DCGSource[pass]);
  821. mtl->Set_Emissive_Color_Source(DIGSource[pass]);
  822. mtl->Set_Lighting(lighting_enabled);
  823. for (int stage=0; stage<MAX_TEX_STAGES; stage++) {
  824. int src = UVSource[pass][stage];
  825. if (src == -1) {
  826. src = 0;
  827. }
  828. mtl->Set_UV_Source(stage,src);
  829. }
  830. }
  831. bool MeshMatDescClass::Do_Mappers_Need_Normals(void)
  832. {
  833. if (DX8Wrapper::Is_Initted() && DX8Wrapper::Get_Current_Caps()->Support_NPatches() && WW3D::Get_NPatches_Level()>1) return true;
  834. for (int pass=0; pass<PassCount; pass++) {
  835. /*
  836. ** Check the materials on this pass to see if any have mappers which require normals
  837. */
  838. if (Material[pass] != NULL) {
  839. if (Material[pass]->Do_Mappers_Need_Normals()) return true;
  840. } else {
  841. VertexMaterialClass * prev_mtl = NULL;
  842. VertexMaterialClass * mtl = Peek_Material(pass,0);
  843. for (int vidx=0; vidx<VertexCount; vidx++) {
  844. mtl = Peek_Material(vidx,pass);
  845. if ((mtl != prev_mtl) && (mtl != NULL)) {
  846. if (mtl->Do_Mappers_Need_Normals()) return true;
  847. prev_mtl = mtl;
  848. }
  849. }
  850. }
  851. }
  852. return false;
  853. }