| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696 |
- /*
- ** Command & Conquer Renegade(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /***********************************************************************************************
- *** 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 ***
- ***********************************************************************************************
- * *
- * Project Name : WW3D *
- * *
- * $Archive:: /Commando/Code/ww3d2/meshmdlio.cpp $*
- * *
- * Author:: Greg Hjelstrom *
- * *
- * $Modtime:: 1/18/02 3:09p $*
- * *
- * $Revision:: 27 $*
- * *
- *---------------------------------------------------------------------------------------------*
- * Functions: *
- * MeshModelClass::Load_W3D -- Load a mesh from a W3D file *
- * MeshModelClass::read_chunks -- read all of the chunks for a mesh *
- * MeshModelClass::read_vertices -- reads the vertex chunk *
- * MeshModelClass::read_texcoords -- read in the texture coordinates chunk *
- * MeshModelClass::read_vertex_normals -- reads a vertex normal chunk from the w3d file *
- * MeshModelClass::read_v3_materials -- Reads in version 3 materials. *
- * MeshModelClass::read_triangles -- read the triangles chunk *
- * MeshModelClass::read_per_tri_materials -- read the material indices for each triangle *
- * MeshModelClass::read_user_text -- read in the user text chunk *
- * MeshModelClass::read_vertex_colors -- read in the vertex colors chunk *
- * MeshModelClass::read_vertex_influences -- read in the vertex influences chunk *
- * MeshModelClass::read_vertex_shade_indices -- read the shade index chunk *
- * MeshModelClass::read_material_info -- read the material info chunk *
- * MeshModelClass::read_shaders -- read the shaders chunk *
- * MeshModelClass::read_vertex_materials -- read the vertex materials chunk *
- * MeshModelClass::read_textures -- read the textures chunk *
- * MeshModelClass::read_material_pass -- read a material pass chunk *
- * MeshModelClass::read_vertex_material_ids -- read the vmat ids for a pass *
- * MeshModelClass::read_shader_ids -- read the shader indexes for a pass *
- * MeshModelClass::read_dcg -- read the per-vertex diffuse color for a pass *
- * MeshModelClass::read_dig -- read the per-vertex diffuse illumination for a pass *
- * MeshModelClass::read_scg -- read the specular color for a pass *
- * MeshModelClass::read_texture_stage -- read texture stage chunks *
- * MeshModelClass::read_texture_ids -- read the texture ids for a pass,stage *
- * MeshModelClass::read_stage_texcoords -- read the texcoords for a pass,stage *
- * MeshModelClass::read_per_face_texcoord_ids -- read uv indices for given (pass,stage). *
- * MeshModelClass::read_prelit_material -- read prelit material chunks. *
- * MeshModelClass::read_aabtree -- loads the aabtree chunk *
- * MeshModelClass::Save -- Save this mesh model! *
- * MeshLoadContextClass::MeshLoadContextClass -- constructor for MeshLoadContextClass *
- * MeshLoadContextClass::~MeshLoadContextClass -- destructor *
- * MeshLoadContextClass::Get_Texcoord_Array -- returns the texture coordinates array *
- * MeshLoadContextClass::Add_Shader -- adds a shader to the array *
- * MeshLoadContextClass::Add_Vertex_Materail -- adds a vertex material *
- * MeshLoadContextClass::Add_Texture -- adds a texture *
- * MeshLoadContextClass::Add_Legacy_Material -- adds a legacy material *
- * MeshLoadContextClass::Peek_Legacy_Shader -- returns a legacy shader *
- * MeshLoadContextClass::Peek_Legacy_Vertex_Material -- returns a pointer to a legacy vmat *
- * MeshLoadContextClass::Peek_Legacy_Texture -- returns a pointer to a texture *
- * MeshSaveContextClass::MeshSaveContextClass -- constructor *
- * MeshSaveContextClass::~MeshSaveContextClass -- destructor *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #include "meshmdl.h"
- #include "aabtree.h"
- #include "matinfo.h"
- #include "vertmaterial.h"
- #include "shader.h"
- #include "texture.h"
- #include "chunkio.h"
- #include "w3derr.h"
- #include "w3d_file.h"
- #include "w3d_util.h"
- #include "assetmgr.h"
- #include "simplevec.h"
- #include "realcrc.h"
- #include "dx8wrapper.h"
- #include <stdio.h>
- #ifdef _UNIX
- #include "osdep/osdep.h"
- #endif
- /**
- ** MeshLoadContextClass
- ** This class is just used as a temporary scratchpad while a mesh is being
- ** loaded. In some cases, a chunk will be encountered before I have a place
- ** to plug it into the mesh model, etc. It is also used to convert from the
- ** old material format to the new one (detecting duplicated materials in the
- ** process).
- **
- ** This object will hold refs to all of the unique material objects. These
- ** objects will later be transferred into the MaterialInfo for the mesh and it
- ** will own the refs for the mesh. The load context object is destroyed once
- ** loading is complete...
- */
- class MeshLoadContextClass
- {
- private:
- MeshLoadContextClass(void);
- ~MeshLoadContextClass(void);
-
- W3dTexCoordStruct * Get_Texcoord_Array(void);
- int Add_Shader(ShaderClass shader);
- int Add_Vertex_Material(VertexMaterialClass * vmat);
- int Add_Texture(TextureClass* tex);
- ShaderClass Peek_Shader(int index) { return Shaders[index]; }
- VertexMaterialClass * Peek_Vertex_Material(int index) { return VertexMaterials[index]; }
- TextureClass * Peek_Texture(int index) { return Textures[index]; }
-
- int Shader_Count(void) { return Shaders.Count(); }
- int Vertex_Material_Count(void) { return VertexMaterials.Count(); }
- int Texture_Count(void) { return Textures.Count(); }
- /*
- ** Legacy material support.
- */
- void Add_Legacy_Material(ShaderClass shader,VertexMaterialClass * vmat,TextureClass * tex);
- ShaderClass Peek_Legacy_Shader(int legacy_material_index);
- VertexMaterialClass * Peek_Legacy_Vertex_Material(int legacy_material_index);
- TextureClass * Peek_Legacy_Texture(int legacy_material_index);
- /*
- ** Redundant UV detection support. The context provides a temporary buffer to
- ** load the uv coordinates into.
- */
- Vector2 * Get_Temporary_UV_Array(int elementcount);
- /*
- ** Currently, the only tool that creates DIG chunks is the lightmap tool. Since DX8 support
- ** for linking the emissive material color to an array seems poor, We're just going to multiply
- ** the DIG array into the DCG array (or make it the DCG array). Now, to properly support the
- ** "alternate material set", we have to know whether we've already encountered a DIG chunk so
- ** these flags provide that functionality.
- */
- void Notify_Loaded_DIG_Chunk(bool onoff = true) { LoadedDIG = onoff; }
- bool Already_Loaded_DIG(void) { return LoadedDIG; }
- private:
- struct LegacyMaterialClass
- {
- LegacyMaterialClass(void) : VertexMaterialIdx(0),ShaderIdx(0),TextureIdx(0) { }
- ~LegacyMaterialClass(void) { }
- void Set_Name(const char * name) { Name=name; }
-
- StringClass Name;
- int VertexMaterialIdx;
- int ShaderIdx;
- int TextureIdx;
- };
-
-
- W3dMeshHeader3Struct Header;
- W3dTexCoordStruct * TexCoords;
- W3dMaterialInfoStruct MatInfo;
- uint32 PrelitChunkID;
- int CurPass;
- int CurTexStage;
- DynamicVectorClass < LegacyMaterialClass * > LegacyMaterials;
- DynamicVectorClass < ShaderClass > Shaders;
- DynamicVectorClass < VertexMaterialClass * > VertexMaterials;
- DynamicVectorClass < unsigned long > VertexMaterialCrcs;
- DynamicVectorClass < TextureClass * > Textures;
- /*
- ** Alternate material data. Any alternate material data will be loaded into
- ** this MeshMatDescClass object. When loading is finished, an alternate MeshMatDescClass
- ** will be allocated in the mesh model. This MeshMatDescClass will be initialized to be
- ** identical to the default MeshMatDescClass and then any data contained in this
- ** MeshMatDescClass will replace the relevant arrays.
- */
- MeshMatDescClass AlternateMatDesc;
- SimpleVecClass<Vector2> TempUVArray;
- /*
- ** Record when we load the DIG chunk
- */
- bool LoadedDIG;
- friend class MeshClass;
- friend class MeshModelClass;
- };
- /*
- ** MeshSaveContextClass
- ** This class is used to pass information between the saving code in a mesh
- */
- class MeshSaveContextClass
- {
- public:
- MeshSaveContextClass(void);
- ~MeshSaveContextClass(void);
- int CurPass;
- int CurStage;
- MaterialCollectorClass Materials;
- };
- /***********************************************************************************************
- * MeshModelClass::Load_W3D -- Load a mesh from a W3D file *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::Load_W3D(ChunkLoadClass & cload)
- {
- MeshLoadContextClass * context = NULL;
- /*
- ** Open the first chunk, it should be the mesh header
- */
- cload.Open_Chunk();
-
- if (cload.Cur_Chunk_ID() != W3D_CHUNK_MESH_HEADER3) {
- WWDEBUG_SAY(("Old format mesh mesh, no longer supported.\n"));
- goto Error;
- }
-
- context = new MeshLoadContextClass;
- if (cload.Read(&(context->Header),sizeof(W3dMeshHeader3Struct)) != sizeof(W3dMeshHeader3Struct)) {
- goto Error;
- }
- cload.Close_Chunk();
- /*
- ** Process the header
- */
- char * tmpname;
- int namelen;
-
- Reset(context->Header.NumTris,context->Header.NumVertices,1);
-
- namelen = strlen(context->Header.ContainerName);
- namelen += strlen(context->Header.MeshName);
- namelen += 2;
- W3dAttributes = context->Header.Attributes;
- SortLevel = context->Header.SortLevel;
- tmpname = new char[namelen];
- memset(tmpname,0,namelen);
- if (strlen(context->Header.ContainerName) > 0) {
- strcpy(tmpname,context->Header.ContainerName);
- strcat(tmpname,".");
- }
- strcat(tmpname,context->Header.MeshName);
- Set_Name(tmpname);
- delete[] tmpname;
- tmpname = NULL;
- context->AlternateMatDesc.Set_Vertex_Count(VertexCount);
- context->AlternateMatDesc.Set_Polygon_Count(PolyCount);
- /*
- ** Set Bounding Info
- */
- BoundBoxMin.Set(context->Header.Min.X,context->Header.Min.Y,context->Header.Min.Z);
- BoundBoxMax.Set(context->Header.Max.X,context->Header.Max.Y,context->Header.Max.Z);
- BoundSphereCenter.Set(context->Header.SphCenter.X,context->Header.SphCenter.Y,context->Header.SphCenter.Z);
- BoundSphereRadius = context->Header.SphRadius;
- /*
- ** Flags
- */
- if (context->Header.Version >= W3D_MAKE_VERSION(4,1)) {
- int geometry_type = context->Header.Attributes & W3D_MESH_FLAG_GEOMETRY_TYPE_MASK;
- switch (geometry_type)
- {
- case W3D_MESH_FLAG_GEOMETRY_TYPE_NORMAL:
- break;
- case W3D_MESH_FLAG_GEOMETRY_TYPE_CAMERA_ALIGNED:
- Set_Flag(ALIGNED,true);
- break;
- case W3D_MESH_FLAG_GEOMETRY_TYPE_CAMERA_ORIENTED:
- Set_Flag(ORIENTED,true);
- break;
- case W3D_MESH_FLAG_GEOMETRY_TYPE_SKIN:
- Set_Flag(SKIN,true);
- Set_Flag(ALLOW_NPATCHES,true);
- break;
- }
- }
- if (context->Header.Attributes & W3D_MESH_FLAG_TWO_SIDED) {
- Set_Flag(TWO_SIDED,true);
- }
- if (context->Header.Attributes & W3D_MESH_FLAG_CAST_SHADOW) {
- Set_Flag(CAST_SHADOW,true);
- }
- if (context->Header.Attributes & W3D_MESH_FLAG_NPATCHABLE) {
- Set_Flag(ALLOW_NPATCHES,true);
- }
- // Configure the load sequence for prelighting.
- if (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_MASK) {
- // Select from the available prelit materials based on current prelit lighting mode.
- // If the model does not have the current prelit mode, select the next highest quality
- // prelit material that is available.
- switch (WW3D::Get_Prelit_Mode()) {
- case WW3D::PRELIT_MODE_LIGHTMAP_MULTI_TEXTURE:
- if (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_LIGHTMAP_MULTI_TEXTURE) {
- context->PrelitChunkID = W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_TEXTURE;
- Set_Flag (PRELIT_LIGHTMAP_MULTI_TEXTURE, true);
- break;
- }
- // Else fall thru...
- case WW3D::PRELIT_MODE_LIGHTMAP_MULTI_PASS:
- if (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_LIGHTMAP_MULTI_PASS) {
- context->PrelitChunkID = W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_PASS;
- Set_Flag (PRELIT_LIGHTMAP_MULTI_PASS, true);
- break;
- }
- // Else fall thru...
- case WW3D::PRELIT_MODE_VERTEX:
- if (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_VERTEX) {
- context->PrelitChunkID = W3D_CHUNK_PRELIT_VERTEX;
- Set_Flag (PRELIT_VERTEX, true);
- break;
- }
- // Else fall thru...
- default:
- // This prelighting option MUST be available if none of the others are available.
- WWASSERT (context->Header.Attributes & W3D_MESH_FLAG_PRELIT_UNLIT);
- context->PrelitChunkID = W3D_CHUNK_PRELIT_UNLIT;
- break;
- }
- } else {
-
- // For backwards compatibility, test for obsolete lightmap flag.
- if (context->Header.Attributes & OBSOLETE_W3D_MESH_FLAG_LIGHTMAPPED) {
- Set_Flag (PRELIT_LIGHTMAP_MULTI_PASS, true);
- }
- // Else this mesh has no prelighting.
- }
- read_chunks(cload,context);
- /*
- ** If this is a pre-3.0 mesh and it has vertex influences,
- ** fixup the bone indices to account for the new root node
- */
- if ((context->Header.Version < W3D_MAKE_VERSION(3,0)) && (Get_Flag(SKIN))) {
- uint16 * links = get_bone_links();
- WWASSERT(links);
-
- for (int bi = 0; bi < Get_Vertex_Count(); bi++) {
- links[bi] += 1;
- }
- }
- /*
- ** If this mesh is collideable and no AABTree was in the file, generate one now
- */
- if ( (((W3dAttributes & W3D_MESH_FLAG_COLLISION_TYPE_MASK) >> W3D_MESH_FLAG_COLLISION_TYPE_SHIFT) != 0) &&
- (CullTree == NULL))
- {
- Generate_Culling_Tree();
- }
- /*
- ** Transfer the materials into the MatInfo
- */
- install_materials(context);
- /*
- ** Delete the temporary LoadInfo object
- */
- delete context;
- /*
- ** Post-process the model: optimize passes, activate fog etc.
- */
- post_process();
- return WW3D_ERROR_OK;
- Error:
- return WW3D_ERROR_LOAD_FAILED;
- }
- /***********************************************************************************************
- * MeshModelClass::read_chunks -- read all of the chunks for a mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_chunks(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- /*
- ** Read in the chunk header
- ** If there are no more chunks within the mesh chunk,
- ** we are done.
- */
- while (cload.Open_Chunk()) {
- /*
- ** Process the chunk
- */
- WW3DErrorType error = WW3D_ERROR_OK;
- switch (cload.Cur_Chunk_ID()) {
- case W3D_CHUNK_VERTICES:
- // call up to MeshGeometryClass
- error = read_vertices(cload);
- break;
- case W3D_CHUNK_SURRENDER_NORMALS:
- case W3D_CHUNK_VERTEX_NORMALS:
- // call up to MeshGeometryClass
- error = read_vertex_normals(cload);
- break;
- case W3D_CHUNK_TEXCOORDS:
- error = read_texcoords(cload,context);
- break;
- case O_W3D_CHUNK_MATERIALS:
- case O_W3D_CHUNK_MATERIALS2:
- WWDEBUG_SAY(( "Obsolete material chunk encountered in mesh: %s.%s\r\n", context->Header.ContainerName,context->Header.MeshName));
- WWASSERT(0);
- break;
- case W3D_CHUNK_MATERIALS3:
- WWDEBUG_SAY(( "Obsolete material chunk encountered in mesh: %s.%s\r\n", context->Header.ContainerName,context->Header.MeshName));
- error = read_v3_materials(cload,context);
- break;
-
- case O_W3D_CHUNK_SURRENDER_TRIANGLES:
- WWASSERT_PRINT( 0, "Obsolete Triangle Chunk Encountered!\r\n" );
- break;
-
- case W3D_CHUNK_TRIANGLES:
- // call up to MeshGeometryClass
- error = read_triangles(cload);
- break;
- case W3D_CHUNK_PER_TRI_MATERIALS:
- error = read_per_tri_materials(cload,context);
- break;
- case W3D_CHUNK_MESH_USER_TEXT:
- // call up to MeshGeometryClass
- error = read_user_text(cload);
- break;
-
- case W3D_CHUNK_VERTEX_COLORS:
- error = read_vertex_colors(cload,context);
- break;
- case W3D_CHUNK_VERTEX_INFLUENCES:
- // call up to MeshGeometryClass
- error = read_vertex_influences(cload);
- break;
- case W3D_CHUNK_VERTEX_SHADE_INDICES:
- // call up to MeshGeometryClass
- error = read_vertex_shade_indices(cload);
- break;
- case W3D_CHUNK_MATERIAL_INFO:
- error = read_material_info(cload,context);
- break;
- case W3D_CHUNK_SHADERS:
- error = read_shaders(cload,context);
- break;
- case W3D_CHUNK_VERTEX_MATERIALS:
- error = read_vertex_materials(cload,context);
- break;
- case W3D_CHUNK_TEXTURES:
- error = read_textures(cload,context);
- break;
- case W3D_CHUNK_MATERIAL_PASS:
- error = read_material_pass(cload,context);
- break;
- case W3D_CHUNK_DEFORM:
- WWDEBUG_SAY(("Obsolete deform chunk encountered in mesh: %s.%s\r\n", context->Header.ContainerName,context->Header.MeshName));
- break;
-
- case W3D_CHUNK_DAMAGE:
- WWDEBUG_SAY(("Obsolete damage chunk encountered in mesh: %s.%s\r\n", context->Header.ContainerName,context->Header.MeshName));
- break;
- case W3D_CHUNK_PRELIT_UNLIT:
- case W3D_CHUNK_PRELIT_VERTEX:
- case W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_PASS:
- case W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_TEXTURE:
- read_prelit_material (cload, context);
- break;
-
- case W3D_CHUNK_AABTREE:
- // call up to MeshGeometryClass
- read_aabtree(cload);
- break;
- default:
- break;
- }
-
- cload.Close_Chunk();
- if (error != WW3D_ERROR_OK) {
- return error;
- }
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_texcoords -- read in the texture coordinates chunk *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 3/6/98 GTH : Created. *
- * 2/16/99 GTH : Moved into MeshModel *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_texcoords(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- W3dTexCoordStruct texcoord;
- Vector2 * uvarray = 0;
- int elementcount = cload.Cur_Chunk_Length() / sizeof (W3dTexCoordStruct);
- uvarray = context->Get_Temporary_UV_Array(elementcount);
- if (uvarray != NULL) {
- /*
- ** Read the uv's into the first u-v pass array
- ** NOTE: this is an obsolete function. Texture coordinates are now
- ** loaded in the pass chunks
- */
- for (int i=0; i<VertexCount; i++) {
- if (cload.Read(&(texcoord),sizeof(W3dTexCoordStruct)) != sizeof(W3dTexCoordStruct)) {
- return WW3D_ERROR_LOAD_FAILED;
- }
- uvarray[i].Set(texcoord.U,1.0f - texcoord.V);
- }
-
- DefMatDesc->Install_UV_Array(context->CurPass,context->CurTexStage,uvarray,elementcount);
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_v3_materials -- Reads in version 3 materials. *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 4/2/98 GTH : Created. *
- * 2/16/99 GTH : Moved into MeshModelClass *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_v3_materials(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- for (unsigned int mi=0; mi<context->Header.NumMaterials; mi++) {
- /*
- ** First, we expect a W3D_CHUNK_MATERIAL3 to wrap the entire material
- */
- if (!cload.Open_Chunk()) goto Error;
- if (cload.Cur_Chunk_ID() != W3D_CHUNK_MATERIAL3) goto Error;
- /*
- ** Inside the MATERIAL3 will be the following:
- **
- ** W3D_MATERIAL3_NAME - name of the material
- ** W3D_MATERIAL3_INFO - equivalent to 1.40 vertex material parameters
- ** W3D_MATERIAL3_DC_MAP - diffuse color mapping
- ** W3D_MAP3_FILENAME - filename of the texture map
- ** W3D_MAP3_INFO - animation, etc information
- ** W3D_MATERIAL3_DI_MAP - diffuse illumination map
- ** W3D_MATERIAL3_SC_MAP - specular color map
- ** W3D_MATERIAL3_SI_MAP - specular illumination map
- */
- VertexMaterialClass * vmat = NULL;
- ShaderClass shader;
- TextureClass * tex = NULL;
- char name[256];
- /*
- ** Read the material name
- */
- if (!cload.Open_Chunk()) goto Error;
- if (cload.Cur_Chunk_ID() != W3D_CHUNK_MATERIAL3_NAME) goto Error;
- cload.Read(name,cload.Cur_Chunk_Length());
- if (!cload.Close_Chunk()) goto Error;
- /*
- ** Read the vertex material parameters
- */
- if (!cload.Open_Chunk()) goto Error;
- W3dMaterial3Struct mat;
- if (cload.Cur_Chunk_ID() != W3D_CHUNK_MATERIAL3_INFO) goto Error;
- if (cload.Read(&mat,sizeof(W3dMaterial3Struct)) != sizeof(W3dMaterial3Struct)) goto Error;
- vmat = new VertexMaterialClass;
- vmat->Init_From_Material3(mat);
- vmat->Set_Name(name);
- shader.Init_From_Material3(mat);
- /*
- ** If this shader does alpha blending, the mesh must be sorted.
- */
- if (shader.Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO) {
- Set_Flag(MeshModelClass::SORT,true);
- }
-
- if (!cload.Close_Chunk()) goto Error;
- /*
- ** Look for the DC map and read it in
- */
- while (cload.Open_Chunk()) {
- if (cload.Cur_Chunk_ID() == W3D_CHUNK_MATERIAL3_DC_MAP) {
- /*
- ** Read in the texture filename
- */
- char filename[_MAX_FNAME + _MAX_EXT];
- if (!cload.Open_Chunk()) goto Error;
- if (cload.Cur_Chunk_ID() != W3D_CHUNK_MAP3_FILENAME) goto Error;
- if (cload.Cur_Chunk_Length() >= sizeof(filename)) goto Error;
- cload.Read(filename,cload.Cur_Chunk_Length());
- if (!cload.Close_Chunk()) goto Error;
- /*
- ** Read in the auxiliary map info
- */
- W3dMap3Struct mapinfo;
- if (!cload.Open_Chunk()) goto Error;
- if (cload.Cur_Chunk_ID() != W3D_CHUNK_MAP3_INFO) goto Error;
- if (cload.Read(&mapinfo,sizeof(W3dMap3Struct)) != sizeof(W3dMap3Struct)) goto Error;
- if (!cload.Close_Chunk()) goto Error;
- if ( mapinfo.FrameCount > 1 ) {
- WWDEBUG_SAY(("ERROR: Obsolete Animated Texture detected in model: %s\r\n",context->Header.MeshName));
- }
- tex = WW3DAssetManager::Get_Instance()->Get_Texture(filename);
- shader.Set_Texturing(ShaderClass::TEXTURING_ENABLE);
- } else if (cload.Cur_Chunk_ID() == W3D_CHUNK_MATERIAL3_SI_MAP) {
- Vector3 diffuse_color;
- vmat->Get_Diffuse( &diffuse_color);
- if ( diffuse_color == Vector3( 0,0,0 ) ) {
- /*
- ** Read in the texture filename
- */
- char filename[_MAX_FNAME + _MAX_EXT];
- if (!cload.Open_Chunk()) goto Error;
- if (cload.Cur_Chunk_ID() != W3D_CHUNK_MAP3_FILENAME) goto Error;
- if (cload.Cur_Chunk_Length() >= sizeof(filename)) goto Error;
- cload.Read(filename,cload.Cur_Chunk_Length());
- if (!cload.Close_Chunk()) goto Error;
- if (tex) tex->Release_Ref();
- /*
- ** Read in the auxiliary map info
- */
- W3dMap3Struct mapinfo;
- if (!cload.Open_Chunk()) goto Error;
- if (cload.Cur_Chunk_ID() != W3D_CHUNK_MAP3_INFO) goto Error;
- if (cload.Read(&mapinfo,sizeof(W3dMap3Struct)) != sizeof(W3dMap3Struct)) goto Error;
- if (!cload.Close_Chunk()) goto Error;
-
- if ( mapinfo.FrameCount > 1 ) {
- WWDEBUG_SAY(("ERROR: Obsolete Animated Texture detected in model: %s\r\n",context->Header.MeshName));
- }
- tex = WW3DAssetManager::Get_Instance()->Get_Texture(filename);
- shader.Set_Texturing(ShaderClass::TEXTURING_ENABLE);
- shader.Set_Dst_Blend_Func(ShaderClass::DSTBLEND_ONE);
- shader.Set_Src_Blend_Func(ShaderClass::SRCBLEND_ONE);
- shader.Set_Primary_Gradient(ShaderClass::GRADIENT_DISABLE);
- }
- }
- cload.Close_Chunk();
- }
- // If not texturing, move the diffuse color to ambient (simulating old behavior)
- if ( shader.Get_Texturing() == ShaderClass::TEXTURING_DISABLE ) {
- Vector3 color;
- vmat->Get_Diffuse( &color );
- vmat->Set_Ambient( color );
- vmat->Set_Diffuse( Vector3( 0, 0, 0 ) );
- }
-
- context->Add_Legacy_Material(shader,vmat,tex);
- vmat->Release_Ref();
- if (tex) tex->Release_Ref();
- vmat = NULL;
- tex = NULL;
- /*
- ** Close the W3D_CHUNK_MATERIAL3
- */
- cload.Close_Chunk();
- }
- /*
- ** Install the default materials to use in the absence of an array
- */
- if (context->Vertex_Material_Count() >= 1) {
- Set_Single_Material(context->Peek_Vertex_Material(0),0);
- }
- if (context->Texture_Count() >= 1) {
- Set_Single_Texture(context->Peek_Texture(0),0);
- }
- if (context->Shader_Count() >= 1) {
- Set_Single_Shader(context->Peek_Shader(0),0);
- }
- return WW3D_ERROR_OK;
- Error:
- return WW3D_ERROR_LOAD_FAILED;
- }
- /***********************************************************************************************
- * MeshModelClass::read_per_tri_materials -- read the material indices for each triangle *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 4/7/98 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_per_tri_materials(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- if (context->Header.NumMaterials == 1) return WW3D_ERROR_OK;
- TriIndex * polys = get_polys();
- bool multi_mtl = (context->Vertex_Material_Count() > 1);
- bool multi_tex = (context->Texture_Count() > 1);
- bool multi_shad = (context->Shader_Count() > 1);
- if (!multi_mtl) {
- Set_Single_Material(context->Peek_Legacy_Vertex_Material(0));
- }
- if (!multi_tex) {
- Set_Single_Texture(context->Peek_Legacy_Texture(0));
- }
- if (!multi_shad) {
- Set_Single_Shader(context->Peek_Legacy_Shader(0));
- }
- /*
- ** Read in each polygon's material id and assign pointer to the
- ** shader, texture, and vertex material as needed.
- */
- for (int i=0; i<Get_Polygon_Count(); i++) {
- // read in the mat id for this poly
- uint16 matid;
- if (cload.Read(&matid,sizeof(uint16)) != sizeof(uint16)) {
- return WW3D_ERROR_LOAD_FAILED;
- }
- if (multi_shad) {
- Set_Shader(i,context->Peek_Legacy_Shader(matid));
- }
- if (multi_tex) {
- Set_Texture(i,context->Peek_Legacy_Texture(matid));
- }
- if (multi_mtl) {
- Set_Material(polys[i].I,context->Peek_Legacy_Vertex_Material(matid));
- Set_Material(polys[i].J,context->Peek_Legacy_Vertex_Material(matid));
- Set_Material(polys[i].K,context->Peek_Legacy_Vertex_Material(matid));
- }
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_vertex_colors -- read in the vertex colors chunk *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 10/28/1997 GH : Created. *
- * 2/16/99 GTH : Moved into MeshModelClass *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_vertex_colors(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- /*
- ** The W3D file format supports arbitrary vertex color arrays for each pass; however since
- ** our conversion to hardware T&L, we only support two unique color arrays. So here is
- ** what is happening in this function:
- **
- ** 1 - If this is the first diffuse color array we've encountered, load the values
- ** 2 - Always set the DCG source for this pass to the array (vertex materials will be fixed later)
- **
- ** A side effect is that if two DCG chunks are encountered, only the first is used...
- */
- if (CurMatDesc->Has_Color_Array(0) == NULL) {
- W3dRGBStruct color;
- unsigned * dcg = Get_Color_Array(0,true);
- assert(dcg != NULL);
- for (int i=0; i<Get_Vertex_Count(); i++) {
- if (cload.Read(&color,sizeof(W3dRGBStruct)) != sizeof(W3dRGBStruct)) {
- return WW3D_ERROR_LOAD_FAILED;
- }
- Vector4 col;
- col.Set((float)color.R / 255.0f,(float)color.G / 255.0f,(float)color.B / 255.0f, 1.0f);
- dcg[i]=DX8Wrapper::Convert_Color(col);
- }
- }
- CurMatDesc->Set_DCG_Source(context->CurPass,VertexMaterialClass::COLOR1);
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_material_info -- read the material info chunk *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_material_info(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- if (cload.Read(&(context->MatInfo),sizeof(W3dMaterialInfoStruct)) != sizeof(W3dMaterialInfoStruct)) {
- return WW3D_ERROR_LOAD_FAILED;
- }
- Set_Pass_Count(context->MatInfo.PassCount);
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_shaders -- read the shaders chunk *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_shaders(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- W3dShaderStruct shader;
- for (unsigned int i=0; i<context->MatInfo.ShaderCount; i++) {
- if (cload.Read(&shader,sizeof(shader)) != sizeof(shader)) {
- return WW3D_ERROR_LOAD_FAILED;
- }
- ShaderClass newshader;
- W3dUtilityClass::Convert_Shader(shader,&newshader);
- int index = context->Add_Shader(newshader);
- WWASSERT(index == (int)i);
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_vertex_materials -- read the vertex materials chunk *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_vertex_materials(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- while (cload.Open_Chunk()) {
- WWASSERT(cload.Cur_Chunk_ID() == W3D_CHUNK_VERTEX_MATERIAL);
- VertexMaterialClass * vmat = NEW_REF(VertexMaterialClass,());
- WW3DErrorType error = vmat->Load_W3D(cload);
- if (error != WW3D_ERROR_OK) {
- return error;
- }
- context->Add_Vertex_Material(vmat);
- vmat->Release_Ref();
- cload.Close_Chunk();
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_textures -- read the textures chunk *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- * 3/05/99 PDS : Broke the guts of this function into a util function in texture.cpp *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_textures(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- // Keep reading textures until there are no more...
- for (TextureClass *newtex = ::Load_Texture (cload);
- newtex != NULL;
- newtex = ::Load_Texture (cload)) {
- // Add this texture to our contex and release our local hold on it
- context->Add_Texture(newtex);
- newtex->Release_Ref();
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_material_pass -- read a material pass chunk *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_material_pass(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- context->CurTexStage = 0;
- while (cload.Open_Chunk()) {
- WW3DErrorType error = WW3D_ERROR_OK;
- switch (cload.Cur_Chunk_ID()) {
- case W3D_CHUNK_VERTEX_MATERIAL_IDS:
- error = read_vertex_material_ids(cload,context);
- break;
- case W3D_CHUNK_SHADER_IDS:
- error = read_shader_ids(cload,context);
- break;
- case W3D_CHUNK_DCG:
- error = read_dcg(cload,context);
- break;
- case W3D_CHUNK_DIG:
- error = read_dig(cload,context);
- break;
- case W3D_CHUNK_SCG:
- error = read_scg(cload,context);
- break;
- case W3D_CHUNK_TEXTURE_STAGE:
- error = read_texture_stage(cload,context);
- break;
- };
- if (error != WW3D_ERROR_OK) {
- return error;
- }
- cload.Close_Chunk();
- }
- context->CurPass++;
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_vertex_material_ids -- read the vmat ids for a pass *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- * 9/1/2000 gth : Added alternate material desc support *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_vertex_material_ids(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- /*
- ** Determine whether this chunk should be read into the default or alternate material description
- */
- MeshMatDescClass * matdesc = DefMatDesc;
- if (DefMatDesc->Has_Material_Data(context->CurPass)) {
- matdesc = &(context->AlternateMatDesc);
- }
- /*
- ** This chunk will either have a single index in it or an array of indices
- ** with the length equal to the vertex count.
- */
- uint32 vmat;
- if (cload.Cur_Chunk_Length() == 1*sizeof(uint32)) {
-
- cload.Read(&vmat,sizeof(uint32));
- matdesc->Set_Single_Material(context->Peek_Vertex_Material(vmat),context->CurPass);
-
- } else {
- for (int i=0; i<Get_Vertex_Count(); i++) {
- cload.Read(&vmat,sizeof(uint32));
- matdesc->Set_Material(i,context->Peek_Vertex_Material(vmat),context->CurPass);
- }
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_shader_ids -- read the shader indexes for a pass *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- * 9/1/2000 gth : Added alternate material desc support *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_shader_ids(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- /*
- ** Determine whether this chunk should be read into the default or alternate material description
- */
- MeshMatDescClass * matdesc = DefMatDesc;
- if (DefMatDesc->Has_Shader_Data(context->CurPass)) {
- matdesc = &(context->AlternateMatDesc);
- }
- /*
- ** Read in the shader id's and plug in the appropriate shader
- */
- uint32 shaderid;
- if (cload.Cur_Chunk_Length() == 1*sizeof(uint32)) {
-
- cload.Read(&shaderid,sizeof(shaderid));
- ShaderClass shader = context->Peek_Shader(shaderid);
- matdesc->Set_Single_Shader(shader,context->CurPass);
- // turn on sorting of pass 0 has non-zero dest blend (unless alpha testing on)
- if ( (context->CurPass == 0) &&
- (shader.Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO) &&
- (shader.Get_Alpha_Test() == ShaderClass::ALPHATEST_DISABLE) &&
- (SortLevel == SORT_LEVEL_NONE) )
- {
- Set_Flag(SORT,true);
- }
-
- } else {
-
- for (int i=0; i<Get_Polygon_Count(); i++) {
- cload.Read(&shaderid,sizeof(uint32));
- ShaderClass shader = context->Peek_Shader(shaderid);
- matdesc->Set_Shader(i,shader,context->CurPass);
- // turn on sorting of pass 0 has non-zero dest blend (unless alpha testing on)
- if ( (context->CurPass == 0) &&
- (shader.Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO) &&
- (shader.Get_Alpha_Test() == ShaderClass::ALPHATEST_DISABLE) &&
- (SortLevel == SORT_LEVEL_NONE) )
- {
- Set_Flag(SORT,true);
- }
- }
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_dcg -- read the per-vertex diffuse color for a pass *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- * 9/1/2000 gth : Added alternate material desc support *
- * 2/9/2001 gth : converted to handle dx8 limitations *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_dcg(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- /*
- ** Determine whether this chunk should be read into the default or alternate material description
- */
- MeshMatDescClass * matdesc = DefMatDesc;
- if (DefMatDesc->Get_DCG_Source(context->CurPass) != VertexMaterialClass::MATERIAL) {
- matdesc = &(context->AlternateMatDesc);
- }
- /*
- ** The W3D file format supports arbitrary vertex color arrays for each pass; however since
- ** our conversion to hardware T&L, we only support two unique color arrays. So here is
- ** what is happening in this function:
- **
- ** 1 - If this is the first diffuse color array we've encountered, load the values.
- ** 2 - Otherwise, if we are in PRELIT_VERTEX mode, put the alpha from this array into the color array.
- ** 3 - Always set the DCG source for this pass to the array.
- **
- ** Our tools *currently* will only generate two color arrays in the case where one of them
- ** is being used for alpha and the other is used for precomputed vertex lighting... This will
- ** break if our tools change. The file format isn't restricting you from defining something
- ** we can't render right now...
- */
- if (matdesc->Has_Color_Array(0) == false) {
- W3dRGBAStruct color;
- unsigned * dcg = matdesc->Get_Color_Array(0);
- for (int i=0; i<Get_Vertex_Count(); i++) {
- cload.Read(&color,sizeof(color));
- Vector4 col;
- W3dUtilityClass::Convert_Color(color,&col);
- dcg[i]=DX8Wrapper::Convert_Color(col);
- }
- } else if (context->PrelitChunkID==W3D_CHUNK_PRELIT_VERTEX) {
-
- W3dRGBAStruct color;
- unsigned * dcg = matdesc->Get_Color_Array(0);
- for (int i=0; i<Get_Vertex_Count(); i++) {
- cload.Read(&color,sizeof(color));
- Vector4 col;
- col=DX8Wrapper::Convert_Color(dcg[i]);
- col.W = float(color.A)/255.0f;
- dcg[i]=DX8Wrapper::Convert_Color(col);
- }
- }
- matdesc->Set_DCG_Source(context->CurPass,VertexMaterialClass::COLOR1);
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_dig -- read the per-vertex diffuse illumination for a pass *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- * 9/1/2000 gth : Added alternate material desc support *
- * 2/9/2001 gth : converted to handle dx8 limitations *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_dig(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- /*
- ** Determine whether this chunk should be read into the default or alternate material description
- */
- MeshMatDescClass * matdesc = DefMatDesc;
- if (context->Already_Loaded_DIG()) {
- matdesc = &(context->AlternateMatDesc);
- }
- context->Notify_Loaded_DIG_Chunk(true);
- /*
- ** It appears that there isn't wide support for having the emissive material color source
- ** be a vertex color array so when there is a pre-existing color array, I'm just multiplying
- ** the DIG values into it.
- */
- W3dRGBAStruct color;
- if (matdesc->Has_Color_Array(0) == false) {
- unsigned * dcg = matdesc->Get_Color_Array(0);
- for (int i=0; i<Get_Vertex_Count(); i++) {
- cload.Read(&color,sizeof(color));
- Vector4 col;
- col.X = float(color.R)/255.0f;
- col.Y = float(color.G)/255.0f;
- col.Z = float(color.B)/255.0f;
- col.W = 1.0f;
- dcg[i]=DX8Wrapper::Convert_Color(col);
- }
- } else {
- unsigned * dcg = matdesc->Get_Color_Array(0);
- for (int i=0; i<Get_Vertex_Count(); i++) {
- cload.Read(&color,sizeof(color));
- Vector4 col=DX8Wrapper::Convert_Color(dcg[i]);
- col.X *= float(color.R)/255.0f;
- col.Y *= float(color.G)/255.0f;
- col.Z *= float(color.B)/255.0f;
- dcg[i]=DX8Wrapper::Convert_Color(col);
- }
- }
- matdesc->Set_DCG_Source(context->CurPass,VertexMaterialClass::COLOR1);
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_scg -- read the specular color for a pass *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- * 9/1/2000 gth : Added alternate material desc support *
- * 2/9/2001 gth : new dx8 code no longer supports this chunk *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_scg(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_texture_stage -- read texture stage chunks *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_texture_stage(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- while (cload.Open_Chunk()) {
- WW3DErrorType error = WW3D_ERROR_OK;
-
- switch(cload.Cur_Chunk_ID()) {
- case W3D_CHUNK_TEXTURE_IDS:
- error = read_texture_ids(cload,context);
- break;
- case W3D_CHUNK_STAGE_TEXCOORDS:
- case W3D_CHUNK_TEXCOORDS:
- error = read_stage_texcoords(cload,context);
- break;
- case W3D_CHUNK_PER_FACE_TEXCOORD_IDS:
- error = read_per_face_texcoord_ids (cload, context);
- break;
- }
- if (error != WW3D_ERROR_OK) {
- return error;
- }
- cload.Close_Chunk();
- }
- context->CurTexStage++;
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_texture_ids -- read the texture ids for a pass,stage *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- * 9/1/2000 gth : Added alternate material desc support *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_texture_ids(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- uint32 texid;
- int pass = context->CurPass;
- int stage = context->CurTexStage;
- /*
- ** Determine whether this chunk should be read into the default or alternate material description
- */
- MeshMatDescClass * matdesc = DefMatDesc;
- if (DefMatDesc->Has_Texture_Data(pass,stage)) {
- matdesc = &(context->AlternateMatDesc);
- }
- /*
- ** Read in the texture(s) array
- */
- if (cload.Cur_Chunk_Length() == 1*sizeof(uint32)) {
- cload.Read(&texid,sizeof(texid));
- matdesc->Set_Single_Texture(context->Peek_Texture(texid),pass,stage);
- } else {
- for (int i=0; i<Get_Polygon_Count(); i++) {
- cload.Read(&texid,sizeof(uint32));
- if (texid != 0xffffffff) {
- matdesc->Set_Texture(i,context->Peek_Texture(texid),pass,stage);
- }
- }
- }
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshModelClass::read_stage_texcoords -- read the texcoords for a pass,stage *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- * 7/14/99 IML : Lightmap support: calculate vertex count directly from chunk size. *
- * 9/1/2000 gth : Added alternate material desc support *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_stage_texcoords(ChunkLoadClass & cload,MeshLoadContextClass * context)
- {
- unsigned elementcount;
- Vector2 *uvs;
- W3dTexCoordStruct texcoord;
-
- /*
- ** Determine whether this chunk should be read into the default or alternate material description
- */
- MeshMatDescClass * matdesc = DefMatDesc;
- if (DefMatDesc->Has_UV(context->CurPass,context->CurTexStage)) {
- matdesc = &(context->AlternateMatDesc);
- }
- /*
- ** Read in the texture coordiantes
- */
- elementcount = cload.Cur_Chunk_Length() / sizeof (W3dTexCoordStruct);
- uvs = context->Get_Temporary_UV_Array(elementcount);
-
- if (uvs != NULL) {
- for (unsigned i = 0; i < elementcount; i++) {
- cload.Read (&texcoord, sizeof (texcoord));
- uvs[i].X = texcoord.U;
- uvs[i].Y = 1.0f - texcoord.V;
- }
- }
- matdesc->Install_UV_Array(context->CurPass,context->CurTexStage,uvs,elementcount);
- return (WW3D_ERROR_OK);
- }
-
- /***********************************************************************************************
- * MeshModelClass::read_per_face_texcoord_ids -- read uv indices for given (pass,stage). *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 02/02/99 IML : Created. *
- * 9/1/2000 gth : Added alternate material desc support *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_per_face_texcoord_ids (ChunkLoadClass &cload, MeshLoadContextClass *context)
- {
- unsigned size;
-
- /*
- ** Determine whether this chunk should be read into the default or alternate material description
- */
- // MeshMatDescClass * matdesc = DefMatDesc;
- // if (DefMatDesc->Has_UVIndex(context->CurPass)) {
- // matdesc = &(context->AlternateMatDesc);
- // }
- /*
- ** Read in the texture coordiante indices
- ** There must be polygon count vectors in this chunk.
- */
- size = sizeof (Vector3i) * Get_Polygon_Count();
- if (cload.Cur_Chunk_Length() == size) {
-
- // Vector3i *uvindices;
- //
- // uvindices = matdesc->Get_UVIndex_Array (context->CurPass, true);
- // WWASSERT (uvindices != NULL);
- //uvindices=new Vector3i[Get_Polygon_Count()];
- // cload.Read (uvindices, size);
- //delete[] uvindices;
- cload.Seek(size);
- return (WW3D_ERROR_OK);
-
- } else {
- return (WW3D_ERROR_LOAD_FAILED);
- }
- }
- /***********************************************************************************************
- * MeshModelClass::read_prelit_material -- read prelit material chunks. *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 02/02/99 IML : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::read_prelit_material (ChunkLoadClass &cload, MeshLoadContextClass *context)
- {
- // If this chunk ID matches the selected prelit chunk ID then load it, otherwise skip it.
- if (cload.Cur_Chunk_ID() == context->PrelitChunkID) {
-
- // While there are chunks in the prelit material chunk wrapper...
- while (cload.Open_Chunk()) {
- WW3DErrorType error = WW3D_ERROR_OK;
- switch (cload.Cur_Chunk_ID()) {
- case W3D_CHUNK_MATERIAL_INFO:
- error = read_material_info (cload,context);
- break;
- case W3D_CHUNK_VERTEX_MATERIALS:
- error = read_vertex_materials (cload,context);
- break;
- case W3D_CHUNK_SHADERS:
- error = read_shaders (cload,context);
- break;
- case W3D_CHUNK_TEXTURES:
- error = read_textures (cload,context);
- break;
- case W3D_CHUNK_MATERIAL_PASS:
- error = read_material_pass (cload,context);
- break;
- default:
-
- // Unknown chunk.
- break;
- }
- cload.Close_Chunk();
- if (error != WW3D_ERROR_OK) return (error);
- }
- }
-
- return (WW3D_ERROR_OK);
- }
- /***********************************************************************************************
- * MeshModelClass::post_process -- post loading, perform any processing on this model. *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 05/02/00 IML : Created. *
- * 7/13/2001 hy : Added static sort postprocessing *
- *=============================================================================================*/
- void MeshModelClass::post_process()
- {
- #if 0
- // we want to allow this now due to usage of the static sort
- // Ensure no sorting, multipass meshes (for they are abomination...)
- if (DefMatDesc->Get_Pass_Count() > 1 && Get_Flag(SORT)) {
- WWDEBUG_SAY(( "Turning SORT off for multipass mesh %s\n",Get_Name() ));
- Set_Flag(SORT, false);
- }
- #endif
- // turn off backface culling if the mesh is supposed to be two-sided
- if (Get_Flag(MeshGeometryClass::TWO_SIDED)) {
- DefMatDesc->Disable_Backface_Culling();
- if (AlternateMatDesc != NULL) {
- AlternateMatDesc->Disable_Backface_Culling();
- }
- }
- // fog activation.
- if (WW3DAssetManager::Get_Instance()->Get_Activate_Fog_On_Load()) {
- post_process_fog();
- }
- // if the mesh is sorting, pick an appropriate static sort level
- // if default isn't set
- if (Get_Flag(SORT) && SortLevel==SORT_LEVEL_NONE && WW3D::Is_Munge_Sort_On_Load_Enabled()) {
- compute_static_sort_levels();
- }
- }
- void MeshModelClass::post_process_fog(void)
- {
- // If two pass...
- if (DefMatDesc->Get_Pass_Count() == 2) {
- // If single shader on both passes...
- if (!DefMatDesc->ShaderArray[0] && !DefMatDesc->ShaderArray[1]) {
- ShaderClass &shader0 = DefMatDesc->Shader [0];
- ShaderClass &shader1 = DefMatDesc->Shader [1];
- // Analyze the mesh to determine if it is the emissive map effect and if it is, fix it up appropriately.
- bool emissive_map_effect = DefMatDesc->PassCount == 2 &&
- shader0.Get_Texturing() == ShaderClass::TEXTURING_DISABLE &&
- shader0.Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE &&
- shader0.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ZERO &&
- shader0.Get_Primary_Gradient() == ShaderClass::GRADIENT_MODULATE &&
- shader0.Get_Secondary_Gradient() == ShaderClass::SECONDARY_GRADIENT_DISABLE &&
- shader1.Get_Texturing() == ShaderClass::TEXTURING_ENABLE &&
- shader1.Get_Src_Blend_Func() == ShaderClass::SRCBLEND_SRC_ALPHA &&
- shader1.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_SRC_COLOR;
- if (emissive_map_effect) {
- // Change the shader/texture setting into an equivalent one which will enable setting fog
- // correctly: Note that we are setting up pass 0 to have a texture now.
- shader0.Set_Texturing(ShaderClass::TEXTURING_ENABLE);
- shader1.Set_Dst_Blend_Func(ShaderClass::DSTBLEND_ONE);
- shader0.Set_Fog_Func(ShaderClass::FOG_ENABLE);
- shader1.Set_Fog_Func(ShaderClass::FOG_SCALE_FRAGMENT);
- // Copy pass 1 texture/texture array to pass 0.
- REF_PTR_SET (DefMatDesc->Texture[0][0], DefMatDesc->Texture [1][0]);
- if (DefMatDesc->TextureArray [1][0]) {
- if (!DefMatDesc->TextureArray [0][0]) {
- DefMatDesc->TextureArray [0][0] = NEW_REF (TexBufferClass, (PolyCount));
- for (int i = 0; i < PolyCount; i++) {
- DefMatDesc->TextureArray [0][0]->Set_Element (i, DefMatDesc->TextureArray [1][0]->Peek_Element (i));
- }
- }
- }
- // Make pass 0 point to the same UV array as pass 1. If pass 1 has a vertex material
- // array, we only take the first one for determining UV source. The UV source is
- // used to set the UV source of all the vertex materials in pass 0.
- int uv_source = 0;
- if (DefMatDesc->MaterialArray[1]) {
- uv_source = DefMatDesc->MaterialArray[1]->Peek_Element(0)->Get_UV_Source(0);
- } else {
- DefMatDesc->Material[1]->Get_UV_Source(0);
- }
- if (DefMatDesc->MaterialArray[0]) {
- for (int i = 0; i < VertexCount; i++) {
- DefMatDesc->MaterialArray[0]->Peek_Element(i)->Set_UV_Source(0, uv_source);
- }
- } else {
- DefMatDesc->Material[0]->Set_UV_Source(0, uv_source);
- }
- return;
- }
-
- // Analyze the mesh to determine if it is the shiny mask effect and if it is, fix it up appropriately.
- bool shiny_mask_effect = shader0.Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE &&
- shader0.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ZERO &&
- shader1.Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE &&
- (shader1.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_SRC_ALPHA ||
- shader1.Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ONE_MINUS_SRC_ALPHA);
- if (shiny_mask_effect) {
- shader0.Set_Fog_Func(ShaderClass::FOG_SCALE_FRAGMENT);
- shader1.Set_Fog_Func(ShaderClass::FOG_ENABLE);
- return;
- }
- }
- }
-
- // Mesh is not one of the special two-pass combinations. Apply a per-pass generic fix-up.
- for (int pass = 0; pass < DefMatDesc->PassCount; pass++) {
- DefMatDesc->Shader [pass].Enable_Fog (Get_Name());
- if (DefMatDesc->ShaderArray [pass]) {
- for (int tri = 0; tri < DefMatDesc->ShaderArray [pass]->Get_Count(); tri++) {
- DefMatDesc->ShaderArray [pass]->Get_Element (tri).Enable_Fog (Get_Name());
- }
- }
- }
- }
- unsigned int MeshModelClass::get_sort_flags(int pass) const
- {
- unsigned int flags = 0;
- ShaderClass::StaticSortCategoryType scat;
- if (Has_Shader_Array(pass)) {
- for (int tri = 0; tri < CurMatDesc->ShaderArray[pass]->Get_Count(); tri++) {
- scat = CurMatDesc->ShaderArray[pass]->Get_Element(tri).Get_SS_Category();
- flags |= (1 << scat);
- }
- } else {
- scat = Get_Single_Shader(pass).Get_SS_Category();
- flags |= (1 << scat);
- }
- return flags;
- }
- unsigned int MeshModelClass::get_sort_flags(void) const
- {
- unsigned int flags = 0;
- for (int pass = 0; pass < Get_Pass_Count(); pass++) {
- flags |= get_sort_flags(pass);
- }
- return flags;
- }
- void MeshModelClass::compute_static_sort_levels(void)
- {
- enum StaticSortCategoryBitFieldType
- {
- SSCAT_OPAQUE_BF = (1 << ShaderClass::SSCAT_OPAQUE),
- SSCAT_ALPHA_TEST_BF = (1 << ShaderClass::SSCAT_ALPHA_TEST),
- SSCAT_ADDITIVE_BF = (1 << ShaderClass::SSCAT_ADDITIVE),
- SSCAT_OTHER_BF = (1 << ShaderClass::SSCAT_OTHER)
- };
- if (get_sort_flags(0) == SSCAT_OPAQUE_BF) {
- SortLevel = SORT_LEVEL_NONE;
- return;
- }
- switch (get_sort_flags())
- {
- case (SSCAT_OPAQUE_BF | SSCAT_ALPHA_TEST_BF):
- SortLevel = SORT_LEVEL_NONE;
- break;
- case SSCAT_ADDITIVE_BF:
- SortLevel = SORT_LEVEL_BIN3;
- break;
- case (SSCAT_ADDITIVE_BF | SSCAT_ALPHA_TEST_BF):
- SortLevel = SORT_LEVEL_BIN2;
- break;
-
- default:
- SortLevel = SORT_LEVEL_BIN1;
- break;
- };
- }
- void MeshModelClass::install_materials(MeshLoadContextClass * context)
- {
- int i;
- /*
- ** If alternate material chunks were loaded, initialize the AlternateMatDesc
- */
- install_alternate_material_desc(context);
-
- /*
- ** Finish configuring the vertex materials and color arrays.
- */
- bool lighting_enabled=true;
- // vertex-lit models need the lighting turned off!
- if (Get_Flag(MeshGeometryClass::PRELIT_VERTEX)) {
- lighting_enabled=false;
- }
- DefMatDesc->Post_Load_Process (lighting_enabled,this);
- if (AlternateMatDesc != NULL) {
- AlternateMatDesc->Post_Load_Process (lighting_enabled,this);
- }
- /*
- ** transfer the refs to our textures into the MatInfo
- */
- for (i=0; i<context->Texture_Count(); i++) {
- MatInfo->Add_Texture(context->Peek_Texture(i));
- }
- /*
- ** transfer the refs to our vertex materials into the MatInfo
- */
- for (i=0; i<context->Vertex_Material_Count(); i++) {
- MatInfo->Add_Vertex_Material(context->Peek_Vertex_Material(i));
- }
- }
- void MeshModelClass::clone_materials(const MeshModelClass & srcmesh)
- {
- /*
- ** Copy the material info and the materials within
- */
- REF_PTR_RELEASE(MatInfo);
- MatInfo = NEW_REF( MaterialInfoClass,(*(srcmesh.MatInfo)));
- /*
- ** remap!
- */
- MaterialRemapperClass remapper(srcmesh.MatInfo, MatInfo);
- remapper.Remap_Mesh(srcmesh.CurMatDesc, CurMatDesc);
- }
- void MeshModelClass::install_alternate_material_desc(MeshLoadContextClass * context)
- {
- if (context->AlternateMatDesc.Is_Empty() == false) {
- WWASSERT(AlternateMatDesc == NULL);
- AlternateMatDesc = new MeshMatDescClass;
- AlternateMatDesc->Init_Alternate(*DefMatDesc,context->AlternateMatDesc);
- }
- }
- /***********************************************************************************************
- * MeshLoadContextClass::MeshLoadContextClass -- constructor for MeshLoadContextClass *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- MeshLoadContextClass::MeshLoadContextClass(void)
- {
- memset(&Header,0,sizeof(Header));
- memset(&MatInfo,0,sizeof(MatInfo));
- PrelitChunkID = 0xffffffff;
- CurPass = 0;
- CurTexStage = 0;
- TexCoords = NULL;
- LoadedDIG = false;
- }
- /***********************************************************************************************
- * MeshLoadContextClass::~MeshLoadContextClass -- destructor *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- MeshLoadContextClass::~MeshLoadContextClass(void)
- {
- int i;
- if (TexCoords != NULL) {
- delete TexCoords;
- TexCoords = NULL;
- }
- for (i=0; i<Textures.Count(); i++) {
- Textures[i]->Release_Ref();
- }
- for (i=0; i<VertexMaterials.Count(); i++) {
- VertexMaterials[i]->Release_Ref();
- }
- for (i=0; i<LegacyMaterials.Count(); i++) {
- delete LegacyMaterials[i];
- }
- }
- /***********************************************************************************************
- * MeshLoadContextClass::Get_Texcoord_Array -- returns the texture coordinates array *
- * *
- * This function mainly exists to support the obsolete version 3.0 w3d files *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- W3dTexCoordStruct * MeshLoadContextClass::Get_Texcoord_Array(void)
- {
- if (TexCoords == NULL) {
- TexCoords = new W3dTexCoordStruct[Header.NumVertices];
- }
- return TexCoords;
- }
- /***********************************************************************************************
- * MeshLoadContextClass::Add_Shader -- adds a shader to the array *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- int MeshLoadContextClass::Add_Shader(ShaderClass shader)
- {
- int index = Shaders.Count();
- Shaders.Add(shader);
- return index;
- }
- /***********************************************************************************************
- * MeshLoadContextClass::Add_Vertex_Materail -- adds a vertex material *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- int MeshLoadContextClass::Add_Vertex_Material(VertexMaterialClass * vmat)
- {
- WWASSERT(vmat != NULL);
- vmat->Add_Ref();
- int index = VertexMaterials.Count();
- VertexMaterials.Add(vmat);
- return index;
- }
- /***********************************************************************************************
- * MeshLoadContextClass::Add_Texture -- adds a texture *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- int MeshLoadContextClass::Add_Texture(TextureClass * tex)
- {
- WWASSERT(tex != NULL);
- tex->Add_Ref();
- int index = Textures.Count();
- Textures.Add(tex);
- return index;
- }
- /***********************************************************************************************
- * MeshLoadContextClass::Add_Legacy_Material -- adds a legacy material *
- * *
- * This function will check to see if the parameters of any of the parts of the material are *
- * duplicated. Only unique shaders/vertexmaterials will be actually added. *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- void MeshLoadContextClass::Add_Legacy_Material(ShaderClass shader,VertexMaterialClass * vmat,TextureClass * tex)
- {
- // create a new legacy material
- LegacyMaterialClass * mat = new LegacyMaterialClass;
- // add the shader if it is unique
- for (int si=0; si<Shaders.Count(); si++) {
- if (Shaders[si] == shader) break;
- }
- if (si == Shaders.Count()) {
- mat->ShaderIdx = Add_Shader(shader);
- } else {
- mat->ShaderIdx = si;
- }
- // add the vertex material if it is unique
- if (vmat == NULL) {
- mat->VertexMaterialIdx = -1;
- } else {
- unsigned long crc = vmat->Get_CRC();
- for (int vi=0; vi<VertexMaterialCrcs.Count(); vi++) {
- if (VertexMaterialCrcs[vi] == crc) break;
- }
- if (vi == VertexMaterials.Count()) {
- mat->VertexMaterialIdx = Add_Vertex_Material(vmat);
- VertexMaterialCrcs.Add(crc);
- WWASSERT(VertexMaterialCrcs.Count() == VertexMaterials.Count());
- } else {
- mat->VertexMaterialIdx = vi;
- }
- }
- // add the texture if it is unique
- if (tex == NULL) {
- mat->TextureIdx = -1;
- } else {
- for (int ti=0; ti<Textures.Count(); ti++) {
- if (Textures[ti] == tex) break;
- if (stricmp(Textures[ti]->Get_Texture_Name(),tex->Get_Texture_Name()) == 0) break;
- }
- if (ti == Textures.Count()) {
- mat->TextureIdx = Add_Texture(tex);
- } else {
- mat->TextureIdx = ti;
- }
- }
- LegacyMaterials.Add(mat);
- }
- /***********************************************************************************************
- * MeshLoadContextClass::Peek_Legacy_Shader -- returns a legacy shader *
- * *
- * This function does the re-indexing to go from the legacy shader index to the actual shader *
- * index and then returns that shader *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- ShaderClass MeshLoadContextClass::Peek_Legacy_Shader(int legacy_material_index)
- {
- WWASSERT(legacy_material_index >= 0);
- WWASSERT(legacy_material_index < LegacyMaterials.Count());
- int si = LegacyMaterials[legacy_material_index]->ShaderIdx;
- return Peek_Shader(si);
- }
- /***********************************************************************************************
- * MeshLoadContextClass::Peek_Legacy_Vertex_Material -- returns a pointer to a legacy vertex ma*
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- VertexMaterialClass * MeshLoadContextClass::Peek_Legacy_Vertex_Material(int legacy_material_index)
- {
- WWASSERT(legacy_material_index >= 0);
- WWASSERT(legacy_material_index < LegacyMaterials.Count());
- int vi = LegacyMaterials[legacy_material_index]->VertexMaterialIdx;
- if (vi != -1) {
- return Peek_Vertex_Material(vi);
- } else {
- return NULL;
- }
- }
- /***********************************************************************************************
- * MeshLoadContextClass::Peek_Legacy_Texture -- returns a pointer to a texture *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- TextureClass * MeshLoadContextClass::Peek_Legacy_Texture(int legacy_material_index)
- {
- WWASSERT(legacy_material_index >= 0);
- WWASSERT(legacy_material_index < LegacyMaterials.Count());
- int ti = LegacyMaterials[legacy_material_index]->TextureIdx;
- if (ti != -1) {
- return Peek_Texture(ti);
- } else {
- return NULL;
- }
- }
- Vector2 * MeshLoadContextClass::Get_Temporary_UV_Array(int elementcount)
- {
- TempUVArray.Uninitialised_Grow(elementcount);
- return &(TempUVArray[0]);
- }
- /***********************************************************************************************
- * MeshSaveContextClass::MeshSaveContextClass -- constructor *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- *=============================================================================================*/
- MeshSaveContextClass::MeshSaveContextClass(void) :
- CurPass(0),
- CurStage(0)
- {
- }
- /***********************************************************************************************
- * MeshSaveContextClass::~MeshSaveContextClass -- destructor *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- *=============================================================================================*/
- MeshSaveContextClass::~MeshSaveContextClass(void)
- {
- }
- #if 0 // MESH SAVING CODE HAS NOT BEEN MAINTAINED... Leaving here for future reference if we ever need it :-)
- /***********************************************************************************************
- * MeshModelClass::Save -- Save this mesh model! *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/16/99 GTH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshModelClass::Save_W3D(ChunkSaveClass & csave)
- {
- MeshSaveContextClass * context = new MeshSaveContextClass;
- context->Materials.Collect_Materials(this);
- write_chunks(csave,context);
- delete context;
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_chunks(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
-
- // write the header
- write_header(csave,context);
- write_user_text(csave,context);
- // write the geometry
- write_triangles(csave,context);
- write_vertices(csave,context);
- write_vertex_normals(csave,context);
- write_vertex_shade_indices(csave,context);
- write_vertex_influences(csave,context);
- //write_cull_tree(csave);
- // material stuff
- write_material_info(csave,context);
- write_vertex_materials(csave,context);
- write_shaders(csave,context);
- write_textures(csave,context);
- // passes
- for (int i=0; i<Get_Pass_Count(); i++) {
- context->CurPass = i;
- write_material_pass(csave,context);
- }
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_header(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
- {
- W3dMeshHeader3Struct header;
- memset(&header,0,sizeof(header));
-
- // Set names
- if (MeshName) {
- char * name = MeshName->Get_Array();
- char * mesh_name = strchr(name,'.');
-
- int hierarchy_name_len = 0;
- if (mesh_name == NULL) {
- mesh_name = name;
- } else {
- hierarchy_name_len = (int)mesh_name - (int)name;
- mesh_name++;
- }
- assert( hierarchy_name_len <= W3D_NAME_LEN);
- strncpy( header.MeshName, mesh_name, W3D_NAME_LEN);
- strncpy( header.ContainerName, name, hierarchy_name_len);
- } else {
- sprintf(header.MeshName,"UnNamed");
- }
- header.Version = W3D_CURRENT_MESH_VERSION;
- header.Attributes = W3dAttributes;
- header.NumTris = Get_Polygon_Count();
- header.NumVertices = Get_Vertex_Count();
- header.NumMaterials = 0;
- header.NumDamageStages = 0;
- header.VertexChannels = W3D_VERTEX_CHANNEL_LOCATION;
- if (Get_Flag(SKIN)) {
- header.VertexChannels |= W3D_VERTEX_CHANNEL_BONEID;
- }
- header.FaceChannels = W3D_FACE_CHANNEL_FACE;
- W3dUtilityClass::Convert_Vector(BoundBoxMin,&(header.Min));
- W3dUtilityClass::Convert_Vector(BoundBoxMax,&(header.Max));
- W3dUtilityClass::Convert_Vector(BoundSphereCenter,&header.SphCenter);
- header.SphRadius = BoundSphereRadius;
- csave.Begin_Chunk(W3D_CHUNK_MESH_HEADER3);
- if (csave.Write(&header,sizeof(header)) != sizeof(header)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_user_text(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
- {
- if (UserText == NULL) return WW3D_ERROR_OK;
- if (strlen(UserText->Get_Array()) < 1) return WW3D_ERROR_OK;
- csave.Begin_Chunk(W3D_CHUNK_MESH_USER_TEXT);
- csave.Write(UserText->Get_Array(),strlen(UserText->Get_Array()) + 1);
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_triangles(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
- {
- if (!csave.Begin_Chunk(W3D_CHUNK_TRIANGLES)) {
- return WW3D_ERROR_LOAD_FAILED;
- }
- TriIndex * poly_verts = Poly->Get_Array();
- Vector4 * poly_eq = (PlaneEq ? PlaneEq->Get_Array() : NULL);
- for (int i=0; i<Get_Polygon_Count(); i++) {
- W3dTriStruct tri;
- memset(&tri,0,sizeof(W3dTriStruct));
- // convert each triangle into surrender format
- tri.Vindex[0] = poly_verts[i].I;
- tri.Vindex[1] = poly_verts[i].J;
- tri.Vindex[2] = poly_verts[i].K;
- if (poly_eq) {
- tri.Attributes = 0;
- tri.Normal.X = poly_eq[i].X;
- tri.Normal.Y = poly_eq[i].Y;
- tri.Normal.Z = poly_eq[i].Z;
- tri.Dist = poly_eq[i].W;
- } else {
-
- Vector3 a,b,normal;
- Vector3 * verts = Vertex->Get_Array();
- const Vector3 & p0= verts[poly_verts[i][0]];
- Vector3::Subtract(verts[poly_verts[i][1]],p0,&a);
- Vector3::Subtract(verts[poly_verts[i][2]],p0,&b);
- Vector3::Cross_Product(a,b,&normal);
- normal.Normalize();
- tri.Attributes = 0;
- tri.Normal.X = normal.X;
- tri.Normal.Y = normal.Y;
- tri.Normal.Z = normal.Z;
- tri.Dist = Vector3::Dot_Product(p0,normal);
- }
- if (csave.Write(&tri,sizeof(W3dTriStruct)) != sizeof(W3dTriStruct)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- }
- if (!csave.End_Chunk()) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_vertices(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
- {
- if (!csave.Begin_Chunk(W3D_CHUNK_VERTICES)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
-
- WWASSERT(Get_Vertex_Count() > 0);
- Vector3 * verts = Vertex->Get_Array();
- for (int i=0; i<Get_Vertex_Count(); i++) {
- W3dVectorStruct vert;
- vert.X = verts[i].X;
- vert.Y = verts[i].Y;
- vert.Z = verts[i].Z;
-
- if (csave.Write(&(vert),sizeof(W3dVectorStruct)) != sizeof(W3dVectorStruct)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- }
-
- if (!csave.End_Chunk()) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_vertex_normals(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
- {
- WWASSERT( Get_Vertex_Count() > 0);
- if (!csave.Begin_Chunk(W3D_CHUNK_VERTEX_NORMALS)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
-
- const Vector3 * verts = Get_Vertex_Normal_Array();
-
- for (int i=0; i<Get_Vertex_Count(); i++) {
- W3dVectorStruct vert;
- vert.X = verts[i].X;
- vert.Y = verts[i].Y;
- vert.Z = verts[i].Z;
-
- if (csave.Write(&(vert),sizeof(W3dVectorStruct)) != sizeof(W3dVectorStruct)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- }
-
- if (!csave.End_Chunk()) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_vertex_shade_indices(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
- {
- WWASSERT(Get_Vertex_Count() > 0);
- if (VertexShadeIdx == NULL) return WW3D_ERROR_OK;
- if (!csave.Begin_Chunk(W3D_CHUNK_VERTEX_SHADE_INDICES)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- for (int i=0; i<Get_Vertex_Count(); i++) {
- uint32 idx = VertexShadeIdx->Get_Array()[i];
- if (csave.Write(&idx,sizeof(idx)) != sizeof(idx)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- }
- if (!csave.End_Chunk()) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_vertex_influences(ChunkSaveClass & csave,MeshSaveContextClass * /*context*/)
- {
- WWASSERT(Get_Vertex_Count() > 0);
- if (VertexBoneLink == NULL) return WW3D_ERROR_OK;
- if (!csave.Begin_Chunk(W3D_CHUNK_VERTEX_INFLUENCES)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- W3dVertInfStruct vinf;
- memset(&vinf,0,sizeof(vinf));
- for (int i=0; i<Get_Vertex_Count(); i++) {
- vinf.BoneIdx = VertexBoneLink->Get_Array()[i];
- if (csave.Write(&vinf,sizeof(vinf)) != sizeof(vinf)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- }
- if (!csave.End_Chunk()) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_material_info(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if (!csave.Begin_Chunk(W3D_CHUNK_MATERIAL_INFO)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- W3dMaterialInfoStruct info;
- memset(&info,0,sizeof(info));
- info.PassCount = DefMatDesc->PassCount;
- info.VertexMaterialCount = context->Materials.Get_Vertex_Material_Count();
- info.TextureCount = context->Materials.Get_Texture_Count();
- info.ShaderCount = context->Materials.Get_Shader_Count();
- if (csave.Write(&info,sizeof(info)) != sizeof(info)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- if (!csave.End_Chunk()) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_shaders(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if (context->Materials.Get_Shader_Count() <= 0) {
- return WW3D_ERROR_OK;
- }
- if (!csave.Begin_Chunk(W3D_CHUNK_SHADERS)) return WW3D_ERROR_SAVE_FAILED;
- for (int si=0; si<context->Materials.Get_Shader_Count(); si++) {
- W3dShaderStruct file_shader;
- ShaderClass shader = context->Materials.Peek_Shader(si);
- W3dUtilityClass::Convert_Shader(shader,&file_shader);
- if (csave.Write(&file_shader,sizeof(file_shader)) != sizeof(file_shader)) {
- return WW3D_ERROR_SAVE_FAILED;
- }
- }
- if (!csave.End_Chunk()) return WW3D_ERROR_SAVE_FAILED;
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_vertex_materials(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if (context->Materials.Get_Vertex_Material_Count() <= 0) return WW3D_ERROR_OK;
- if (!csave.Begin_Chunk(W3D_CHUNK_VERTEX_MATERIALS)) return WW3D_ERROR_SAVE_FAILED;
-
- for (int vi=0; vi<context->Materials.Get_Vertex_Material_Count(); vi++) {
-
- csave.Begin_Chunk(W3D_CHUNK_VERTEX_MATERIAL);
- VertexMaterialClass * vmat = context->Materials.Peek_Vertex_Material(vi);
- vmat->Save_W3D(csave);
- csave.End_Chunk();
- }
- if (!csave.End_Chunk()) return WW3D_ERROR_SAVE_FAILED;
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_textures(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if (context->Materials.Get_Texture_Count() <= 0) return WW3D_ERROR_OK;
- if (!csave.Begin_Chunk(W3D_CHUNK_TEXTURES)) return WW3D_ERROR_SAVE_FAILED;
-
- for (int ti=0; ti<context->Materials.Get_Texture_Count(); ti++) {
- TextureClass * tex = context->Materials.Peek_Texture(ti);
- Save_Texture(tex,csave);
- }
- if (!csave.End_Chunk()) return WW3D_ERROR_SAVE_FAILED;
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_material_pass(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- context->CurStage = 0;
- csave.Begin_Chunk(W3D_CHUNK_MATERIAL_PASS);
- write_vertex_material_ids(csave,context);
- write_shader_ids(csave,context);
- write_dcg(csave,context);
- write_dig(csave,context);
- write_scg(csave,context);
- write_texture_stage(csave,context);
- write_texture_stage(csave,context);
- csave.End_Chunk();
- context->CurPass++;
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_vertex_material_ids(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- // first check if all vertex material pointers are Null (is this legal?)
- if ( (DefMatDesc->Material[context->CurPass] == NULL) &&
- (DefMatDesc->MaterialArray[context->CurPass] == NULL)) return WW3D_ERROR_OK;
- csave.Begin_Chunk(W3D_CHUNK_VERTEX_MATERIAL_IDS);
- uint32 id = 0;
- if (DefMatDesc->MaterialArray[context->CurPass] == NULL) {
-
- id = context->Materials.Find_Vertex_Material(DefMatDesc->Material[context->CurPass]);
- csave.Write(&id,sizeof(id));
- } else {
- VertexMaterialClass ** array = DefMatDesc->MaterialArray[context->CurPass]->Get_Array();
- for (int vi=0; vi<Get_Vertex_Count(); vi++) {
- id = context->Materials.Find_Vertex_Material(array[vi]);
- csave.Write(&id,sizeof(id));
- }
- }
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_shader_ids(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- csave.Begin_Chunk(W3D_CHUNK_SHADER_IDS);
- uint32 id = 0;
- if (DefMatDesc->ShaderArray[context->CurPass] == NULL) {
-
- id = context->Materials.Find_Shader(DefMatDesc->Shader[context->CurPass]);
- csave.Write(&id,sizeof(id));
- } else {
-
- ShaderClass * array = DefMatDesc->ShaderArray[context->CurPass]->Get_Array();
- for (int pi=0; pi<Get_Polygon_Count(); pi++) {
- id = context->Materials.Find_Shader(array[pi]);
- csave.Write(&id,sizeof(id));
- }
- }
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_scg(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if (DefMatDesc->SCG[context->CurPass] == NULL) return WW3D_ERROR_OK;
- csave.Begin_Chunk(W3D_CHUNK_SCG);
-
- W3dRGBAStruct color;
- Vector4 * color_array = DefMatDesc->SCG[context->CurPass]->Get_Array();
- for (int vi=0; vi<Get_Vertex_Count(); vi++) {
- W3dUtilityClass::Convert_Color(color_array[vi],&color);
- csave.Write(&color,sizeof(color));
- }
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_dig(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if (DefMatDesc->DIG[context->CurPass] == NULL) return WW3D_ERROR_OK;
- csave.Begin_Chunk(W3D_CHUNK_DIG);
- W3dRGBStruct color;
- Vector3 * color_array = DefMatDesc->DIG[context->CurPass]->Get_Array();
- for (int vi=0; vi<Get_Vertex_Count(); vi++) {
- W3dUtilityClass::Convert_Color(color_array[vi],&color);
- csave.Write(&color,sizeof(color));
- }
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_dcg(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if (DefMatDesc->DCG[context->CurPass] == NULL) return WW3D_ERROR_OK;
- csave.Begin_Chunk(W3D_CHUNK_DCG);
- W3dRGBAStruct color;
- Vector4 * color_array = DefMatDesc->DCG[context->CurPass]->Get_Array();
- for (int vi=0; vi<Get_Vertex_Count(); vi++) {
- W3dUtilityClass::Convert_Color(color_array[vi],&color);
- csave.Write(&color,sizeof(color));
- }
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_texture_stage(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if ( (DefMatDesc->Texture[context->CurPass][context->CurStage] == NULL) &&
- (DefMatDesc->TextureArray[context->CurPass][context->CurStage] == NULL)) return WW3D_ERROR_OK;
-
- csave.Begin_Chunk(W3D_CHUNK_TEXTURE_STAGE);
- write_texture_ids(csave,context);
- write_stage_texcoords(csave,context);
- csave.End_Chunk();
- context->CurStage++;
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_texture_ids(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- csave.Begin_Chunk(W3D_CHUNK_TEXTURE_IDS);
-
- uint32 id = 0;
- if (DefMatDesc->TextureArray[context->CurPass][context->CurStage] == NULL) {
-
- id = context->Materials.Find_Texture(DefMatDesc->Texture[context->CurPass][context->CurStage]);
- csave.Write(&id,sizeof(id));
- } else {
-
- srTextureIFace ** array = DefMatDesc->TextureArray[context->CurPass][context->CurStage]->Get_Array();
- for (int pi=0; pi<Get_Polygon_Count(); pi++) {
- id = context->Materials.Find_Texture(array[pi]);
- csave.Write(&id,sizeof(id));
- }
- }
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- WW3DErrorType MeshModelClass::write_stage_texcoords(ChunkSaveClass & csave,MeshSaveContextClass * context)
- {
- if (DefMatDesc->UV[context->CurPass][context->CurStage] == NULL) return WW3D_ERROR_OK;
- csave.Begin_Chunk(W3D_CHUNK_STAGE_TEXCOORDS);
- W3dTexCoordStruct tex;
- Vector2 * array = DefMatDesc->UV[context->CurPass][context->CurStage]->Get_Array();
- for (int vi=0; vi<Get_Vertex_Count(); vi++) {
- tex.U = array[vi].X;
- tex.V = 1.0f - array[vi].Y;
-
- csave.Write(&tex,sizeof(tex));
- }
- csave.End_Chunk();
- return WW3D_ERROR_OK;
- }
- #endif // 0 (disabled mesh saving code)
|