| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551 |
- /*
- ** Command & Conquer Generals(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/>.
- */
- /* $Header: /VSS_Sync/ww3d2/mesh.cpp 56 8/29/01 9:50p Vss_sync $ */
- /***********************************************************************************************
- *** Confidential - Westwood Studios ***
- ***********************************************************************************************
- * *
- * Project Name : Commando / G 3D engine *
- * *
- * File Name : MESH.CPP *
- * *
- * Programmer : Greg Hjelstrom *
- * *
- * Start Date : 06/11/97 *
- * *
- * Last Update : June 12, 1997 [GH] *
- * *
- *---------------------------------------------------------------------------------------------*
- * Functions: *
- * MeshClass::MeshClass -- Constructor for MeshClass *
- * MeshClass::MeshClass -- Copy Constructor for MeshClass *
- * MeshClass::operator == -- assignment operator for MeshClass *
- * MeshClass::~MeshClass -- destructor *
- * MeshClass::Contains -- Determines whether mesh contains a (worldspace) point. *
- * MeshClass::Free -- Releases all memory/assets in use by this mesh *
- * MeshClass::Clone -- Creates a clone of this mesh *
- * MeshClass::Get_Name -- returns the name of the mesh *
- * MeshClass::Set_Name -- sets the name of this mesh *
- * MeshClass::Get_W3D_Flags -- access to the W3D flags *
- * MeshClass::Get_User_Text -- access to the text buffer *
- * MeshClass::Scale -- Scales the mesh *
- * MeshClass::Scale -- Scales the mesh *
- * MeshClass::Init -- Init the mesh from a MeshBuilder object *
- * MeshClass::Load -- creates a mesh out of a mesh chunk in a .w3d file *
- * MeshClass::Cast_Ray -- compute a ray intersection with this mesh *
- * MeshClass::Cast_AABox -- cast an AABox against this mesh *
- * MeshClass::Cast_OBBox -- Cast an obbox against this mesh *
- * MeshClass::Intersect_AABox -- test for intersection with given AABox *
- * MeshClass::Intersect_OBBox -- test for intersection with the given OBBox *
- * MeshClass::Generate_Culling_Tree -- Generates a hierarchical culling tree for the mesh *
- * MeshClass::Direct_Load -- read the w3d file directly into this mesh object *
- * MeshClass::read_chunks -- read all of the chunks from the .wtm file *
- * MeshClass::read_vertices -- reads the vertex chunk *
- * MeshClass::read_texcoords -- read in the texture coordinates chunk *
- * MeshClass::install_texture_coordinates -- installs the given u-v's in each channel that is*
- * MeshClass::read_vertex_normals -- reads a surrender normal chunk from the wtm file *
- * MeshClass::read_v3_materials -- Reads in version 3 materials. *
- * MeshClass::read_map -- Reads definition of a texture map from the file *
- * MeshClass::read_triangles -- read the triangles chunk *
- * MeshClass::read_per_tri_materials -- read the material indices for each triangle *
- * MeshClass::read_user_text -- read in the user text chunk *
- * MeshClass::read_vertex_colors -- read in the vertex colors chunk *
- * MeshClass::read_vertex_influences -- read in the vertex influences chunk *
- * MeshClass::Get_Material_Info -- returns a pointer to the material info *
- * MeshClass::Create_Decal -- creates a decal on this mesh *
- * MeshClass::Delete_Decal -- removes a decal from this mesh *
- * MeshClass::Get_Num_Polys -- returns the number of polys (tris) in this mesh *
- * MeshClass::Render -- renders this mesh *
- * MeshClass::Special_Render -- special render function for meshes *
- * MeshClass::update_skin -- deforms the mesh *
- * MeshClass::clone_materials -- clone the materials for this mesh *
- * MeshClass::install_materials -- transfers the materials into the mesh *
- * MeshClass::Get_Model -- user access to the mesh model *
- * MeshClass::Get_Obj_Space_Bounding_Sphere -- returns obj-space bounding sphere *
- * MeshClass::Get_Obj_Space_Bounding_Box -- returns the obj-space bounding box *
- * MeshClass::Get_Deformed_Vertices -- Gets the deformed vertices for a skin *
- * MeshClass::Get_Deformed_Vertices -- Gets the deformed vertices for a skin *
- * MeshClass::Render_Material_Pass -- Render a procedural material pass for this mesh *
- * MeshClass::Replace_VertexMaterial -- Replaces existing vertex material with a new one. Wi *
- * MeshClass::Make_Unique -- Makes mesh unique in the renderer, but still shares system ram *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #include "mesh.h"
- #include <assert.h>
- #include <string.h>
- #include "w3d_file.h"
- #include "assetmgr.h"
- #include "w3derr.h"
- #include "wwdebug.h"
- #include "vertmaterial.h"
- #include "shader.h"
- #include "matinfo.h"
- #include "htree.h"
- #include "meshbuild.h"
- #include "tri.h"
- #include "aaplane.h"
- #include "aabtree.h"
- #include "chunkio.h"
- #include "w3d_util.h"
- #include "meshmdl.h"
- #include "meshgeometry.h"
- #include "ww3d.h"
- #include "camera.h"
- #include "texture.h"
- #include "rinfo.h"
- #include "coltest.h"
- #include "inttest.h"
- #include "decalmsh.h"
- #include "decalsys.h"
- #include "dx8polygonrenderer.h"
- #include "dx8indexbuffer.h"
- #include "dx8renderer.h"
- #include "visrasterizer.h"
- #include "wwmemlog.h"
- #include <stdio.h>
- bool MeshClass::Legacy_Meshes_Fogged = true;
- /*
- ** This #define causes the collision code to always recompute the triangle normals rather
- ** than using the ones in the model.
- ** TODO: ensure that the models have unit normals and start re-using them again or write
- ** collision code to handle non-unit normals!
- */
- #if (OPTIMIZE_PLANEEQ_RAM)
- #define COMPUTE_NORMALS
- #endif
- /*
- ** Temporary storage used during decal creation
- */
- static DynamicVectorClass<Vector3> _TempVertexBuffer;
- /***********************************************************************************************
- * MeshClass::MeshClass -- Constructor for MeshClass *
- * *
- * Initializes an empty mesh class *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/6/98 GTH : Created. *
- *=============================================================================================*/
- MeshClass::MeshClass(void) :
- Model(NULL),
- DecalMesh(NULL),
- LightEnvironment(NULL),
- BaseVertexOffset(0),
- NextVisibleSkin(NULL),
- m_alphaOverride(1.0f),
- m_materialPassAlphaOverride(1.0f),
- m_materialPassEmissiveOverride(1.0f)
- {
- }
- /***********************************************************************************************
- * MeshClass::MeshClass -- Copy Constructor for MeshClass *
- * *
- * Creates a mesh which is a copy of the given mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- *=============================================================================================*/
- MeshClass::MeshClass(const MeshClass & that) :
- RenderObjClass(that),
- Model(NULL),
- DecalMesh(NULL),
- LightEnvironment(NULL),
- BaseVertexOffset(that.BaseVertexOffset),
- NextVisibleSkin(NULL),
- m_alphaOverride(1.0f),
- m_materialPassAlphaOverride(1.0f),
- m_materialPassEmissiveOverride(1.0f)
- {
- REF_PTR_SET(Model,that.Model); // mesh instances share models by default
- }
- /***********************************************************************************************
- * operator == -- assignment operator for MeshClass *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/6/98 GTH : Created. *
- *=============================================================================================*/
- MeshClass & MeshClass::operator = (const MeshClass & that)
- {
- if (this != &that) {
- RenderObjClass::operator = (that);
- REF_PTR_SET(Model,that.Model); // mesh instances share models by default
- BaseVertexOffset = that.BaseVertexOffset;
- // just dont copy the decals or light environment
- REF_PTR_RELEASE(DecalMesh);
- LightEnvironment = NULL;
- }
- return * this;
- }
- /***********************************************************************************************
- * MeshClass::~MeshClass -- destructor *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/6/98 GTH : Created. *
- *=============================================================================================*/
- MeshClass::~MeshClass(void)
- {
- Free();
- }
- /***********************************************************************************************
- * MeshClass::Contains -- Determines whether mesh contains a (worldspace) point. *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: Assumes mesh is a closed manifold - results are undefined otherwise *
- * This function will NOT work for skins *
- * *
- * HISTORY: *
- * 8/30/00 NH : Created. *
- *=============================================================================================*/
- bool MeshClass::Contains(const Vector3 &point)
- {
- // Transform point to object space and pass on to model
- Vector3 obj_point;
- Matrix3D::Inverse_Transform_Vector(Transform, point, &obj_point);
- return Model->Contains(obj_point);
- }
- /***********************************************************************************************
- * MeshClass::Free -- Releases all memory/assets in use by this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/6/98 GTH : Created. *
- *=============================================================================================*/
- void MeshClass::Free(void)
- {
- REF_PTR_RELEASE(Model);
- REF_PTR_RELEASE(DecalMesh);
- }
- /***********************************************************************************************
- * MeshClass::Clone -- Creates a clone of this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/6/98 GTH : Created. *
- *=============================================================================================*/
- RenderObjClass * MeshClass::Clone(void) const
- {
- return NEW_REF( MeshClass, (*this));
- }
- /***********************************************************************************************
- * MeshClass::Get_Name -- returns the name of the mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 5/15/98 GTH : Created. *
- *=============================================================================================*/
- const char * MeshClass::Get_Name(void) const
- {
- return Model->Get_Name();
- }
- /***********************************************************************************************
- * MeshClass::Set_Name -- sets the name of this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 3/9/99 GTH : Created. *
- *=============================================================================================*/
- void MeshClass::Set_Name(const char * name)
- {
- Model->Set_Name(name);
- }
- /***********************************************************************************************
- * MeshClass::Get_W3D_Flags -- access to the W3D flags *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 5/15/98 GTH : Created. *
- *=============================================================================================*/
- uint32 MeshClass::Get_W3D_Flags(void)
- {
- return Model->W3dAttributes;
- }
- /***********************************************************************************************
- * MeshClass::Get_User_Text -- access to the text buffer *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 5/15/98 GTH : Created. *
- *=============================================================================================*/
- const char * MeshClass::Get_User_Text(void) const
- {
- return Model->Get_User_Text();
- }
- /***********************************************************************************************
- * MeshClass::Get_Material_Info -- returns a pointer to the material info *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 5/20/98 GTH : Created. *
- *=============================================================================================*/
- MaterialInfoClass * MeshClass::Get_Material_Info(void)
- {
- if (Model) {
- if (Model->MatInfo) {
- Model->MatInfo->Add_Ref();
- return Model->MatInfo;
- }
- }
- return NULL;
- }
- /***********************************************************************************************
- * MeshClass::Get_Model -- user access to the mesh model *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 2/4/99 GTH : Created. *
- *=============================================================================================*/
- MeshModelClass * MeshClass::Get_Model(void)
- {
- if (Model != NULL) {
- Model->Add_Ref();
- }
- return Model;
- }
- /***********************************************************************************************
- * MeshClass::Scale -- Scales the mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- *=============================================================================================*/
- void MeshClass::Scale(float scale)
- {
- if (scale==1.0f) return;
- Vector3 sc;
- sc.X = sc.Y = sc.Z = scale;
- Make_Unique();
- Model->Make_Geometry_Unique();
- Model->Scale(sc);
-
- Invalidate_Cached_Bounding_Volumes();
- // Now update the object space bounding volumes of this object's container:
- RenderObjClass *container = Get_Container();
- if (container) container->Update_Obj_Space_Bounding_Volumes();
- }
- /***********************************************************************************************
- * MeshClass::Scale -- Scales the mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/6/98 GTH : Created. *
- * 12/8/98 GTH : Modified the box scaling to use the non-uniform parameters *
- *=============================================================================================*/
- void MeshClass::Scale(float scalex, float scaley, float scalez)
- {
- // scale the surrender mesh model
- Vector3 sc;
- sc.X = scalex;
- sc.Y = scaley;
- sc.Z = scalez;
- Make_Unique();
- Model->Make_Geometry_Unique();
- Model->Scale(sc);
-
- Invalidate_Cached_Bounding_Volumes();
- // Now update the object space bounding volumes of this object's container:
- RenderObjClass *container = Get_Container();
- if (container) container->Update_Obj_Space_Bounding_Volumes();
- }
- /***********************************************************************************************
- * MeshClass::Get_Deformed_Vertices -- Gets the deformed vertices for a skin *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 3/4/2001 gth : Created. *
- *=============================================================================================*/
- void MeshClass::Get_Deformed_Vertices(Vector3 *dst_vert, Vector3 *dst_norm)
- {
- WWASSERT(Model->Get_Flag(MeshGeometryClass::SKIN));
- Model->get_deformed_vertices(dst_vert,dst_norm,Container->Get_HTree());
- }
- /***********************************************************************************************
- * MeshClass::Get_Deformed_Vertices -- Gets the deformed vertices for a skin *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 3/4/2001 gth : Created. *
- *=============================================================================================*/
- void MeshClass::Get_Deformed_Vertices(Vector3 *dst_vert)
- {
- WWASSERT(Model->Get_Flag(MeshGeometryClass::SKIN));
- WWASSERT(Container != NULL);
- WWASSERT(Container->Get_HTree() != NULL);
- Model->get_deformed_vertices(dst_vert,Container->Get_HTree());
- }
- void MeshClass::Compose_Deformed_Vertex_Buffer(
- VertexFormatXYZNDUV2* verts,
- const Vector2* uv0,
- const Vector2* uv1,
- const unsigned* diffuse)
- {
- WWASSERT(Model->Get_Flag(MeshGeometryClass::SKIN));
- Model->compose_deformed_vertex_buffer(verts,uv0,uv1,diffuse,Container->Get_HTree());
- }
- /***********************************************************************************************
- * MeshClass::Create_Decal -- creates a decal on this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/26/00 gth : Created. *
- *=============================================================================================*/
- void MeshClass::Create_Decal(DecalGeneratorClass * generator)
- {
- WWMEMLOG(MEM_GEOMETRY);
- if (WW3D::Are_Decals_Enabled() == false) {
- return;
- }
- if (Is_Translucent() && (generator->Is_Applied_To_Translucent_Meshes() == false)) {
- return;
- }
-
- if (!Model->Get_Flag(MeshGeometryClass::SKIN)) {
-
- // Rigid mesh
- Matrix3D modeltm_inv;
- OBBoxClass localbox;
- Get_Transform().Get_Orthogonal_Inverse(modeltm_inv);
- OBBoxClass::Transform(modeltm_inv, generator->Get_Bounding_Volume(), &localbox);
- // generate apt, if it is not empty, add a decal.
- SimpleDynVecClass<uint32> apt;
- Model->Generate_Rigid_APT(localbox, apt);
-
- if (apt.Count() > 0) {
- if (DecalMesh == NULL) {
- DecalMesh = NEW_REF(RigidDecalMeshClass, (this, generator->Peek_Decal_System()));
- }
- DecalMesh->Create_Decal(generator, localbox, apt);
- }
- } else {
-
- WWDEBUG_SAY(("PERFORMANCE WARNING: Decal being applied to a SKIN mesh!\r\n"));
- // Skin
- // The deformed worldspace vertices are used both for the APT and in Create_Decal() to
- // generate the texture coordinates.
- int vertex_count = Model->Get_Vertex_Count();
- if (_TempVertexBuffer.Count() < vertex_count) _TempVertexBuffer.Resize(vertex_count);
- Vector3 *dst_vert = &(_TempVertexBuffer[0]);
- Get_Deformed_Vertices(dst_vert);
- // generate apt, if it is not empty, add a decal.
- SimpleDynVecClass<uint32> apt;
- OBBoxClass worldbox = generator->Get_Bounding_Volume();
- // We compare the worldspace box vs. the worldspace vertices
- Model->Generate_Skin_APT(worldbox, apt, dst_vert);
-
- // if it is not empty, add a decal
- if (apt.Count() > 0) {
- if (DecalMesh == NULL) {
- DecalMesh = NEW_REF(SkinDecalMeshClass, (this, generator->Peek_Decal_System()));
- }
- DecalMesh->Create_Decal(generator, worldbox, apt, &_TempVertexBuffer);
- }
- }
- }
- /***********************************************************************************************
- * MeshClass::Delete_Decal -- removes a decal from this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/26/00 gth : Created. *
- *=============================================================================================*/
- void MeshClass::Delete_Decal(uint32 decal_id)
- {
- if (DecalMesh != NULL) {
- DecalMesh->Delete_Decal(decal_id);
- }
- }
- /***********************************************************************************************
- * MeshClass::Get_Num_Polys -- returns the number of polys (tris) in this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/6/98 GTH : Created. *
- *=============================================================================================*/
- int MeshClass::Get_Num_Polys(void) const
- {
- if (Model) {
- return Model->Get_Polygon_Count();
- } else {
- return 0;
- }
- }
- /***********************************************************************************************
- * MeshClass::Render -- renders this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- void MeshClass::Render(RenderInfoClass & rinfo)
- {
- if (Is_Not_Hidden_At_All() == false) {
- return;
- }
- // If static sort lists are enabled and this mesh has a sort level, put it on the list instead
- // of rendering it.
- unsigned int sort_level = (unsigned int)Model->Get_Sort_Level();
- if (WW3D::Are_Static_Sort_Lists_Enabled() && sort_level != SORT_LEVEL_NONE) {
- Set_Lighting_Environment(rinfo.light_environment);
- //Add custom alpha
- m_alphaOverride = rinfo.alphaOverride;
- m_materialPassAlphaOverride = rinfo.materialPassAlphaOverride;
- m_materialPassEmissiveOverride = rinfo.materialPassEmissiveOverride;
- WW3D::Add_To_Static_Sort_List(this, sort_level);
- } else {
- const FrustumClass & frustum=rinfo.Camera.Get_Frustum();
- if ( Model->Get_Flag(MeshGeometryClass::SKIN) ||
- CollisionMath::Overlap_Test(frustum,Get_Bounding_Box())!=CollisionMath::OUTSIDE )
- {
- bool rendered_something = false;
-
- /*
- ** If this mesh model has never been rendered, we need to generate the DX8 datastructures
- */
- if (Model->PolygonRendererList.Is_Empty()) {
- Model->Register_For_Rendering();
- WWASSERT(!Model->PolygonRendererList.Is_Empty());
- }
- /*
- ** Plug in lighting
- */
- if (sort_level == SORT_LEVEL_NONE) //sorting ones get their environment set above.
- { Set_Lighting_Environment(rinfo.light_environment);
- //Add custom alpha
- m_alphaOverride = rinfo.alphaOverride;
- m_materialPassAlphaOverride = rinfo.materialPassAlphaOverride;
- m_materialPassEmissiveOverride = rinfo.materialPassEmissiveOverride;
- }
- /*
- ** Process texture reductions:
- */
- // Model->Process_Texture_Reduction();
- /*
- ** Look up the FVF container that this mesh is in
- ** TODO: make this a little nicer?
- */
- DX8FVFCategoryContainer * fvf_container = Model->PolygonRendererList.Peek_Head()->Get_Texture_Category()->Get_Container();
- if ((rinfo.Current_Override_Flags() & RenderInfoClass::RINFO_OVERRIDE_ADDITIONAL_PASSES_ONLY) == 0) {
- /*
- ** Link each polygon renderer for this mesh into the visible list
- */
- DX8PolygonRendererListIterator it(&(Model->PolygonRendererList));
- while (!it.Is_Done()) {
- DX8PolygonRendererClass* polygon_renderer=it.Peek_Obj();
- polygon_renderer->Get_Texture_Category()->Add_Render_Task(polygon_renderer,this);
- it.Next();
- }
- rendered_something = true;
- }
- /*
- ** If the rendering context specifies procedural material passes, register them
- ** for rendering
- */
- for (int i=0; i<rinfo.Additional_Pass_Count(); i++) {
-
- MaterialPassClass * matpass = rinfo.Peek_Additional_Pass(i);
- if ((!Is_Translucent()) || (matpass->Is_Enabled_On_Translucent_Meshes())) {
- fvf_container->Add_Visible_Material_Pass(matpass,this);
- rendered_something = true;
- }
- }
- /*
- ** If we rendered any base or procedural passes and this is a skin, we need
- ** to tell the mesh rendering system to process this skin
- */
- if (rendered_something && Model->Get_Flag(MeshGeometryClass::SKIN)) {
- //WWASSERT(dynamic_cast<DX8SkinFVFCategoryContainer *>(fvf_container) != NULL);
- static_cast<DX8SkinFVFCategoryContainer*>(fvf_container)->Add_Visible_Skin(this);
- }
- /*
- ** If we have a decal mesh, link it into the mesh rendering system
- */
- if (DecalMesh != NULL) {
- const SphereClass & ws_sphere = Get_Bounding_Sphere();
- Vector3 cam_space_sphere_center;
- rinfo.Camera.Transform_To_View_Space(cam_space_sphere_center,ws_sphere.Center);
- if (-cam_space_sphere_center.Z - ws_sphere.Radius < WW3D::Get_Decal_Rejection_Distance()) {
- TheDX8MeshRenderer.Add_To_Render_List(DecalMesh);
- }
- }
- }
- }
- }
- /***********************************************************************************************
- * MeshClass::Render_Material_Pass -- Render a procedural material pass for this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 3/4/2001 gth : Created. *
- *=============================================================================================*/
- void MeshClass::Render_Material_Pass(MaterialPassClass * pass,IndexBufferClass * ib)
- {
- //Added to allow dynamic opacity on additional render passed
- //without having to create a new material pass per object instance. -MW
- float oldOpacity=-1.0f;
- Vector3 oldEmissive(-1,-1,-1);
- if (LightEnvironment != NULL) {
- DX8Wrapper::Set_Light_Environment(LightEnvironment);
- }
- if (Model->Get_Flag(MeshModelClass::SKIN)) {
- /*
- ** In the case of skin meshes, we need to render our polys with the identity transform
- */
- if (m_materialPassAlphaOverride != 1.0f)
- { VertexMaterialClass *mat=pass->Peek_Material();
- if (mat)
- {
- oldOpacity=mat->Get_Opacity();
- mat->Set_Opacity(m_materialPassAlphaOverride);
- }
- }
- if (m_materialPassEmissiveOverride != 1.0f)
- { VertexMaterialClass *mat=pass->Peek_Material();
- if (mat)
- {
- mat->Get_Emissive(&oldEmissive);
- mat->Set_Emissive(m_materialPassEmissiveOverride*oldEmissive);
- }
- }
- pass->Install_Materials();
- DX8Wrapper::Set_Index_Buffer(ib,0);
- SNAPSHOT_SAY(("Set_World_Identity\n"));
- DX8Wrapper::Set_World_Identity();
- DX8PolygonRendererListIterator it(&Model->PolygonRendererList);
- while (!it.Is_Done()) {
- if (it.Peek_Obj()->Get_Pass() == 0)
- it.Peek_Obj()->Render(BaseVertexOffset);
- it.Next();
- }
- if (oldOpacity >= 0)
- { //opacity was modified for this mesh instance, so need to restore the material setting which may be shared
- //among other instances.
- pass->Peek_Material()->Set_Opacity(oldOpacity);
- }
- if (oldEmissive.X >= 0)
- { //emissive was modified for this mesh instance, so need to restore the material setting which may be shared
- //among other instances.
- pass->Peek_Material()->Set_Emissive(oldEmissive);
- }
- //MW: Need uninstall custom materials in case they leave D3D in unknown state
- pass->UnInstall_Materials();
- } else if ((pass->Get_Cull_Volume() != NULL) && (MaterialPassClass::Is_Per_Polygon_Culling_Enabled())) {
-
- /*
- ** Generate the APT
- */
- static SimpleDynVecClass<uint32> _apt;
- _apt.Delete_All(false);
-
- Matrix3D modeltminv;
- Get_Transform().Get_Orthogonal_Inverse(modeltminv);
-
- OBBoxClass localbox;
- OBBoxClass::Transform(modeltminv,*(pass->Get_Cull_Volume()),&localbox);
- Vector3 view_dir;
- localbox.Basis.Get_Z_Vector(&view_dir);
- view_dir = -view_dir;
-
- if (Model->Has_Cull_Tree()) {
- Model->Generate_Rigid_APT(localbox,view_dir,_apt);
- } else {
- Model->Generate_Rigid_APT(view_dir,_apt);
- }
-
- if (_apt.Count() > 0) {
- int buftype = BUFFER_TYPE_DYNAMIC_DX8;
- if (Model->Get_Flag(MeshGeometryClass::SORT) && WW3D::Is_Sorting_Enabled()) {
- buftype = BUFFER_TYPE_DYNAMIC_SORTING;
- }
- /*
- ** Spew triangles in the APT into the dynamic index buffer
- */
- int min_v = Model->Get_Vertex_Count();
- int max_v = 0;
- DynamicIBAccessClass dynamic_ib(buftype,_apt.Count() * 3);
- {
- DynamicIBAccessClass::WriteLockClass lock(&dynamic_ib);
- unsigned short * indices = lock.Get_Index_Array();
- const Vector3i * polys = Model->Get_Polygon_Array();
- for (int i=0; i < _apt.Count(); i++)
- {
- unsigned v0 = polys[_apt[i]].I;
- unsigned v1 = polys[_apt[i]].J;
- unsigned v2 = polys[_apt[i]].K;
- indices[i*3 + 0] = (unsigned short)v0;
- indices[i*3 + 1] = (unsigned short)v1;
- indices[i*3 + 2] = (unsigned short)v2;
- min_v = WWMath::Min(v0,min_v);
- min_v = WWMath::Min(v1,min_v);
- min_v = WWMath::Min(v2,min_v);
- max_v = WWMath::Max(v0,max_v);
- max_v = WWMath::Max(v1,max_v);
- max_v = WWMath::Max(v2,max_v);
- }
- }
- /*
- ** Render
- */
- int vertex_offset = Model->PolygonRendererList.Peek_Head()->Get_Vertex_Offset();
- pass->Install_Materials();
-
- DX8Wrapper::Set_Transform(D3DTS_WORLD,Get_Transform());
- DX8Wrapper::Set_Index_Buffer(dynamic_ib,vertex_offset);
- DX8Wrapper::Draw_Triangles(
- 0,
- _apt.Count(),
- min_v,
- max_v-min_v+1);
- //MW: Need uninstall custom materials in case they leave D3D in unknown state
- pass->UnInstall_Materials();
- }
- } else {
-
- /*
- ** Normal mesh case, render polys with this mesh's transform
- */
- if (m_materialPassAlphaOverride != 1.0f)
- { VertexMaterialClass *mat=pass->Peek_Material();
- if (mat)
- {
- oldOpacity=mat->Get_Opacity();
- mat->Set_Opacity(m_materialPassAlphaOverride);
- }
- }
- if (m_materialPassEmissiveOverride != 1.0f)
- { VertexMaterialClass *mat=pass->Peek_Material();
- if (mat)
- {
- mat->Get_Emissive(&oldEmissive);
- mat->Set_Emissive(m_materialPassEmissiveOverride*oldEmissive);
- }
- }
- pass->Install_Materials();
- DX8Wrapper::Set_Index_Buffer(ib,0);
- SNAPSHOT_SAY(("Set_World_Transform\n"));
- DX8Wrapper::Set_Transform(D3DTS_WORLD,Transform);
- DX8PolygonRendererListIterator it(&Model->PolygonRendererList);
- while (!it.Is_Done()) {
- if (it.Peek_Obj()->Get_Pass() == 0)
- it.Peek_Obj()->Render(BaseVertexOffset);
- it.Next();
- }
-
- if (oldOpacity >= 0)
- { //opacity was modified for this mesh instance, so need to restore the material setting which may be shared
- //among other instances.
- pass->Peek_Material()->Set_Opacity(oldOpacity);
- }
- if (oldEmissive.X >= 0)
- { //emissive was modified for this mesh instance, so need to restore the material setting which may be shared
- //among other instances.
- pass->Peek_Material()->Set_Emissive(oldEmissive);
- }
- //MW: Need uninstall custom materials in case they leave D3D in unknown state
- pass->UnInstall_Materials();
- }
- }
- /***********************************************************************************************
- * MeshClass::Special_Render -- special render function for meshes *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 12/10/98 GTH : Created. *
- *=============================================================================================*/
- void MeshClass::Special_Render(SpecialRenderInfoClass & rinfo)
- {
- if ((Is_Not_Hidden_At_All() == false) && (rinfo.RenderType != SpecialRenderInfoClass::RENDER_SHADOW)) {
- return;
- }
-
- if (rinfo.RenderType == SpecialRenderInfoClass::RENDER_VIS) {
-
- WWASSERT(rinfo.VisRasterizer != NULL);
- if (Model->Get_Flag(MeshModelClass::SKIN) == 0) {
- rinfo.VisRasterizer->Set_Model_Transform(Transform);
- rinfo.VisRasterizer->Render_Triangles( Model->Get_Vertex_Array(),
- Model->Get_Vertex_Count(),
- Model->Get_Polygon_Array(),
- Model->Get_Polygon_Count(),
- Get_Bounding_Box() );
- } else {
- int vertex_count = Model->Get_Vertex_Count();
- if (_TempVertexBuffer.Count() < vertex_count) _TempVertexBuffer.Resize(vertex_count);
- Vector3 *dst_vert = &(_TempVertexBuffer[0]);
- Get_Deformed_Vertices(dst_vert);
- rinfo.VisRasterizer->Set_Model_Transform(Matrix3D::Identity);
- rinfo.VisRasterizer->Render_Triangles( dst_vert,
- Model->Get_Vertex_Count(),
- Model->Get_Polygon_Array(),
- Model->Get_Polygon_Count(),
- Get_Bounding_Box() );
- }
- }
- if (rinfo.RenderType == SpecialRenderInfoClass::RENDER_SHADOW) {
- const HTreeClass * htree = NULL;
- if (Container!=NULL) {
- htree = Container->Get_HTree();
- }
- Model->Shadow_Render(rinfo,Transform,htree);
- }
- }
- void MeshClass::Replace_Texture(TextureClass* texture,TextureClass* new_texture)
- {
- Model->Replace_Texture(texture,new_texture);
- }
- /***********************************************************************************************
- * MeshClass::Replace_VertexMaterial -- Replaces existing vertex material with a new one. Will *
- * *
- * *
- * *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 4/2/2001 hy : Created. *
- *=============================================================================================*/
- void MeshClass::Replace_VertexMaterial(VertexMaterialClass* vmat,VertexMaterialClass* new_vmat)
- {
- Model->Replace_VertexMaterial(vmat,new_vmat);
- }
- /***********************************************************************************************
- * MeshClass::Make_Unique -- Makes mesh unique in the renderer, but still shares system ram ge *
- * *
- * *
- * *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 4/2/2001 hy : Created. *
- *=============================================================================================*/
- void MeshClass::Make_Unique()
- {
- if (Model->Num_Refs()==1) return;
- MeshModelClass *newmesh=NEW_REF(MeshModelClass,(*Model));
- REF_PTR_SET(Model,newmesh);
- REF_PTR_RELEASE(newmesh);
- }
- /***********************************************************************************************
- * MeshClass::Load -- creates a mesh out of a mesh chunk in a .w3d file *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 06/12/1997 GH : Created. *
- *=============================================================================================*/
- WW3DErrorType MeshClass::Load_W3D(ChunkLoadClass & cload)
- {
- Vector3 boxmin,boxmax;
- /*
- ** Make sure this mesh is "empty"
- */
- Free();
- /*
- ** Create empty MaterialInfo and Model
- */
- Model = NEW_REF(MeshModelClass,());
- if (Model == NULL) {
- WWDEBUG_SAY(("MeshClass::Load - Failed to allocate model\r\n"));
- return WW3D_ERROR_LOAD_FAILED;
- }
- /*
- ** Create and read in the model...
- */
- if (Model->Load_W3D(cload) != WW3D_ERROR_OK) {
- Free();
- return WW3D_ERROR_LOAD_FAILED;
- }
- /*
- ** Pull interesting stuff out of the w3d attributes bits
- */
- int col_bits = (Model->W3dAttributes & W3D_MESH_FLAG_COLLISION_TYPE_MASK) >> W3D_MESH_FLAG_COLLISION_TYPE_SHIFT;
- Set_Collision_Type( col_bits << 1 );
- Set_Hidden(Model->W3dAttributes & W3D_MESH_FLAG_HIDDEN);
-
- /*
- ** Indicate whether this mesh is translucent. The mesh is considered translucent
- ** if sorting has been enabled (alpha blending on pass 0) or if pass0 contains alpha-test.
- ** This flag is mainly being used by visibility preprocessing code in Renegade.
- */
- int is_translucent = Model->Get_Flag(MeshModelClass::SORT);
- int is_alpha = 0; //keep track of alpha pieces that require sorting (including static sort lists).
- int is_additive = 0;
- if (Model->Has_Shader_Array(0)) {
- for (int i=0; i<Model->Get_Polygon_Count(); i++) {
- is_translucent |= (Model->Get_Shader(i,0).Get_Alpha_Test() == ShaderClass::ALPHATEST_ENABLE);
- is_alpha |= (Model->Get_Shader(i,0).Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO ||
- Model->Get_Shader(i,0).Get_Src_Blend_Func() != ShaderClass::SRCBLEND_ONE) && (Model->Get_Shader(i,0).Get_Alpha_Test() != ShaderClass::ALPHATEST_ENABLE);
- is_additive |= (Model->Get_Shader(i,0).Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ONE &&
- Model->Get_Shader(i,0).Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE);
- }
- } else {
- is_translucent |= (Model->Get_Single_Shader(0).Get_Alpha_Test() == ShaderClass::ALPHATEST_ENABLE);
- is_alpha |= (Model->Get_Single_Shader(0).Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO ||
- Model->Get_Single_Shader(0).Get_Src_Blend_Func() != ShaderClass::SRCBLEND_ONE) && (Model->Get_Single_Shader(0).Get_Alpha_Test() != ShaderClass::ALPHATEST_ENABLE);
- is_additive |= (Model->Get_Single_Shader(0).Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ONE &&
- Model->Get_Single_Shader(0).Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE);
- }
- Set_Translucent(is_translucent);
- Set_Alpha(is_alpha);
- Set_Additive(is_additive);
- return WW3D_ERROR_OK;
- }
- /***********************************************************************************************
- * MeshClass::Cast_Ray -- compute a ray intersection with this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 6/17/98 GTH : Created. *
- *=============================================================================================*/
- bool MeshClass::Cast_Ray(RayCollisionTestClass & raytest)
- {
- if ((Get_Collision_Type() & raytest.CollisionType) == 0) return false;
- //Modified for 'Generals' so we could select trees but filter out headlight beams, etc. -MW
- if (raytest.CheckTranslucent && Is_Alpha()!=0)
- return false;
- if (Is_Hidden() && !raytest.CheckHidden) return false;
- Matrix3D world_to_obj;
- Matrix3D world=Get_Transform();
- // if aligned or oriented rotate the mesh so that it's aligned to the ray
- if (Model->Get_Flag(MeshModelClass::ALIGNED)) {
- Vector3 mesh_position;
- world.Get_Translation(&mesh_position);
- world.Obj_Look_At(mesh_position,mesh_position - raytest.Ray.Get_Dir(),0.0f);
- } else if (Model->Get_Flag(MeshModelClass::ORIENTED)) {
- Vector3 mesh_position;
- world.Get_Translation(&mesh_position);
- world.Obj_Look_At(mesh_position,raytest.Ray.Get_P0(),0.0f);
- }
- world.Get_Inverse(world_to_obj);
- RayCollisionTestClass objray(raytest,world_to_obj);
- WWASSERT(Model);
-
- bool hit = Model->Cast_Ray(objray);
-
- // transform result back into original coordinate system
- if (hit) {
- raytest.CollidedRenderObj = this;
- Matrix3D::Rotate_Vector(world,raytest.Result->Normal, &(raytest.Result->Normal));
- if (raytest.Result->ComputeContactPoint) {
- Matrix3D::Transform_Vector(world,raytest.Result->ContactPoint, &(raytest.Result->ContactPoint));
- }
- }
-
- return hit;
- }
- /***********************************************************************************************
- * MeshClass::Cast_AABox -- cast an AABox against this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 6/17/98 GTH : Created. *
- *=============================================================================================*/
- bool MeshClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
- {
- if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
- WWASSERT(Model);
- // This function analyses the tranform to call optimized functions in certain cases
- bool hit = Model->Cast_World_Space_AABox(boxtest, Get_Transform());
- if (hit) {
- boxtest.CollidedRenderObj = this;
- }
- return hit;
- }
- /***********************************************************************************************
- * Cast_OBBox -- Cast an obbox against this mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 6/17/98 GTH : Created. *
- *=============================================================================================*/
- bool MeshClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
- {
- if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
- /*
- ** transform into the local coordinate system of the mesh.
- */
- const Matrix3D & tm = Get_Transform();
- Matrix3D world_to_obj;
- tm.Get_Orthogonal_Inverse(world_to_obj);
- OBBoxCollisionTestClass localtest(boxtest,world_to_obj);
- WWASSERT(Model);
- bool hit = Model->Cast_OBBox(localtest);
- /*
- ** If we hit, transform the result of the test back to the original coordinate system.
- */
- if (hit) {
- boxtest.CollidedRenderObj = this;
- Matrix3D::Rotate_Vector(tm,boxtest.Result->Normal, &(boxtest.Result->Normal));
- if (boxtest.Result->ComputeContactPoint) {
- Matrix3D::Transform_Vector(tm,boxtest.Result->ContactPoint, &(boxtest.Result->ContactPoint));
- }
- }
- return hit;
- }
- /***********************************************************************************************
- * MeshClass::Intersect_AABox -- test for intersection with given AABox *
- * *
- * The AAbox given is assumed to be in world space. Since meshes aren't generally in world *
- * space, the test must be transformed into our local coordinate system (which turns it into *
- * an OBBox...) *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/19/00 gth : Created. *
- *=============================================================================================*/
- bool MeshClass::Intersect_AABox(AABoxIntersectionTestClass & boxtest)
- {
- if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
- Matrix3D inv_tm;
- Get_Transform().Get_Orthogonal_Inverse(inv_tm);
- OBBoxIntersectionTestClass local_test(boxtest,inv_tm);
- WWASSERT(Model);
- return Model->Intersect_OBBox(local_test);
- }
- /***********************************************************************************************
- * MeshClass::Intersect_OBBox -- test for intersection with the given OBBox *
- * *
- * The given OBBox is assumed to be in world space so we need to transform it into the mesh's *
- * local coordinate system. *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/19/00 gth : Created. *
- *=============================================================================================*/
- bool MeshClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
- {
- if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
- Matrix3D inv_tm;
- Get_Transform().Get_Orthogonal_Inverse(inv_tm);
- OBBoxIntersectionTestClass local_test(boxtest,inv_tm);
- WWASSERT(Model);
- return Model->Intersect_OBBox(local_test);
- }
- /***********************************************************************************************
- * MeshClass::Get_Obj_Space_Bounding_Sphere -- returns obj-space bounding sphere *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/19/00 gth : Created. *
- *=============================================================================================*/
- void MeshClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
- {
- if (Model) {
- Model->Get_Bounding_Sphere(&sphere);
- } else {
- sphere.Center.Set(0,0,0);
- sphere.Radius = 1.0f;
- }
- }
- /***********************************************************************************************
- * MeshClass::Get_Obj_Space_Bounding_Box -- returns the obj-space bounding box *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 1/19/00 gth : Created. *
- *=============================================================================================*/
- void MeshClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
- {
- if (Model) {
- Model->Get_Bounding_Box(&box);
- } else {
- box.Init(Vector3(0,0,0),Vector3(1,1,1));
- }
- }
- /***********************************************************************************************
- * MeshClass::Generate_Culling_Tree -- Generates a hierarchical culling tree for the mesh *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 6/18/98 GTH : Created. *
- *=============================================================================================*/
- void MeshClass::Generate_Culling_Tree(void)
- {
- Model->Generate_Culling_Tree();
- }
- /***********************************************************************************************
- * MeshClass::Add_Dependencies_To_List -- Add dependent files to the list. *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 3/18/99 PDS : Created. *
- *=============================================================================================*/
- void MeshClass::Add_Dependencies_To_List
- (
- DynamicVectorClass<StringClass> &file_list,
- bool textures_only
- )
- {
- //
- // Get a pointer to this mesh's material information object
- //
- MaterialInfoClass *material = Get_Material_Info ();
- if (material != NULL) {
-
- //
- // Loop through all the textures and add their filenames to our list
- //
- for (int index = 0; index < material->Texture_Count (); index ++) {
-
- //
- // Add this texture's filename to the list
- //
- TextureClass *texture = material->Peek_Texture (index);
- if (texture != NULL) {
- file_list.Add (texture->Get_Full_Path ());
- }
- }
-
- //
- // Release our hold on the material information object
- //
- material->Release_Ref ();
- }
- RenderObjClass::Add_Dependencies_To_List (file_list, textures_only);
- return ;
- }
- /***********************************************************************************************
- * MeshClass::Update_Cached_Bounding_Volumes -- default collision sphere. *
- * *
- * INPUT: *
- * *
- * OUTPUT: *
- * *
- * WARNINGS: *
- * *
- * HISTORY: *
- * 5/14/2001 NH : Created. *
- *=============================================================================================*/
- void MeshClass::Update_Cached_Bounding_Volumes(void) const
- {
- Get_Obj_Space_Bounding_Sphere(CachedBoundingSphere);
- #ifdef ALLOW_TEMPORARIES
- CachedBoundingSphere.Center = Get_Transform() * CachedBoundingSphere.Center;
- #else
- Get_Transform().mulVector3(CachedBoundingSphere.Center);
- #endif
- // If we are camera-aligned or -oriented, we don't know which way we are facing at this point,
- // so the box we return needs to contain the sphere. Otherewise do the normal computation.
- if (Model->Get_Flag(MeshModelClass::ALIGNED) || Model->Get_Flag(MeshModelClass::ORIENTED)) {
- CachedBoundingBox.Center = CachedBoundingSphere.Center;
- CachedBoundingBox.Extent.Set(CachedBoundingSphere.Radius, CachedBoundingSphere.Radius, CachedBoundingSphere.Radius);
- } else {
- Get_Obj_Space_Bounding_Box(CachedBoundingBox);
- CachedBoundingBox.Transform(Get_Transform());
- }
- Validate_Cached_Bounding_Volumes();
- }
- // This utility function recurses throughout the subobjects of a renderobject, and for each
- // MeshClass it finds it sets the given MeshModel flag on its model. This is useful for stuff
- // like making a RenderObjects' polys sort.
- void Set_MeshModel_Flag(RenderObjClass *robj, int flag, int onoff)
- {
- if (robj->Class_ID() == RenderObjClass::CLASSID_MESH) {
- // Set flag on model (the assumption is that meshes don't have subobjects)
- MeshClass *mesh = (MeshClass *)robj;
- MeshModelClass *model = mesh->Get_Model();
- model->Set_Flag((MeshModelClass::FlagsType)flag, onoff != 0);
- model->Release_Ref();
- } else {
- // Recurse to subobjects (if any)
- int num_obj = robj->Get_Num_Sub_Objects();
- RenderObjClass *sub_obj;
- for (int i = 0; i < num_obj; i++) {
- sub_obj = robj->Get_Sub_Object(i);
- if (sub_obj) {
- Set_MeshModel_Flag(sub_obj, flag, onoff);
- sub_obj->Release_Ref();
- }
- }
- }
- }
- int MeshClass::Get_Sort_Level(void) const
- {
- if (Model) {
- return (Model->Get_Sort_Level());
- }
- return(SORT_LEVEL_NONE);
- }
- void MeshClass::Set_Sort_Level(int level)
- {
- if (Model) {
- Model->Set_Sort_Level(level);
- }
- }
- int MeshClass::Get_Draw_Call_Count(void) const
- {
- if (Model != NULL) {
- // Prefer to return the number of polygon renderers
- int prcount = Model->PolygonRendererList.Count();
- if (prcount > 0) {
- return prcount;
- }
-
- // Otherwise if we have textures, return the number of textures (e.g. dont have prs when sorting)
- if ((Model->MatInfo != NULL) && (Model->MatInfo->Texture_Count() > 0)) {
- return Model->MatInfo->Texture_Count();
- }
- // Otherwise, return 1
- return 1;
- } else {
- return 0;
- }
- }
|