meshmdlio.cpp 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696
  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/meshmdlio.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 1/18/02 3:09p $*
  29. * *
  30. * $Revision:: 27 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * MeshModelClass::Load_W3D -- Load a mesh from a W3D file *
  35. * MeshModelClass::read_chunks -- read all of the chunks for a mesh *
  36. * MeshModelClass::read_vertices -- reads the vertex chunk *
  37. * MeshModelClass::read_texcoords -- read in the texture coordinates chunk *
  38. * MeshModelClass::read_vertex_normals -- reads a vertex normal chunk from the w3d file *
  39. * MeshModelClass::read_v3_materials -- Reads in version 3 materials. *
  40. * MeshModelClass::read_triangles -- read the triangles chunk *
  41. * MeshModelClass::read_per_tri_materials -- read the material indices for each triangle *
  42. * MeshModelClass::read_user_text -- read in the user text chunk *
  43. * MeshModelClass::read_vertex_colors -- read in the vertex colors chunk *
  44. * MeshModelClass::read_vertex_influences -- read in the vertex influences chunk *
  45. * MeshModelClass::read_vertex_shade_indices -- read the shade index chunk *
  46. * MeshModelClass::read_material_info -- read the material info chunk *
  47. * MeshModelClass::read_shaders -- read the shaders chunk *
  48. * MeshModelClass::read_vertex_materials -- read the vertex materials chunk *
  49. * MeshModelClass::read_textures -- read the textures chunk *
  50. * MeshModelClass::read_material_pass -- read a material pass chunk *
  51. * MeshModelClass::read_vertex_material_ids -- read the vmat ids for a pass *
  52. * MeshModelClass::read_shader_ids -- read the shader indexes for a pass *
  53. * MeshModelClass::read_dcg -- read the per-vertex diffuse color for a pass *
  54. * MeshModelClass::read_dig -- read the per-vertex diffuse illumination for a pass *
  55. * MeshModelClass::read_scg -- read the specular color for a pass *
  56. * MeshModelClass::read_texture_stage -- read texture stage chunks *
  57. * MeshModelClass::read_texture_ids -- read the texture ids for a pass,stage *
  58. * MeshModelClass::read_stage_texcoords -- read the texcoords for a pass,stage *
  59. * MeshModelClass::read_per_face_texcoord_ids -- read uv indices for given (pass,stage). *
  60. * MeshModelClass::read_prelit_material -- read prelit material chunks. *
  61. * MeshModelClass::read_aabtree -- loads the aabtree chunk *
  62. * MeshModelClass::Save -- Save this mesh model! *
  63. * MeshLoadContextClass::MeshLoadContextClass -- constructor for MeshLoadContextClass *
  64. * MeshLoadContextClass::~MeshLoadContextClass -- destructor *
  65. * MeshLoadContextClass::Get_Texcoord_Array -- returns the texture coordinates array *
  66. * MeshLoadContextClass::Add_Shader -- adds a shader to the array *
  67. * MeshLoadContextClass::Add_Vertex_Materail -- adds a vertex material *
  68. * MeshLoadContextClass::Add_Texture -- adds a texture *
  69. * MeshLoadContextClass::Add_Legacy_Material -- adds a legacy material *
  70. * MeshLoadContextClass::Peek_Legacy_Shader -- returns a legacy shader *
  71. * MeshLoadContextClass::Peek_Legacy_Vertex_Material -- returns a pointer to a legacy vmat *
  72. * MeshLoadContextClass::Peek_Legacy_Texture -- returns a pointer to a texture *
  73. * MeshSaveContextClass::MeshSaveContextClass -- constructor *
  74. * MeshSaveContextClass::~MeshSaveContextClass -- destructor *
  75. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  76. #include "meshmdl.h"
  77. #include "aabtree.h"
  78. #include "matinfo.h"
  79. #include "vertmaterial.h"
  80. #include "shader.h"
  81. #include "texture.h"
  82. #include "chunkio.h"
  83. #include "w3derr.h"
  84. #include "w3d_file.h"
  85. #include "w3d_util.h"
  86. #include "assetmgr.h"
  87. #include "simplevec.h"
  88. #include "realcrc.h"
  89. #include "dx8wrapper.h"
  90. #include <stdio.h>
  91. #ifdef _UNIX
  92. #include "osdep/osdep.h"
  93. #endif
  94. /**
  95. ** MeshLoadContextClass
  96. ** This class is just used as a temporary scratchpad while a mesh is being
  97. ** loaded. In some cases, a chunk will be encountered before I have a place
  98. ** to plug it into the mesh model, etc. It is also used to convert from the
  99. ** old material format to the new one (detecting duplicated materials in the
  100. ** process).
  101. **
  102. ** This object will hold refs to all of the unique material objects. These
  103. ** objects will later be transferred into the MaterialInfo for the mesh and it
  104. ** will own the refs for the mesh. The load context object is destroyed once
  105. ** loading is complete...
  106. */
  107. class MeshLoadContextClass
  108. {
  109. private:
  110. MeshLoadContextClass(void);
  111. ~MeshLoadContextClass(void);
  112. W3dTexCoordStruct * Get_Texcoord_Array(void);
  113. int Add_Shader(ShaderClass shader);
  114. int Add_Vertex_Material(VertexMaterialClass * vmat);
  115. int Add_Texture(TextureClass* tex);
  116. ShaderClass Peek_Shader(int index) { return Shaders[index]; }
  117. VertexMaterialClass * Peek_Vertex_Material(int index) { return VertexMaterials[index]; }
  118. TextureClass * Peek_Texture(int index) { return Textures[index]; }
  119. int Shader_Count(void) { return Shaders.Count(); }
  120. int Vertex_Material_Count(void) { return VertexMaterials.Count(); }
  121. int Texture_Count(void) { return Textures.Count(); }
  122. /*
  123. ** Legacy material support.
  124. */
  125. void Add_Legacy_Material(ShaderClass shader,VertexMaterialClass * vmat,TextureClass * tex);
  126. ShaderClass Peek_Legacy_Shader(int legacy_material_index);
  127. VertexMaterialClass * Peek_Legacy_Vertex_Material(int legacy_material_index);
  128. TextureClass * Peek_Legacy_Texture(int legacy_material_index);
  129. /*
  130. ** Redundant UV detection support. The context provides a temporary buffer to
  131. ** load the uv coordinates into.
  132. */
  133. Vector2 * Get_Temporary_UV_Array(int elementcount);
  134. /*
  135. ** Currently, the only tool that creates DIG chunks is the lightmap tool. Since DX8 support
  136. ** for linking the emissive material color to an array seems poor, We're just going to multiply
  137. ** the DIG array into the DCG array (or make it the DCG array). Now, to properly support the
  138. ** "alternate material set", we have to know whether we've already encountered a DIG chunk so
  139. ** these flags provide that functionality.
  140. */
  141. void Notify_Loaded_DIG_Chunk(bool onoff = true) { LoadedDIG = onoff; }
  142. bool Already_Loaded_DIG(void) { return LoadedDIG; }
  143. private:
  144. struct LegacyMaterialClass
  145. {
  146. LegacyMaterialClass(void) : VertexMaterialIdx(0),ShaderIdx(0),TextureIdx(0) { }
  147. ~LegacyMaterialClass(void) { }
  148. void Set_Name(const char * name) { Name=name; }
  149. StringClass Name;
  150. int VertexMaterialIdx;
  151. int ShaderIdx;
  152. int TextureIdx;
  153. };
  154. W3dMeshHeader3Struct Header;
  155. W3dTexCoordStruct * TexCoords;
  156. W3dMaterialInfoStruct MatInfo;
  157. uint32 PrelitChunkID;
  158. int CurPass;
  159. int CurTexStage;
  160. DynamicVectorClass < LegacyMaterialClass * > LegacyMaterials;
  161. DynamicVectorClass < ShaderClass > Shaders;
  162. DynamicVectorClass < VertexMaterialClass * > VertexMaterials;
  163. DynamicVectorClass < unsigned long > VertexMaterialCrcs;
  164. DynamicVectorClass < TextureClass * > Textures;
  165. /*
  166. ** Alternate material data. Any alternate material data will be loaded into
  167. ** this MeshMatDescClass object. When loading is finished, an alternate MeshMatDescClass
  168. ** will be allocated in the mesh model. This MeshMatDescClass will be initialized to be
  169. ** identical to the default MeshMatDescClass and then any data contained in this
  170. ** MeshMatDescClass will replace the relevant arrays.
  171. */
  172. MeshMatDescClass AlternateMatDesc;
  173. SimpleVecClass<Vector2> TempUVArray;
  174. /*
  175. ** Record when we load the DIG chunk
  176. */
  177. bool LoadedDIG;
  178. friend class MeshClass;
  179. friend class MeshModelClass;
  180. };
  181. /*
  182. ** MeshSaveContextClass
  183. ** This class is used to pass information between the saving code in a mesh
  184. */
  185. class MeshSaveContextClass
  186. {
  187. public:
  188. MeshSaveContextClass(void);
  189. ~MeshSaveContextClass(void);
  190. int CurPass;
  191. int CurStage;
  192. MaterialCollectorClass Materials;
  193. };
  194. /***********************************************************************************************
  195. * MeshModelClass::Load_W3D -- Load a mesh from a W3D file *
  196. * *
  197. * INPUT: *
  198. * *
  199. * OUTPUT: *
  200. * *
  201. * WARNINGS: *
  202. * *
  203. * HISTORY: *
  204. * 2/16/99 GTH : Created. *
  205. *=============================================================================================*/
  206. WW3DErrorType MeshModelClass::Load_W3D(ChunkLoadClass & cload)
  207. {
  208. MeshLoadContextClass * context = NULL;
  209. /*
  210. ** Open the first chunk, it should be the mesh header
  211. */
  212. cload.Open_Chunk();
  213. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MESH_HEADER3) {
  214. WWDEBUG_SAY(("Old format mesh mesh, no longer supported.\n"));
  215. goto Error;
  216. }
  217. context = new MeshLoadContextClass;
  218. if (cload.Read(&(context->Header),sizeof(W3dMeshHeader3Struct)) != sizeof(W3dMeshHeader3Struct)) {
  219. goto Error;
  220. }
  221. cload.Close_Chunk();
  222. /*
  223. ** Process the header
  224. */
  225. char * tmpname;
  226. int namelen;
  227. Reset(context->Header.NumTris,context->Header.NumVertices,1);
  228. namelen = strlen(context->Header.ContainerName);
  229. namelen += strlen(context->Header.MeshName);
  230. namelen += 2;
  231. W3dAttributes = context->Header.Attributes;
  232. SortLevel = context->Header.SortLevel;
  233. tmpname = new char[namelen];
  234. memset(tmpname,0,namelen);
  235. if (strlen(context->Header.ContainerName) > 0) {
  236. strcpy(tmpname,context->Header.ContainerName);
  237. strcat(tmpname,".");
  238. }
  239. strcat(tmpname,context->Header.MeshName);
  240. Set_Name(tmpname);
  241. delete[] tmpname;
  242. tmpname = NULL;
  243. context->AlternateMatDesc.Set_Vertex_Count(VertexCount);
  244. context->AlternateMatDesc.Set_Polygon_Count(PolyCount);
  245. /*
  246. ** Set Bounding Info
  247. */
  248. BoundBoxMin.Set(context->Header.Min.X,context->Header.Min.Y,context->Header.Min.Z);
  249. BoundBoxMax.Set(context->Header.Max.X,context->Header.Max.Y,context->Header.Max.Z);
  250. BoundSphereCenter.Set(context->Header.SphCenter.X,context->Header.SphCenter.Y,context->Header.SphCenter.Z);
  251. BoundSphereRadius = context->Header.SphRadius;
  252. /*
  253. ** Flags
  254. */
  255. if (context->Header.Version >= W3D_MAKE_VERSION(4,1)) {
  256. int geometry_type = context->Header.Attributes & W3D_MESH_FLAG_GEOMETRY_TYPE_MASK;
  257. switch (geometry_type)
  258. {
  259. case W3D_MESH_FLAG_GEOMETRY_TYPE_NORMAL:
  260. break;
  261. case W3D_MESH_FLAG_GEOMETRY_TYPE_CAMERA_ALIGNED:
  262. Set_Flag(ALIGNED,true);
  263. break;
  264. case W3D_MESH_FLAG_GEOMETRY_TYPE_CAMERA_ORIENTED:
  265. Set_Flag(ORIENTED,true);
  266. break;
  267. case W3D_MESH_FLAG_GEOMETRY_TYPE_SKIN:
  268. Set_Flag(SKIN,true);
  269. Set_Flag(ALLOW_NPATCHES,true);
  270. break;
  271. }
  272. }
  273. if (context->Header.Attributes & W3D_MESH_FLAG_TWO_SIDED) {
  274. Set_Flag(TWO_SIDED,true);
  275. }
  276. if (context->Header.Attributes & W3D_MESH_FLAG_CAST_SHADOW) {
  277. Set_Flag(CAST_SHADOW,true);
  278. }
  279. if (context->Header.Attributes & W3D_MESH_FLAG_NPATCHABLE) {
  280. Set_Flag(ALLOW_NPATCHES,true);
  281. }
  282. // Configure the load sequence for prelighting.
  283. if (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_MASK) {
  284. // Select from the available prelit materials based on current prelit lighting mode.
  285. // If the model does not have the current prelit mode, select the next highest quality
  286. // prelit material that is available.
  287. switch (WW3D::Get_Prelit_Mode()) {
  288. case WW3D::PRELIT_MODE_LIGHTMAP_MULTI_TEXTURE:
  289. if (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_LIGHTMAP_MULTI_TEXTURE) {
  290. context->PrelitChunkID = W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_TEXTURE;
  291. Set_Flag (PRELIT_LIGHTMAP_MULTI_TEXTURE, true);
  292. break;
  293. }
  294. // Else fall thru...
  295. case WW3D::PRELIT_MODE_LIGHTMAP_MULTI_PASS:
  296. if (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_LIGHTMAP_MULTI_PASS) {
  297. context->PrelitChunkID = W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_PASS;
  298. Set_Flag (PRELIT_LIGHTMAP_MULTI_PASS, true);
  299. break;
  300. }
  301. // Else fall thru...
  302. case WW3D::PRELIT_MODE_VERTEX:
  303. if (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_VERTEX) {
  304. context->PrelitChunkID = W3D_CHUNK_PRELIT_VERTEX;
  305. Set_Flag (PRELIT_VERTEX, true);
  306. break;
  307. }
  308. // Else fall thru...
  309. default:
  310. // This prelighting option MUST be available if none of the others are available.
  311. WWASSERT (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_UNLIT);
  312. context->PrelitChunkID = W3D_CHUNK_PRELIT_UNLIT;
  313. break;
  314. }
  315. } else {
  316. // For backwards compatibility, test for obsolete lightmap flag.
  317. if (context->Header.Attributes & OBSOLETE_W3D_MESH_FLAG_LIGHTMAPPED) {
  318. Set_Flag (PRELIT_LIGHTMAP_MULTI_PASS, true);
  319. }
  320. // Else this mesh has no prelighting.
  321. }
  322. read_chunks(cload,context);
  323. /*
  324. ** If this is a pre-3.0 mesh and it has vertex influences,
  325. ** fixup the bone indices to account for the new root node
  326. */
  327. if ((context->Header.Version < W3D_MAKE_VERSION(3,0)) && (Get_Flag(SKIN))) {
  328. uint16 * links = get_bone_links();
  329. WWASSERT(links);
  330. for (int bi = 0; bi < Get_Vertex_Count(); bi++) {
  331. links[bi] += 1;
  332. }
  333. }
  334. /*
  335. ** If this mesh is collideable and no AABTree was in the file, generate one now
  336. */
  337. if ( (((W3dAttributes & W3D_MESH_FLAG_COLLISION_TYPE_MASK) >> W3D_MESH_FLAG_COLLISION_TYPE_SHIFT) != 0) &&
  338. (CullTree == NULL))
  339. {
  340. Generate_Culling_Tree();
  341. }
  342. /*
  343. ** Transfer the materials into the MatInfo
  344. */
  345. install_materials(context);
  346. /*
  347. ** Delete the temporary LoadInfo object
  348. */
  349. delete context;
  350. /*
  351. ** Post-process the model: optimize passes, activate fog etc.
  352. */
  353. post_process();
  354. return WW3D_ERROR_OK;
  355. Error:
  356. return WW3D_ERROR_LOAD_FAILED;
  357. }
  358. /***********************************************************************************************
  359. * MeshModelClass::read_chunks -- read all of the chunks for a mesh *
  360. * *
  361. * INPUT: *
  362. * *
  363. * OUTPUT: *
  364. * *
  365. * WARNINGS: *
  366. * *
  367. * HISTORY: *
  368. * 2/16/99 GTH : Created. *
  369. *=============================================================================================*/
  370. WW3DErrorType MeshModelClass::read_chunks(ChunkLoadClass & cload,MeshLoadContextClass * context)
  371. {
  372. /*
  373. ** Read in the chunk header
  374. ** If there are no more chunks within the mesh chunk,
  375. ** we are done.
  376. */
  377. while (cload.Open_Chunk()) {
  378. /*
  379. ** Process the chunk
  380. */
  381. WW3DErrorType error = WW3D_ERROR_OK;
  382. switch (cload.Cur_Chunk_ID()) {
  383. case W3D_CHUNK_VERTICES:
  384. // call up to MeshGeometryClass
  385. error = read_vertices(cload);
  386. break;
  387. case W3D_CHUNK_SURRENDER_NORMALS:
  388. case W3D_CHUNK_VERTEX_NORMALS:
  389. // call up to MeshGeometryClass
  390. error = read_vertex_normals(cload);
  391. break;
  392. case W3D_CHUNK_TEXCOORDS:
  393. error = read_texcoords(cload,context);
  394. break;
  395. case O_W3D_CHUNK_MATERIALS:
  396. case O_W3D_CHUNK_MATERIALS2:
  397. WWDEBUG_SAY(( "Obsolete material chunk encountered in mesh: %s.%s\r\n", context->Header.ContainerName,context->Header.MeshName));
  398. WWASSERT(0);
  399. break;
  400. case W3D_CHUNK_MATERIALS3:
  401. WWDEBUG_SAY(( "Obsolete material chunk encountered in mesh: %s.%s\r\n", context->Header.ContainerName,context->Header.MeshName));
  402. error = read_v3_materials(cload,context);
  403. break;
  404. case O_W3D_CHUNK_SURRENDER_TRIANGLES:
  405. WWASSERT_PRINT( 0, "Obsolete Triangle Chunk Encountered!\r\n" );
  406. break;
  407. case W3D_CHUNK_TRIANGLES:
  408. // call up to MeshGeometryClass
  409. error = read_triangles(cload);
  410. break;
  411. case W3D_CHUNK_PER_TRI_MATERIALS:
  412. error = read_per_tri_materials(cload,context);
  413. break;
  414. case W3D_CHUNK_MESH_USER_TEXT:
  415. // call up to MeshGeometryClass
  416. error = read_user_text(cload);
  417. break;
  418. case W3D_CHUNK_VERTEX_COLORS:
  419. error = read_vertex_colors(cload,context);
  420. break;
  421. case W3D_CHUNK_VERTEX_INFLUENCES:
  422. // call up to MeshGeometryClass
  423. error = read_vertex_influences(cload);
  424. break;
  425. case W3D_CHUNK_VERTEX_SHADE_INDICES:
  426. // call up to MeshGeometryClass
  427. error = read_vertex_shade_indices(cload);
  428. break;
  429. case W3D_CHUNK_MATERIAL_INFO:
  430. error = read_material_info(cload,context);
  431. break;
  432. case W3D_CHUNK_SHADERS:
  433. error = read_shaders(cload,context);
  434. break;
  435. case W3D_CHUNK_VERTEX_MATERIALS:
  436. error = read_vertex_materials(cload,context);
  437. break;
  438. case W3D_CHUNK_TEXTURES:
  439. error = read_textures(cload,context);
  440. break;
  441. case W3D_CHUNK_MATERIAL_PASS:
  442. error = read_material_pass(cload,context);
  443. break;
  444. case W3D_CHUNK_DEFORM:
  445. WWDEBUG_SAY(("Obsolete deform chunk encountered in mesh: %s.%s\r\n", context->Header.ContainerName,context->Header.MeshName));
  446. break;
  447. case W3D_CHUNK_DAMAGE:
  448. WWDEBUG_SAY(("Obsolete damage chunk encountered in mesh: %s.%s\r\n", context->Header.ContainerName,context->Header.MeshName));
  449. break;
  450. case W3D_CHUNK_PRELIT_UNLIT:
  451. case W3D_CHUNK_PRELIT_VERTEX:
  452. case W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_PASS:
  453. case W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_TEXTURE:
  454. read_prelit_material (cload, context);
  455. break;
  456. case W3D_CHUNK_AABTREE:
  457. // call up to MeshGeometryClass
  458. read_aabtree(cload);
  459. break;
  460. default:
  461. break;
  462. }
  463. cload.Close_Chunk();
  464. if (error != WW3D_ERROR_OK) {
  465. return error;
  466. }
  467. }
  468. return WW3D_ERROR_OK;
  469. }
  470. /***********************************************************************************************
  471. * MeshModelClass::read_texcoords -- read in the texture coordinates chunk *
  472. * *
  473. * INPUT: *
  474. * *
  475. * OUTPUT: *
  476. * *
  477. * WARNINGS: *
  478. * *
  479. * HISTORY: *
  480. * 3/6/98 GTH : Created. *
  481. * 2/16/99 GTH : Moved into MeshModel *
  482. *=============================================================================================*/
  483. WW3DErrorType MeshModelClass::read_texcoords(ChunkLoadClass & cload,MeshLoadContextClass * context)
  484. {
  485. W3dTexCoordStruct texcoord;
  486. Vector2 * uvarray = 0;
  487. int elementcount = cload.Cur_Chunk_Length() / sizeof (W3dTexCoordStruct);
  488. uvarray = context->Get_Temporary_UV_Array(elementcount);
  489. if (uvarray != NULL) {
  490. /*
  491. ** Read the uv's into the first u-v pass array
  492. ** NOTE: this is an obsolete function. Texture coordinates are now
  493. ** loaded in the pass chunks
  494. */
  495. for (int i=0; i<VertexCount; i++) {
  496. if (cload.Read(&(texcoord),sizeof(W3dTexCoordStruct)) != sizeof(W3dTexCoordStruct)) {
  497. return WW3D_ERROR_LOAD_FAILED;
  498. }
  499. uvarray[i].Set(texcoord.U,1.0f - texcoord.V);
  500. }
  501. DefMatDesc->Install_UV_Array(context->CurPass,context->CurTexStage,uvarray,elementcount);
  502. }
  503. return WW3D_ERROR_OK;
  504. }
  505. /***********************************************************************************************
  506. * MeshModelClass::read_v3_materials -- Reads in version 3 materials. *
  507. * *
  508. * INPUT: *
  509. * *
  510. * OUTPUT: *
  511. * *
  512. * WARNINGS: *
  513. * *
  514. * HISTORY: *
  515. * 4/2/98 GTH : Created. *
  516. * 2/16/99 GTH : Moved into MeshModelClass *
  517. *=============================================================================================*/
  518. WW3DErrorType MeshModelClass::read_v3_materials(ChunkLoadClass & cload,MeshLoadContextClass * context)
  519. {
  520. for (unsigned int mi=0; mi<context->Header.NumMaterials; mi++) {
  521. /*
  522. ** First, we expect a W3D_CHUNK_MATERIAL3 to wrap the entire material
  523. */
  524. if (!cload.Open_Chunk()) goto Error;
  525. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MATERIAL3) goto Error;
  526. /*
  527. ** Inside the MATERIAL3 will be the following:
  528. **
  529. ** W3D_MATERIAL3_NAME - name of the material
  530. ** W3D_MATERIAL3_INFO - equivalent to 1.40 vertex material parameters
  531. ** W3D_MATERIAL3_DC_MAP - diffuse color mapping
  532. ** W3D_MAP3_FILENAME - filename of the texture map
  533. ** W3D_MAP3_INFO - animation, etc information
  534. ** W3D_MATERIAL3_DI_MAP - diffuse illumination map
  535. ** W3D_MATERIAL3_SC_MAP - specular color map
  536. ** W3D_MATERIAL3_SI_MAP - specular illumination map
  537. */
  538. VertexMaterialClass * vmat = NULL;
  539. ShaderClass shader;
  540. TextureClass * tex = NULL;
  541. char name[256];
  542. /*
  543. ** Read the material name
  544. */
  545. if (!cload.Open_Chunk()) goto Error;
  546. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MATERIAL3_NAME) goto Error;
  547. cload.Read(name,cload.Cur_Chunk_Length());
  548. if (!cload.Close_Chunk()) goto Error;
  549. /*
  550. ** Read the vertex material parameters
  551. */
  552. if (!cload.Open_Chunk()) goto Error;
  553. W3dMaterial3Struct mat;
  554. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MATERIAL3_INFO) goto Error;
  555. if (cload.Read(&mat,sizeof(W3dMaterial3Struct)) != sizeof(W3dMaterial3Struct)) goto Error;
  556. vmat = new VertexMaterialClass;
  557. vmat->Init_From_Material3(mat);
  558. vmat->Set_Name(name);
  559. shader.Init_From_Material3(mat);
  560. /*
  561. ** If this shader does alpha blending, the mesh must be sorted.
  562. */
  563. if (shader.Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO) {
  564. Set_Flag(MeshModelClass::SORT,true);
  565. }
  566. if (!cload.Close_Chunk()) goto Error;
  567. /*
  568. ** Look for the DC map and read it in
  569. */
  570. while (cload.Open_Chunk()) {
  571. if (cload.Cur_Chunk_ID() == W3D_CHUNK_MATERIAL3_DC_MAP) {
  572. /*
  573. ** Read in the texture filename
  574. */
  575. char filename[_MAX_FNAME + _MAX_EXT];
  576. if (!cload.Open_Chunk()) goto Error;
  577. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MAP3_FILENAME) goto Error;
  578. if (cload.Cur_Chunk_Length() >= sizeof(filename)) goto Error;
  579. cload.Read(filename,cload.Cur_Chunk_Length());
  580. if (!cload.Close_Chunk()) goto Error;
  581. /*
  582. ** Read in the auxiliary map info
  583. */
  584. W3dMap3Struct mapinfo;
  585. if (!cload.Open_Chunk()) goto Error;
  586. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MAP3_INFO) goto Error;
  587. if (cload.Read(&mapinfo,sizeof(W3dMap3Struct)) != sizeof(W3dMap3Struct)) goto Error;
  588. if (!cload.Close_Chunk()) goto Error;
  589. if ( mapinfo.FrameCount > 1 ) {
  590. WWDEBUG_SAY(("ERROR: Obsolete Animated Texture detected in model: %s\r\n",context->Header.MeshName));
  591. }
  592. tex = WW3DAssetManager::Get_Instance()->Get_Texture(filename);
  593. shader.Set_Texturing(ShaderClass::TEXTURING_ENABLE);
  594. } else if (cload.Cur_Chunk_ID() == W3D_CHUNK_MATERIAL3_SI_MAP) {
  595. Vector3 diffuse_color;
  596. vmat->Get_Diffuse( &diffuse_color);
  597. if ( diffuse_color == Vector3( 0,0,0 ) ) {
  598. /*
  599. ** Read in the texture filename
  600. */
  601. char filename[_MAX_FNAME + _MAX_EXT];
  602. if (!cload.Open_Chunk()) goto Error;
  603. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MAP3_FILENAME) goto Error;
  604. if (cload.Cur_Chunk_Length() >= sizeof(filename)) goto Error;
  605. cload.Read(filename,cload.Cur_Chunk_Length());
  606. if (!cload.Close_Chunk()) goto Error;
  607. if (tex) tex->Release_Ref();
  608. /*
  609. ** Read in the auxiliary map info
  610. */
  611. W3dMap3Struct mapinfo;
  612. if (!cload.Open_Chunk()) goto Error;
  613. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MAP3_INFO) goto Error;
  614. if (cload.Read(&mapinfo,sizeof(W3dMap3Struct)) != sizeof(W3dMap3Struct)) goto Error;
  615. if (!cload.Close_Chunk()) goto Error;
  616. if ( mapinfo.FrameCount > 1 ) {
  617. WWDEBUG_SAY(("ERROR: Obsolete Animated Texture detected in model: %s\r\n",context->Header.MeshName));
  618. }
  619. tex = WW3DAssetManager::Get_Instance()->Get_Texture(filename);
  620. shader.Set_Texturing(ShaderClass::TEXTURING_ENABLE);
  621. shader.Set_Dst_Blend_Func(ShaderClass::DSTBLEND_ONE);
  622. shader.Set_Src_Blend_Func(ShaderClass::SRCBLEND_ONE);
  623. shader.Set_Primary_Gradient(ShaderClass::GRADIENT_DISABLE);
  624. }
  625. }
  626. cload.Close_Chunk();
  627. }
  628. // If not texturing, move the diffuse color to ambient (simulating old behavior)
  629. if ( shader.Get_Texturing() == ShaderClass::TEXTURING_DISABLE ) {
  630. Vector3 color;
  631. vmat->Get_Diffuse( &color );
  632. vmat->Set_Ambient( color );
  633. vmat->Set_Diffuse( Vector3( 0, 0, 0 ) );
  634. }
  635. context->Add_Legacy_Material(shader,vmat,tex);
  636. vmat->Release_Ref();
  637. if (tex) tex->Release_Ref();
  638. vmat = NULL;
  639. tex = NULL;
  640. /*
  641. ** Close the W3D_CHUNK_MATERIAL3
  642. */
  643. cload.Close_Chunk();
  644. }
  645. /*
  646. ** Install the default materials to use in the absence of an array
  647. */
  648. if (context->Vertex_Material_Count() >= 1) {
  649. Set_Single_Material(context->Peek_Vertex_Material(0),0);
  650. }
  651. if (context->Texture_Count() >= 1) {
  652. Set_Single_Texture(context->Peek_Texture(0),0);
  653. }
  654. if (context->Shader_Count() >= 1) {
  655. Set_Single_Shader(context->Peek_Shader(0),0);
  656. }
  657. return WW3D_ERROR_OK;
  658. Error:
  659. return WW3D_ERROR_LOAD_FAILED;
  660. }
  661. /***********************************************************************************************
  662. * MeshModelClass::read_per_tri_materials -- read the material indices for each triangle *
  663. * *
  664. * INPUT: *
  665. * *
  666. * OUTPUT: *
  667. * *
  668. * WARNINGS: *
  669. * *
  670. * HISTORY: *
  671. * 4/7/98 GTH : Created. *
  672. *=============================================================================================*/
  673. WW3DErrorType MeshModelClass::read_per_tri_materials(ChunkLoadClass & cload,MeshLoadContextClass * context)
  674. {
  675. if (context->Header.NumMaterials == 1) return WW3D_ERROR_OK;
  676. TriIndex * polys = get_polys();
  677. bool multi_mtl = (context->Vertex_Material_Count() > 1);
  678. bool multi_tex = (context->Texture_Count() > 1);
  679. bool multi_shad = (context->Shader_Count() > 1);
  680. if (!multi_mtl) {
  681. Set_Single_Material(context->Peek_Legacy_Vertex_Material(0));
  682. }
  683. if (!multi_tex) {
  684. Set_Single_Texture(context->Peek_Legacy_Texture(0));
  685. }
  686. if (!multi_shad) {
  687. Set_Single_Shader(context->Peek_Legacy_Shader(0));
  688. }
  689. /*
  690. ** Read in each polygon's material id and assign pointer to the
  691. ** shader, texture, and vertex material as needed.
  692. */
  693. for (int i=0; i<Get_Polygon_Count(); i++) {
  694. // read in the mat id for this poly
  695. uint16 matid;
  696. if (cload.Read(&matid,sizeof(uint16)) != sizeof(uint16)) {
  697. return WW3D_ERROR_LOAD_FAILED;
  698. }
  699. if (multi_shad) {
  700. Set_Shader(i,context->Peek_Legacy_Shader(matid));
  701. }
  702. if (multi_tex) {
  703. Set_Texture(i,context->Peek_Legacy_Texture(matid));
  704. }
  705. if (multi_mtl) {
  706. Set_Material(polys[i].I,context->Peek_Legacy_Vertex_Material(matid));
  707. Set_Material(polys[i].J,context->Peek_Legacy_Vertex_Material(matid));
  708. Set_Material(polys[i].K,context->Peek_Legacy_Vertex_Material(matid));
  709. }
  710. }
  711. return WW3D_ERROR_OK;
  712. }
  713. /***********************************************************************************************
  714. * MeshModelClass::read_vertex_colors -- read in the vertex colors chunk *
  715. * *
  716. * INPUT: *
  717. * *
  718. * OUTPUT: *
  719. * *
  720. * WARNINGS: *
  721. * *
  722. * HISTORY: *
  723. * 10/28/1997 GH : Created. *
  724. * 2/16/99 GTH : Moved into MeshModelClass *
  725. *=============================================================================================*/
  726. WW3DErrorType MeshModelClass::read_vertex_colors(ChunkLoadClass & cload,MeshLoadContextClass * context)
  727. {
  728. /*
  729. ** The W3D file format supports arbitrary vertex color arrays for each pass; however since
  730. ** our conversion to hardware T&L, we only support two unique color arrays. So here is
  731. ** what is happening in this function:
  732. **
  733. ** 1 - If this is the first diffuse color array we've encountered, load the values
  734. ** 2 - Always set the DCG source for this pass to the array (vertex materials will be fixed later)
  735. **
  736. ** A side effect is that if two DCG chunks are encountered, only the first is used...
  737. */
  738. if (CurMatDesc->Has_Color_Array(0) == NULL) {
  739. W3dRGBStruct color;
  740. unsigned * dcg = Get_Color_Array(0,true);
  741. assert(dcg != NULL);
  742. for (int i=0; i<Get_Vertex_Count(); i++) {
  743. if (cload.Read(&color,sizeof(W3dRGBStruct)) != sizeof(W3dRGBStruct)) {
  744. return WW3D_ERROR_LOAD_FAILED;
  745. }
  746. Vector4 col;
  747. col.Set((float)color.R / 255.0f,(float)color.G / 255.0f,(float)color.B / 255.0f, 1.0f);
  748. dcg[i]=DX8Wrapper::Convert_Color(col);
  749. }
  750. }
  751. CurMatDesc->Set_DCG_Source(context->CurPass,VertexMaterialClass::COLOR1);
  752. return WW3D_ERROR_OK;
  753. }
  754. /***********************************************************************************************
  755. * MeshModelClass::read_material_info -- read the material info chunk *
  756. * *
  757. * INPUT: *
  758. * *
  759. * OUTPUT: *
  760. * *
  761. * WARNINGS: *
  762. * *
  763. * HISTORY: *
  764. * 2/16/99 GTH : Created. *
  765. *=============================================================================================*/
  766. WW3DErrorType MeshModelClass::read_material_info(ChunkLoadClass & cload,MeshLoadContextClass * context)
  767. {
  768. if (cload.Read(&(context->MatInfo),sizeof(W3dMaterialInfoStruct)) != sizeof(W3dMaterialInfoStruct)) {
  769. return WW3D_ERROR_LOAD_FAILED;
  770. }
  771. Set_Pass_Count(context->MatInfo.PassCount);
  772. return WW3D_ERROR_OK;
  773. }
  774. /***********************************************************************************************
  775. * MeshModelClass::read_shaders -- read the shaders chunk *
  776. * *
  777. * INPUT: *
  778. * *
  779. * OUTPUT: *
  780. * *
  781. * WARNINGS: *
  782. * *
  783. * HISTORY: *
  784. * 2/16/99 GTH : Created. *
  785. *=============================================================================================*/
  786. WW3DErrorType MeshModelClass::read_shaders(ChunkLoadClass & cload,MeshLoadContextClass * context)
  787. {
  788. W3dShaderStruct shader;
  789. for (unsigned int i=0; i<context->MatInfo.ShaderCount; i++) {
  790. if (cload.Read(&shader,sizeof(shader)) != sizeof(shader)) {
  791. return WW3D_ERROR_LOAD_FAILED;
  792. }
  793. ShaderClass newshader;
  794. W3dUtilityClass::Convert_Shader(shader,&newshader);
  795. int index = context->Add_Shader(newshader);
  796. WWASSERT(index == (int)i);
  797. }
  798. return WW3D_ERROR_OK;
  799. }
  800. /***********************************************************************************************
  801. * MeshModelClass::read_vertex_materials -- read the vertex materials chunk *
  802. * *
  803. * INPUT: *
  804. * *
  805. * OUTPUT: *
  806. * *
  807. * WARNINGS: *
  808. * *
  809. * HISTORY: *
  810. * 2/16/99 GTH : Created. *
  811. *=============================================================================================*/
  812. WW3DErrorType MeshModelClass::read_vertex_materials(ChunkLoadClass & cload,MeshLoadContextClass * context)
  813. {
  814. while (cload.Open_Chunk()) {
  815. WWASSERT(cload.Cur_Chunk_ID() == W3D_CHUNK_VERTEX_MATERIAL);
  816. VertexMaterialClass * vmat = NEW_REF(VertexMaterialClass,());
  817. WW3DErrorType error = vmat->Load_W3D(cload);
  818. if (error != WW3D_ERROR_OK) {
  819. return error;
  820. }
  821. context->Add_Vertex_Material(vmat);
  822. vmat->Release_Ref();
  823. cload.Close_Chunk();
  824. }
  825. return WW3D_ERROR_OK;
  826. }
  827. /***********************************************************************************************
  828. * MeshModelClass::read_textures -- read the textures chunk *
  829. * *
  830. * INPUT: *
  831. * *
  832. * OUTPUT: *
  833. * *
  834. * WARNINGS: *
  835. * *
  836. * HISTORY: *
  837. * 2/16/99 GTH : Created. *
  838. * 3/05/99 PDS : Broke the guts of this function into a util function in texture.cpp *
  839. *=============================================================================================*/
  840. WW3DErrorType MeshModelClass::read_textures(ChunkLoadClass & cload,MeshLoadContextClass * context)
  841. {
  842. // Keep reading textures until there are no more...
  843. for (TextureClass *newtex = ::Load_Texture (cload);
  844. newtex != NULL;
  845. newtex = ::Load_Texture (cload)) {
  846. // Add this texture to our contex and release our local hold on it
  847. context->Add_Texture(newtex);
  848. newtex->Release_Ref();
  849. }
  850. return WW3D_ERROR_OK;
  851. }
  852. /***********************************************************************************************
  853. * MeshModelClass::read_material_pass -- read a material pass chunk *
  854. * *
  855. * INPUT: *
  856. * *
  857. * OUTPUT: *
  858. * *
  859. * WARNINGS: *
  860. * *
  861. * HISTORY: *
  862. * 2/16/99 GTH : Created. *
  863. *=============================================================================================*/
  864. WW3DErrorType MeshModelClass::read_material_pass(ChunkLoadClass & cload,MeshLoadContextClass * context)
  865. {
  866. context->CurTexStage = 0;
  867. while (cload.Open_Chunk()) {
  868. WW3DErrorType error = WW3D_ERROR_OK;
  869. switch (cload.Cur_Chunk_ID()) {
  870. case W3D_CHUNK_VERTEX_MATERIAL_IDS:
  871. error = read_vertex_material_ids(cload,context);
  872. break;
  873. case W3D_CHUNK_SHADER_IDS:
  874. error = read_shader_ids(cload,context);
  875. break;
  876. case W3D_CHUNK_DCG:
  877. error = read_dcg(cload,context);
  878. break;
  879. case W3D_CHUNK_DIG:
  880. error = read_dig(cload,context);
  881. break;
  882. case W3D_CHUNK_SCG:
  883. error = read_scg(cload,context);
  884. break;
  885. case W3D_CHUNK_TEXTURE_STAGE:
  886. error = read_texture_stage(cload,context);
  887. break;
  888. };
  889. if (error != WW3D_ERROR_OK) {
  890. return error;
  891. }
  892. cload.Close_Chunk();
  893. }
  894. context->CurPass++;
  895. return WW3D_ERROR_OK;
  896. }
  897. /***********************************************************************************************
  898. * MeshModelClass::read_vertex_material_ids -- read the vmat ids for a pass *
  899. * *
  900. * INPUT: *
  901. * *
  902. * OUTPUT: *
  903. * *
  904. * WARNINGS: *
  905. * *
  906. * HISTORY: *
  907. * 2/16/99 GTH : Created. *
  908. * 9/1/2000 gth : Added alternate material desc support *
  909. *=============================================================================================*/
  910. WW3DErrorType MeshModelClass::read_vertex_material_ids(ChunkLoadClass & cload,MeshLoadContextClass * context)
  911. {
  912. /*
  913. ** Determine whether this chunk should be read into the default or alternate material description
  914. */
  915. MeshMatDescClass * matdesc = DefMatDesc;
  916. if (DefMatDesc->Has_Material_Data(context->CurPass)) {
  917. matdesc = &(context->AlternateMatDesc);
  918. }
  919. /*
  920. ** This chunk will either have a single index in it or an array of indices
  921. ** with the length equal to the vertex count.
  922. */
  923. uint32 vmat;
  924. if (cload.Cur_Chunk_Length() == 1*sizeof(uint32)) {
  925. cload.Read(&vmat,sizeof(uint32));
  926. matdesc->Set_Single_Material(context->Peek_Vertex_Material(vmat),context->CurPass);
  927. } else {
  928. for (int i=0; i<Get_Vertex_Count(); i++) {
  929. cload.Read(&vmat,sizeof(uint32));
  930. matdesc->Set_Material(i,context->Peek_Vertex_Material(vmat),context->CurPass);
  931. }
  932. }
  933. return WW3D_ERROR_OK;
  934. }
  935. /***********************************************************************************************
  936. * MeshModelClass::read_shader_ids -- read the shader indexes for a pass *
  937. * *
  938. * INPUT: *
  939. * *
  940. * OUTPUT: *
  941. * *
  942. * WARNINGS: *
  943. * *
  944. * HISTORY: *
  945. * 2/16/99 GTH : Created. *
  946. * 9/1/2000 gth : Added alternate material desc support *
  947. *=============================================================================================*/
  948. WW3DErrorType MeshModelClass::read_shader_ids(ChunkLoadClass & cload,MeshLoadContextClass * context)
  949. {
  950. /*
  951. ** Determine whether this chunk should be read into the default or alternate material description
  952. */
  953. MeshMatDescClass * matdesc = DefMatDesc;
  954. if (DefMatDesc->Has_Shader_Data(context->CurPass)) {
  955. matdesc = &(context->AlternateMatDesc);
  956. }
  957. /*
  958. ** Read in the shader id's and plug in the appropriate shader
  959. */
  960. uint32 shaderid;
  961. if (cload.Cur_Chunk_Length() == 1*sizeof(uint32)) {
  962. cload.Read(&shaderid,sizeof(shaderid));
  963. ShaderClass shader = context->Peek_Shader(shaderid);
  964. matdesc->Set_Single_Shader(shader,context->CurPass);
  965. // turn on sorting of pass 0 has non-zero dest blend (unless alpha testing on)
  966. if ( (context->CurPass == 0) &&
  967. (shader.Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO) &&
  968. (shader.Get_Alpha_Test() == ShaderClass::ALPHATEST_DISABLE) &&
  969. (SortLevel == SORT_LEVEL_NONE) )
  970. {
  971. Set_Flag(SORT,true);
  972. }
  973. } else {
  974. for (int i=0; i<Get_Polygon_Count(); i++) {
  975. cload.Read(&shaderid,sizeof(uint32));
  976. ShaderClass shader = context->Peek_Shader(shaderid);
  977. matdesc->Set_Shader(i,shader,context->CurPass);
  978. // turn on sorting of pass 0 has non-zero dest blend (unless alpha testing on)
  979. if ( (context->CurPass == 0) &&
  980. (shader.Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO) &&
  981. (shader.Get_Alpha_Test() == ShaderClass::ALPHATEST_DISABLE) &&
  982. (SortLevel == SORT_LEVEL_NONE) )
  983. {
  984. Set_Flag(SORT,true);
  985. }
  986. }
  987. }
  988. return WW3D_ERROR_OK;
  989. }
  990. /***********************************************************************************************
  991. * MeshModelClass::read_dcg -- read the per-vertex diffuse color for a pass *
  992. * *
  993. * INPUT: *
  994. * *
  995. * OUTPUT: *
  996. * *
  997. * WARNINGS: *
  998. * *
  999. * HISTORY: *
  1000. * 2/16/99 GTH : Created. *
  1001. * 9/1/2000 gth : Added alternate material desc support *
  1002. * 2/9/2001 gth : converted to handle dx8 limitations *
  1003. *=============================================================================================*/
  1004. WW3DErrorType MeshModelClass::read_dcg(ChunkLoadClass & cload,MeshLoadContextClass * context)
  1005. {
  1006. /*
  1007. ** Determine whether this chunk should be read into the default or alternate material description
  1008. */
  1009. MeshMatDescClass * matdesc = DefMatDesc;
  1010. if (DefMatDesc->Get_DCG_Source(context->CurPass) != VertexMaterialClass::MATERIAL) {
  1011. matdesc = &(context->AlternateMatDesc);
  1012. }
  1013. /*
  1014. ** The W3D file format supports arbitrary vertex color arrays for each pass; however since
  1015. ** our conversion to hardware T&L, we only support two unique color arrays. So here is
  1016. ** what is happening in this function:
  1017. **
  1018. ** 1 - If this is the first diffuse color array we've encountered, load the values.
  1019. ** 2 - Otherwise, if we are in PRELIT_VERTEX mode, put the alpha from this array into the color array.
  1020. ** 3 - Always set the DCG source for this pass to the array.
  1021. **
  1022. ** Our tools *currently* will only generate two color arrays in the case where one of them
  1023. ** is being used for alpha and the other is used for precomputed vertex lighting... This will
  1024. ** break if our tools change. The file format isn't restricting you from defining something
  1025. ** we can't render right now...
  1026. */
  1027. if (matdesc->Has_Color_Array(0) == false) {
  1028. W3dRGBAStruct color;
  1029. unsigned * dcg = matdesc->Get_Color_Array(0);
  1030. for (int i=0; i<Get_Vertex_Count(); i++) {
  1031. cload.Read(&color,sizeof(color));
  1032. Vector4 col;
  1033. W3dUtilityClass::Convert_Color(color,&col);
  1034. dcg[i]=DX8Wrapper::Convert_Color(col);
  1035. }
  1036. } else if (context->PrelitChunkID==W3D_CHUNK_PRELIT_VERTEX) {
  1037. W3dRGBAStruct color;
  1038. unsigned * dcg = matdesc->Get_Color_Array(0);
  1039. for (int i=0; i<Get_Vertex_Count(); i++) {
  1040. cload.Read(&color,sizeof(color));
  1041. Vector4 col;
  1042. col=DX8Wrapper::Convert_Color(dcg[i]);
  1043. col.W = float(color.A)/255.0f;
  1044. dcg[i]=DX8Wrapper::Convert_Color(col);
  1045. }
  1046. }
  1047. matdesc->Set_DCG_Source(context->CurPass,VertexMaterialClass::COLOR1);
  1048. return WW3D_ERROR_OK;
  1049. }
  1050. /***********************************************************************************************
  1051. * MeshModelClass::read_dig -- read the per-vertex diffuse illumination for a pass *
  1052. * *
  1053. * INPUT: *
  1054. * *
  1055. * OUTPUT: *
  1056. * *
  1057. * WARNINGS: *
  1058. * *
  1059. * HISTORY: *
  1060. * 2/16/99 GTH : Created. *
  1061. * 9/1/2000 gth : Added alternate material desc support *
  1062. * 2/9/2001 gth : converted to handle dx8 limitations *
  1063. *=============================================================================================*/
  1064. WW3DErrorType MeshModelClass::read_dig(ChunkLoadClass & cload,MeshLoadContextClass * context)
  1065. {
  1066. /*
  1067. ** Determine whether this chunk should be read into the default or alternate material description
  1068. */
  1069. MeshMatDescClass * matdesc = DefMatDesc;
  1070. if (context->Already_Loaded_DIG()) {
  1071. matdesc = &(context->AlternateMatDesc);
  1072. }
  1073. context->Notify_Loaded_DIG_Chunk(true);
  1074. /*
  1075. ** It appears that there isn't wide support for having the emissive material color source
  1076. ** be a vertex color array so when there is a pre-existing color array, I'm just multiplying
  1077. ** the DIG values into it.
  1078. */
  1079. W3dRGBAStruct color;
  1080. if (matdesc->Has_Color_Array(0) == false) {
  1081. unsigned * dcg = matdesc->Get_Color_Array(0);
  1082. for (int i=0; i<Get_Vertex_Count(); i++) {
  1083. cload.Read(&color,sizeof(color));
  1084. Vector4 col;
  1085. col.X = float(color.R)/255.0f;
  1086. col.Y = float(color.G)/255.0f;
  1087. col.Z = float(color.B)/255.0f;
  1088. col.W = 1.0f;
  1089. dcg[i]=DX8Wrapper::Convert_Color(col);
  1090. }
  1091. } else {
  1092. unsigned * dcg = matdesc->Get_Color_Array(0);
  1093. for (int i=0; i<Get_Vertex_Count(); i++) {
  1094. cload.Read(&color,sizeof(color));
  1095. Vector4 col=DX8Wrapper::Convert_Color(dcg[i]);
  1096. col.X *= float(color.R)/255.0f;
  1097. col.Y *= float(color.G)/255.0f;
  1098. col.Z *= float(color.B)/255.0f;
  1099. dcg[i]=DX8Wrapper::Convert_Color(col);
  1100. }
  1101. }
  1102. matdesc->Set_DCG_Source(context->CurPass,VertexMaterialClass::COLOR1);
  1103. return WW3D_ERROR_OK;
  1104. }
  1105. /***********************************************************************************************
  1106. * MeshModelClass::read_scg -- read the specular color for a pass *
  1107. * *
  1108. * INPUT: *
  1109. * *
  1110. * OUTPUT: *
  1111. * *
  1112. * WARNINGS: *
  1113. * *
  1114. * HISTORY: *
  1115. * 2/16/99 GTH : Created. *
  1116. * 9/1/2000 gth : Added alternate material desc support *
  1117. * 2/9/2001 gth : new dx8 code no longer supports this chunk *
  1118. *=============================================================================================*/
  1119. WW3DErrorType MeshModelClass::read_scg(ChunkLoadClass & cload,MeshLoadContextClass * context)
  1120. {
  1121. return WW3D_ERROR_OK;
  1122. }
  1123. /***********************************************************************************************
  1124. * MeshModelClass::read_texture_stage -- read texture stage chunks *
  1125. * *
  1126. * INPUT: *
  1127. * *
  1128. * OUTPUT: *
  1129. * *
  1130. * WARNINGS: *
  1131. * *
  1132. * HISTORY: *
  1133. * 2/16/99 GTH : Created. *
  1134. *=============================================================================================*/
  1135. WW3DErrorType MeshModelClass::read_texture_stage(ChunkLoadClass & cload,MeshLoadContextClass * context)
  1136. {
  1137. while (cload.Open_Chunk()) {
  1138. WW3DErrorType error = WW3D_ERROR_OK;
  1139. switch(cload.Cur_Chunk_ID()) {
  1140. case W3D_CHUNK_TEXTURE_IDS:
  1141. error = read_texture_ids(cload,context);
  1142. break;
  1143. case W3D_CHUNK_STAGE_TEXCOORDS:
  1144. case W3D_CHUNK_TEXCOORDS:
  1145. error = read_stage_texcoords(cload,context);
  1146. break;
  1147. case W3D_CHUNK_PER_FACE_TEXCOORD_IDS:
  1148. error = read_per_face_texcoord_ids (cload, context);
  1149. break;
  1150. }
  1151. if (error != WW3D_ERROR_OK) {
  1152. return error;
  1153. }
  1154. cload.Close_Chunk();
  1155. }
  1156. context->CurTexStage++;
  1157. return WW3D_ERROR_OK;
  1158. }
  1159. /***********************************************************************************************
  1160. * MeshModelClass::read_texture_ids -- read the texture ids for a pass,stage *
  1161. * *
  1162. * INPUT: *
  1163. * *
  1164. * OUTPUT: *
  1165. * *
  1166. * WARNINGS: *
  1167. * *
  1168. * HISTORY: *
  1169. * 2/16/99 GTH : Created. *
  1170. * 9/1/2000 gth : Added alternate material desc support *
  1171. *=============================================================================================*/
  1172. WW3DErrorType MeshModelClass::read_texture_ids(ChunkLoadClass & cload,MeshLoadContextClass * context)
  1173. {
  1174. uint32 texid;
  1175. int pass = context->CurPass;
  1176. int stage = context->CurTexStage;
  1177. /*
  1178. ** Determine whether this chunk should be read into the default or alternate material description
  1179. */
  1180. MeshMatDescClass * matdesc = DefMatDesc;
  1181. if (DefMatDesc->Has_Texture_Data(pass,stage)) {
  1182. matdesc = &(context->AlternateMatDesc);
  1183. }
  1184. /*
  1185. ** Read in the texture(s) array
  1186. */
  1187. if (cload.Cur_Chunk_Length() == 1*sizeof(uint32)) {
  1188. cload.Read(&texid,sizeof(texid));
  1189. matdesc->Set_Single_Texture(context->Peek_Texture(texid),pass,stage);
  1190. } else {
  1191. for (int i=0; i<Get_Polygon_Count(); i++) {
  1192. cload.Read(&texid,sizeof(uint32));
  1193. if (texid != 0xffffffff) {
  1194. matdesc->Set_Texture(i,context->Peek_Texture(texid),pass,stage);
  1195. }
  1196. }
  1197. }
  1198. return WW3D_ERROR_OK;
  1199. }
  1200. /***********************************************************************************************
  1201. * MeshModelClass::read_stage_texcoords -- read the texcoords for a pass,stage *
  1202. * *
  1203. * INPUT: *
  1204. * *
  1205. * OUTPUT: *
  1206. * *
  1207. * WARNINGS: *
  1208. * *
  1209. * HISTORY: *
  1210. * 2/16/99 GTH : Created. *
  1211. * 7/14/99 IML : Lightmap support: calculate vertex count directly from chunk size. *
  1212. * 9/1/2000 gth : Added alternate material desc support *
  1213. *=============================================================================================*/
  1214. WW3DErrorType MeshModelClass::read_stage_texcoords(ChunkLoadClass & cload,MeshLoadContextClass * context)
  1215. {
  1216. unsigned elementcount;
  1217. Vector2 *uvs;
  1218. W3dTexCoordStruct texcoord;
  1219. /*
  1220. ** Determine whether this chunk should be read into the default or alternate material description
  1221. */
  1222. MeshMatDescClass * matdesc = DefMatDesc;
  1223. if (DefMatDesc->Has_UV(context->CurPass,context->CurTexStage)) {
  1224. matdesc = &(context->AlternateMatDesc);
  1225. }
  1226. /*
  1227. ** Read in the texture coordiantes
  1228. */
  1229. elementcount = cload.Cur_Chunk_Length() / sizeof (W3dTexCoordStruct);
  1230. uvs = context->Get_Temporary_UV_Array(elementcount);
  1231. if (uvs != NULL) {
  1232. for (unsigned i = 0; i < elementcount; i++) {
  1233. cload.Read (&texcoord, sizeof (texcoord));
  1234. uvs[i].X = texcoord.U;
  1235. uvs[i].Y = 1.0f - texcoord.V;
  1236. }
  1237. }
  1238. matdesc->Install_UV_Array(context->CurPass,context->CurTexStage,uvs,elementcount);
  1239. return (WW3D_ERROR_OK);
  1240. }
  1241. /***********************************************************************************************
  1242. * MeshModelClass::read_per_face_texcoord_ids -- read uv indices for given (pass,stage). *
  1243. * *
  1244. * INPUT: *
  1245. * *
  1246. * OUTPUT: *
  1247. * *
  1248. * WARNINGS: *
  1249. * *
  1250. * HISTORY: *
  1251. * 02/02/99 IML : Created. *
  1252. * 9/1/2000 gth : Added alternate material desc support *
  1253. *=============================================================================================*/
  1254. WW3DErrorType MeshModelClass::read_per_face_texcoord_ids (ChunkLoadClass &cload, MeshLoadContextClass *context)
  1255. {
  1256. unsigned size;
  1257. /*
  1258. ** Determine whether this chunk should be read into the default or alternate material description
  1259. */
  1260. // MeshMatDescClass * matdesc = DefMatDesc;
  1261. // if (DefMatDesc->Has_UVIndex(context->CurPass)) {
  1262. // matdesc = &(context->AlternateMatDesc);
  1263. // }
  1264. /*
  1265. ** Read in the texture coordiante indices
  1266. ** There must be polygon count vectors in this chunk.
  1267. */
  1268. size = sizeof (Vector3i) * Get_Polygon_Count();
  1269. if (cload.Cur_Chunk_Length() == size) {
  1270. // Vector3i *uvindices;
  1271. //
  1272. // uvindices = matdesc->Get_UVIndex_Array (context->CurPass, true);
  1273. // WWASSERT (uvindices != NULL);
  1274. //uvindices=new Vector3i[Get_Polygon_Count()];
  1275. // cload.Read (uvindices, size);
  1276. //delete[] uvindices;
  1277. cload.Seek(size);
  1278. return (WW3D_ERROR_OK);
  1279. } else {
  1280. return (WW3D_ERROR_LOAD_FAILED);
  1281. }
  1282. }
  1283. /***********************************************************************************************
  1284. * MeshModelClass::read_prelit_material -- read prelit material chunks. *
  1285. * *
  1286. * INPUT: *
  1287. * *
  1288. * OUTPUT: *
  1289. * *
  1290. * WARNINGS: *
  1291. * *
  1292. * HISTORY: *
  1293. * 02/02/99 IML : Created. *
  1294. *=============================================================================================*/
  1295. WW3DErrorType MeshModelClass::read_prelit_material (ChunkLoadClass &cload, MeshLoadContextClass *context)
  1296. {
  1297. // If this chunk ID matches the selected prelit chunk ID then load it, otherwise skip it.
  1298. if (cload.Cur_Chunk_ID() == context->PrelitChunkID) {
  1299. // While there are chunks in the prelit material chunk wrapper...
  1300. while (cload.Open_Chunk()) {
  1301. WW3DErrorType error = WW3D_ERROR_OK;
  1302. switch (cload.Cur_Chunk_ID()) {
  1303. case W3D_CHUNK_MATERIAL_INFO:
  1304. error = read_material_info (cload,context);
  1305. break;
  1306. case W3D_CHUNK_VERTEX_MATERIALS:
  1307. error = read_vertex_materials (cload,context);
  1308. break;
  1309. case W3D_CHUNK_SHADERS:
  1310. error = read_shaders (cload,context);
  1311. break;
  1312. case W3D_CHUNK_TEXTURES:
  1313. error = read_textures (cload,context);
  1314. break;
  1315. case W3D_CHUNK_MATERIAL_PASS:
  1316. error = read_material_pass (cload,context);
  1317. break;
  1318. default:
  1319. // Unknown chunk.
  1320. break;
  1321. }
  1322. cload.Close_Chunk();
  1323. if (error != WW3D_ERROR_OK) return (error);
  1324. }
  1325. }
  1326. return (WW3D_ERROR_OK);
  1327. }
  1328. /***********************************************************************************************
  1329. * MeshModelClass::post_process -- post loading, perform any processing on this model. *
  1330. * *
  1331. * INPUT: *
  1332. * *
  1333. * OUTPUT: *
  1334. * *
  1335. * WARNINGS: *
  1336. * *
  1337. * HISTORY: *
  1338. * 05/02/00 IML : Created. *
  1339. * 7/13/2001 hy : Added static sort postprocessing *
  1340. *=============================================================================================*/
  1341. void MeshModelClass::post_process()
  1342. {
  1343. #if 0
  1344. // we want to allow this now due to usage of the static sort
  1345. // Ensure no sorting, multipass meshes (for they are abomination...)
  1346. if (DefMatDesc->Get_Pass_Count() > 1 && Get_Flag(SORT)) {
  1347. WWDEBUG_SAY(( "Turning SORT off for multipass mesh %s\n",Get_Name() ));
  1348. Set_Flag(SORT, false);
  1349. }
  1350. #endif
  1351. // turn off backface culling if the mesh is supposed to be two-sided
  1352. if (Get_Flag(MeshGeometryClass::TWO_SIDED)) {
  1353. DefMatDesc->Disable_Backface_Culling();
  1354. if (AlternateMatDesc != NULL) {
  1355. AlternateMatDesc->Disable_Backface_Culling();
  1356. }
  1357. }
  1358. // fog activation.
  1359. if (WW3DAssetManager::Get_Instance()->Get_Activate_Fog_On_Load()) {
  1360. post_process_fog();
  1361. }
  1362. // if the mesh is sorting, pick an appropriate static sort level
  1363. // if default isn't set
  1364. if (Get_Flag(SORT) && SortLevel==SORT_LEVEL_NONE && WW3D::Is_Munge_Sort_On_Load_Enabled()) {
  1365. compute_static_sort_levels();
  1366. }
  1367. }
  1368. void MeshModelClass::post_process_fog(void)
  1369. {
  1370. // If two pass...
  1371. if (DefMatDesc->Get_Pass_Count() == 2) {
  1372. // If single shader on both passes...
  1373. if (!DefMatDesc->ShaderArray[0] && !DefMatDesc->ShaderArray[1]) {
  1374. ShaderClass &shader0 = DefMatDesc->Shader [0];
  1375. ShaderClass &shader1 = DefMatDesc->Shader [1];
  1376. // Analyze the mesh to determine if it is the emissive map effect and if it is, fix it up appropriately.
  1377. bool emissive_map_effect = DefMatDesc->PassCount == 2 &&
  1378. shader0.Get_Texturing() == ShaderClass::TEXTURING_DISABLE &&
  1379. shader0.Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE &&
  1380. shader0.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ZERO &&
  1381. shader0.Get_Primary_Gradient() == ShaderClass::GRADIENT_MODULATE &&
  1382. shader0.Get_Secondary_Gradient() == ShaderClass::SECONDARY_GRADIENT_DISABLE &&
  1383. shader1.Get_Texturing() == ShaderClass::TEXTURING_ENABLE &&
  1384. shader1.Get_Src_Blend_Func() == ShaderClass::SRCBLEND_SRC_ALPHA &&
  1385. shader1.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_SRC_COLOR;
  1386. if (emissive_map_effect) {
  1387. // Change the shader/texture setting into an equivalent one which will enable setting fog
  1388. // correctly: Note that we are setting up pass 0 to have a texture now.
  1389. shader0.Set_Texturing(ShaderClass::TEXTURING_ENABLE);
  1390. shader1.Set_Dst_Blend_Func(ShaderClass::DSTBLEND_ONE);
  1391. shader0.Set_Fog_Func(ShaderClass::FOG_ENABLE);
  1392. shader1.Set_Fog_Func(ShaderClass::FOG_SCALE_FRAGMENT);
  1393. // Copy pass 1 texture/texture array to pass 0.
  1394. REF_PTR_SET (DefMatDesc->Texture[0][0], DefMatDesc->Texture [1][0]);
  1395. if (DefMatDesc->TextureArray [1][0]) {
  1396. if (!DefMatDesc->TextureArray [0][0]) {
  1397. DefMatDesc->TextureArray [0][0] = NEW_REF (TexBufferClass, (PolyCount));
  1398. for (int i = 0; i < PolyCount; i++) {
  1399. DefMatDesc->TextureArray [0][0]->Set_Element (i, DefMatDesc->TextureArray [1][0]->Peek_Element (i));
  1400. }
  1401. }
  1402. }
  1403. // Make pass 0 point to the same UV array as pass 1. If pass 1 has a vertex material
  1404. // array, we only take the first one for determining UV source. The UV source is
  1405. // used to set the UV source of all the vertex materials in pass 0.
  1406. int uv_source = 0;
  1407. if (DefMatDesc->MaterialArray[1]) {
  1408. uv_source = DefMatDesc->MaterialArray[1]->Peek_Element(0)->Get_UV_Source(0);
  1409. } else {
  1410. DefMatDesc->Material[1]->Get_UV_Source(0);
  1411. }
  1412. if (DefMatDesc->MaterialArray[0]) {
  1413. for (int i = 0; i < VertexCount; i++) {
  1414. DefMatDesc->MaterialArray[0]->Peek_Element(i)->Set_UV_Source(0, uv_source);
  1415. }
  1416. } else {
  1417. DefMatDesc->Material[0]->Set_UV_Source(0, uv_source);
  1418. }
  1419. return;
  1420. }
  1421. // Analyze the mesh to determine if it is the shiny mask effect and if it is, fix it up appropriately.
  1422. bool shiny_mask_effect = shader0.Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE &&
  1423. shader0.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ZERO &&
  1424. shader1.Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE &&
  1425. (shader1.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_SRC_ALPHA ||
  1426. shader1.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ONE_MINUS_SRC_ALPHA);
  1427. if (shiny_mask_effect) {
  1428. shader0.Set_Fog_Func(ShaderClass::FOG_SCALE_FRAGMENT);
  1429. shader1.Set_Fog_Func(ShaderClass::FOG_ENABLE);
  1430. return;
  1431. }
  1432. }
  1433. }
  1434. // Mesh is not one of the special two-pass combinations. Apply a per-pass generic fix-up.
  1435. for (int pass = 0; pass < DefMatDesc->PassCount; pass++) {
  1436. DefMatDesc->Shader [pass].Enable_Fog (Get_Name());
  1437. if (DefMatDesc->ShaderArray [pass]) {
  1438. for (int tri = 0; tri < DefMatDesc->ShaderArray [pass]->Get_Count(); tri++) {
  1439. DefMatDesc->ShaderArray [pass]->Get_Element (tri).Enable_Fog (Get_Name());
  1440. }
  1441. }
  1442. }
  1443. }
  1444. unsigned int MeshModelClass::get_sort_flags(int pass) const
  1445. {
  1446. unsigned int flags = 0;
  1447. ShaderClass::StaticSortCategoryType scat;
  1448. if (Has_Shader_Array(pass)) {
  1449. for (int tri = 0; tri < CurMatDesc->ShaderArray[pass]->Get_Count(); tri++) {
  1450. scat = CurMatDesc->ShaderArray[pass]->Get_Element(tri).Get_SS_Category();
  1451. flags |= (1 << scat);
  1452. }
  1453. } else {
  1454. scat = Get_Single_Shader(pass).Get_SS_Category();
  1455. flags |= (1 << scat);
  1456. }
  1457. return flags;
  1458. }
  1459. unsigned int MeshModelClass::get_sort_flags(void) const
  1460. {
  1461. unsigned int flags = 0;
  1462. for (int pass = 0; pass < Get_Pass_Count(); pass++) {
  1463. flags |= get_sort_flags(pass);
  1464. }
  1465. return flags;
  1466. }
  1467. void MeshModelClass::compute_static_sort_levels(void)
  1468. {
  1469. enum StaticSortCategoryBitFieldType
  1470. {
  1471. SSCAT_OPAQUE_BF = (1 << ShaderClass::SSCAT_OPAQUE),
  1472. SSCAT_ALPHA_TEST_BF = (1 << ShaderClass::SSCAT_ALPHA_TEST),
  1473. SSCAT_ADDITIVE_BF = (1 << ShaderClass::SSCAT_ADDITIVE),
  1474. SSCAT_OTHER_BF = (1 << ShaderClass::SSCAT_OTHER)
  1475. };
  1476. if (get_sort_flags(0) == SSCAT_OPAQUE_BF) {
  1477. SortLevel = SORT_LEVEL_NONE;
  1478. return;
  1479. }
  1480. switch (get_sort_flags())
  1481. {
  1482. case (SSCAT_OPAQUE_BF | SSCAT_ALPHA_TEST_BF):
  1483. SortLevel = SORT_LEVEL_NONE;
  1484. break;
  1485. case SSCAT_ADDITIVE_BF:
  1486. SortLevel = SORT_LEVEL_BIN3;
  1487. break;
  1488. case (SSCAT_ADDITIVE_BF | SSCAT_ALPHA_TEST_BF):
  1489. SortLevel = SORT_LEVEL_BIN2;
  1490. break;
  1491. default:
  1492. SortLevel = SORT_LEVEL_BIN1;
  1493. break;
  1494. };
  1495. }
  1496. void MeshModelClass::install_materials(MeshLoadContextClass * context)
  1497. {
  1498. int i;
  1499. /*
  1500. ** If alternate material chunks were loaded, initialize the AlternateMatDesc
  1501. */
  1502. install_alternate_material_desc(context);
  1503. /*
  1504. ** Finish configuring the vertex materials and color arrays.
  1505. */
  1506. bool lighting_enabled=true;
  1507. // vertex-lit models need the lighting turned off!
  1508. if (Get_Flag(MeshGeometryClass::PRELIT_VERTEX)) {
  1509. lighting_enabled=false;
  1510. }
  1511. DefMatDesc->Post_Load_Process (lighting_enabled,this);
  1512. if (AlternateMatDesc != NULL) {
  1513. AlternateMatDesc->Post_Load_Process (lighting_enabled,this);
  1514. }
  1515. /*
  1516. ** transfer the refs to our textures into the MatInfo
  1517. */
  1518. for (i=0; i<context->Texture_Count(); i++) {
  1519. MatInfo->Add_Texture(context->Peek_Texture(i));
  1520. }
  1521. /*
  1522. ** transfer the refs to our vertex materials into the MatInfo
  1523. */
  1524. for (i=0; i<context->Vertex_Material_Count(); i++) {
  1525. MatInfo->Add_Vertex_Material(context->Peek_Vertex_Material(i));
  1526. }
  1527. }
  1528. void MeshModelClass::clone_materials(const MeshModelClass & srcmesh)
  1529. {
  1530. /*
  1531. ** Copy the material info and the materials within
  1532. */
  1533. REF_PTR_RELEASE(MatInfo);
  1534. MatInfo = NEW_REF( MaterialInfoClass,(*(srcmesh.MatInfo)));
  1535. /*
  1536. ** remap!
  1537. */
  1538. MaterialRemapperClass remapper(srcmesh.MatInfo, MatInfo);
  1539. remapper.Remap_Mesh(srcmesh.CurMatDesc, CurMatDesc);
  1540. }
  1541. void MeshModelClass::install_alternate_material_desc(MeshLoadContextClass * context)
  1542. {
  1543. if (context->AlternateMatDesc.Is_Empty() == false) {
  1544. WWASSERT(AlternateMatDesc == NULL);
  1545. AlternateMatDesc = new MeshMatDescClass;
  1546. AlternateMatDesc->Init_Alternate(*DefMatDesc,context->AlternateMatDesc);
  1547. }
  1548. }
  1549. /***********************************************************************************************
  1550. * MeshLoadContextClass::MeshLoadContextClass -- constructor for MeshLoadContextClass *
  1551. * *
  1552. * INPUT: *
  1553. * *
  1554. * OUTPUT: *
  1555. * *
  1556. * WARNINGS: *
  1557. * *
  1558. * HISTORY: *
  1559. * 12/10/98 GTH : Created. *
  1560. *=============================================================================================*/
  1561. MeshLoadContextClass::MeshLoadContextClass(void)
  1562. {
  1563. memset(&Header,0,sizeof(Header));
  1564. memset(&MatInfo,0,sizeof(MatInfo));
  1565. PrelitChunkID = 0xffffffff;
  1566. CurPass = 0;
  1567. CurTexStage = 0;
  1568. TexCoords = NULL;
  1569. LoadedDIG = false;
  1570. }
  1571. /***********************************************************************************************
  1572. * MeshLoadContextClass::~MeshLoadContextClass -- destructor *
  1573. * *
  1574. * INPUT: *
  1575. * *
  1576. * OUTPUT: *
  1577. * *
  1578. * WARNINGS: *
  1579. * *
  1580. * HISTORY: *
  1581. * 12/10/98 GTH : Created. *
  1582. *=============================================================================================*/
  1583. MeshLoadContextClass::~MeshLoadContextClass(void)
  1584. {
  1585. int i;
  1586. if (TexCoords != NULL) {
  1587. delete TexCoords;
  1588. TexCoords = NULL;
  1589. }
  1590. for (i=0; i<Textures.Count(); i++) {
  1591. Textures[i]->Release_Ref();
  1592. }
  1593. for (i=0; i<VertexMaterials.Count(); i++) {
  1594. VertexMaterials[i]->Release_Ref();
  1595. }
  1596. for (i=0; i<LegacyMaterials.Count(); i++) {
  1597. delete LegacyMaterials[i];
  1598. }
  1599. }
  1600. /***********************************************************************************************
  1601. * MeshLoadContextClass::Get_Texcoord_Array -- returns the texture coordinates array *
  1602. * *
  1603. * This function mainly exists to support the obsolete version 3.0 w3d files *
  1604. * *
  1605. * INPUT: *
  1606. * *
  1607. * OUTPUT: *
  1608. * *
  1609. * WARNINGS: *
  1610. * *
  1611. * HISTORY: *
  1612. * 12/10/98 GTH : Created. *
  1613. *=============================================================================================*/
  1614. W3dTexCoordStruct * MeshLoadContextClass::Get_Texcoord_Array(void)
  1615. {
  1616. if (TexCoords == NULL) {
  1617. TexCoords = new W3dTexCoordStruct[Header.NumVertices];
  1618. }
  1619. return TexCoords;
  1620. }
  1621. /***********************************************************************************************
  1622. * MeshLoadContextClass::Add_Shader -- adds a shader to the array *
  1623. * *
  1624. * INPUT: *
  1625. * *
  1626. * OUTPUT: *
  1627. * *
  1628. * WARNINGS: *
  1629. * *
  1630. * HISTORY: *
  1631. * 12/10/98 GTH : Created. *
  1632. *=============================================================================================*/
  1633. int MeshLoadContextClass::Add_Shader(ShaderClass shader)
  1634. {
  1635. int index = Shaders.Count();
  1636. Shaders.Add(shader);
  1637. return index;
  1638. }
  1639. /***********************************************************************************************
  1640. * MeshLoadContextClass::Add_Vertex_Materail -- adds a vertex material *
  1641. * *
  1642. * INPUT: *
  1643. * *
  1644. * OUTPUT: *
  1645. * *
  1646. * WARNINGS: *
  1647. * *
  1648. * HISTORY: *
  1649. * 12/10/98 GTH : Created. *
  1650. *=============================================================================================*/
  1651. int MeshLoadContextClass::Add_Vertex_Material(VertexMaterialClass * vmat)
  1652. {
  1653. WWASSERT(vmat != NULL);
  1654. vmat->Add_Ref();
  1655. int index = VertexMaterials.Count();
  1656. VertexMaterials.Add(vmat);
  1657. return index;
  1658. }
  1659. /***********************************************************************************************
  1660. * MeshLoadContextClass::Add_Texture -- adds a texture *
  1661. * *
  1662. * INPUT: *
  1663. * *
  1664. * OUTPUT: *
  1665. * *
  1666. * WARNINGS: *
  1667. * *
  1668. * HISTORY: *
  1669. * 12/10/98 GTH : Created. *
  1670. *=============================================================================================*/
  1671. int MeshLoadContextClass::Add_Texture(TextureClass * tex)
  1672. {
  1673. WWASSERT(tex != NULL);
  1674. tex->Add_Ref();
  1675. int index = Textures.Count();
  1676. Textures.Add(tex);
  1677. return index;
  1678. }
  1679. /***********************************************************************************************
  1680. * MeshLoadContextClass::Add_Legacy_Material -- adds a legacy material *
  1681. * *
  1682. * This function will check to see if the parameters of any of the parts of the material are *
  1683. * duplicated. Only unique shaders/vertexmaterials will be actually added. *
  1684. * *
  1685. * INPUT: *
  1686. * *
  1687. * OUTPUT: *
  1688. * *
  1689. * WARNINGS: *
  1690. * *
  1691. * HISTORY: *
  1692. * 12/10/98 GTH : Created. *
  1693. *=============================================================================================*/
  1694. void MeshLoadContextClass::Add_Legacy_Material(ShaderClass shader,VertexMaterialClass * vmat,TextureClass * tex)
  1695. {
  1696. // create a new legacy material
  1697. LegacyMaterialClass * mat = new LegacyMaterialClass;
  1698. // add the shader if it is unique
  1699. for (int si=0; si<Shaders.Count(); si++) {
  1700. if (Shaders[si] == shader) break;
  1701. }
  1702. if (si == Shaders.Count()) {
  1703. mat->ShaderIdx = Add_Shader(shader);
  1704. } else {
  1705. mat->ShaderIdx = si;
  1706. }
  1707. // add the vertex material if it is unique
  1708. if (vmat == NULL) {
  1709. mat->VertexMaterialIdx = -1;
  1710. } else {
  1711. unsigned long crc = vmat->Get_CRC();
  1712. for (int vi=0; vi<VertexMaterialCrcs.Count(); vi++) {
  1713. if (VertexMaterialCrcs[vi] == crc) break;
  1714. }
  1715. if (vi == VertexMaterials.Count()) {
  1716. mat->VertexMaterialIdx = Add_Vertex_Material(vmat);
  1717. VertexMaterialCrcs.Add(crc);
  1718. WWASSERT(VertexMaterialCrcs.Count() == VertexMaterials.Count());
  1719. } else {
  1720. mat->VertexMaterialIdx = vi;
  1721. }
  1722. }
  1723. // add the texture if it is unique
  1724. if (tex == NULL) {
  1725. mat->TextureIdx = -1;
  1726. } else {
  1727. for (int ti=0; ti<Textures.Count(); ti++) {
  1728. if (Textures[ti] == tex) break;
  1729. if (stricmp(Textures[ti]->Get_Texture_Name(),tex->Get_Texture_Name()) == 0) break;
  1730. }
  1731. if (ti == Textures.Count()) {
  1732. mat->TextureIdx = Add_Texture(tex);
  1733. } else {
  1734. mat->TextureIdx = ti;
  1735. }
  1736. }
  1737. LegacyMaterials.Add(mat);
  1738. }
  1739. /***********************************************************************************************
  1740. * MeshLoadContextClass::Peek_Legacy_Shader -- returns a legacy shader *
  1741. * *
  1742. * This function does the re-indexing to go from the legacy shader index to the actual shader *
  1743. * index and then returns that shader *
  1744. * *
  1745. * INPUT: *
  1746. * *
  1747. * OUTPUT: *
  1748. * *
  1749. * WARNINGS: *
  1750. * *
  1751. * HISTORY: *
  1752. * 12/10/98 GTH : Created. *
  1753. *=============================================================================================*/
  1754. ShaderClass MeshLoadContextClass::Peek_Legacy_Shader(int legacy_material_index)
  1755. {
  1756. WWASSERT(legacy_material_index >= 0);
  1757. WWASSERT(legacy_material_index < LegacyMaterials.Count());
  1758. int si = LegacyMaterials[legacy_material_index]->ShaderIdx;
  1759. return Peek_Shader(si);
  1760. }
  1761. /***********************************************************************************************
  1762. * MeshLoadContextClass::Peek_Legacy_Vertex_Material -- returns a pointer to a legacy vertex ma*
  1763. * *
  1764. * INPUT: *
  1765. * *
  1766. * OUTPUT: *
  1767. * *
  1768. * WARNINGS: *
  1769. * *
  1770. * HISTORY: *
  1771. * 12/10/98 GTH : Created. *
  1772. *=============================================================================================*/
  1773. VertexMaterialClass * MeshLoadContextClass::Peek_Legacy_Vertex_Material(int legacy_material_index)
  1774. {
  1775. WWASSERT(legacy_material_index >= 0);
  1776. WWASSERT(legacy_material_index < LegacyMaterials.Count());
  1777. int vi = LegacyMaterials[legacy_material_index]->VertexMaterialIdx;
  1778. if (vi != -1) {
  1779. return Peek_Vertex_Material(vi);
  1780. } else {
  1781. return NULL;
  1782. }
  1783. }
  1784. /***********************************************************************************************
  1785. * MeshLoadContextClass::Peek_Legacy_Texture -- returns a pointer to a texture *
  1786. * *
  1787. * INPUT: *
  1788. * *
  1789. * OUTPUT: *
  1790. * *
  1791. * WARNINGS: *
  1792. * *
  1793. * HISTORY: *
  1794. * 12/10/98 GTH : Created. *
  1795. *=============================================================================================*/
  1796. TextureClass * MeshLoadContextClass::Peek_Legacy_Texture(int legacy_material_index)
  1797. {
  1798. WWASSERT(legacy_material_index >= 0);
  1799. WWASSERT(legacy_material_index < LegacyMaterials.Count());
  1800. int ti = LegacyMaterials[legacy_material_index]->TextureIdx;
  1801. if (ti != -1) {
  1802. return Peek_Texture(ti);
  1803. } else {
  1804. return NULL;
  1805. }
  1806. }
  1807. Vector2 * MeshLoadContextClass::Get_Temporary_UV_Array(int elementcount)
  1808. {
  1809. TempUVArray.Uninitialised_Grow(elementcount);
  1810. return &(TempUVArray[0]);
  1811. }
  1812. /***********************************************************************************************
  1813. * MeshSaveContextClass::MeshSaveContextClass -- constructor *
  1814. * *
  1815. * INPUT: *
  1816. * *
  1817. * OUTPUT: *
  1818. * *
  1819. * WARNINGS: *
  1820. * *
  1821. * HISTORY: *
  1822. *=============================================================================================*/
  1823. MeshSaveContextClass::MeshSaveContextClass(void) :
  1824. CurPass(0),
  1825. CurStage(0)
  1826. {
  1827. }
  1828. /***********************************************************************************************
  1829. * MeshSaveContextClass::~MeshSaveContextClass -- destructor *
  1830. * *
  1831. * INPUT: *
  1832. * *
  1833. * OUTPUT: *
  1834. * *
  1835. * WARNINGS: *
  1836. * *
  1837. * HISTORY: *
  1838. *=============================================================================================*/
  1839. MeshSaveContextClass::~MeshSaveContextClass(void)
  1840. {
  1841. }
  1842. #if 0 // MESH SAVING CODE HAS NOT BEEN MAINTAINED... Leaving here for future reference if we ever need it :-)
  1843. /***********************************************************************************************
  1844. * MeshModelClass::Save -- Save this mesh model! *
  1845. * *
  1846. * INPUT: *
  1847. * *
  1848. * OUTPUT: *
  1849. * *
  1850. * WARNINGS: *
  1851. * *
  1852. * HISTORY: *
  1853. * 2/16/99 GTH : Created. *
  1854. *=============================================================================================*/
  1855. WW3DErrorType MeshModelClass::Save_W3D(ChunkSaveClass & csave)
  1856. {
  1857. MeshSaveContextClass * context = new MeshSaveContextClass;
  1858. context->Materials.Collect_Materials(this);
  1859. write_chunks(csave,context);
  1860. delete context;
  1861. return WW3D_ERROR_OK;
  1862. }
  1863. WW3DErrorType MeshModelClass::write_chunks(ChunkSaveClass & csave,MeshSaveContextClass * context)
  1864. {
  1865. // write the header
  1866. write_header(csave,context);
  1867. write_user_text(csave,context);
  1868. // write the geometry
  1869. write_triangles(csave,context);
  1870. write_vertices(csave,context);
  1871. write_vertex_normals(csave,context);
  1872. write_vertex_shade_indices(csave,context);
  1873. write_vertex_influences(csave,context);
  1874. //write_cull_tree(csave);
  1875. // material stuff
  1876. write_material_info(csave,context);
  1877. write_vertex_materials(csave,context);
  1878. write_shaders(csave,context);
  1879. write_textures(csave,context);
  1880. // passes
  1881. for (int i=0; i<Get_Pass_Count(); i++) {
  1882. context->CurPass = i;
  1883. write_material_pass(csave,context);
  1884. }
  1885. return WW3D_ERROR_OK;
  1886. }
  1887. WW3DErrorType MeshModelClass::write_header(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
  1888. {
  1889. W3dMeshHeader3Struct header;
  1890. memset(&header,0,sizeof(header));
  1891. // Set names
  1892. if (MeshName) {
  1893. char * name = MeshName->Get_Array();
  1894. char * mesh_name = strchr(name,'.');
  1895. int hierarchy_name_len = 0;
  1896. if (mesh_name == NULL) {
  1897. mesh_name = name;
  1898. } else {
  1899. hierarchy_name_len = (int)mesh_name - (int)name;
  1900. mesh_name++;
  1901. }
  1902. assert( hierarchy_name_len <= W3D_NAME_LEN);
  1903. strncpy( header.MeshName, mesh_name, W3D_NAME_LEN);
  1904. strncpy( header.ContainerName, name, hierarchy_name_len);
  1905. } else {
  1906. sprintf(header.MeshName,"UnNamed");
  1907. }
  1908. header.Version = W3D_CURRENT_MESH_VERSION;
  1909. header.Attributes = W3dAttributes;
  1910. header.NumTris = Get_Polygon_Count();
  1911. header.NumVertices = Get_Vertex_Count();
  1912. header.NumMaterials = 0;
  1913. header.NumDamageStages = 0;
  1914. header.VertexChannels = W3D_VERTEX_CHANNEL_LOCATION;
  1915. if (Get_Flag(SKIN)) {
  1916. header.VertexChannels |= W3D_VERTEX_CHANNEL_BONEID;
  1917. }
  1918. header.FaceChannels = W3D_FACE_CHANNEL_FACE;
  1919. W3dUtilityClass::Convert_Vector(BoundBoxMin,&(header.Min));
  1920. W3dUtilityClass::Convert_Vector(BoundBoxMax,&(header.Max));
  1921. W3dUtilityClass::Convert_Vector(BoundSphereCenter,&header.SphCenter);
  1922. header.SphRadius = BoundSphereRadius;
  1923. csave.Begin_Chunk(W3D_CHUNK_MESH_HEADER3);
  1924. if (csave.Write(&header,sizeof(header)) != sizeof(header)) {
  1925. return WW3D_ERROR_SAVE_FAILED;
  1926. }
  1927. csave.End_Chunk();
  1928. return WW3D_ERROR_OK;
  1929. }
  1930. WW3DErrorType MeshModelClass::write_user_text(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
  1931. {
  1932. if (UserText == NULL) return WW3D_ERROR_OK;
  1933. if (strlen(UserText->Get_Array()) < 1) return WW3D_ERROR_OK;
  1934. csave.Begin_Chunk(W3D_CHUNK_MESH_USER_TEXT);
  1935. csave.Write(UserText->Get_Array(),strlen(UserText->Get_Array()) + 1);
  1936. csave.End_Chunk();
  1937. return WW3D_ERROR_OK;
  1938. }
  1939. WW3DErrorType MeshModelClass::write_triangles(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
  1940. {
  1941. if (!csave.Begin_Chunk(W3D_CHUNK_TRIANGLES)) {
  1942. return WW3D_ERROR_LOAD_FAILED;
  1943. }
  1944. TriIndex * poly_verts = Poly->Get_Array();
  1945. Vector4 * poly_eq = (PlaneEq ? PlaneEq->Get_Array() : NULL);
  1946. for (int i=0; i<Get_Polygon_Count(); i++) {
  1947. W3dTriStruct tri;
  1948. memset(&tri,0,sizeof(W3dTriStruct));
  1949. // convert each triangle into surrender format
  1950. tri.Vindex[0] = poly_verts[i].I;
  1951. tri.Vindex[1] = poly_verts[i].J;
  1952. tri.Vindex[2] = poly_verts[i].K;
  1953. if (poly_eq) {
  1954. tri.Attributes = 0;
  1955. tri.Normal.X = poly_eq[i].X;
  1956. tri.Normal.Y = poly_eq[i].Y;
  1957. tri.Normal.Z = poly_eq[i].Z;
  1958. tri.Dist = poly_eq[i].W;
  1959. } else {
  1960. Vector3 a,b,normal;
  1961. Vector3 * verts = Vertex->Get_Array();
  1962. const Vector3 & p0= verts[poly_verts[i][0]];
  1963. Vector3::Subtract(verts[poly_verts[i][1]],p0,&a);
  1964. Vector3::Subtract(verts[poly_verts[i][2]],p0,&b);
  1965. Vector3::Cross_Product(a,b,&normal);
  1966. normal.Normalize();
  1967. tri.Attributes = 0;
  1968. tri.Normal.X = normal.X;
  1969. tri.Normal.Y = normal.Y;
  1970. tri.Normal.Z = normal.Z;
  1971. tri.Dist = Vector3::Dot_Product(p0,normal);
  1972. }
  1973. if (csave.Write(&tri,sizeof(W3dTriStruct)) != sizeof(W3dTriStruct)) {
  1974. return WW3D_ERROR_SAVE_FAILED;
  1975. }
  1976. }
  1977. if (!csave.End_Chunk()) {
  1978. return WW3D_ERROR_SAVE_FAILED;
  1979. }
  1980. return WW3D_ERROR_OK;
  1981. }
  1982. WW3DErrorType MeshModelClass::write_vertices(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
  1983. {
  1984. if (!csave.Begin_Chunk(W3D_CHUNK_VERTICES)) {
  1985. return WW3D_ERROR_SAVE_FAILED;
  1986. }
  1987. WWASSERT(Get_Vertex_Count() > 0);
  1988. Vector3 * verts = Vertex->Get_Array();
  1989. for (int i=0; i<Get_Vertex_Count(); i++) {
  1990. W3dVectorStruct vert;
  1991. vert.X = verts[i].X;
  1992. vert.Y = verts[i].Y;
  1993. vert.Z = verts[i].Z;
  1994. if (csave.Write(&(vert),sizeof(W3dVectorStruct)) != sizeof(W3dVectorStruct)) {
  1995. return WW3D_ERROR_SAVE_FAILED;
  1996. }
  1997. }
  1998. if (!csave.End_Chunk()) {
  1999. return WW3D_ERROR_SAVE_FAILED;
  2000. }
  2001. return WW3D_ERROR_OK;
  2002. }
  2003. WW3DErrorType MeshModelClass::write_vertex_normals(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
  2004. {
  2005. WWASSERT( Get_Vertex_Count() > 0);
  2006. if (!csave.Begin_Chunk(W3D_CHUNK_VERTEX_NORMALS)) {
  2007. return WW3D_ERROR_SAVE_FAILED;
  2008. }
  2009. const Vector3 * verts = Get_Vertex_Normal_Array();
  2010. for (int i=0; i<Get_Vertex_Count(); i++) {
  2011. W3dVectorStruct vert;
  2012. vert.X = verts[i].X;
  2013. vert.Y = verts[i].Y;
  2014. vert.Z = verts[i].Z;
  2015. if (csave.Write(&(vert),sizeof(W3dVectorStruct)) != sizeof(W3dVectorStruct)) {
  2016. return WW3D_ERROR_SAVE_FAILED;
  2017. }
  2018. }
  2019. if (!csave.End_Chunk()) {
  2020. return WW3D_ERROR_SAVE_FAILED;
  2021. }
  2022. return WW3D_ERROR_OK;
  2023. }
  2024. WW3DErrorType MeshModelClass::write_vertex_shade_indices(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
  2025. {
  2026. WWASSERT(Get_Vertex_Count() > 0);
  2027. if (VertexShadeIdx == NULL) return WW3D_ERROR_OK;
  2028. if (!csave.Begin_Chunk(W3D_CHUNK_VERTEX_SHADE_INDICES)) {
  2029. return WW3D_ERROR_SAVE_FAILED;
  2030. }
  2031. for (int i=0; i<Get_Vertex_Count(); i++) {
  2032. uint32 idx = VertexShadeIdx->Get_Array()[i];
  2033. if (csave.Write(&idx,sizeof(idx)) != sizeof(idx)) {
  2034. return WW3D_ERROR_SAVE_FAILED;
  2035. }
  2036. }
  2037. if (!csave.End_Chunk()) {
  2038. return WW3D_ERROR_SAVE_FAILED;
  2039. }
  2040. return WW3D_ERROR_OK;
  2041. }
  2042. WW3DErrorType MeshModelClass::write_vertex_influences(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
  2043. {
  2044. WWASSERT(Get_Vertex_Count() > 0);
  2045. if (VertexBoneLink == NULL) return WW3D_ERROR_OK;
  2046. if (!csave.Begin_Chunk(W3D_CHUNK_VERTEX_INFLUENCES)) {
  2047. return WW3D_ERROR_SAVE_FAILED;
  2048. }
  2049. W3dVertInfStruct vinf;
  2050. memset(&vinf,0,sizeof(vinf));
  2051. for (int i=0; i<Get_Vertex_Count(); i++) {
  2052. vinf.BoneIdx = VertexBoneLink->Get_Array()[i];
  2053. if (csave.Write(&vinf,sizeof(vinf)) != sizeof(vinf)) {
  2054. return WW3D_ERROR_SAVE_FAILED;
  2055. }
  2056. }
  2057. if (!csave.End_Chunk()) {
  2058. return WW3D_ERROR_SAVE_FAILED;
  2059. }
  2060. return WW3D_ERROR_OK;
  2061. }
  2062. WW3DErrorType MeshModelClass::write_material_info(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2063. {
  2064. if (!csave.Begin_Chunk(W3D_CHUNK_MATERIAL_INFO)) {
  2065. return WW3D_ERROR_SAVE_FAILED;
  2066. }
  2067. W3dMaterialInfoStruct info;
  2068. memset(&info,0,sizeof(info));
  2069. info.PassCount = DefMatDesc->PassCount;
  2070. info.VertexMaterialCount = context->Materials.Get_Vertex_Material_Count();
  2071. info.TextureCount = context->Materials.Get_Texture_Count();
  2072. info.ShaderCount = context->Materials.Get_Shader_Count();
  2073. if (csave.Write(&info,sizeof(info)) != sizeof(info)) {
  2074. return WW3D_ERROR_SAVE_FAILED;
  2075. }
  2076. if (!csave.End_Chunk()) {
  2077. return WW3D_ERROR_SAVE_FAILED;
  2078. }
  2079. return WW3D_ERROR_OK;
  2080. }
  2081. WW3DErrorType MeshModelClass::write_shaders(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2082. {
  2083. if (context->Materials.Get_Shader_Count() <= 0) {
  2084. return WW3D_ERROR_OK;
  2085. }
  2086. if (!csave.Begin_Chunk(W3D_CHUNK_SHADERS)) return WW3D_ERROR_SAVE_FAILED;
  2087. for (int si=0; si<context->Materials.Get_Shader_Count(); si++) {
  2088. W3dShaderStruct file_shader;
  2089. ShaderClass shader = context->Materials.Peek_Shader(si);
  2090. W3dUtilityClass::Convert_Shader(shader,&file_shader);
  2091. if (csave.Write(&file_shader,sizeof(file_shader)) != sizeof(file_shader)) {
  2092. return WW3D_ERROR_SAVE_FAILED;
  2093. }
  2094. }
  2095. if (!csave.End_Chunk()) return WW3D_ERROR_SAVE_FAILED;
  2096. return WW3D_ERROR_OK;
  2097. }
  2098. WW3DErrorType MeshModelClass::write_vertex_materials(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2099. {
  2100. if (context->Materials.Get_Vertex_Material_Count() <= 0) return WW3D_ERROR_OK;
  2101. if (!csave.Begin_Chunk(W3D_CHUNK_VERTEX_MATERIALS)) return WW3D_ERROR_SAVE_FAILED;
  2102. for (int vi=0; vi<context->Materials.Get_Vertex_Material_Count(); vi++) {
  2103. csave.Begin_Chunk(W3D_CHUNK_VERTEX_MATERIAL);
  2104. VertexMaterialClass * vmat = context->Materials.Peek_Vertex_Material(vi);
  2105. vmat->Save_W3D(csave);
  2106. csave.End_Chunk();
  2107. }
  2108. if (!csave.End_Chunk()) return WW3D_ERROR_SAVE_FAILED;
  2109. return WW3D_ERROR_OK;
  2110. }
  2111. WW3DErrorType MeshModelClass::write_textures(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2112. {
  2113. if (context->Materials.Get_Texture_Count() <= 0) return WW3D_ERROR_OK;
  2114. if (!csave.Begin_Chunk(W3D_CHUNK_TEXTURES)) return WW3D_ERROR_SAVE_FAILED;
  2115. for (int ti=0; ti<context->Materials.Get_Texture_Count(); ti++) {
  2116. TextureClass * tex = context->Materials.Peek_Texture(ti);
  2117. Save_Texture(tex,csave);
  2118. }
  2119. if (!csave.End_Chunk()) return WW3D_ERROR_SAVE_FAILED;
  2120. return WW3D_ERROR_OK;
  2121. }
  2122. WW3DErrorType MeshModelClass::write_material_pass(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2123. {
  2124. context->CurStage = 0;
  2125. csave.Begin_Chunk(W3D_CHUNK_MATERIAL_PASS);
  2126. write_vertex_material_ids(csave,context);
  2127. write_shader_ids(csave,context);
  2128. write_dcg(csave,context);
  2129. write_dig(csave,context);
  2130. write_scg(csave,context);
  2131. write_texture_stage(csave,context);
  2132. write_texture_stage(csave,context);
  2133. csave.End_Chunk();
  2134. context->CurPass++;
  2135. return WW3D_ERROR_OK;
  2136. }
  2137. WW3DErrorType MeshModelClass::write_vertex_material_ids(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2138. {
  2139. // first check if all vertex material pointers are Null (is this legal?)
  2140. if ( (DefMatDesc->Material[context->CurPass] == NULL) &&
  2141. (DefMatDesc->MaterialArray[context->CurPass] == NULL)) return WW3D_ERROR_OK;
  2142. csave.Begin_Chunk(W3D_CHUNK_VERTEX_MATERIAL_IDS);
  2143. uint32 id = 0;
  2144. if (DefMatDesc->MaterialArray[context->CurPass] == NULL) {
  2145. id = context->Materials.Find_Vertex_Material(DefMatDesc->Material[context->CurPass]);
  2146. csave.Write(&id,sizeof(id));
  2147. } else {
  2148. VertexMaterialClass ** array = DefMatDesc->MaterialArray[context->CurPass]->Get_Array();
  2149. for (int vi=0; vi<Get_Vertex_Count(); vi++) {
  2150. id = context->Materials.Find_Vertex_Material(array[vi]);
  2151. csave.Write(&id,sizeof(id));
  2152. }
  2153. }
  2154. csave.End_Chunk();
  2155. return WW3D_ERROR_OK;
  2156. }
  2157. WW3DErrorType MeshModelClass::write_shader_ids(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2158. {
  2159. csave.Begin_Chunk(W3D_CHUNK_SHADER_IDS);
  2160. uint32 id = 0;
  2161. if (DefMatDesc->ShaderArray[context->CurPass] == NULL) {
  2162. id = context->Materials.Find_Shader(DefMatDesc->Shader[context->CurPass]);
  2163. csave.Write(&id,sizeof(id));
  2164. } else {
  2165. ShaderClass * array = DefMatDesc->ShaderArray[context->CurPass]->Get_Array();
  2166. for (int pi=0; pi<Get_Polygon_Count(); pi++) {
  2167. id = context->Materials.Find_Shader(array[pi]);
  2168. csave.Write(&id,sizeof(id));
  2169. }
  2170. }
  2171. csave.End_Chunk();
  2172. return WW3D_ERROR_OK;
  2173. }
  2174. WW3DErrorType MeshModelClass::write_scg(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2175. {
  2176. if (DefMatDesc->SCG[context->CurPass] == NULL) return WW3D_ERROR_OK;
  2177. csave.Begin_Chunk(W3D_CHUNK_SCG);
  2178. W3dRGBAStruct color;
  2179. Vector4 * color_array = DefMatDesc->SCG[context->CurPass]->Get_Array();
  2180. for (int vi=0; vi<Get_Vertex_Count(); vi++) {
  2181. W3dUtilityClass::Convert_Color(color_array[vi],&color);
  2182. csave.Write(&color,sizeof(color));
  2183. }
  2184. csave.End_Chunk();
  2185. return WW3D_ERROR_OK;
  2186. }
  2187. WW3DErrorType MeshModelClass::write_dig(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2188. {
  2189. if (DefMatDesc->DIG[context->CurPass] == NULL) return WW3D_ERROR_OK;
  2190. csave.Begin_Chunk(W3D_CHUNK_DIG);
  2191. W3dRGBStruct color;
  2192. Vector3 * color_array = DefMatDesc->DIG[context->CurPass]->Get_Array();
  2193. for (int vi=0; vi<Get_Vertex_Count(); vi++) {
  2194. W3dUtilityClass::Convert_Color(color_array[vi],&color);
  2195. csave.Write(&color,sizeof(color));
  2196. }
  2197. csave.End_Chunk();
  2198. return WW3D_ERROR_OK;
  2199. }
  2200. WW3DErrorType MeshModelClass::write_dcg(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2201. {
  2202. if (DefMatDesc->DCG[context->CurPass] == NULL) return WW3D_ERROR_OK;
  2203. csave.Begin_Chunk(W3D_CHUNK_DCG);
  2204. W3dRGBAStruct color;
  2205. Vector4 * color_array = DefMatDesc->DCG[context->CurPass]->Get_Array();
  2206. for (int vi=0; vi<Get_Vertex_Count(); vi++) {
  2207. W3dUtilityClass::Convert_Color(color_array[vi],&color);
  2208. csave.Write(&color,sizeof(color));
  2209. }
  2210. csave.End_Chunk();
  2211. return WW3D_ERROR_OK;
  2212. }
  2213. WW3DErrorType MeshModelClass::write_texture_stage(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2214. {
  2215. if ( (DefMatDesc->Texture[context->CurPass][context->CurStage] == NULL) &&
  2216. (DefMatDesc->TextureArray[context->CurPass][context->CurStage] == NULL)) return WW3D_ERROR_OK;
  2217. csave.Begin_Chunk(W3D_CHUNK_TEXTURE_STAGE);
  2218. write_texture_ids(csave,context);
  2219. write_stage_texcoords(csave,context);
  2220. csave.End_Chunk();
  2221. context->CurStage++;
  2222. return WW3D_ERROR_OK;
  2223. }
  2224. WW3DErrorType MeshModelClass::write_texture_ids(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2225. {
  2226. csave.Begin_Chunk(W3D_CHUNK_TEXTURE_IDS);
  2227. uint32 id = 0;
  2228. if (DefMatDesc->TextureArray[context->CurPass][context->CurStage] == NULL) {
  2229. id = context->Materials.Find_Texture(DefMatDesc->Texture[context->CurPass][context->CurStage]);
  2230. csave.Write(&id,sizeof(id));
  2231. } else {
  2232. srTextureIFace ** array = DefMatDesc->TextureArray[context->CurPass][context->CurStage]->Get_Array();
  2233. for (int pi=0; pi<Get_Polygon_Count(); pi++) {
  2234. id = context->Materials.Find_Texture(array[pi]);
  2235. csave.Write(&id,sizeof(id));
  2236. }
  2237. }
  2238. csave.End_Chunk();
  2239. return WW3D_ERROR_OK;
  2240. }
  2241. WW3DErrorType MeshModelClass::write_stage_texcoords(ChunkSaveClass & csave,MeshSaveContextClass * context)
  2242. {
  2243. if (DefMatDesc->UV[context->CurPass][context->CurStage] == NULL) return WW3D_ERROR_OK;
  2244. csave.Begin_Chunk(W3D_CHUNK_STAGE_TEXCOORDS);
  2245. W3dTexCoordStruct tex;
  2246. Vector2 * array = DefMatDesc->UV[context->CurPass][context->CurStage]->Get_Array();
  2247. for (int vi=0; vi<Get_Vertex_Count(); vi++) {
  2248. tex.U = array[vi].X;
  2249. tex.V = 1.0f - array[vi].Y;
  2250. csave.Write(&tex,sizeof(tex));
  2251. }
  2252. csave.End_Chunk();
  2253. return WW3D_ERROR_OK;
  2254. }
  2255. #endif // 0 (disabled mesh saving code)