w3dmtl.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 : Max2W3d *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/max2w3d/w3dmtl.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 8/22/01 7:47a $*
  29. * *
  30. * $Revision:: 32 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "w3dmtl.h"
  36. #include <Max.h>
  37. #include <StdMat.h>
  38. #include "gamemtl.h"
  39. #include "realcrc.h"
  40. static W3dRGBStruct Color_To_W3d(Color & c)
  41. {
  42. W3dRGBStruct wc;
  43. wc.R = (c.r * 255.0f);
  44. wc.G = (c.g * 255.0f);
  45. wc.B = (c.b * 255.0f);
  46. wc.pad = 0;
  47. return wc;
  48. }
  49. /*
  50. Implementation of W3dMapClass
  51. */
  52. W3dMapClass::W3dMapClass(const W3dMapClass & that)
  53. {
  54. Set_Filename(that.Filename);
  55. if (that.AnimInfo) {
  56. Set_Anim_Info(that.AnimInfo);
  57. }
  58. }
  59. W3dMapClass & W3dMapClass::operator = (const W3dMapClass & that)
  60. {
  61. if (this != &that) {
  62. Set_Filename(that.Filename);
  63. Set_Anim_Info(that.AnimInfo);
  64. }
  65. return *this;
  66. }
  67. W3dMapClass::~W3dMapClass(void)
  68. {
  69. if (Filename) free(Filename);
  70. if (AnimInfo) delete AnimInfo;
  71. }
  72. void W3dMapClass::Reset(void)
  73. {
  74. if (Filename) free(Filename);
  75. if (AnimInfo) delete AnimInfo;
  76. Filename = NULL;
  77. AnimInfo = NULL;
  78. }
  79. void W3dMapClass::Set_Filename(const char * fullpath)
  80. {
  81. if (Filename) {
  82. free(Filename);
  83. }
  84. if (fullpath) {
  85. char name[_MAX_FNAME];
  86. char exten[_MAX_EXT];
  87. char fname[_MAX_FNAME+_MAX_EXT+2];
  88. _splitpath(fullpath,NULL,NULL,name,exten);
  89. _makepath(fname,NULL,NULL,name,exten);
  90. //strupr(fname); (gth) need to preserve case since unix/PS2 is case sensitive...
  91. Filename = strdup(fname);
  92. } else {
  93. Filename = NULL;
  94. }
  95. }
  96. void W3dMapClass::Set_Anim_Info(const W3dTextureInfoStruct * info)
  97. {
  98. if (info == NULL) {
  99. if (AnimInfo) {
  100. delete AnimInfo;
  101. AnimInfo = NULL;
  102. return;
  103. }
  104. } else {
  105. if (AnimInfo == NULL) {
  106. AnimInfo = new W3dTextureInfoStruct;
  107. }
  108. *AnimInfo = *info;
  109. }
  110. }
  111. void W3dMapClass::Set_Anim_Info(int framecount,float framerate)
  112. {
  113. if (AnimInfo == NULL) {
  114. AnimInfo = new W3dTextureInfoStruct;
  115. }
  116. AnimInfo->FrameCount = framecount;
  117. AnimInfo->FrameRate = framerate;
  118. }
  119. /*
  120. Implementation of W3dMaterialClass
  121. */
  122. W3dMaterialClass::W3dMaterialClass(void)
  123. {
  124. PassCount = 0;
  125. SortLevel = SORT_LEVEL_NONE;
  126. for (int pass = 0; pass < MAX_PASSES; pass++) {
  127. Materials[pass] = NULL;
  128. W3d_Shader_Reset(&(Shaders[pass]));
  129. for (int stage = 0; stage < MAX_STAGES; stage++) {
  130. Textures[pass][stage] = NULL;
  131. MapChannel[pass][stage] = 1;
  132. MapperArgs[pass][stage] = NULL;
  133. }
  134. }
  135. }
  136. W3dMaterialClass::~W3dMaterialClass(void)
  137. {
  138. Free();
  139. }
  140. void W3dMaterialClass::Free(void)
  141. {
  142. for (int pass = 0; pass < MAX_PASSES; pass++) {
  143. if (Materials[pass]) {
  144. delete Materials[pass];
  145. Materials[pass] = NULL;
  146. }
  147. for (int stage = 0; stage < MAX_STAGES; stage++) {
  148. if (Textures[pass][stage]) {
  149. delete Textures[pass][stage];
  150. Textures[pass][stage] = NULL;
  151. }
  152. if (MapperArgs[pass][stage]) {
  153. delete [] MapperArgs[pass][stage];
  154. MapperArgs[pass][stage] = NULL;
  155. }
  156. }
  157. }
  158. }
  159. void W3dMaterialClass::Reset(void)
  160. {
  161. Free();
  162. SortLevel = SORT_LEVEL_NONE;
  163. for (int pass=0; pass < MAX_PASSES; pass++) {
  164. W3d_Shader_Reset(&(Shaders[pass]));
  165. for (int stage=0; stage < MAX_STAGES; stage++) {
  166. MapChannel[pass][stage] = 1;
  167. }
  168. }
  169. }
  170. void W3dMaterialClass::Set_Surface_Type(unsigned int type)
  171. {
  172. SurfaceType = type;
  173. }
  174. void W3dMaterialClass::Set_Sort_Level(int level)
  175. {
  176. assert(level <= MAX_SORT_LEVEL);
  177. SortLevel = level;
  178. }
  179. void W3dMaterialClass::Set_Pass_Count(int count)
  180. {
  181. assert(count >= 0);
  182. assert(count < MAX_PASSES);
  183. PassCount = count;
  184. }
  185. void W3dMaterialClass::Set_Vertex_Material(const W3dVertexMaterialStruct & vmat,int pass)
  186. {
  187. assert(pass >= 0);
  188. assert(pass < PassCount);
  189. if (Materials[pass] == NULL) {
  190. Materials[pass] = new W3dVertexMaterialStruct;
  191. }
  192. *(Materials[pass]) = vmat;
  193. }
  194. void W3dMaterialClass::Set_Mapper_Args(const char *args_buffer, int pass, int stage)
  195. {
  196. assert(pass >= 0);
  197. assert(pass < PassCount);
  198. assert(stage >= 0);
  199. assert(stage < MAX_STAGES);
  200. if (MapperArgs[pass][stage] != NULL) {
  201. delete [] MapperArgs[pass][stage];
  202. MapperArgs[pass][stage] = NULL;
  203. }
  204. if (args_buffer) {
  205. int len = strlen(args_buffer);
  206. MapperArgs[pass][stage] = new char [len + 1];
  207. strcpy(MapperArgs[pass][stage], args_buffer);
  208. }
  209. }
  210. void W3dMaterialClass::Set_Shader(const W3dShaderStruct & shader,int pass)
  211. {
  212. assert(pass >= 0);
  213. assert(pass < PassCount);
  214. Shaders[pass] = shader;
  215. }
  216. void W3dMaterialClass::Set_Texture(const W3dMapClass & map,int pass,int stage)
  217. {
  218. assert(pass >= 0);
  219. assert(pass < PassCount);
  220. if (Textures[pass][stage] == NULL) {
  221. Textures[pass][stage] = new W3dMapClass;
  222. }
  223. *(Textures[pass][stage]) = map;
  224. }
  225. void W3dMaterialClass::Set_Map_Channel(int pass,int stage,int channel)
  226. {
  227. assert(pass >= 0);
  228. assert(pass < PassCount);
  229. MapChannel[pass][stage] = channel;
  230. }
  231. unsigned int W3dMaterialClass::Get_Surface_Type(void) const
  232. {
  233. return SurfaceType;
  234. }
  235. int W3dMaterialClass::Get_Sort_Level(void) const
  236. {
  237. return SortLevel;
  238. }
  239. int W3dMaterialClass::Get_Pass_Count(void) const
  240. {
  241. return PassCount;
  242. }
  243. W3dVertexMaterialStruct * W3dMaterialClass::Get_Vertex_Material(int pass ) const
  244. {
  245. assert(pass >= 0);
  246. assert(pass < PassCount);
  247. return Materials[pass];
  248. }
  249. const char * W3dMaterialClass::Get_Mapper_Args(int pass, int stage) const
  250. {
  251. assert(pass >= 0);
  252. assert(pass < PassCount);
  253. assert(stage >= 0);
  254. assert(stage < MAX_STAGES);
  255. return MapperArgs[pass][stage];
  256. }
  257. W3dShaderStruct W3dMaterialClass::Get_Shader(int pass) const
  258. {
  259. assert(pass >= 0);
  260. assert(pass < PassCount);
  261. return Shaders[pass];
  262. }
  263. W3dMapClass * W3dMaterialClass::Get_Texture(int pass,int stage) const
  264. {
  265. assert(pass >= 0);
  266. assert(pass < PassCount);
  267. assert(stage >= 0);
  268. assert(stage < MAX_STAGES);
  269. return Textures[pass][stage];
  270. }
  271. int W3dMaterialClass::Get_Map_Channel(int pass,int stage) const
  272. {
  273. assert(pass >= 0);
  274. assert(pass < PassCount);
  275. assert(stage >= 0);
  276. assert(stage < MAX_STAGES);
  277. return MapChannel[pass][stage];
  278. }
  279. void W3dMaterialClass::Init(Mtl * mtl)
  280. {
  281. bool ps2_mat = false;
  282. Reset();
  283. if (mtl->ClassID() == PS2GameMaterialClassID)
  284. {
  285. ps2_mat = true;
  286. }
  287. if ((mtl->ClassID() == GameMaterialClassID) || ps2_mat) {
  288. Init((GameMtl*)mtl);
  289. return;
  290. }
  291. Texmap * tmap;
  292. PassCount = 1;
  293. tmap = mtl->GetSubTexmap(ID_RL);
  294. if (tmap && tmap->ClassID() == Class_ID(BMTEX_CLASS_ID,0)) {
  295. PassCount++;
  296. }
  297. W3dVertexMaterialStruct mat;
  298. W3dShaderStruct shader;
  299. W3dMapClass tex;
  300. /*
  301. ** Setting up the diffuse color pass
  302. */
  303. W3d_Shader_Reset(&shader);
  304. mat.Attributes = 0;
  305. mat.Emissive.R = mat.Emissive.G = mat.Emissive.B = 0; //(uint8)(255 .0f * mtl->GetSelfIllum());
  306. Color diffuse = mtl->GetDiffuse();
  307. mat.Diffuse.R = (uint8)(diffuse.r * 255.0f);
  308. mat.Diffuse.G = (uint8)(diffuse.g * 255.0f);
  309. mat.Diffuse.B = (uint8)(diffuse.b * 255.0f);
  310. mat.Ambient = mat.Diffuse;
  311. Color specular = mtl->GetSpecular();
  312. mat.Specular.R = (uint8)(specular.r * 255.0f);
  313. mat.Specular.G = (uint8)(specular.g * 255.0f);
  314. mat.Specular.B = (uint8)(specular.b * 255.0f);
  315. mat.Shininess = mtl->GetShininess();
  316. mat.Opacity = 1.0f - mtl->GetXParency();
  317. mat.Translucency = 0.0f;
  318. tmap = mtl->GetSubTexmap(ID_DI);
  319. if (tmap && tmap->ClassID() == Class_ID(BMTEX_CLASS_ID,0)) {
  320. char * tname = ((BitmapTex *)tmap)->GetMapName();
  321. if (tname) {
  322. tex.Set_Filename(tname);
  323. mat.Diffuse.R = mat.Diffuse.G = mat.Diffuse.B = 255;
  324. W3d_Shader_Set_Texturing(&shader,W3DSHADER_TEXTURING_ENABLE);
  325. Set_Texture(tex,0,0);
  326. }
  327. }
  328. if (mat.Opacity != 1.0f) {
  329. W3d_Shader_Set_Dest_Blend_Func(&shader,W3DSHADER_DESTBLENDFUNC_ONE_MINUS_SRC_ALPHA);
  330. W3d_Shader_Set_Src_Blend_Func(&shader,W3DSHADER_SRCBLENDFUNC_SRC_ALPHA);
  331. }
  332. Set_Vertex_Material(mat,0);
  333. Set_Shader(shader,0);
  334. /*
  335. ** Setting up the reflection pass (envmap)
  336. */
  337. if (PassCount == 2) {
  338. W3d_Shader_Reset(&shader);
  339. if (ps2_mat)
  340. {
  341. W3d_Shader_Set_Pri_Gradient(&shader,PSS_PRIGRADIENT_MODULATE);
  342. }
  343. else
  344. {
  345. W3d_Shader_Set_Pri_Gradient(&shader,W3DSHADER_PRIGRADIENT_MODULATE);
  346. }
  347. W3d_Shader_Set_Sec_Gradient(&shader,W3DSHADER_SECGRADIENT_DISABLE);
  348. W3d_Shader_Set_Depth_Mask(&shader,W3DSHADER_DEPTHMASK_WRITE_DISABLE);
  349. W3d_Shader_Set_Dest_Blend_Func(&shader,W3DSHADER_DESTBLENDFUNC_ONE);
  350. W3d_Shader_Set_Src_Blend_Func(&shader,W3DSHADER_SRCBLENDFUNC_ONE);
  351. W3d_Shader_Set_Texturing(&shader,W3DSHADER_TEXTURING_ENABLE);
  352. // PS2 specific paramaters.
  353. W3d_Shader_Set_PS2_Param_A(&shader, PSS_SRC);
  354. W3d_Shader_Set_PS2_Param_B(&shader, PSS_ZERO);
  355. W3d_Shader_Set_PS2_Param_B(&shader, PSS_ONE);
  356. W3d_Shader_Set_PS2_Param_B(&shader, PSS_DEST);
  357. W3d_Vertex_Material_Reset(&mat);
  358. mat.Diffuse.R = mat.Diffuse.G = mat.Diffuse.B = 128;
  359. mat.Attributes &= W3DVERTMAT_STAGE0_MAPPING_MASK;
  360. mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_ENVIRONMENT;
  361. tmap = mtl->GetSubTexmap(ID_RL);
  362. if (tmap && tmap->ClassID() == Class_ID(BMTEX_CLASS_ID,0)) {
  363. char * tname = ((BitmapTex *)tmap)->GetMapName();
  364. if (tname) {
  365. tex.Set_Filename(tname);
  366. Set_Texture(tex,1);
  367. }
  368. }
  369. Set_Vertex_Material(mat,1);
  370. Set_Shader(shader,1);
  371. }
  372. }
  373. void W3dMaterialClass::Init(GameMtl * gamemtl)
  374. {
  375. Reset();
  376. PassCount = gamemtl->Get_Pass_Count();
  377. Set_Surface_Type (gamemtl->Get_Surface_Type ());
  378. Set_Sort_Level(gamemtl->Get_Sort_Level());
  379. for (int pass=0;pass<gamemtl->Get_Pass_Count(); pass++) {
  380. /*
  381. ** set up the shader
  382. */
  383. W3dShaderStruct shader;
  384. W3d_Shader_Reset(&shader);
  385. shader.DepthCompare = gamemtl->Get_Depth_Compare(pass);
  386. shader.DepthMask = gamemtl->Get_Depth_Mask(pass);
  387. shader.AlphaTest = gamemtl->Get_Alpha_Test(pass);
  388. shader.DestBlend = gamemtl->Get_Dest_Blend(pass);
  389. shader.PriGradient = gamemtl->Get_Pri_Gradient(pass);
  390. shader.SecGradient = gamemtl->Get_Sec_Gradient(pass);
  391. shader.SrcBlend = gamemtl->Get_Src_Blend(pass);
  392. shader.DetailColorFunc = gamemtl->Get_Detail_Color_Func(pass);
  393. shader.DetailAlphaFunc = gamemtl->Get_Detail_Alpha_Func(pass);
  394. shader.Texturing = W3DSHADER_TEXTURING_DISABLE;
  395. shader.PostDetailColorFunc = gamemtl->Get_Detail_Color_Func(pass); // (gth) set up the post-detail options
  396. shader.PostDetailAlphaFunc = gamemtl->Get_Detail_Alpha_Func(pass);
  397. // PS2 specific paramaters.
  398. W3d_Shader_Set_PS2_Param_A(&shader, gamemtl->Get_PS2_Shader_Param_A(pass));
  399. W3d_Shader_Set_PS2_Param_B(&shader, gamemtl->Get_PS2_Shader_Param_B(pass));
  400. W3d_Shader_Set_PS2_Param_C(&shader, gamemtl->Get_PS2_Shader_Param_C(pass));
  401. W3d_Shader_Set_PS2_Param_D(&shader, gamemtl->Get_PS2_Shader_Param_D(pass));
  402. /*
  403. ** set up the vertex material
  404. */
  405. W3dVertexMaterialStruct mat;
  406. mat.Attributes = 0;
  407. if (gamemtl->Get_Copy_Specular_To_Diffuse(pass)) {
  408. mat.Attributes |= W3DVERTMAT_COPY_SPECULAR_TO_DIFFUSE;
  409. }
  410. // mapping type for stage 0
  411. switch(gamemtl->Get_Mapping_Type(pass, 0))
  412. {
  413. case GAMEMTL_MAPPING_UV: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_UV; break;
  414. case GAMEMTL_MAPPING_ENV: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_ENVIRONMENT; break;
  415. case GAMEMTL_MAPPING_CHEAP_ENV: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_CHEAP_ENVIRONMENT; break;
  416. case GAMEMTL_MAPPING_SCREEN: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_SCREEN; break;
  417. case GAMEMTL_MAPPING_LINEAR_OFFSET: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_LINEAR_OFFSET; break;
  418. case GAMEMTL_MAPPING_SILHOUETTE: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_SILHOUETTE; break;
  419. case GAMEMTL_MAPPING_SCALE: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_SCALE; break;
  420. case GAMEMTL_MAPPING_GRID: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_GRID; break;
  421. case GAMEMTL_MAPPING_ROTATE: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_ROTATE; break;
  422. case GAMEMTL_MAPPING_SINE_LINEAR_OFFSET: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_SINE_LINEAR_OFFSET; break;
  423. case GAMEMTL_MAPPING_STEP_LINEAR_OFFSET: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_STEP_LINEAR_OFFSET; break;
  424. case GAMEMTL_MAPPING_ZIGZAG_LINEAR_OFFSET: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_ZIGZAG_LINEAR_OFFSET; break;
  425. case GAMEMTL_MAPPING_WS_CLASSIC_ENV: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_WS_CLASSIC_ENV; break;
  426. case GAMEMTL_MAPPING_WS_ENVIRONMENT: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_WS_ENVIRONMENT; break;
  427. case GAMEMTL_MAPPING_GRID_CLASSIC_ENV: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_GRID_CLASSIC_ENV; break;
  428. case GAMEMTL_MAPPING_GRID_ENVIRONMENT: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_GRID_ENVIRONMENT; break;
  429. case GAMEMTL_MAPPING_RANDOM: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_RANDOM; break;
  430. case GAMEMTL_MAPPING_EDGE: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_EDGE; break;
  431. case GAMEMTL_MAPPING_BUMPENV: mat.Attributes |= W3DVERTMAT_STAGE0_MAPPING_BUMPENV; break;
  432. };
  433. // mapping type for stage 1
  434. switch(gamemtl->Get_Mapping_Type(pass, 1))
  435. {
  436. case GAMEMTL_MAPPING_UV: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_UV; break;
  437. case GAMEMTL_MAPPING_ENV: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_ENVIRONMENT; break;
  438. case GAMEMTL_MAPPING_CHEAP_ENV: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_CHEAP_ENVIRONMENT; break;
  439. case GAMEMTL_MAPPING_SCREEN: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_SCREEN; break;
  440. case GAMEMTL_MAPPING_LINEAR_OFFSET: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_LINEAR_OFFSET; break;
  441. case GAMEMTL_MAPPING_SILHOUETTE: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_SILHOUETTE; break;
  442. case GAMEMTL_MAPPING_SCALE: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_SCALE; break;
  443. case GAMEMTL_MAPPING_GRID: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_GRID; break;
  444. case GAMEMTL_MAPPING_ROTATE: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_ROTATE; break;
  445. case GAMEMTL_MAPPING_SINE_LINEAR_OFFSET: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_SINE_LINEAR_OFFSET; break;
  446. case GAMEMTL_MAPPING_STEP_LINEAR_OFFSET: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_STEP_LINEAR_OFFSET; break;
  447. case GAMEMTL_MAPPING_ZIGZAG_LINEAR_OFFSET: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_ZIGZAG_LINEAR_OFFSET; break;
  448. case GAMEMTL_MAPPING_WS_CLASSIC_ENV: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_WS_CLASSIC_ENV; break;
  449. case GAMEMTL_MAPPING_WS_ENVIRONMENT: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_WS_ENVIRONMENT; break;
  450. case GAMEMTL_MAPPING_GRID_CLASSIC_ENV: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_GRID_CLASSIC_ENV; break;
  451. case GAMEMTL_MAPPING_GRID_ENVIRONMENT: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_GRID_ENVIRONMENT; break;
  452. case GAMEMTL_MAPPING_RANDOM: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_RANDOM; break;
  453. case GAMEMTL_MAPPING_EDGE: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_EDGE; break;
  454. case GAMEMTL_MAPPING_BUMPENV: mat.Attributes |= W3DVERTMAT_STAGE1_MAPPING_BUMPENV; break;
  455. };
  456. switch(gamemtl->Get_PSX_Translucency(pass))
  457. {
  458. case GAMEMTL_PSX_TRANS_NONE: mat.Attributes |= W3DVERTMAT_PSX_TRANS_NONE; break;
  459. case GAMEMTL_PSX_TRANS_100: mat.Attributes |= W3DVERTMAT_PSX_TRANS_100; break;
  460. case GAMEMTL_PSX_TRANS_50: mat.Attributes |= W3DVERTMAT_PSX_TRANS_50; break;
  461. case GAMEMTL_PSX_TRANS_25: mat.Attributes |= W3DVERTMAT_PSX_TRANS_25; break;
  462. case GAMEMTL_PSX_TRANS_MINUS_100:mat.Attributes |= W3DVERTMAT_PSX_TRANS_MINUS_100; break;
  463. };
  464. if (!gamemtl->Get_PSX_Lighting(pass)) {
  465. mat.Attributes |= W3DVERTMAT_PSX_NO_RT_LIGHTING;
  466. }
  467. mat.Ambient = Color_To_W3d(gamemtl->Get_Ambient(pass,0));
  468. mat.Diffuse = Color_To_W3d(gamemtl->Get_Diffuse(pass,0));
  469. mat.Specular = Color_To_W3d(gamemtl->Get_Specular(pass,0));
  470. mat.Emissive = Color_To_W3d(gamemtl->Get_Emissive(pass,0));
  471. mat.Shininess = gamemtl->Get_Shininess(pass,0);
  472. mat.Opacity = gamemtl->Get_Opacity(pass,0);
  473. mat.Translucency = gamemtl->Get_Translucency(pass,0);
  474. /*
  475. ** install the two textures if present
  476. */
  477. W3dMapClass w3dmap;
  478. BitmapTex * tex = NULL;
  479. for (int stage=0; stage < MAX_STAGES; stage++) {
  480. if (gamemtl->Get_Texture_Enable(pass,stage) && gamemtl->Get_Texture(pass,stage)) {
  481. w3dmap.Reset();
  482. // get the filename for the w3dmap texture
  483. tex = (BitmapTex *)gamemtl->Get_Texture(pass,stage);
  484. assert(tex->GetMapName());
  485. w3dmap.Set_Filename(tex->GetMapName());
  486. // get the animation and flags for the w3dmap texture
  487. W3dTextureInfoStruct texinfo;
  488. memset(&texinfo,0,sizeof(texinfo));
  489. texinfo.AnimType = gamemtl->Get_Texture_Anim_Type(pass,stage);
  490. if (gamemtl->Get_Texture_Publish(pass,stage)) {
  491. texinfo.Attributes = texinfo.Attributes | W3DTEXTURE_PUBLISH;
  492. }
  493. if (gamemtl->Get_Texture_No_LOD(pass,stage)) {
  494. texinfo.Attributes = texinfo.Attributes | W3DTEXTURE_NO_LOD;
  495. }
  496. if (gamemtl->Get_Texture_Clamp_U(pass,stage)) {
  497. texinfo.Attributes = texinfo.Attributes | W3DTEXTURE_CLAMP_U;
  498. }
  499. if (gamemtl->Get_Texture_Clamp_V(pass,stage)) {
  500. texinfo.Attributes = texinfo.Attributes | W3DTEXTURE_CLAMP_V;
  501. }
  502. if (gamemtl->Get_Texture_Alpha_Bitmap(pass,stage)) {
  503. texinfo.Attributes = texinfo.Attributes | W3DTEXTURE_ALPHA_BITMAP;
  504. }
  505. texinfo.Attributes = texinfo.Attributes | (
  506. (gamemtl->Get_Texture_Hint(pass,stage) << W3DTEXTURE_HINT_SHIFT)
  507. & W3DTEXTURE_HINT_MASK);
  508. // If the shader indicates bump-environment mapping mark this texture as a bumpmap.
  509. if ((stage == 0) && (gamemtl->Get_Pri_Gradient (pass) == W3DSHADER_PRIGRADIENT_BUMPENVMAP)) {
  510. texinfo.Attributes |= W3DTEXTURE_TYPE_BUMPMAP;
  511. }
  512. texinfo.FrameCount = gamemtl->Get_Texture_Frame_Count(pass,stage);
  513. texinfo.FrameRate = gamemtl->Get_Texture_Frame_Rate(pass,stage);
  514. if ((texinfo.FrameCount > 1) || (texinfo.Attributes != 0)) {
  515. w3dmap.Set_Anim_Info(&texinfo);
  516. }
  517. // plug it in and turn on texturing in the shader
  518. Set_Texture(w3dmap,pass,stage);
  519. shader.Texturing = W3DSHADER_TEXTURING_ENABLE;
  520. // copy over the mapping channel
  521. Set_Map_Channel(pass,stage,gamemtl->Get_Map_Channel(pass,stage));
  522. // copy over the mapper args
  523. Set_Mapper_Args(gamemtl->Get_Mapping_Arg_Buffer(pass, stage), pass, stage);
  524. } else {
  525. break; // break out of the loop (and dont put in stage 1 if stage 0 is missing...)
  526. }
  527. }
  528. Set_Shader(shader,pass);
  529. Set_Vertex_Material(mat,pass);
  530. }
  531. }
  532. bool W3dMaterialClass::Is_Multi_Pass_Transparent(void) const
  533. {
  534. return ((PassCount >= 2) && (Get_Shader(0).DestBlend != W3DSHADER_DESTBLENDFUNC_ZERO));
  535. }
  536. /*
  537. Implementation of W3dMaterialDescClass::VertClass
  538. */
  539. W3dMaterialDescClass::VertMatClass::VertMatClass(void) :
  540. PassIndex(-1),
  541. Crc(0),
  542. Name(NULL)
  543. {
  544. for (int stage=0; stage < W3dMaterialClass::MAX_STAGES; ++stage) {
  545. MapperArgs[stage] = NULL;
  546. }
  547. }
  548. W3dMaterialDescClass::VertMatClass::~VertMatClass(void)
  549. {
  550. if (Name) free(Name);
  551. for (int stage=0; stage < W3dMaterialClass::MAX_STAGES; ++stage) {
  552. if (MapperArgs[stage]) {
  553. delete [] (MapperArgs[stage]);
  554. MapperArgs[stage] = NULL;
  555. }
  556. }
  557. }
  558. W3dMaterialDescClass::VertMatClass &
  559. W3dMaterialDescClass::VertMatClass::operator = (const VertMatClass & that)
  560. {
  561. if (this != &that) {
  562. Material = that.Material;
  563. PassIndex = that.PassIndex;
  564. Crc = that.Crc;
  565. Set_Name(that.Name);
  566. for (int stage=0; stage < W3dMaterialClass::MAX_STAGES; stage++) {
  567. Set_Mapper_Args(that.MapperArgs[stage], stage);
  568. }
  569. }
  570. return *this;
  571. }
  572. bool W3dMaterialDescClass::VertMatClass::operator != (const VertMatClass & that)
  573. {
  574. return !(*this == that);
  575. }
  576. bool W3dMaterialDescClass::VertMatClass::operator == (const VertMatClass & that)
  577. {
  578. assert(0); return false;
  579. }
  580. void W3dMaterialDescClass::VertMatClass::Set_Name(const char * name)
  581. {
  582. if (Name) free(Name);
  583. if (name) {
  584. Name = strdup(name);
  585. } else {
  586. Name = NULL;
  587. }
  588. }
  589. void W3dMaterialDescClass::VertMatClass::Set_Mapper_Args(const char * args, int stage)
  590. {
  591. if (MapperArgs[stage]) {
  592. delete [] (MapperArgs[stage]);
  593. MapperArgs[stage] = NULL;
  594. }
  595. if (args) {
  596. int len = strlen(args);
  597. MapperArgs[stage] = new char [len + 1];
  598. strcpy(MapperArgs[stage], args);
  599. } else {
  600. MapperArgs[stage] = NULL;
  601. }
  602. }
  603. /*
  604. Implementation of W3dMaterialDescClass
  605. */
  606. W3dMaterialDescClass::MaterialRemapClass::MaterialRemapClass(void)
  607. {
  608. PassCount = -1;
  609. for (int pass=0; pass<W3dMaterialClass::MAX_PASSES; pass++) {
  610. VertexMaterialIdx[pass] = -1;
  611. ShaderIdx[pass] = -1;
  612. for (int stage=0; stage<W3dMaterialClass::MAX_STAGES; stage++) {
  613. TextureIdx[pass][stage] = -1;
  614. }
  615. }
  616. }
  617. bool W3dMaterialDescClass::MaterialRemapClass::operator != (const MaterialRemapClass & that)
  618. {
  619. return !(*this == that);
  620. }
  621. bool W3dMaterialDescClass::MaterialRemapClass::operator == (const MaterialRemapClass & that)
  622. {
  623. for (int pass=0; pass<W3dMaterialClass::MAX_PASSES; pass++) {
  624. if (VertexMaterialIdx[pass] != that.VertexMaterialIdx[pass]) return false;
  625. if (ShaderIdx[pass] != that.ShaderIdx[pass]) return false;
  626. for (int stage=0; stage<W3dMaterialClass::MAX_STAGES; stage++) {
  627. if (TextureIdx[pass][stage] != that.TextureIdx[pass][stage]) return false;
  628. }
  629. }
  630. return true;
  631. }
  632. W3dMaterialDescClass::W3dMaterialDescClass(void)
  633. {
  634. Reset();
  635. }
  636. W3dMaterialDescClass::~W3dMaterialDescClass(void)
  637. {
  638. }
  639. void W3dMaterialDescClass::Reset(void)
  640. {
  641. PassCount = -1;
  642. SortLevel = -1;
  643. MaterialRemaps.Clear();
  644. Shaders.Clear();
  645. VertexMaterials.Clear();
  646. Textures.Clear();
  647. }
  648. W3dMaterialDescClass::ErrorType W3dMaterialDescClass::Add_Material(const W3dMaterialClass & mat,const char * name)
  649. {
  650. /*
  651. ** If passes hasn't been set yet, set it.
  652. */
  653. if (PassCount == -1) {
  654. PassCount = mat.Get_Pass_Count();
  655. }
  656. /*
  657. ** Same for sort level.
  658. */
  659. if (SortLevel == -1) {
  660. SortLevel = mat.Get_Sort_Level();
  661. }
  662. /*
  663. ** Verify that this material uses the same number of passes that any
  664. ** other materials we have use.
  665. */
  666. if (mat.Get_Pass_Count() != PassCount) {
  667. return INCONSISTENT_PASSES;
  668. }
  669. if (mat.Is_Multi_Pass_Transparent()) {
  670. char msg[100];
  671. sprintf(msg,"Enable Multipass-Transparency?");
  672. HWND window=GetForegroundWindow();
  673. if (IDYES!=MessageBox(window,msg,"Confirmation",MB_YESNO|MB_ICONINFORMATION|MB_TASKMODAL))
  674. return MULTIPASS_TRANSPARENT;
  675. }
  676. /*
  677. ** Verify that this material uses the same sort level as all other
  678. ** materials used on this mesh.
  679. */
  680. if (mat.Get_Sort_Level() != SortLevel) {
  681. return INCONSISTENT_SORT_LEVEL;
  682. }
  683. /*
  684. ** Ok, lets re-index this material and store the unique parts
  685. */
  686. MaterialRemapClass remap;
  687. for (int pass=0; pass<PassCount; pass++) {
  688. remap.VertexMaterialIdx[pass] = Add_Vertex_Material(
  689. mat.Get_Vertex_Material(pass),mat.Get_Mapper_Args(pass, 0),mat.Get_Mapper_Args(pass, 1),pass,name);
  690. remap.ShaderIdx[pass] = Add_Shader(mat.Get_Shader(pass),pass);
  691. for (int stage=0; stage<W3dMaterialClass::MAX_STAGES; stage++) {
  692. remap.TextureIdx[pass][stage] = Add_Texture(mat.Get_Texture(pass,stage),pass,stage);
  693. remap.MapChannel[pass][stage] = mat.Get_Map_Channel(pass,stage);
  694. }
  695. }
  696. MaterialRemaps.Add(remap);
  697. return OK;
  698. }
  699. int W3dMaterialDescClass::Material_Count(void)
  700. {
  701. return MaterialRemaps.Count();
  702. }
  703. int W3dMaterialDescClass::Pass_Count(void)
  704. {
  705. return PassCount;
  706. }
  707. int W3dMaterialDescClass::Vertex_Material_Count(void)
  708. {
  709. return VertexMaterials.Count();
  710. }
  711. int W3dMaterialDescClass::Shader_Count(void)
  712. {
  713. return Shaders.Count();
  714. }
  715. int W3dMaterialDescClass::Texture_Count(void)
  716. {
  717. return Textures.Count();
  718. }
  719. int W3dMaterialDescClass::Get_Sort_Level(void)
  720. {
  721. return SortLevel;
  722. }
  723. W3dVertexMaterialStruct * W3dMaterialDescClass::Get_Vertex_Material(int vmat_index)
  724. {
  725. assert(vmat_index >= 0);
  726. assert(vmat_index < VertexMaterials.Count());
  727. return &(VertexMaterials[vmat_index].Material);
  728. }
  729. const char * W3dMaterialDescClass::Get_Mapper_Args(int vmat_index, int stage)
  730. {
  731. assert(vmat_index >= 0);
  732. assert(vmat_index < VertexMaterials.Count());
  733. assert(stage >= 0);
  734. assert(stage < W3dMaterialClass::MAX_STAGES);
  735. return VertexMaterials[vmat_index].MapperArgs[stage];
  736. }
  737. W3dShaderStruct * W3dMaterialDescClass::Get_Shader(int shader_index)
  738. {
  739. assert(shader_index >= 0);
  740. assert(shader_index < Shaders.Count());
  741. return &(Shaders[shader_index].Shader);
  742. }
  743. W3dMapClass * W3dMaterialDescClass::Get_Texture(int texture_index)
  744. {
  745. assert(texture_index >= 0);
  746. assert(texture_index < Textures.Count());
  747. return &(Textures[texture_index].Map);
  748. }
  749. int W3dMaterialDescClass::Get_Vertex_Material_Index(int mat_index,int pass)
  750. {
  751. assert(mat_index >= 0);
  752. assert(mat_index < MaterialRemaps.Count());
  753. assert(pass >= 0);
  754. assert(pass < PassCount);
  755. return MaterialRemaps[mat_index].VertexMaterialIdx[pass];
  756. }
  757. int W3dMaterialDescClass::Get_Shader_Index(int mat_index,int pass)
  758. {
  759. assert(mat_index >= 0);
  760. assert(mat_index < MaterialRemaps.Count());
  761. assert(pass >= 0);
  762. assert(pass < PassCount);
  763. return MaterialRemaps[mat_index].ShaderIdx[pass];
  764. }
  765. int W3dMaterialDescClass::Get_Texture_Index(int mat_index,int pass,int stage)
  766. {
  767. assert(mat_index >= 0);
  768. assert(mat_index < MaterialRemaps.Count());
  769. assert(pass >= 0);
  770. assert(pass < PassCount);
  771. assert(stage >= 0);
  772. assert(stage < W3dMaterialClass::MAX_STAGES);
  773. return MaterialRemaps[mat_index].TextureIdx[pass][stage];
  774. }
  775. W3dVertexMaterialStruct * W3dMaterialDescClass::Get_Vertex_Material(int mat_index,int pass)
  776. {
  777. int index = Get_Vertex_Material_Index(mat_index,pass);
  778. if (index == -1) {
  779. return NULL;
  780. } else {
  781. return Get_Vertex_Material(index);
  782. }
  783. }
  784. const char * W3dMaterialDescClass::Get_Mapper_Args(int mat_index,int pass,int stage)
  785. {
  786. int index = Get_Vertex_Material_Index(mat_index,pass);
  787. if (index == -1) {
  788. return NULL;
  789. } else {
  790. return Get_Mapper_Args(index,stage);
  791. }
  792. }
  793. W3dShaderStruct * W3dMaterialDescClass::Get_Shader(int mat_index,int pass)
  794. {
  795. int index = Get_Shader_Index(mat_index,pass);
  796. if (index == -1) {
  797. return NULL;
  798. } else {
  799. return Get_Shader(index);
  800. }
  801. }
  802. W3dMapClass * W3dMaterialDescClass::Get_Texture(int mat_index,int pass,int stage)
  803. {
  804. int index = Get_Texture_Index(mat_index,stage,pass);
  805. if (index == -1) {
  806. return NULL;
  807. } else {
  808. return Get_Texture(index);
  809. }
  810. }
  811. int W3dMaterialDescClass::Get_Map_Channel(int mat_index,int pass,int stage)
  812. {
  813. return MaterialRemaps[mat_index].MapChannel[pass][stage];
  814. }
  815. const char * W3dMaterialDescClass::Get_Vertex_Material_Name(int mat_index,int pass)
  816. {
  817. int index = Get_Vertex_Material_Index(mat_index,pass);
  818. if (index == -1) {
  819. return NULL;
  820. } else {
  821. return VertexMaterials[index].Name;
  822. }
  823. }
  824. const char * W3dMaterialDescClass::Get_Vertex_Material_Name(int vmat_index)
  825. {
  826. return VertexMaterials[vmat_index].Name;
  827. }
  828. bool W3dMaterialDescClass::Stage_Needs_Texture_Coordinates(int pass,int stage)
  829. {
  830. for (int mi=0; mi<MaterialRemaps.Count();mi++) {
  831. W3dShaderStruct * shader = Get_Shader(mi,pass);
  832. if (shader) {
  833. if (stage == 0) {
  834. if (W3d_Shader_Get_Texturing(shader) == W3DSHADER_TEXTURING_ENABLE) return true;
  835. }
  836. if (stage == 1) {
  837. if ((W3d_Shader_Get_Detail_Color_Func(shader) != W3DSHADER_DETAILCOLORFUNC_DISABLE) ||
  838. (W3d_Shader_Get_Detail_Alpha_Func(shader) != W3DSHADER_DETAILALPHAFUNC_DISABLE)) {
  839. return true;
  840. }
  841. }
  842. }
  843. }
  844. return false;
  845. }
  846. bool W3dMaterialDescClass::Pass_Uses_Vertex_Alpha(int pass)
  847. {
  848. /*
  849. ** Only the last alpha pass gets vertex alpha
  850. */
  851. int max_alpha_pass = -1;
  852. for (int pi=0; pi<Pass_Count(); pi++) {
  853. if (Pass_Uses_Alpha(pi)) {
  854. max_alpha_pass = pi;
  855. }
  856. }
  857. return (max_alpha_pass == pass);
  858. }
  859. bool W3dMaterialDescClass::Pass_Uses_Alpha(int pass)
  860. {
  861. for (int mi=0; mi<MaterialRemaps.Count(); mi++) {
  862. W3dShaderStruct * shader = Get_Shader(mi,pass);
  863. int dst_blend = W3d_Shader_Get_Dest_Blend_Func(shader);
  864. int src_blend = W3d_Shader_Get_Src_Blend_Func(shader);
  865. int alpha_test = W3d_Shader_Get_Alpha_Test(shader);
  866. if ( (dst_blend == W3DSHADER_DESTBLENDFUNC_SRC_ALPHA) ||
  867. (dst_blend == W3DSHADER_DESTBLENDFUNC_ONE_MINUS_SRC_ALPHA) ||
  868. (src_blend == W3DSHADER_SRCBLENDFUNC_SRC_ALPHA) ||
  869. (src_blend == W3DSHADER_SRCBLENDFUNC_ONE_MINUS_SRC_ALPHA) ||
  870. (alpha_test != 0) )
  871. {
  872. return true;
  873. }
  874. }
  875. return false;
  876. }
  877. int W3dMaterialDescClass::Add_Vertex_Material(W3dVertexMaterialStruct * vmat,
  878. const char *mapper_args0,const char *mapper_args1,int pass,const char * name)
  879. {
  880. if (vmat == NULL) {
  881. return -1;
  882. }
  883. int crc = Compute_Crc(*vmat, mapper_args0, mapper_args1);
  884. for (int vi=0; vi<VertexMaterials.Count(); vi++) {
  885. if ((crc == VertexMaterials[vi].Crc) && (pass == VertexMaterials[vi].PassIndex)) {
  886. break;
  887. }
  888. }
  889. if (vi == VertexMaterials.Count()) {
  890. VertMatClass vm;
  891. vm.Material = *vmat;
  892. vm.Crc = crc;
  893. vm.PassIndex = pass;
  894. vm.Set_Name(name);
  895. vm.Set_Mapper_Args(mapper_args0, 0);
  896. vm.Set_Mapper_Args(mapper_args1, 1);
  897. VertexMaterials.Add(vm);
  898. }
  899. return vi;
  900. }
  901. int W3dMaterialDescClass::Add_Shader(const W3dShaderStruct & shader,int pass)
  902. {
  903. int crc = Compute_Crc(shader);
  904. for (int si=0; si<Shaders.Count(); si++) {
  905. if (crc == Shaders[si].Crc) break;
  906. }
  907. if (si == Shaders.Count()) {
  908. ShadeClass s;
  909. s.Shader = shader;
  910. s.Crc = crc;
  911. Shaders.Add(s);
  912. }
  913. return si;
  914. }
  915. int W3dMaterialDescClass::Add_Texture(W3dMapClass * map,int pass,int stage)
  916. {
  917. if ((map == NULL) || (map->Filename == NULL)) {
  918. return -1;
  919. }
  920. int crc = Compute_Crc(*map);
  921. for (int ti=0; ti<Textures.Count(); ti++) {
  922. if (crc == Textures[ti].Crc) {
  923. break;
  924. }
  925. }
  926. if (ti == Textures.Count()) {
  927. TexClass tex;
  928. tex.Map = *map;
  929. tex.Crc = crc;
  930. Textures.Add(tex);
  931. }
  932. return ti;
  933. }
  934. unsigned long W3dMaterialDescClass::Compute_Crc(const W3dVertexMaterialStruct & vmat,
  935. const char *mapper_args0,
  936. const char *mapper_args1)
  937. {
  938. unsigned long crc = 0;
  939. crc = CRC_Memory((const unsigned char *)&vmat.Attributes,sizeof(vmat.Attributes),crc);
  940. crc = CRC_Memory((const unsigned char *)&vmat.Ambient,sizeof(vmat.Ambient),crc);
  941. crc = CRC_Memory((const unsigned char *)&vmat.Diffuse,sizeof(vmat.Diffuse),crc);
  942. crc = CRC_Memory((const unsigned char *)&vmat.Specular,sizeof(vmat.Specular),crc);
  943. crc = CRC_Memory((const unsigned char *)&vmat.Emissive,sizeof(vmat.Emissive),crc);
  944. crc = CRC_Memory((const unsigned char *)&vmat.Shininess,sizeof(vmat.Shininess),crc);
  945. crc = CRC_Memory((const unsigned char *)&vmat.Opacity,sizeof(vmat.Opacity),crc);
  946. crc = CRC_Memory((const unsigned char *)&vmat.Translucency,sizeof(vmat.Translucency),crc);
  947. // Add mapper args string to crc. We are stripping out spaces, tabs, and
  948. // leading/trailing newlines before computing the CRC so two strings will
  949. // only differ if they have visible differences.
  950. crc = Add_String_To_Crc(mapper_args0, crc);
  951. crc = Add_String_To_Crc(mapper_args1, crc);
  952. return crc;
  953. }
  954. unsigned long W3dMaterialDescClass::Compute_Crc(const W3dShaderStruct & shader)
  955. {
  956. unsigned long crc = 0;
  957. crc = CRC_Memory((const unsigned char *)&shader,sizeof(shader),crc);
  958. return crc;
  959. }
  960. unsigned long W3dMaterialDescClass::Compute_Crc(const W3dMapClass & map)
  961. {
  962. unsigned long crc = 0;
  963. if (map.AnimInfo != NULL) {
  964. crc = CRC_Memory((const unsigned char *)&map.AnimInfo->Attributes,sizeof(map.AnimInfo->Attributes),crc);
  965. crc = CRC_Memory((const unsigned char *)&map.AnimInfo->AnimType,sizeof(map.AnimInfo->AnimType),crc);
  966. crc = CRC_Memory((const unsigned char *)&map.AnimInfo->FrameCount,sizeof(map.AnimInfo->FrameCount),crc);
  967. crc = CRC_Memory((const unsigned char *)&map.AnimInfo->FrameRate,sizeof(map.AnimInfo->FrameRate),crc);
  968. }
  969. crc = CRC_Stringi(map.Filename, crc);
  970. return crc;
  971. }
  972. unsigned long W3dMaterialDescClass::Add_String_To_Crc(const char *str, unsigned long in_crc)
  973. {
  974. unsigned long out_crc = in_crc;
  975. if (str) {
  976. int len = strlen(str);
  977. char *temp = new char[len + 1];
  978. const char *p_in = str;
  979. // skip leading spaces, tabs, newlines
  980. for (; *p_in == ' ' || *p_in == '\t' || *p_in == '\r' || *p_in == '\n'; p_in++);
  981. // copy string, skipping spaces and tabs
  982. char * p_out = temp;
  983. int count = 0;
  984. for (; *p_in; p_in++) {
  985. for (; *p_in == ' ' || *p_in == '\t'; p_in++);
  986. if (!(*p_in)) break;
  987. *p_out = *p_in;
  988. p_out++;
  989. count++;
  990. }
  991. *p_out = 0;
  992. // Erase any trailing newlines:
  993. if (count) {
  994. // p_out now points to the ending null - make it point to the
  995. // character before it (the last character of the string proper)
  996. p_out--;
  997. for (; *p_out == '\r' || *p_out == '\n'; p_out--) {
  998. *p_out = '\000';
  999. count--;
  1000. }
  1001. }
  1002. out_crc = CRC_Memory((const unsigned char *)temp,count,in_crc);
  1003. delete [] temp;
  1004. }
  1005. return out_crc;
  1006. }