mesh.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /VSS_Sync/ww3d2/mesh.cpp 56 8/29/01 9:50p Vss_sync $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando / G 3D engine *
  24. * *
  25. * File Name : MESH.CPP *
  26. * *
  27. * Programmer : Greg Hjelstrom *
  28. * *
  29. * Start Date : 06/11/97 *
  30. * *
  31. * Last Update : June 12, 1997 [GH] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * MeshClass::MeshClass -- Constructor for MeshClass *
  36. * MeshClass::MeshClass -- Copy Constructor for MeshClass *
  37. * MeshClass::operator == -- assignment operator for MeshClass *
  38. * MeshClass::~MeshClass -- destructor *
  39. * MeshClass::Contains -- Determines whether mesh contains a (worldspace) point. *
  40. * MeshClass::Free -- Releases all memory/assets in use by this mesh *
  41. * MeshClass::Clone -- Creates a clone of this mesh *
  42. * MeshClass::Get_Name -- returns the name of the mesh *
  43. * MeshClass::Set_Name -- sets the name of this mesh *
  44. * MeshClass::Get_W3D_Flags -- access to the W3D flags *
  45. * MeshClass::Get_User_Text -- access to the text buffer *
  46. * MeshClass::Scale -- Scales the mesh *
  47. * MeshClass::Scale -- Scales the mesh *
  48. * MeshClass::Init -- Init the mesh from a MeshBuilder object *
  49. * MeshClass::Load -- creates a mesh out of a mesh chunk in a .w3d file *
  50. * MeshClass::Cast_Ray -- compute a ray intersection with this mesh *
  51. * MeshClass::Cast_AABox -- cast an AABox against this mesh *
  52. * MeshClass::Cast_OBBox -- Cast an obbox against this mesh *
  53. * MeshClass::Intersect_AABox -- test for intersection with given AABox *
  54. * MeshClass::Intersect_OBBox -- test for intersection with the given OBBox *
  55. * MeshClass::Generate_Culling_Tree -- Generates a hierarchical culling tree for the mesh *
  56. * MeshClass::Direct_Load -- read the w3d file directly into this mesh object *
  57. * MeshClass::read_chunks -- read all of the chunks from the .wtm file *
  58. * MeshClass::read_vertices -- reads the vertex chunk *
  59. * MeshClass::read_texcoords -- read in the texture coordinates chunk *
  60. * MeshClass::install_texture_coordinates -- installs the given u-v's in each channel that is*
  61. * MeshClass::read_vertex_normals -- reads a surrender normal chunk from the wtm file *
  62. * MeshClass::read_v3_materials -- Reads in version 3 materials. *
  63. * MeshClass::read_map -- Reads definition of a texture map from the file *
  64. * MeshClass::read_triangles -- read the triangles chunk *
  65. * MeshClass::read_per_tri_materials -- read the material indices for each triangle *
  66. * MeshClass::read_user_text -- read in the user text chunk *
  67. * MeshClass::read_vertex_colors -- read in the vertex colors chunk *
  68. * MeshClass::read_vertex_influences -- read in the vertex influences chunk *
  69. * MeshClass::Get_Material_Info -- returns a pointer to the material info *
  70. * MeshClass::Create_Decal -- creates a decal on this mesh *
  71. * MeshClass::Delete_Decal -- removes a decal from this mesh *
  72. * MeshClass::Get_Num_Polys -- returns the number of polys (tris) in this mesh *
  73. * MeshClass::Render -- renders this mesh *
  74. * MeshClass::Special_Render -- special render function for meshes *
  75. * MeshClass::update_skin -- deforms the mesh *
  76. * MeshClass::clone_materials -- clone the materials for this mesh *
  77. * MeshClass::install_materials -- transfers the materials into the mesh *
  78. * MeshClass::Get_Model -- user access to the mesh model *
  79. * MeshClass::Get_Obj_Space_Bounding_Sphere -- returns obj-space bounding sphere *
  80. * MeshClass::Get_Obj_Space_Bounding_Box -- returns the obj-space bounding box *
  81. * MeshClass::Get_Deformed_Vertices -- Gets the deformed vertices for a skin *
  82. * MeshClass::Get_Deformed_Vertices -- Gets the deformed vertices for a skin *
  83. * MeshClass::Render_Material_Pass -- Render a procedural material pass for this mesh *
  84. * MeshClass::Replace_VertexMaterial -- Replaces existing vertex material with a new one. Wi *
  85. * MeshClass::Make_Unique -- Makes mesh unique in the renderer, but still shares system ram *
  86. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  87. #include "mesh.h"
  88. #include <assert.h>
  89. #include <string.h>
  90. #include "w3d_file.h"
  91. #include "assetmgr.h"
  92. #include "w3derr.h"
  93. #include "wwdebug.h"
  94. #include "vertmaterial.h"
  95. #include "shader.h"
  96. #include "matinfo.h"
  97. #include "htree.h"
  98. #include "meshbuild.h"
  99. #include "tri.h"
  100. #include "aaplane.h"
  101. #include "aabtree.h"
  102. #include "chunkio.h"
  103. #include "w3d_util.h"
  104. #include "meshmdl.h"
  105. #include "meshgeometry.h"
  106. #include "ww3d.h"
  107. #include "camera.h"
  108. #include "texture.h"
  109. #include "rinfo.h"
  110. #include "coltest.h"
  111. #include "inttest.h"
  112. #include "decalmsh.h"
  113. #include "decalsys.h"
  114. #include "dx8polygonrenderer.h"
  115. #include "dx8indexbuffer.h"
  116. #include "dx8renderer.h"
  117. #include "visrasterizer.h"
  118. #include "wwmemlog.h"
  119. #include <stdio.h>
  120. bool MeshClass::Legacy_Meshes_Fogged = true;
  121. /*
  122. ** This #define causes the collision code to always recompute the triangle normals rather
  123. ** than using the ones in the model.
  124. ** TODO: ensure that the models have unit normals and start re-using them again or write
  125. ** collision code to handle non-unit normals!
  126. */
  127. #if (OPTIMIZE_PLANEEQ_RAM)
  128. #define COMPUTE_NORMALS
  129. #endif
  130. /*
  131. ** Temporary storage used during decal creation
  132. */
  133. static DynamicVectorClass<Vector3> _TempVertexBuffer;
  134. /***********************************************************************************************
  135. * MeshClass::MeshClass -- Constructor for MeshClass *
  136. * *
  137. * Initializes an empty mesh class *
  138. * *
  139. * INPUT: *
  140. * *
  141. * OUTPUT: *
  142. * *
  143. * WARNINGS: *
  144. * *
  145. * HISTORY: *
  146. * 1/6/98 GTH : Created. *
  147. *=============================================================================================*/
  148. MeshClass::MeshClass(void) :
  149. Model(NULL),
  150. DecalMesh(NULL),
  151. LightEnvironment(NULL),
  152. BaseVertexOffset(0),
  153. NextVisibleSkin(NULL),
  154. m_alphaOverride(1.0f),
  155. m_materialPassAlphaOverride(1.0f),
  156. m_materialPassEmissiveOverride(1.0f)
  157. {
  158. }
  159. /***********************************************************************************************
  160. * MeshClass::MeshClass -- Copy Constructor for MeshClass *
  161. * *
  162. * Creates a mesh which is a copy of the given mesh *
  163. * *
  164. * INPUT: *
  165. * *
  166. * OUTPUT: *
  167. * *
  168. * WARNINGS: *
  169. * *
  170. * HISTORY: *
  171. *=============================================================================================*/
  172. MeshClass::MeshClass(const MeshClass & that) :
  173. RenderObjClass(that),
  174. Model(NULL),
  175. DecalMesh(NULL),
  176. LightEnvironment(NULL),
  177. BaseVertexOffset(that.BaseVertexOffset),
  178. NextVisibleSkin(NULL),
  179. m_alphaOverride(1.0f),
  180. m_materialPassAlphaOverride(1.0f),
  181. m_materialPassEmissiveOverride(1.0f)
  182. {
  183. REF_PTR_SET(Model,that.Model); // mesh instances share models by default
  184. }
  185. /***********************************************************************************************
  186. * operator == -- assignment operator for MeshClass *
  187. * *
  188. * INPUT: *
  189. * *
  190. * OUTPUT: *
  191. * *
  192. * WARNINGS: *
  193. * *
  194. * HISTORY: *
  195. * 1/6/98 GTH : Created. *
  196. *=============================================================================================*/
  197. MeshClass & MeshClass::operator = (const MeshClass & that)
  198. {
  199. if (this != &that) {
  200. RenderObjClass::operator = (that);
  201. REF_PTR_SET(Model,that.Model); // mesh instances share models by default
  202. BaseVertexOffset = that.BaseVertexOffset;
  203. // just dont copy the decals or light environment
  204. REF_PTR_RELEASE(DecalMesh);
  205. LightEnvironment = NULL;
  206. }
  207. return * this;
  208. }
  209. /***********************************************************************************************
  210. * MeshClass::~MeshClass -- destructor *
  211. * *
  212. * INPUT: *
  213. * *
  214. * OUTPUT: *
  215. * *
  216. * WARNINGS: *
  217. * *
  218. * HISTORY: *
  219. * 1/6/98 GTH : Created. *
  220. *=============================================================================================*/
  221. MeshClass::~MeshClass(void)
  222. {
  223. Free();
  224. }
  225. /***********************************************************************************************
  226. * MeshClass::Contains -- Determines whether mesh contains a (worldspace) point. *
  227. * *
  228. * INPUT: *
  229. * *
  230. * OUTPUT: *
  231. * *
  232. * WARNINGS: Assumes mesh is a closed manifold - results are undefined otherwise *
  233. * This function will NOT work for skins *
  234. * *
  235. * HISTORY: *
  236. * 8/30/00 NH : Created. *
  237. *=============================================================================================*/
  238. bool MeshClass::Contains(const Vector3 &point)
  239. {
  240. // Transform point to object space and pass on to model
  241. Vector3 obj_point;
  242. Matrix3D::Inverse_Transform_Vector(Transform, point, &obj_point);
  243. return Model->Contains(obj_point);
  244. }
  245. /***********************************************************************************************
  246. * MeshClass::Free -- Releases all memory/assets in use by this mesh *
  247. * *
  248. * INPUT: *
  249. * *
  250. * OUTPUT: *
  251. * *
  252. * WARNINGS: *
  253. * *
  254. * HISTORY: *
  255. * 1/6/98 GTH : Created. *
  256. *=============================================================================================*/
  257. void MeshClass::Free(void)
  258. {
  259. REF_PTR_RELEASE(Model);
  260. REF_PTR_RELEASE(DecalMesh);
  261. }
  262. /***********************************************************************************************
  263. * MeshClass::Clone -- Creates a clone of this mesh *
  264. * *
  265. * INPUT: *
  266. * *
  267. * OUTPUT: *
  268. * *
  269. * WARNINGS: *
  270. * *
  271. * HISTORY: *
  272. * 1/6/98 GTH : Created. *
  273. *=============================================================================================*/
  274. RenderObjClass * MeshClass::Clone(void) const
  275. {
  276. return NEW_REF( MeshClass, (*this));
  277. }
  278. /***********************************************************************************************
  279. * MeshClass::Get_Name -- returns the name of the mesh *
  280. * *
  281. * INPUT: *
  282. * *
  283. * OUTPUT: *
  284. * *
  285. * WARNINGS: *
  286. * *
  287. * HISTORY: *
  288. * 5/15/98 GTH : Created. *
  289. *=============================================================================================*/
  290. const char * MeshClass::Get_Name(void) const
  291. {
  292. return Model->Get_Name();
  293. }
  294. /***********************************************************************************************
  295. * MeshClass::Set_Name -- sets the name of this mesh *
  296. * *
  297. * INPUT: *
  298. * *
  299. * OUTPUT: *
  300. * *
  301. * WARNINGS: *
  302. * *
  303. * HISTORY: *
  304. * 3/9/99 GTH : Created. *
  305. *=============================================================================================*/
  306. void MeshClass::Set_Name(const char * name)
  307. {
  308. Model->Set_Name(name);
  309. }
  310. /***********************************************************************************************
  311. * MeshClass::Get_W3D_Flags -- access to the W3D flags *
  312. * *
  313. * INPUT: *
  314. * *
  315. * OUTPUT: *
  316. * *
  317. * WARNINGS: *
  318. * *
  319. * HISTORY: *
  320. * 5/15/98 GTH : Created. *
  321. *=============================================================================================*/
  322. uint32 MeshClass::Get_W3D_Flags(void)
  323. {
  324. return Model->W3dAttributes;
  325. }
  326. /***********************************************************************************************
  327. * MeshClass::Get_User_Text -- access to the text buffer *
  328. * *
  329. * INPUT: *
  330. * *
  331. * OUTPUT: *
  332. * *
  333. * WARNINGS: *
  334. * *
  335. * HISTORY: *
  336. * 5/15/98 GTH : Created. *
  337. *=============================================================================================*/
  338. const char * MeshClass::Get_User_Text(void) const
  339. {
  340. return Model->Get_User_Text();
  341. }
  342. /***********************************************************************************************
  343. * MeshClass::Get_Material_Info -- returns a pointer to the material info *
  344. * *
  345. * INPUT: *
  346. * *
  347. * OUTPUT: *
  348. * *
  349. * WARNINGS: *
  350. * *
  351. * HISTORY: *
  352. * 5/20/98 GTH : Created. *
  353. *=============================================================================================*/
  354. MaterialInfoClass * MeshClass::Get_Material_Info(void)
  355. {
  356. if (Model) {
  357. if (Model->MatInfo) {
  358. Model->MatInfo->Add_Ref();
  359. return Model->MatInfo;
  360. }
  361. }
  362. return NULL;
  363. }
  364. /***********************************************************************************************
  365. * MeshClass::Get_Model -- user access to the mesh model *
  366. * *
  367. * INPUT: *
  368. * *
  369. * OUTPUT: *
  370. * *
  371. * WARNINGS: *
  372. * *
  373. * HISTORY: *
  374. * 2/4/99 GTH : Created. *
  375. *=============================================================================================*/
  376. MeshModelClass * MeshClass::Get_Model(void)
  377. {
  378. if (Model != NULL) {
  379. Model->Add_Ref();
  380. }
  381. return Model;
  382. }
  383. /***********************************************************************************************
  384. * MeshClass::Scale -- Scales the mesh *
  385. * *
  386. * INPUT: *
  387. * *
  388. * OUTPUT: *
  389. * *
  390. * WARNINGS: *
  391. * *
  392. * HISTORY: *
  393. *=============================================================================================*/
  394. void MeshClass::Scale(float scale)
  395. {
  396. if (scale==1.0f) return;
  397. Vector3 sc;
  398. sc.X = sc.Y = sc.Z = scale;
  399. Make_Unique();
  400. Model->Make_Geometry_Unique();
  401. Model->Scale(sc);
  402. Invalidate_Cached_Bounding_Volumes();
  403. // Now update the object space bounding volumes of this object's container:
  404. RenderObjClass *container = Get_Container();
  405. if (container) container->Update_Obj_Space_Bounding_Volumes();
  406. }
  407. /***********************************************************************************************
  408. * MeshClass::Scale -- Scales the mesh *
  409. * *
  410. * INPUT: *
  411. * *
  412. * OUTPUT: *
  413. * *
  414. * WARNINGS: *
  415. * *
  416. * HISTORY: *
  417. * 1/6/98 GTH : Created. *
  418. * 12/8/98 GTH : Modified the box scaling to use the non-uniform parameters *
  419. *=============================================================================================*/
  420. void MeshClass::Scale(float scalex, float scaley, float scalez)
  421. {
  422. // scale the surrender mesh model
  423. Vector3 sc;
  424. sc.X = scalex;
  425. sc.Y = scaley;
  426. sc.Z = scalez;
  427. Make_Unique();
  428. Model->Make_Geometry_Unique();
  429. Model->Scale(sc);
  430. Invalidate_Cached_Bounding_Volumes();
  431. // Now update the object space bounding volumes of this object's container:
  432. RenderObjClass *container = Get_Container();
  433. if (container) container->Update_Obj_Space_Bounding_Volumes();
  434. }
  435. /***********************************************************************************************
  436. * MeshClass::Get_Deformed_Vertices -- Gets the deformed vertices for a skin *
  437. * *
  438. * INPUT: *
  439. * *
  440. * OUTPUT: *
  441. * *
  442. * WARNINGS: *
  443. * *
  444. * HISTORY: *
  445. * 3/4/2001 gth : Created. *
  446. *=============================================================================================*/
  447. void MeshClass::Get_Deformed_Vertices(Vector3 *dst_vert, Vector3 *dst_norm)
  448. {
  449. WWASSERT(Model->Get_Flag(MeshGeometryClass::SKIN));
  450. Model->get_deformed_vertices(dst_vert,dst_norm,Container->Get_HTree());
  451. }
  452. /***********************************************************************************************
  453. * MeshClass::Get_Deformed_Vertices -- Gets the deformed vertices for a skin *
  454. * *
  455. * INPUT: *
  456. * *
  457. * OUTPUT: *
  458. * *
  459. * WARNINGS: *
  460. * *
  461. * HISTORY: *
  462. * 3/4/2001 gth : Created. *
  463. *=============================================================================================*/
  464. void MeshClass::Get_Deformed_Vertices(Vector3 *dst_vert)
  465. {
  466. WWASSERT(Model->Get_Flag(MeshGeometryClass::SKIN));
  467. WWASSERT(Container != NULL);
  468. WWASSERT(Container->Get_HTree() != NULL);
  469. Model->get_deformed_vertices(dst_vert,Container->Get_HTree());
  470. }
  471. void MeshClass::Compose_Deformed_Vertex_Buffer(
  472. VertexFormatXYZNDUV2* verts,
  473. const Vector2* uv0,
  474. const Vector2* uv1,
  475. const unsigned* diffuse)
  476. {
  477. WWASSERT(Model->Get_Flag(MeshGeometryClass::SKIN));
  478. Model->compose_deformed_vertex_buffer(verts,uv0,uv1,diffuse,Container->Get_HTree());
  479. }
  480. /***********************************************************************************************
  481. * MeshClass::Create_Decal -- creates a decal on this mesh *
  482. * *
  483. * INPUT: *
  484. * *
  485. * OUTPUT: *
  486. * *
  487. * WARNINGS: *
  488. * *
  489. * HISTORY: *
  490. * 1/26/00 gth : Created. *
  491. *=============================================================================================*/
  492. void MeshClass::Create_Decal(DecalGeneratorClass * generator)
  493. {
  494. WWMEMLOG(MEM_GEOMETRY);
  495. if (WW3D::Are_Decals_Enabled() == false) {
  496. return;
  497. }
  498. if (Is_Translucent() && (generator->Is_Applied_To_Translucent_Meshes() == false)) {
  499. return;
  500. }
  501. if (!Model->Get_Flag(MeshGeometryClass::SKIN)) {
  502. // Rigid mesh
  503. Matrix3D modeltm_inv;
  504. OBBoxClass localbox;
  505. Get_Transform().Get_Orthogonal_Inverse(modeltm_inv);
  506. OBBoxClass::Transform(modeltm_inv, generator->Get_Bounding_Volume(), &localbox);
  507. // generate apt, if it is not empty, add a decal.
  508. SimpleDynVecClass<uint32> apt;
  509. Model->Generate_Rigid_APT(localbox, apt);
  510. if (apt.Count() > 0) {
  511. if (DecalMesh == NULL) {
  512. DecalMesh = NEW_REF(RigidDecalMeshClass, (this, generator->Peek_Decal_System()));
  513. }
  514. DecalMesh->Create_Decal(generator, localbox, apt);
  515. }
  516. } else {
  517. WWDEBUG_SAY(("PERFORMANCE WARNING: Decal being applied to a SKIN mesh!\r\n"));
  518. // Skin
  519. // The deformed worldspace vertices are used both for the APT and in Create_Decal() to
  520. // generate the texture coordinates.
  521. int vertex_count = Model->Get_Vertex_Count();
  522. if (_TempVertexBuffer.Count() < vertex_count) _TempVertexBuffer.Resize(vertex_count);
  523. Vector3 *dst_vert = &(_TempVertexBuffer[0]);
  524. Get_Deformed_Vertices(dst_vert);
  525. // generate apt, if it is not empty, add a decal.
  526. SimpleDynVecClass<uint32> apt;
  527. OBBoxClass worldbox = generator->Get_Bounding_Volume();
  528. // We compare the worldspace box vs. the worldspace vertices
  529. Model->Generate_Skin_APT(worldbox, apt, dst_vert);
  530. // if it is not empty, add a decal
  531. if (apt.Count() > 0) {
  532. if (DecalMesh == NULL) {
  533. DecalMesh = NEW_REF(SkinDecalMeshClass, (this, generator->Peek_Decal_System()));
  534. }
  535. DecalMesh->Create_Decal(generator, worldbox, apt, &_TempVertexBuffer);
  536. }
  537. }
  538. }
  539. /***********************************************************************************************
  540. * MeshClass::Delete_Decal -- removes a decal from this mesh *
  541. * *
  542. * INPUT: *
  543. * *
  544. * OUTPUT: *
  545. * *
  546. * WARNINGS: *
  547. * *
  548. * HISTORY: *
  549. * 1/26/00 gth : Created. *
  550. *=============================================================================================*/
  551. void MeshClass::Delete_Decal(uint32 decal_id)
  552. {
  553. if (DecalMesh != NULL) {
  554. DecalMesh->Delete_Decal(decal_id);
  555. }
  556. }
  557. /***********************************************************************************************
  558. * MeshClass::Get_Num_Polys -- returns the number of polys (tris) in this mesh *
  559. * *
  560. * INPUT: *
  561. * *
  562. * OUTPUT: *
  563. * *
  564. * WARNINGS: *
  565. * *
  566. * HISTORY: *
  567. * 1/6/98 GTH : Created. *
  568. *=============================================================================================*/
  569. int MeshClass::Get_Num_Polys(void) const
  570. {
  571. if (Model) {
  572. return Model->Get_Polygon_Count();
  573. } else {
  574. return 0;
  575. }
  576. }
  577. /***********************************************************************************************
  578. * MeshClass::Render -- renders this mesh *
  579. * *
  580. * INPUT: *
  581. * *
  582. * OUTPUT: *
  583. * *
  584. * WARNINGS: *
  585. * *
  586. * HISTORY: *
  587. * 12/10/98 GTH : Created. *
  588. *=============================================================================================*/
  589. void MeshClass::Render(RenderInfoClass & rinfo)
  590. {
  591. if (Is_Not_Hidden_At_All() == false) {
  592. return;
  593. }
  594. // If static sort lists are enabled and this mesh has a sort level, put it on the list instead
  595. // of rendering it.
  596. unsigned int sort_level = (unsigned int)Model->Get_Sort_Level();
  597. if (WW3D::Are_Static_Sort_Lists_Enabled() && sort_level != SORT_LEVEL_NONE) {
  598. Set_Lighting_Environment(rinfo.light_environment);
  599. //Add custom alpha
  600. m_alphaOverride = rinfo.alphaOverride;
  601. m_materialPassAlphaOverride = rinfo.materialPassAlphaOverride;
  602. m_materialPassEmissiveOverride = rinfo.materialPassEmissiveOverride;
  603. WW3D::Add_To_Static_Sort_List(this, sort_level);
  604. } else {
  605. const FrustumClass & frustum=rinfo.Camera.Get_Frustum();
  606. if ( Model->Get_Flag(MeshGeometryClass::SKIN) ||
  607. CollisionMath::Overlap_Test(frustum,Get_Bounding_Box())!=CollisionMath::OUTSIDE )
  608. {
  609. bool rendered_something = false;
  610. /*
  611. ** If this mesh model has never been rendered, we need to generate the DX8 datastructures
  612. */
  613. if (Model->PolygonRendererList.Is_Empty()) {
  614. Model->Register_For_Rendering();
  615. WWASSERT(!Model->PolygonRendererList.Is_Empty());
  616. }
  617. /*
  618. ** Plug in lighting
  619. */
  620. if (sort_level == SORT_LEVEL_NONE) //sorting ones get their environment set above.
  621. { Set_Lighting_Environment(rinfo.light_environment);
  622. //Add custom alpha
  623. m_alphaOverride = rinfo.alphaOverride;
  624. m_materialPassAlphaOverride = rinfo.materialPassAlphaOverride;
  625. m_materialPassEmissiveOverride = rinfo.materialPassEmissiveOverride;
  626. }
  627. /*
  628. ** Process texture reductions:
  629. */
  630. // Model->Process_Texture_Reduction();
  631. /*
  632. ** Look up the FVF container that this mesh is in
  633. ** TODO: make this a little nicer?
  634. */
  635. DX8FVFCategoryContainer * fvf_container = Model->PolygonRendererList.Peek_Head()->Get_Texture_Category()->Get_Container();
  636. if ((rinfo.Current_Override_Flags() & RenderInfoClass::RINFO_OVERRIDE_ADDITIONAL_PASSES_ONLY) == 0) {
  637. /*
  638. ** Link each polygon renderer for this mesh into the visible list
  639. */
  640. DX8PolygonRendererListIterator it(&(Model->PolygonRendererList));
  641. while (!it.Is_Done()) {
  642. DX8PolygonRendererClass* polygon_renderer=it.Peek_Obj();
  643. polygon_renderer->Get_Texture_Category()->Add_Render_Task(polygon_renderer,this);
  644. it.Next();
  645. }
  646. rendered_something = true;
  647. }
  648. /*
  649. ** If the rendering context specifies procedural material passes, register them
  650. ** for rendering
  651. */
  652. for (int i=0; i<rinfo.Additional_Pass_Count(); i++) {
  653. MaterialPassClass * matpass = rinfo.Peek_Additional_Pass(i);
  654. if ((!Is_Translucent()) || (matpass->Is_Enabled_On_Translucent_Meshes())) {
  655. fvf_container->Add_Visible_Material_Pass(matpass,this);
  656. rendered_something = true;
  657. }
  658. }
  659. /*
  660. ** If we rendered any base or procedural passes and this is a skin, we need
  661. ** to tell the mesh rendering system to process this skin
  662. */
  663. if (rendered_something && Model->Get_Flag(MeshGeometryClass::SKIN)) {
  664. //WWASSERT(dynamic_cast<DX8SkinFVFCategoryContainer *>(fvf_container) != NULL);
  665. static_cast<DX8SkinFVFCategoryContainer*>(fvf_container)->Add_Visible_Skin(this);
  666. }
  667. /*
  668. ** If we have a decal mesh, link it into the mesh rendering system
  669. */
  670. if (DecalMesh != NULL) {
  671. const SphereClass & ws_sphere = Get_Bounding_Sphere();
  672. Vector3 cam_space_sphere_center;
  673. rinfo.Camera.Transform_To_View_Space(cam_space_sphere_center,ws_sphere.Center);
  674. if (-cam_space_sphere_center.Z - ws_sphere.Radius < WW3D::Get_Decal_Rejection_Distance()) {
  675. TheDX8MeshRenderer.Add_To_Render_List(DecalMesh);
  676. }
  677. }
  678. }
  679. }
  680. }
  681. /***********************************************************************************************
  682. * MeshClass::Render_Material_Pass -- Render a procedural material pass for this mesh *
  683. * *
  684. * INPUT: *
  685. * *
  686. * OUTPUT: *
  687. * *
  688. * WARNINGS: *
  689. * *
  690. * HISTORY: *
  691. * 3/4/2001 gth : Created. *
  692. *=============================================================================================*/
  693. void MeshClass::Render_Material_Pass(MaterialPassClass * pass,IndexBufferClass * ib)
  694. {
  695. //Added to allow dynamic opacity on additional render passed
  696. //without having to create a new material pass per object instance. -MW
  697. float oldOpacity=-1.0f;
  698. Vector3 oldEmissive(-1,-1,-1);
  699. if (LightEnvironment != NULL) {
  700. DX8Wrapper::Set_Light_Environment(LightEnvironment);
  701. }
  702. if (Model->Get_Flag(MeshModelClass::SKIN)) {
  703. /*
  704. ** In the case of skin meshes, we need to render our polys with the identity transform
  705. */
  706. if (m_materialPassAlphaOverride != 1.0f)
  707. { VertexMaterialClass *mat=pass->Peek_Material();
  708. if (mat)
  709. {
  710. oldOpacity=mat->Get_Opacity();
  711. mat->Set_Opacity(m_materialPassAlphaOverride);
  712. }
  713. }
  714. if (m_materialPassEmissiveOverride != 1.0f)
  715. { VertexMaterialClass *mat=pass->Peek_Material();
  716. if (mat)
  717. {
  718. mat->Get_Emissive(&oldEmissive);
  719. mat->Set_Emissive(m_materialPassEmissiveOverride*oldEmissive);
  720. }
  721. }
  722. pass->Install_Materials();
  723. DX8Wrapper::Set_Index_Buffer(ib,0);
  724. SNAPSHOT_SAY(("Set_World_Identity\n"));
  725. DX8Wrapper::Set_World_Identity();
  726. DX8PolygonRendererListIterator it(&Model->PolygonRendererList);
  727. while (!it.Is_Done()) {
  728. if (it.Peek_Obj()->Get_Pass() == 0)
  729. it.Peek_Obj()->Render(BaseVertexOffset);
  730. it.Next();
  731. }
  732. if (oldOpacity >= 0)
  733. { //opacity was modified for this mesh instance, so need to restore the material setting which may be shared
  734. //among other instances.
  735. pass->Peek_Material()->Set_Opacity(oldOpacity);
  736. }
  737. if (oldEmissive.X >= 0)
  738. { //emissive was modified for this mesh instance, so need to restore the material setting which may be shared
  739. //among other instances.
  740. pass->Peek_Material()->Set_Emissive(oldEmissive);
  741. }
  742. //MW: Need uninstall custom materials in case they leave D3D in unknown state
  743. pass->UnInstall_Materials();
  744. } else if ((pass->Get_Cull_Volume() != NULL) && (MaterialPassClass::Is_Per_Polygon_Culling_Enabled())) {
  745. /*
  746. ** Generate the APT
  747. */
  748. static SimpleDynVecClass<uint32> _apt;
  749. _apt.Delete_All(false);
  750. Matrix3D modeltminv;
  751. Get_Transform().Get_Orthogonal_Inverse(modeltminv);
  752. OBBoxClass localbox;
  753. OBBoxClass::Transform(modeltminv,*(pass->Get_Cull_Volume()),&localbox);
  754. Vector3 view_dir;
  755. localbox.Basis.Get_Z_Vector(&view_dir);
  756. view_dir = -view_dir;
  757. if (Model->Has_Cull_Tree()) {
  758. Model->Generate_Rigid_APT(localbox,view_dir,_apt);
  759. } else {
  760. Model->Generate_Rigid_APT(view_dir,_apt);
  761. }
  762. if (_apt.Count() > 0) {
  763. int buftype = BUFFER_TYPE_DYNAMIC_DX8;
  764. if (Model->Get_Flag(MeshGeometryClass::SORT) && WW3D::Is_Sorting_Enabled()) {
  765. buftype = BUFFER_TYPE_DYNAMIC_SORTING;
  766. }
  767. /*
  768. ** Spew triangles in the APT into the dynamic index buffer
  769. */
  770. int min_v = Model->Get_Vertex_Count();
  771. int max_v = 0;
  772. DynamicIBAccessClass dynamic_ib(buftype,_apt.Count() * 3);
  773. {
  774. DynamicIBAccessClass::WriteLockClass lock(&dynamic_ib);
  775. unsigned short * indices = lock.Get_Index_Array();
  776. const Vector3i * polys = Model->Get_Polygon_Array();
  777. for (int i=0; i < _apt.Count(); i++)
  778. {
  779. unsigned v0 = polys[_apt[i]].I;
  780. unsigned v1 = polys[_apt[i]].J;
  781. unsigned v2 = polys[_apt[i]].K;
  782. indices[i*3 + 0] = (unsigned short)v0;
  783. indices[i*3 + 1] = (unsigned short)v1;
  784. indices[i*3 + 2] = (unsigned short)v2;
  785. min_v = WWMath::Min(v0,min_v);
  786. min_v = WWMath::Min(v1,min_v);
  787. min_v = WWMath::Min(v2,min_v);
  788. max_v = WWMath::Max(v0,max_v);
  789. max_v = WWMath::Max(v1,max_v);
  790. max_v = WWMath::Max(v2,max_v);
  791. }
  792. }
  793. /*
  794. ** Render
  795. */
  796. int vertex_offset = Model->PolygonRendererList.Peek_Head()->Get_Vertex_Offset();
  797. pass->Install_Materials();
  798. DX8Wrapper::Set_Transform(D3DTS_WORLD,Get_Transform());
  799. DX8Wrapper::Set_Index_Buffer(dynamic_ib,vertex_offset);
  800. DX8Wrapper::Draw_Triangles(
  801. 0,
  802. _apt.Count(),
  803. min_v,
  804. max_v-min_v+1);
  805. //MW: Need uninstall custom materials in case they leave D3D in unknown state
  806. pass->UnInstall_Materials();
  807. }
  808. } else {
  809. /*
  810. ** Normal mesh case, render polys with this mesh's transform
  811. */
  812. if (m_materialPassAlphaOverride != 1.0f)
  813. { VertexMaterialClass *mat=pass->Peek_Material();
  814. if (mat)
  815. {
  816. oldOpacity=mat->Get_Opacity();
  817. mat->Set_Opacity(m_materialPassAlphaOverride);
  818. }
  819. }
  820. if (m_materialPassEmissiveOverride != 1.0f)
  821. { VertexMaterialClass *mat=pass->Peek_Material();
  822. if (mat)
  823. {
  824. mat->Get_Emissive(&oldEmissive);
  825. mat->Set_Emissive(m_materialPassEmissiveOverride*oldEmissive);
  826. }
  827. }
  828. pass->Install_Materials();
  829. DX8Wrapper::Set_Index_Buffer(ib,0);
  830. SNAPSHOT_SAY(("Set_World_Transform\n"));
  831. DX8Wrapper::Set_Transform(D3DTS_WORLD,Transform);
  832. DX8PolygonRendererListIterator it(&Model->PolygonRendererList);
  833. while (!it.Is_Done()) {
  834. if (it.Peek_Obj()->Get_Pass() == 0)
  835. it.Peek_Obj()->Render(BaseVertexOffset);
  836. it.Next();
  837. }
  838. if (oldOpacity >= 0)
  839. { //opacity was modified for this mesh instance, so need to restore the material setting which may be shared
  840. //among other instances.
  841. pass->Peek_Material()->Set_Opacity(oldOpacity);
  842. }
  843. if (oldEmissive.X >= 0)
  844. { //emissive was modified for this mesh instance, so need to restore the material setting which may be shared
  845. //among other instances.
  846. pass->Peek_Material()->Set_Emissive(oldEmissive);
  847. }
  848. //MW: Need uninstall custom materials in case they leave D3D in unknown state
  849. pass->UnInstall_Materials();
  850. }
  851. }
  852. /***********************************************************************************************
  853. * MeshClass::Special_Render -- special render function for meshes *
  854. * *
  855. * INPUT: *
  856. * *
  857. * OUTPUT: *
  858. * *
  859. * WARNINGS: *
  860. * *
  861. * HISTORY: *
  862. * 12/10/98 GTH : Created. *
  863. *=============================================================================================*/
  864. void MeshClass::Special_Render(SpecialRenderInfoClass & rinfo)
  865. {
  866. if ((Is_Not_Hidden_At_All() == false) && (rinfo.RenderType != SpecialRenderInfoClass::RENDER_SHADOW)) {
  867. return;
  868. }
  869. if (rinfo.RenderType == SpecialRenderInfoClass::RENDER_VIS) {
  870. WWASSERT(rinfo.VisRasterizer != NULL);
  871. if (Model->Get_Flag(MeshModelClass::SKIN) == 0) {
  872. rinfo.VisRasterizer->Set_Model_Transform(Transform);
  873. rinfo.VisRasterizer->Render_Triangles( Model->Get_Vertex_Array(),
  874. Model->Get_Vertex_Count(),
  875. Model->Get_Polygon_Array(),
  876. Model->Get_Polygon_Count(),
  877. Get_Bounding_Box() );
  878. } else {
  879. int vertex_count = Model->Get_Vertex_Count();
  880. if (_TempVertexBuffer.Count() < vertex_count) _TempVertexBuffer.Resize(vertex_count);
  881. Vector3 *dst_vert = &(_TempVertexBuffer[0]);
  882. Get_Deformed_Vertices(dst_vert);
  883. rinfo.VisRasterizer->Set_Model_Transform(Matrix3D::Identity);
  884. rinfo.VisRasterizer->Render_Triangles( dst_vert,
  885. Model->Get_Vertex_Count(),
  886. Model->Get_Polygon_Array(),
  887. Model->Get_Polygon_Count(),
  888. Get_Bounding_Box() );
  889. }
  890. }
  891. if (rinfo.RenderType == SpecialRenderInfoClass::RENDER_SHADOW) {
  892. const HTreeClass * htree = NULL;
  893. if (Container!=NULL) {
  894. htree = Container->Get_HTree();
  895. }
  896. Model->Shadow_Render(rinfo,Transform,htree);
  897. }
  898. }
  899. void MeshClass::Replace_Texture(TextureClass* texture,TextureClass* new_texture)
  900. {
  901. Model->Replace_Texture(texture,new_texture);
  902. }
  903. /***********************************************************************************************
  904. * MeshClass::Replace_VertexMaterial -- Replaces existing vertex material with a new one. Will *
  905. * *
  906. * *
  907. * *
  908. * *
  909. * INPUT: *
  910. * *
  911. * OUTPUT: *
  912. * *
  913. * WARNINGS: *
  914. * *
  915. * HISTORY: *
  916. * 4/2/2001 hy : Created. *
  917. *=============================================================================================*/
  918. void MeshClass::Replace_VertexMaterial(VertexMaterialClass* vmat,VertexMaterialClass* new_vmat)
  919. {
  920. Model->Replace_VertexMaterial(vmat,new_vmat);
  921. }
  922. /***********************************************************************************************
  923. * MeshClass::Make_Unique -- Makes mesh unique in the renderer, but still shares system ram ge *
  924. * *
  925. * *
  926. * *
  927. * *
  928. * INPUT: *
  929. * *
  930. * OUTPUT: *
  931. * *
  932. * WARNINGS: *
  933. * *
  934. * HISTORY: *
  935. * 4/2/2001 hy : Created. *
  936. *=============================================================================================*/
  937. void MeshClass::Make_Unique()
  938. {
  939. if (Model->Num_Refs()==1) return;
  940. MeshModelClass *newmesh=NEW_REF(MeshModelClass,(*Model));
  941. REF_PTR_SET(Model,newmesh);
  942. REF_PTR_RELEASE(newmesh);
  943. }
  944. /***********************************************************************************************
  945. * MeshClass::Load -- creates a mesh out of a mesh chunk in a .w3d file *
  946. * *
  947. * INPUT: *
  948. * *
  949. * OUTPUT: *
  950. * *
  951. * WARNINGS: *
  952. * *
  953. * HISTORY: *
  954. * 06/12/1997 GH : Created. *
  955. *=============================================================================================*/
  956. WW3DErrorType MeshClass::Load_W3D(ChunkLoadClass & cload)
  957. {
  958. Vector3 boxmin,boxmax;
  959. /*
  960. ** Make sure this mesh is "empty"
  961. */
  962. Free();
  963. /*
  964. ** Create empty MaterialInfo and Model
  965. */
  966. Model = NEW_REF(MeshModelClass,());
  967. if (Model == NULL) {
  968. WWDEBUG_SAY(("MeshClass::Load - Failed to allocate model\r\n"));
  969. return WW3D_ERROR_LOAD_FAILED;
  970. }
  971. /*
  972. ** Create and read in the model...
  973. */
  974. if (Model->Load_W3D(cload) != WW3D_ERROR_OK) {
  975. Free();
  976. return WW3D_ERROR_LOAD_FAILED;
  977. }
  978. /*
  979. ** Pull interesting stuff out of the w3d attributes bits
  980. */
  981. int col_bits = (Model->W3dAttributes & W3D_MESH_FLAG_COLLISION_TYPE_MASK) >> W3D_MESH_FLAG_COLLISION_TYPE_SHIFT;
  982. Set_Collision_Type( col_bits << 1 );
  983. Set_Hidden(Model->W3dAttributes & W3D_MESH_FLAG_HIDDEN);
  984. /*
  985. ** Indicate whether this mesh is translucent. The mesh is considered translucent
  986. ** if sorting has been enabled (alpha blending on pass 0) or if pass0 contains alpha-test.
  987. ** This flag is mainly being used by visibility preprocessing code in Renegade.
  988. */
  989. int is_translucent = Model->Get_Flag(MeshModelClass::SORT);
  990. int is_alpha = 0; //keep track of alpha pieces that require sorting (including static sort lists).
  991. int is_additive = 0;
  992. if (Model->Has_Shader_Array(0)) {
  993. for (int i=0; i<Model->Get_Polygon_Count(); i++) {
  994. is_translucent |= (Model->Get_Shader(i,0).Get_Alpha_Test() == ShaderClass::ALPHATEST_ENABLE);
  995. is_alpha |= (Model->Get_Shader(i,0).Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO ||
  996. Model->Get_Shader(i,0).Get_Src_Blend_Func() != ShaderClass::SRCBLEND_ONE) && (Model->Get_Shader(i,0).Get_Alpha_Test() != ShaderClass::ALPHATEST_ENABLE);
  997. is_additive |= (Model->Get_Shader(i,0).Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ONE &&
  998. Model->Get_Shader(i,0).Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE);
  999. }
  1000. } else {
  1001. is_translucent |= (Model->Get_Single_Shader(0).Get_Alpha_Test() == ShaderClass::ALPHATEST_ENABLE);
  1002. is_alpha |= (Model->Get_Single_Shader(0).Get_Dst_Blend_Func() != ShaderClass::DSTBLEND_ZERO ||
  1003. Model->Get_Single_Shader(0).Get_Src_Blend_Func() != ShaderClass::SRCBLEND_ONE) && (Model->Get_Single_Shader(0).Get_Alpha_Test() != ShaderClass::ALPHATEST_ENABLE);
  1004. is_additive |= (Model->Get_Single_Shader(0).Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ONE &&
  1005. Model->Get_Single_Shader(0).Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE);
  1006. }
  1007. Set_Translucent(is_translucent);
  1008. Set_Alpha(is_alpha);
  1009. Set_Additive(is_additive);
  1010. return WW3D_ERROR_OK;
  1011. }
  1012. /***********************************************************************************************
  1013. * MeshClass::Cast_Ray -- compute a ray intersection with this mesh *
  1014. * *
  1015. * INPUT: *
  1016. * *
  1017. * OUTPUT: *
  1018. * *
  1019. * WARNINGS: *
  1020. * *
  1021. * HISTORY: *
  1022. * 6/17/98 GTH : Created. *
  1023. *=============================================================================================*/
  1024. bool MeshClass::Cast_Ray(RayCollisionTestClass & raytest)
  1025. {
  1026. if ((Get_Collision_Type() & raytest.CollisionType) == 0) return false;
  1027. //Modified for 'Generals' so we could select trees but filter out headlight beams, etc. -MW
  1028. if (raytest.CheckTranslucent && Is_Alpha()!=0)
  1029. return false;
  1030. if (Is_Hidden() && !raytest.CheckHidden) return false;
  1031. Matrix3D world_to_obj;
  1032. Matrix3D world=Get_Transform();
  1033. // if aligned or oriented rotate the mesh so that it's aligned to the ray
  1034. if (Model->Get_Flag(MeshModelClass::ALIGNED)) {
  1035. Vector3 mesh_position;
  1036. world.Get_Translation(&mesh_position);
  1037. world.Obj_Look_At(mesh_position,mesh_position - raytest.Ray.Get_Dir(),0.0f);
  1038. } else if (Model->Get_Flag(MeshModelClass::ORIENTED)) {
  1039. Vector3 mesh_position;
  1040. world.Get_Translation(&mesh_position);
  1041. world.Obj_Look_At(mesh_position,raytest.Ray.Get_P0(),0.0f);
  1042. }
  1043. world.Get_Inverse(world_to_obj);
  1044. RayCollisionTestClass objray(raytest,world_to_obj);
  1045. WWASSERT(Model);
  1046. bool hit = Model->Cast_Ray(objray);
  1047. // transform result back into original coordinate system
  1048. if (hit) {
  1049. raytest.CollidedRenderObj = this;
  1050. Matrix3D::Rotate_Vector(world,raytest.Result->Normal, &(raytest.Result->Normal));
  1051. if (raytest.Result->ComputeContactPoint) {
  1052. Matrix3D::Transform_Vector(world,raytest.Result->ContactPoint, &(raytest.Result->ContactPoint));
  1053. }
  1054. }
  1055. return hit;
  1056. }
  1057. /***********************************************************************************************
  1058. * MeshClass::Cast_AABox -- cast an AABox against this mesh *
  1059. * *
  1060. * INPUT: *
  1061. * *
  1062. * OUTPUT: *
  1063. * *
  1064. * WARNINGS: *
  1065. * *
  1066. * HISTORY: *
  1067. * 6/17/98 GTH : Created. *
  1068. *=============================================================================================*/
  1069. bool MeshClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
  1070. {
  1071. if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
  1072. WWASSERT(Model);
  1073. // This function analyses the tranform to call optimized functions in certain cases
  1074. bool hit = Model->Cast_World_Space_AABox(boxtest, Get_Transform());
  1075. if (hit) {
  1076. boxtest.CollidedRenderObj = this;
  1077. }
  1078. return hit;
  1079. }
  1080. /***********************************************************************************************
  1081. * Cast_OBBox -- Cast an obbox against this mesh *
  1082. * *
  1083. * INPUT: *
  1084. * *
  1085. * OUTPUT: *
  1086. * *
  1087. * WARNINGS: *
  1088. * *
  1089. * HISTORY: *
  1090. * 6/17/98 GTH : Created. *
  1091. *=============================================================================================*/
  1092. bool MeshClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
  1093. {
  1094. if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
  1095. /*
  1096. ** transform into the local coordinate system of the mesh.
  1097. */
  1098. const Matrix3D & tm = Get_Transform();
  1099. Matrix3D world_to_obj;
  1100. tm.Get_Orthogonal_Inverse(world_to_obj);
  1101. OBBoxCollisionTestClass localtest(boxtest,world_to_obj);
  1102. WWASSERT(Model);
  1103. bool hit = Model->Cast_OBBox(localtest);
  1104. /*
  1105. ** If we hit, transform the result of the test back to the original coordinate system.
  1106. */
  1107. if (hit) {
  1108. boxtest.CollidedRenderObj = this;
  1109. Matrix3D::Rotate_Vector(tm,boxtest.Result->Normal, &(boxtest.Result->Normal));
  1110. if (boxtest.Result->ComputeContactPoint) {
  1111. Matrix3D::Transform_Vector(tm,boxtest.Result->ContactPoint, &(boxtest.Result->ContactPoint));
  1112. }
  1113. }
  1114. return hit;
  1115. }
  1116. /***********************************************************************************************
  1117. * MeshClass::Intersect_AABox -- test for intersection with given AABox *
  1118. * *
  1119. * The AAbox given is assumed to be in world space. Since meshes aren't generally in world *
  1120. * space, the test must be transformed into our local coordinate system (which turns it into *
  1121. * an OBBox...) *
  1122. * *
  1123. * INPUT: *
  1124. * *
  1125. * OUTPUT: *
  1126. * *
  1127. * WARNINGS: *
  1128. * *
  1129. * HISTORY: *
  1130. * 1/19/00 gth : Created. *
  1131. *=============================================================================================*/
  1132. bool MeshClass::Intersect_AABox(AABoxIntersectionTestClass & boxtest)
  1133. {
  1134. if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
  1135. Matrix3D inv_tm;
  1136. Get_Transform().Get_Orthogonal_Inverse(inv_tm);
  1137. OBBoxIntersectionTestClass local_test(boxtest,inv_tm);
  1138. WWASSERT(Model);
  1139. return Model->Intersect_OBBox(local_test);
  1140. }
  1141. /***********************************************************************************************
  1142. * MeshClass::Intersect_OBBox -- test for intersection with the given OBBox *
  1143. * *
  1144. * The given OBBox is assumed to be in world space so we need to transform it into the mesh's *
  1145. * local coordinate system. *
  1146. * *
  1147. * INPUT: *
  1148. * *
  1149. * OUTPUT: *
  1150. * *
  1151. * WARNINGS: *
  1152. * *
  1153. * HISTORY: *
  1154. * 1/19/00 gth : Created. *
  1155. *=============================================================================================*/
  1156. bool MeshClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
  1157. {
  1158. if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
  1159. Matrix3D inv_tm;
  1160. Get_Transform().Get_Orthogonal_Inverse(inv_tm);
  1161. OBBoxIntersectionTestClass local_test(boxtest,inv_tm);
  1162. WWASSERT(Model);
  1163. return Model->Intersect_OBBox(local_test);
  1164. }
  1165. /***********************************************************************************************
  1166. * MeshClass::Get_Obj_Space_Bounding_Sphere -- returns obj-space bounding sphere *
  1167. * *
  1168. * INPUT: *
  1169. * *
  1170. * OUTPUT: *
  1171. * *
  1172. * WARNINGS: *
  1173. * *
  1174. * HISTORY: *
  1175. * 1/19/00 gth : Created. *
  1176. *=============================================================================================*/
  1177. void MeshClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  1178. {
  1179. if (Model) {
  1180. Model->Get_Bounding_Sphere(&sphere);
  1181. } else {
  1182. sphere.Center.Set(0,0,0);
  1183. sphere.Radius = 1.0f;
  1184. }
  1185. }
  1186. /***********************************************************************************************
  1187. * MeshClass::Get_Obj_Space_Bounding_Box -- returns the obj-space bounding box *
  1188. * *
  1189. * INPUT: *
  1190. * *
  1191. * OUTPUT: *
  1192. * *
  1193. * WARNINGS: *
  1194. * *
  1195. * HISTORY: *
  1196. * 1/19/00 gth : Created. *
  1197. *=============================================================================================*/
  1198. void MeshClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  1199. {
  1200. if (Model) {
  1201. Model->Get_Bounding_Box(&box);
  1202. } else {
  1203. box.Init(Vector3(0,0,0),Vector3(1,1,1));
  1204. }
  1205. }
  1206. /***********************************************************************************************
  1207. * MeshClass::Generate_Culling_Tree -- Generates a hierarchical culling tree for the mesh *
  1208. * *
  1209. * INPUT: *
  1210. * *
  1211. * OUTPUT: *
  1212. * *
  1213. * WARNINGS: *
  1214. * *
  1215. * HISTORY: *
  1216. * 6/18/98 GTH : Created. *
  1217. *=============================================================================================*/
  1218. void MeshClass::Generate_Culling_Tree(void)
  1219. {
  1220. Model->Generate_Culling_Tree();
  1221. }
  1222. /***********************************************************************************************
  1223. * MeshClass::Add_Dependencies_To_List -- Add dependent files to the list. *
  1224. * *
  1225. * INPUT: *
  1226. * *
  1227. * OUTPUT: *
  1228. * *
  1229. * WARNINGS: *
  1230. * *
  1231. * HISTORY: *
  1232. * 3/18/99 PDS : Created. *
  1233. *=============================================================================================*/
  1234. void MeshClass::Add_Dependencies_To_List
  1235. (
  1236. DynamicVectorClass<StringClass> &file_list,
  1237. bool textures_only
  1238. )
  1239. {
  1240. //
  1241. // Get a pointer to this mesh's material information object
  1242. //
  1243. MaterialInfoClass *material = Get_Material_Info ();
  1244. if (material != NULL) {
  1245. //
  1246. // Loop through all the textures and add their filenames to our list
  1247. //
  1248. for (int index = 0; index < material->Texture_Count (); index ++) {
  1249. //
  1250. // Add this texture's filename to the list
  1251. //
  1252. TextureClass *texture = material->Peek_Texture (index);
  1253. if (texture != NULL) {
  1254. file_list.Add (texture->Get_Full_Path ());
  1255. }
  1256. }
  1257. //
  1258. // Release our hold on the material information object
  1259. //
  1260. material->Release_Ref ();
  1261. }
  1262. RenderObjClass::Add_Dependencies_To_List (file_list, textures_only);
  1263. return ;
  1264. }
  1265. /***********************************************************************************************
  1266. * MeshClass::Update_Cached_Bounding_Volumes -- default collision sphere. *
  1267. * *
  1268. * INPUT: *
  1269. * *
  1270. * OUTPUT: *
  1271. * *
  1272. * WARNINGS: *
  1273. * *
  1274. * HISTORY: *
  1275. * 5/14/2001 NH : Created. *
  1276. *=============================================================================================*/
  1277. void MeshClass::Update_Cached_Bounding_Volumes(void) const
  1278. {
  1279. Get_Obj_Space_Bounding_Sphere(CachedBoundingSphere);
  1280. #ifdef ALLOW_TEMPORARIES
  1281. CachedBoundingSphere.Center = Get_Transform() * CachedBoundingSphere.Center;
  1282. #else
  1283. Get_Transform().mulVector3(CachedBoundingSphere.Center);
  1284. #endif
  1285. // If we are camera-aligned or -oriented, we don't know which way we are facing at this point,
  1286. // so the box we return needs to contain the sphere. Otherewise do the normal computation.
  1287. if (Model->Get_Flag(MeshModelClass::ALIGNED) || Model->Get_Flag(MeshModelClass::ORIENTED)) {
  1288. CachedBoundingBox.Center = CachedBoundingSphere.Center;
  1289. CachedBoundingBox.Extent.Set(CachedBoundingSphere.Radius, CachedBoundingSphere.Radius, CachedBoundingSphere.Radius);
  1290. } else {
  1291. Get_Obj_Space_Bounding_Box(CachedBoundingBox);
  1292. CachedBoundingBox.Transform(Get_Transform());
  1293. }
  1294. Validate_Cached_Bounding_Volumes();
  1295. }
  1296. // This utility function recurses throughout the subobjects of a renderobject, and for each
  1297. // MeshClass it finds it sets the given MeshModel flag on its model. This is useful for stuff
  1298. // like making a RenderObjects' polys sort.
  1299. void Set_MeshModel_Flag(RenderObjClass *robj, int flag, int onoff)
  1300. {
  1301. if (robj->Class_ID() == RenderObjClass::CLASSID_MESH) {
  1302. // Set flag on model (the assumption is that meshes don't have subobjects)
  1303. MeshClass *mesh = (MeshClass *)robj;
  1304. MeshModelClass *model = mesh->Get_Model();
  1305. model->Set_Flag((MeshModelClass::FlagsType)flag, onoff != 0);
  1306. model->Release_Ref();
  1307. } else {
  1308. // Recurse to subobjects (if any)
  1309. int num_obj = robj->Get_Num_Sub_Objects();
  1310. RenderObjClass *sub_obj;
  1311. for (int i = 0; i < num_obj; i++) {
  1312. sub_obj = robj->Get_Sub_Object(i);
  1313. if (sub_obj) {
  1314. Set_MeshModel_Flag(sub_obj, flag, onoff);
  1315. sub_obj->Release_Ref();
  1316. }
  1317. }
  1318. }
  1319. }
  1320. int MeshClass::Get_Sort_Level(void) const
  1321. {
  1322. if (Model) {
  1323. return (Model->Get_Sort_Level());
  1324. }
  1325. return(SORT_LEVEL_NONE);
  1326. }
  1327. void MeshClass::Set_Sort_Level(int level)
  1328. {
  1329. if (Model) {
  1330. Model->Set_Sort_Level(level);
  1331. }
  1332. }
  1333. int MeshClass::Get_Draw_Call_Count(void) const
  1334. {
  1335. if (Model != NULL) {
  1336. // Prefer to return the number of polygon renderers
  1337. int prcount = Model->PolygonRendererList.Count();
  1338. if (prcount > 0) {
  1339. return prcount;
  1340. }
  1341. // Otherwise if we have textures, return the number of textures (e.g. dont have prs when sorting)
  1342. if ((Model->MatInfo != NULL) && (Model->MatInfo->Texture_Count() > 0)) {
  1343. return Model->MatInfo->Texture_Count();
  1344. }
  1345. // Otherwise, return 1
  1346. return 1;
  1347. } else {
  1348. return 0;
  1349. }
  1350. }