shdmesh.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWShade *
  23. * *
  24. * $Archive:: wwshade/shdmesh.cpp $*
  25. * *
  26. * Org Author:: Jani P *
  27. * *
  28. * Author : Kenny Mitchell *
  29. * *
  30. * $Modtime:: 07/12/02 10:31a $*
  31. * *
  32. * $Revision:: 1 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. *---------------------------------------------------------------------------------------------*/
  36. #include "shdmesh.h"
  37. #include "shdsubmesh.h"
  38. #include "shdrenderer.h"
  39. #include "rinfo.h"
  40. #include "camera.h"
  41. #include "dx8wrapper.h"
  42. #include "wwdebug.h"
  43. #include "wwprofile.h"
  44. #include "mesh.h"
  45. #include "meshmdl.h"
  46. ShdMeshClass::ShdMeshClass()
  47. : Name("UnNamed"),
  48. LightEnvironment(NULL),
  49. Applying_Shadow_Map(false)
  50. {
  51. }
  52. ShdMeshClass::ShdMeshClass(const ShdMeshClass & src)
  53. : RenderObjClass(src),
  54. Name(src.Name),
  55. LightEnvironment(NULL),
  56. Applying_Shadow_Map(false)
  57. {
  58. Free();
  59. SubMeshes.Resize(src.SubMeshes.Length());
  60. for (int i=0;i<SubMeshes.Length();++i)
  61. {
  62. SubMeshes[i].Mesh=NULL;
  63. SubMeshes[i].Renderer=NULL;
  64. REF_PTR_SET(SubMeshes[i].Mesh,src.SubMeshes[i].Mesh);
  65. }
  66. }
  67. ShdMeshClass::~ShdMeshClass()
  68. {
  69. Free();
  70. // TODO TODO TODO!!!!!
  71. }
  72. RenderObjClass * ShdMeshClass::Clone() const
  73. {
  74. return new ShdMeshClass(*this);
  75. }
  76. const char * ShdMeshClass::Get_Name() const
  77. {
  78. return Name;
  79. }
  80. void ShdMeshClass::Set_Name(const char * name)
  81. {
  82. Name = name;
  83. }
  84. /***********************************************************************************************
  85. * ShdMeshClass::Free -- Releases all memory/assets in use by this mesh *
  86. * *
  87. * INPUT: *
  88. * *
  89. * OUTPUT: *
  90. * *
  91. * WARNINGS: *
  92. * *
  93. * HISTORY: *
  94. * 1/6/98 GTH : Created. *
  95. *=============================================================================================*/
  96. void ShdMeshClass::Free()
  97. {
  98. for (int i=0;i<SubMeshes.Length();++i)
  99. {
  100. REF_PTR_RELEASE(SubMeshes[i].Mesh);
  101. if (SubMeshes[i].Renderer)
  102. {
  103. delete SubMeshes[i].Renderer;
  104. SubMeshes[i].Renderer=NULL;
  105. }
  106. }
  107. }
  108. int ShdMeshClass::Get_Num_Polys() const
  109. {
  110. int count = 0;
  111. for (int i=0; i<SubMeshes.Length(); i++)
  112. {
  113. count+= SubMeshes[i].Mesh->Get_Polygon_Count();
  114. }
  115. return count;
  116. }
  117. int ShdMeshClass::Get_Num_Vertices(void) const
  118. {
  119. int count = 0;
  120. for (int i=0; i<SubMeshes.Length(); i++)
  121. {
  122. count+= SubMeshes[i].Mesh->Get_Vertex_Count();
  123. }
  124. return count;
  125. }
  126. void ShdMeshClass::Render(RenderInfoClass& rinfo)
  127. {
  128. WWPROFILE("ShdMeshClass::Render");
  129. if (Is_Not_Hidden_At_All() == false)
  130. {
  131. return;
  132. }
  133. // DX8_RECORD_MESH_RENDER();
  134. // TODO: Static sort lists
  135. Set_Lighting_Environment(rinfo.light_environment);
  136. const FrustumClass & frustum=rinfo.Camera.Get_Frustum();
  137. // if rendering shadow remember camera info
  138. if ((rinfo.Current_Override_Flags()&RenderInfoClass::RINFO_OVERRIDE_SHADOW_RENDERING))
  139. {
  140. // is generating shadow map
  141. Set_Is_Self_Shadowed();
  142. // set texture projector
  143. Texture_Projector=rinfo.Texture_Projector;
  144. Unset_Is_Applying_Shadow_Map();
  145. }
  146. else if (Is_Self_Shadowed())
  147. {
  148. // is applying shadow map
  149. Set_Is_Applying_Shadow_Map();
  150. Unset_Is_Self_Shadowed();
  151. }
  152. else
  153. {
  154. Unset_Is_Applying_Shadow_Map();
  155. }
  156. // TODO: What to do with SKINS?
  157. if (1)//CollisionMath::Overlap_Test(frustum,Get_Bounding_Box())!=CollisionMath::OUTSIDE )
  158. {
  159. // bool rendered_something = false;
  160. // TODO: Override flags, decals and material passes (probably in the submesh rendering)
  161. for (int i=0;i<SubMeshes.Length();++i)
  162. {
  163. if (!SubMeshes[i].Renderer)
  164. {
  165. SubMeshes[i].Renderer=ShdRendererClass::Peek_Instance()->Register_Mesh(this,SubMeshes[i].Mesh);
  166. }
  167. SubMeshes[i].Renderer->Render(rinfo);
  168. }
  169. // TODO: RendererDebugger
  170. }
  171. }
  172. //void ShdMeshClass::Render_Material_Pass(MaterialPassClass * pass,IndexBufferClass * ib)
  173. //{
  174. //
  175. //}
  176. void ShdMeshClass::Special_Render(SpecialRenderInfoClass & rinfo)
  177. {
  178. }
  179. /***********************************************************************************************
  180. * ShdMeshClass::Cast_Ray -- compute a ray intersection with this mesh *
  181. * *
  182. * INPUT: *
  183. * *
  184. * OUTPUT: *
  185. * *
  186. * WARNINGS: *
  187. * *
  188. * HISTORY: *
  189. * 6/17/98 GTH : Created. *
  190. *=============================================================================================*/
  191. bool ShdMeshClass::Cast_Ray(RayCollisionTestClass & raytest)
  192. {
  193. if ((Get_Collision_Type() & raytest.CollisionType) == 0) return false;
  194. if ((Is_Translucent()!=0) && (!raytest.CheckTranslucent)) return false;
  195. if (Is_Animation_Hidden()) return false;
  196. if (raytest.Result->StartBad) return false;
  197. Matrix3D world_to_obj;
  198. Matrix3D world=Get_Transform();
  199. // if aligned or oriented rotate the mesh so that it's aligned to the ray
  200. /* if (Model->Get_Flag(MeshModelClass::ALIGNED)) {
  201. Vector3 mesh_position;
  202. world.Get_Translation(&mesh_position);
  203. world.Obj_Look_At(mesh_position,mesh_position - raytest.Ray.Get_Dir(),0.0f);
  204. } else if (Model->Get_Flag(MeshModelClass::ORIENTED)) {
  205. Vector3 mesh_position;
  206. world.Get_Translation(&mesh_position);
  207. world.Obj_Look_At(mesh_position,raytest.Ray.Get_P0(),0.0f);
  208. }
  209. */
  210. world.Get_Orthogonal_Inverse(world_to_obj);
  211. RayCollisionTestClass objray(raytest,world_to_obj);
  212. for (int i=0;i<SubMeshes.Length();++i) {
  213. if (SubMeshes[i].Mesh->Cast_Ray(objray)) {
  214. // transform result back into original coordinate system
  215. raytest.CollidedRenderObj = this;
  216. Matrix3D::Rotate_Vector(world,raytest.Result->Normal, &(raytest.Result->Normal));
  217. if (raytest.Result->ComputeContactPoint) {
  218. Matrix3D::Transform_Vector(world,raytest.Result->ContactPoint, &(raytest.Result->ContactPoint));
  219. }
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. /***********************************************************************************************
  226. * ShdMeshClass::Cast_AABox -- cast an AABox against this mesh *
  227. * *
  228. * INPUT: *
  229. * *
  230. * OUTPUT: *
  231. * *
  232. * WARNINGS: *
  233. * *
  234. * HISTORY: *
  235. * 6/17/98 GTH : Created. *
  236. *=============================================================================================*/
  237. bool ShdMeshClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
  238. {
  239. if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
  240. if (boxtest.Result->StartBad) return false;
  241. for (int i=0;i<SubMeshes.Length();++i) {
  242. // This function analyses the tranform to call optimized functions in certain cases
  243. if (SubMeshes[i].Mesh->Cast_World_Space_AABox(boxtest, Get_Transform())) {
  244. boxtest.CollidedRenderObj = this;
  245. return true;
  246. }
  247. }
  248. return false;
  249. }
  250. /***********************************************************************************************
  251. * Cast_OBBox -- Cast an obbox against this mesh *
  252. * *
  253. * INPUT: *
  254. * *
  255. * OUTPUT: *
  256. * *
  257. * WARNINGS: *
  258. * *
  259. * HISTORY: *
  260. * 6/17/98 GTH : Created. *
  261. *=============================================================================================*/
  262. bool ShdMeshClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
  263. {
  264. if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
  265. if (boxtest.Result->StartBad) return false;
  266. /*
  267. ** transform into the local coordinate system of the mesh.
  268. */
  269. const Matrix3D & tm = Get_Transform();
  270. Matrix3D world_to_obj;
  271. tm.Get_Orthogonal_Inverse(world_to_obj);
  272. OBBoxCollisionTestClass localtest(boxtest,world_to_obj);
  273. for (int i=0;i<SubMeshes.Length();++i) {
  274. if (SubMeshes[i].Mesh->Cast_OBBox(localtest)) {
  275. /*
  276. ** If we hit, transform the result of the test back to the original coordinate system.
  277. */
  278. boxtest.CollidedRenderObj = this;
  279. Matrix3D::Rotate_Vector(tm,boxtest.Result->Normal, &(boxtest.Result->Normal));
  280. if (boxtest.Result->ComputeContactPoint) {
  281. Matrix3D::Transform_Vector(tm,boxtest.Result->ContactPoint, &(boxtest.Result->ContactPoint));
  282. }
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. /***********************************************************************************************
  289. * ShdMeshClass::Intersect_AABox -- test for intersection with given AABox *
  290. * *
  291. * The AAbox given is assumed to be in world space. Since meshes aren't generally in world *
  292. * space, the test must be transformed into our local coordinate system (which turns it into *
  293. * an OBBox...) *
  294. * *
  295. * INPUT: *
  296. * *
  297. * OUTPUT: *
  298. * *
  299. * WARNINGS: *
  300. * *
  301. * HISTORY: *
  302. * 1/19/00 gth : Created. *
  303. *=============================================================================================*/
  304. bool ShdMeshClass::Intersect_AABox(AABoxIntersectionTestClass & boxtest)
  305. {
  306. if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
  307. Matrix3D inv_tm;
  308. Get_Transform().Get_Orthogonal_Inverse(inv_tm);
  309. OBBoxIntersectionTestClass local_test(boxtest,inv_tm);
  310. for (int i=0;i<SubMeshes.Length();++i) {
  311. if (SubMeshes[i].Mesh->Intersect_OBBox(local_test)) return true;
  312. }
  313. return false;
  314. }
  315. /***********************************************************************************************
  316. * ShdMeshClass::Intersect_OBBox -- test for intersection with the given OBBox *
  317. * *
  318. * The given OBBox is assumed to be in world space so we need to transform it into the mesh's *
  319. * local coordinate system. *
  320. * *
  321. * INPUT: *
  322. * *
  323. * OUTPUT: *
  324. * *
  325. * WARNINGS: *
  326. * *
  327. * HISTORY: *
  328. * 1/19/00 gth : Created. *
  329. *=============================================================================================*/
  330. bool ShdMeshClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
  331. {
  332. if ((Get_Collision_Type() & boxtest.CollisionType) == 0) return false;
  333. Matrix3D inv_tm;
  334. Get_Transform().Get_Orthogonal_Inverse(inv_tm);
  335. OBBoxIntersectionTestClass local_test(boxtest,inv_tm);
  336. for (int i=0;i<SubMeshes.Length();++i) {
  337. if (SubMeshes[i].Mesh->Intersect_OBBox(local_test)) return true;
  338. }
  339. return false;
  340. }
  341. /***********************************************************************************************
  342. * ShdMeshClass::Add_Dependencies_To_List -- Add dependent files to the list. *
  343. * *
  344. * INPUT: *
  345. * *
  346. * OUTPUT: *
  347. * *
  348. * WARNINGS: *
  349. * *
  350. * HISTORY: *
  351. * 3/18/99 PDS : Created. *
  352. * 6/05/02 KJM : Added dependency calculation for shader system
  353. *=============================================================================================*/
  354. void ShdMeshClass::Add_Dependencies_To_List
  355. (
  356. DynamicVectorClass<StringClass> &file_list,
  357. bool textures_only
  358. )
  359. {
  360. // loop through sub meshes
  361. for (int i=0;i<SubMeshes.Length();i++)
  362. {
  363. ShdInterfaceClass* shd=SubMeshes[i].Mesh->Peek_Shader();
  364. for (int tidx=0;tidx<shd->Get_Texture_Count();tidx++)
  365. {
  366. TextureClass* texture=shd->Peek_Texture(tidx);
  367. if (texture)
  368. {
  369. file_list.Add(texture->Get_Full_Path());
  370. }
  371. }
  372. }
  373. RenderObjClass::Add_Dependencies_To_List (file_list, textures_only);
  374. return ;
  375. }
  376. /***********************************************************************************************
  377. * ShdMeshClass::Update_Cached_Bounding_Volumes -- default collision sphere. *
  378. * *
  379. * INPUT: *
  380. * *
  381. * OUTPUT: *
  382. * *
  383. * WARNINGS: *
  384. * *
  385. * HISTORY: *
  386. * 5/14/2001 NH : Created. *
  387. *=============================================================================================*/
  388. void ShdMeshClass::Update_Cached_Bounding_Volumes(void) const
  389. {
  390. Get_Obj_Space_Bounding_Sphere(CachedBoundingSphere);
  391. Matrix3D::Transform_Vector(Get_Transform(),CachedBoundingSphere.Center,&CachedBoundingSphere.Center);
  392. // If we are camera-aligned or -oriented, we don't know which way we are facing at this point,
  393. // so the box we return needs to contain the sphere. Otherewise do the normal computation.
  394. /* if (Model->Get_Flag(MeshModelClass::ALIGNED) || Model->Get_Flag(MeshModelClass::ORIENTED)) {
  395. CachedBoundingBox.Center = CachedBoundingSphere.Center;
  396. CachedBoundingBox.Extent.Set(CachedBoundingSphere.Radius, CachedBoundingSphere.Radius, CachedBoundingSphere.Radius);
  397. } else {
  398. */ Get_Obj_Space_Bounding_Box(CachedBoundingBox);
  399. CachedBoundingBox.Transform(Get_Transform());
  400. // }
  401. Validate_Cached_Bounding_Volumes();
  402. }
  403. /***********************************************************************************************
  404. * ShdMeshClass::Get_Obj_Space_Bounding_Sphere -- returns obj-space bounding sphere *
  405. * *
  406. * INPUT: *
  407. * *
  408. * OUTPUT: *
  409. * *
  410. * WARNINGS: *
  411. * *
  412. * HISTORY: *
  413. * 1/19/00 gth : Created. *
  414. *=============================================================================================*/
  415. void ShdMeshClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  416. {
  417. if (SubMeshes.Length()) {
  418. SubMeshes[0].Mesh->Get_Bounding_Sphere(&sphere);
  419. // If there are more than one submesh, merge all bounding spheres
  420. for (int i=1;i<SubMeshes.Length();++i) {
  421. SphereClass tmp_s;
  422. SubMeshes[i].Mesh->Get_Bounding_Sphere(&tmp_s);
  423. sphere+=tmp_s;
  424. }
  425. }
  426. else {
  427. sphere.Center.Set(0,0,0);
  428. sphere.Radius = 1.0f;
  429. }
  430. }
  431. /***********************************************************************************************
  432. * ShdMeshClass::Get_Obj_Space_Bounding_Box -- returns the obj-space bounding box *
  433. * *
  434. * INPUT: *
  435. * *
  436. * OUTPUT: *
  437. * *
  438. * WARNINGS: *
  439. * *
  440. * HISTORY: *
  441. * 1/19/00 gth : Created. *
  442. *=============================================================================================*/
  443. void ShdMeshClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  444. {
  445. if (SubMeshes.Length()) {
  446. SubMeshes[0].Mesh->Get_Bounding_Box(&box);
  447. // If there are more than one submesh, merge all bounding boxes
  448. for (int i=1;i<SubMeshes.Length();++i) {
  449. AABoxClass tmp_b;
  450. SubMeshes[i].Mesh->Get_Bounding_Box(&tmp_b);
  451. box.Add_Box(tmp_b);
  452. }
  453. }
  454. else {
  455. box.Init(Vector3(0,0,0),Vector3(1,1,1));
  456. }
  457. }
  458. void ShdMeshClass::Init_From_Legacy_Mesh(MeshClass* mesh)
  459. {
  460. Set_Name(mesh->Get_Name());
  461. MeshModelClass* model=mesh->Peek_Model();
  462. if (model)
  463. {
  464. int first_poly=0;
  465. int sub_mesh_count=0;
  466. while (first_poly<model->Get_Polygon_Count()) {
  467. sub_mesh_count++;
  468. ShdSubMeshClass * sub_mesh = NEW_REF( ShdSubMeshClass, () );
  469. sub_mesh->Init_From_Legacy_Mesh_Model(model,first_poly);
  470. SubMeshes.Resize(sub_mesh_count);
  471. SubMeshes[sub_mesh_count-1].Mesh=sub_mesh;
  472. SubMeshes[sub_mesh_count-1].Renderer=NULL;
  473. if (sub_mesh->Get_Visible_Polygon_Count()==0) break;
  474. first_poly+=sub_mesh->Get_Visible_Polygon_Count();
  475. }
  476. }
  477. // Pull interesting stuff out of the w3d attributes bits
  478. Set_Collision_Type(mesh->Get_Collision_Type());
  479. Set_Hidden(mesh->Is_Hidden());
  480. Set_Translucent(mesh->Is_Translucent());
  481. }
  482. /***********************************************************************************************
  483. * ShdMeshClass::Load -- creates a shader mesh out of a shader mesh chunk in a .w3d file *
  484. * *
  485. * INPUT: *
  486. * *
  487. * OUTPUT: *
  488. * *
  489. * WARNINGS: *
  490. * *
  491. * HISTORY: *
  492. * 05/21/2002 KM : Created. *
  493. *=============================================================================================*/
  494. WW3DErrorType ShdMeshClass::Load_W3D(ChunkLoadClass& cload)
  495. {
  496. // Open the first chunk, it should be the shader mesh name
  497. cload.Open_Chunk();
  498. if (cload.Cur_Chunk_ID()!=W3D_CHUNK_SHDMESH_NAME)
  499. {
  500. WWDEBUG_SAY(("Invalid format shader mesh.\n"));
  501. return WW3D_ERROR_LOAD_FAILED;
  502. }
  503. // read name
  504. char buf[MAX_PATH];
  505. cload.Read(buf,cload.Cur_Chunk_Length());
  506. cload.Close_Chunk();
  507. Set_Name(buf);
  508. // open header
  509. W3dShdMeshHeaderStruct hdr;
  510. cload.Open_Chunk();
  511. if
  512. (
  513. cload.Read
  514. (
  515. &hdr,
  516. sizeof(W3dShdMeshHeaderStruct)
  517. )!=sizeof(W3dShdMeshHeaderStruct)
  518. )
  519. {
  520. return WW3D_ERROR_LOAD_FAILED;
  521. }
  522. cload.Close_Chunk();
  523. // Process the header
  524. // Set Bounding Info
  525. Vector3 min(hdr.BoxMin.X,hdr.BoxMin.Y,hdr.BoxMin.Z);
  526. Vector3 max(hdr.BoxMax.X,hdr.BoxMax.Y,hdr.BoxMax.Z);
  527. CachedBoundingBox.Init_Min_Max(min,max);
  528. Vector3 cntr(hdr.SphCenter.X,hdr.SphCenter.Y,hdr.SphCenter.Z);
  529. CachedBoundingSphere.Init(cntr,hdr.SphRadius);
  530. // Flags (todo)
  531. // user text
  532. // next are the sub meshes
  533. Free();
  534. SubMeshes.Resize(hdr.NumSubMeshes);
  535. for (int i=0;i<SubMeshes.Length(); )
  536. {
  537. cload.Open_Chunk();
  538. if (cload.Cur_Chunk_ID()==W3D_CHUNK_SHDMESH_USER_TEXT)
  539. {
  540. // todo
  541. cload.Read(buf,cload.Cur_Chunk_Length());
  542. }
  543. else
  544. {
  545. ShdSubMeshClass* ssmesh=NEW_REF(ShdSubMeshClass,());
  546. if (ssmesh==NULL)
  547. {
  548. WWDEBUG_SAY(("ShdMeshClass::Load_W3D - Failed to allocate sub mesh\r\n"));
  549. return WW3D_ERROR_LOAD_FAILED;
  550. }
  551. SubMeshes[i].Mesh=ssmesh;
  552. SubMeshes[i].Renderer=NULL;
  553. REF_PTR_SET(SubMeshes[i].Mesh,SubMeshes[i].Mesh);
  554. ssmesh->Load_W3D(cload);
  555. // assign each sub-mesh with a name in the format: <parentmesh>.<index>
  556. StringClass tmp;
  557. tmp.Format("%s.%d",Name,i);
  558. ssmesh->Set_Name(tmp);
  559. i++;
  560. }
  561. cload.Close_Chunk();
  562. }
  563. // Pull interesting stuff out of the w3d attributes bits
  564. int col_bits = (hdr.Attributes & W3D_MESH_FLAG_COLLISION_TYPE_MASK) >> W3D_MESH_FLAG_COLLISION_TYPE_SHIFT;
  565. Set_Collision_Type( col_bits << 1 );
  566. Set_Hidden(hdr.Attributes & W3D_MESH_FLAG_HIDDEN);
  567. for (i=0;i<SubMeshes.Length(); i++) {
  568. bool shadow = (hdr.Attributes & W3D_MESH_FLAG_CAST_SHADOW) == W3D_MESH_FLAG_CAST_SHADOW;
  569. SubMeshes[i].Mesh->Set_Flag(MeshGeometryClass::CAST_SHADOW,shadow);
  570. }
  571. // Indicate whether this mesh is translucent. The mesh is considered translucent
  572. // if sorting has been enabled (alpha blending on pass 0) or if pass0 contains alpha-test.
  573. // This flag is used to determine if the mesh can cast a geometric shadow.
  574. bool is_translucent = false;
  575. for (i=0;i<SubMeshes.Length(); i++) {
  576. if (SubMeshes[i].Mesh) {
  577. if (SubMeshes[i].Mesh->Is_Sorting()) {
  578. Set_Translucent(true);
  579. }
  580. ShdInterfaceClass * shader = SubMeshes[i].Mesh->Peek_Shader();
  581. if ((shader) && (!shader->Is_Opaque())) {
  582. Set_Translucent(true);
  583. }
  584. }
  585. }
  586. return WW3D_ERROR_OK;
  587. }
  588. int ShdMeshClass::Get_Sub_Mesh_Count(void) const
  589. {
  590. return SubMeshes.Length();
  591. }
  592. ShdSubMeshClass * ShdMeshClass::Peek_Sub_Mesh(int i) const
  593. {
  594. return SubMeshes[i].Mesh;
  595. }