decalmsh.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/decalmsh.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 7/26/01 9:03a $*
  31. * *
  32. * $Revision:: 20 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * DecalMeshClass::DecalMeshClass -- Constructor *
  37. * DecalMeshClass::~DecalMeshClass -- Destructor *
  38. * RigidDecalMeshClass::RigidDecalMeshClass -- Constructor *
  39. * RigidDecalMeshClass::~RigidDecalMeshClass -- Destructor *
  40. * RigidDecalMeshClass::Render -- Render the decals *
  41. * RigidDecalMeshClass::Process_Material_Run -- scans the mesh for material runs *
  42. * RigidDecalMeshClass::Create_Decal -- Generate a new decal *
  43. * RigidDecalMeshClass::Delete_Decal -- Delete a decal *
  44. * SkinDecalMeshClass::SkinDecalMeshClass -- Constructor *
  45. * SkinDecalMeshClass::~SkinDecalMeshClass -- Destructor *
  46. * SkinDecalMeshClass::Render -- Render the decals *
  47. * SkinDecalMeshClass::Create_Decal -- Generate a new decal *
  48. * SkinDecalMeshClass::Delete_Decal -- Delete a decal *
  49. * SkinDecalMeshClass::Process_Material_Run -- scans the mesh for material runs *
  50. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  51. #include "decalmsh.h"
  52. #include "decalsys.h"
  53. #include "rinfo.h"
  54. #include "mesh.h"
  55. #include "meshmdl.h"
  56. #include "plane.h"
  57. #include "statistics.h"
  58. #include "dx8vertexbuffer.h"
  59. #include "dx8indexbuffer.h"
  60. #include "simplevec.h"
  61. #include "texture.h"
  62. #include "dx8wrapper.h"
  63. #define DISABLE_CLIPPING 0
  64. /**
  65. ** DecalPolyClass - This class is used to clip polygons as they are
  66. ** added to a RigidDecalMesh.
  67. **
  68. ** Data needed to add a poly to the decal mesh:
  69. ** connectivity - generated on the fly after the poly is clipped
  70. ** planeeq - constant for entire poly, copy from source after done
  71. ** verts - plug into DecalPolyClass, clip, pull back out
  72. ** vnorms - plug into DecalPolyClass, clip, copy back out
  73. ** texcoords - compute after poly is clipped
  74. ** material - contstant for entire poly, get from generator
  75. ** shader - constant for entire poly, get from generator
  76. ** texture - constant for entire poly, get from generator
  77. */
  78. class DecalPolyClass
  79. {
  80. public:
  81. void Reset(void);
  82. void Add_Vertex(const Vector3 & point,const Vector3 & normal);
  83. void Clip(const PlaneClass & plane,DecalPolyClass & dest) const;
  84. SimpleDynVecClass<Vector3> Verts;
  85. SimpleDynVecClass<Vector3> VertNorms;
  86. };
  87. void DecalPolyClass::Reset(void)
  88. {
  89. Verts.Delete_All(false);
  90. VertNorms.Delete_All(false);
  91. }
  92. void DecalPolyClass::Add_Vertex(const Vector3 & point,const Vector3 & norm)
  93. {
  94. Verts.Add(point);
  95. VertNorms.Add(norm);
  96. }
  97. void DecalPolyClass::Clip(const PlaneClass & plane,DecalPolyClass & dest) const
  98. {
  99. dest.Reset();
  100. if (Verts.Count() <= 2) return;
  101. // temporary variables used in clipping
  102. int i = 0;
  103. int iprev = Verts.Count() - 1;
  104. bool cur_point_in_front;
  105. bool prev_point_in_front;
  106. float alpha;
  107. Vector3 int_point;
  108. Vector3 int_normal;
  109. // perform clipping
  110. prev_point_in_front = plane.In_Front(Verts[iprev]);
  111. #if DISABLE_CLIPPING
  112. prev_point_in_front = true;
  113. #endif
  114. for (int j=0; j<Verts.Count(); j++) {
  115. cur_point_in_front = plane.In_Front(Verts[i]);
  116. #if DISABLE_CLIPPING
  117. cur_point_in_front = true;
  118. #endif
  119. if (prev_point_in_front) {
  120. if (cur_point_in_front) {
  121. // Previous vertex was in front of plane and this vertex is in
  122. // front of the plane so we emit this vertex.
  123. dest.Add_Vertex(Verts[i],VertNorms[i]);
  124. } else {
  125. // Previous vert was in front, this vert is behind, compute
  126. // the intersection and emit the point.
  127. plane.Compute_Intersection(Verts[iprev],Verts[i],&alpha);
  128. Vector3::Lerp(Verts[iprev],Verts[i],alpha,&int_point);
  129. Vector3::Lerp(VertNorms[iprev],VertNorms[i],alpha,&int_normal);
  130. dest.Add_Vertex(int_point,int_normal);
  131. }
  132. } else {
  133. if (cur_point_in_front) {
  134. // segment is going from the back halfspace to the front halfspace
  135. // compute the intersection and emit it, then continue
  136. // the edge into the front halfspace and emit the end point.
  137. plane.Compute_Intersection(Verts[iprev],Verts[i],&alpha);
  138. Vector3::Lerp(Verts[iprev],Verts[i],alpha,&int_point);
  139. Vector3::Lerp(VertNorms[iprev],VertNorms[i],alpha,&int_normal);
  140. dest.Add_Vertex(int_point,int_normal);
  141. dest.Add_Vertex(Verts[i],VertNorms[i]);
  142. }
  143. }
  144. prev_point_in_front = cur_point_in_front;
  145. iprev = i;
  146. i = (i+1)%(Verts.Count());
  147. }
  148. }
  149. static DecalPolyClass _DecalPoly0;
  150. static DecalPolyClass _DecalPoly1;
  151. /*
  152. ** DecalMeshClass Implementation
  153. */
  154. /***********************************************************************************************
  155. * DecalMeshClass::DecalMeshClass -- Constructor *
  156. * *
  157. * INPUT: *
  158. * *
  159. * OUTPUT: *
  160. * *
  161. * WARNINGS: *
  162. * *
  163. * HISTORY: *
  164. * 1/26/00 gth : Created. *
  165. *=============================================================================================*/
  166. DecalMeshClass::DecalMeshClass(MeshClass * parent,DecalSystemClass * system) :
  167. Parent(parent),
  168. DecalSystem(system)
  169. {
  170. WWASSERT(Parent != NULL);
  171. WWASSERT(DecalSystem != NULL);
  172. }
  173. /***********************************************************************************************
  174. * DecalMeshClass::~DecalMeshClass -- Destructor *
  175. * *
  176. * INPUT: *
  177. * *
  178. * OUTPUT: *
  179. * *
  180. * WARNINGS: *
  181. * *
  182. * HISTORY: *
  183. * 1/26/00 gth : Created. *
  184. *=============================================================================================*/
  185. DecalMeshClass::~DecalMeshClass(void)
  186. {
  187. }
  188. /*
  189. ** RigidDecalMeshClass Implementation
  190. */
  191. /***********************************************************************************************
  192. * RigidDecalMeshClass::RigidDecalMeshClass -- Constructor *
  193. * *
  194. * INPUT: *
  195. * *
  196. * OUTPUT: *
  197. * *
  198. * WARNINGS: *
  199. * *
  200. * HISTORY: *
  201. * 1/31/00 NH : Created. *
  202. *=============================================================================================*/
  203. RigidDecalMeshClass::RigidDecalMeshClass(MeshClass * parent, DecalSystemClass * system) :
  204. DecalMeshClass(parent, system)
  205. {
  206. }
  207. /***********************************************************************************************
  208. * RigidDecalMeshClass::~RigidDecalMeshClass -- Destructor *
  209. * *
  210. * INPUT: *
  211. * *
  212. * OUTPUT: *
  213. * *
  214. * WARNINGS: *
  215. * *
  216. * HISTORY: *
  217. * 1/31/00 NH : Created. *
  218. *=============================================================================================*/
  219. RigidDecalMeshClass::~RigidDecalMeshClass(void)
  220. {
  221. int i;
  222. // Notify the system that this decal mesh is being destroyed.
  223. for (i=0; i<Decals.Count(); i++) {
  224. DecalSystem->Decal_Mesh_Destroyed(Decals[i].DecalID,this);
  225. }
  226. // Release all of our references. The memory in the arrays will automatically be
  227. // released by the SimpleDynVecClass...
  228. for (i=0; i<Polys.Count(); i++) {
  229. REF_PTR_RELEASE(Textures[i]);
  230. }
  231. for (i=0; i<Verts.Count(); i++) {
  232. REF_PTR_RELEASE(VertexMaterials[i]);
  233. }
  234. }
  235. /***********************************************************************************************
  236. * RigidDecalMeshClass::Render -- Render the decals *
  237. * *
  238. * INPUT: *
  239. * *
  240. * OUTPUT: *
  241. * *
  242. * WARNINGS: *
  243. * *
  244. * HISTORY: *
  245. * 1/26/00 gth : Created. *
  246. *=============================================================================================*/
  247. void RigidDecalMeshClass::Render(void)
  248. {
  249. if ((Decals.Count() == 0) || (WW3D::Are_Decals_Enabled() == false)) return;
  250. /*
  251. ** Install the mesh'es transform. NOTE, this could go wrong if someone changes the
  252. ** transform between the time that the mesh is rendered and the time that the decal
  253. ** mesh is rendered... It shouldn't happen though.
  254. */
  255. DX8Wrapper::Set_Transform(D3DTS_WORLD,Parent->Get_Transform());
  256. /*
  257. ** Copy the vertices into the dynamic vb
  258. */
  259. DynamicVBAccessClass dynamic_vb(BUFFER_TYPE_DYNAMIC_DX8,dynamic_fvf_type,Verts.Count());
  260. {
  261. DynamicVBAccessClass::WriteLockClass lock(&dynamic_vb);
  262. VertexFormatXYZNDUV2 * vertex = lock.Get_Formatted_Vertex_Array();
  263. for (int i=0; i<Verts.Count(); i++) {
  264. vertex->x = Verts[i].X;
  265. vertex->y = Verts[i].Y;
  266. vertex->z = Verts[i].Z;
  267. vertex->nx = VertNorms[i].X;
  268. vertex->ny = VertNorms[i].Y;
  269. vertex->nz = VertNorms[i].Z;
  270. vertex->diffuse = 0xFFFFFFFF;
  271. vertex->u1 = TexCoords[i].X;
  272. vertex->v1 = TexCoords[i].Y;
  273. vertex->u2 = 0.0f;
  274. vertex->v2 = 0.0f;
  275. vertex++;
  276. }
  277. }
  278. /*
  279. ** Copy the indices into the dynamic ib
  280. */
  281. DynamicIBAccessClass dynamic_ib(BUFFER_TYPE_DYNAMIC_DX8,Polys.Count() * 3);
  282. {
  283. DynamicIBAccessClass::WriteLockClass lock(&dynamic_ib);
  284. unsigned short * indices = lock.Get_Index_Array();
  285. for (int i=0; i < Polys.Count(); i++)
  286. {
  287. indices[i*3 + 0] = (unsigned short)Polys[i].I;
  288. indices[i*3 + 1] = (unsigned short)Polys[i].J;
  289. indices[i*3 + 2] = (unsigned short)Polys[i].K;
  290. }
  291. }
  292. /*
  293. ** Render in runs of constant material settings
  294. */
  295. int cur_poly_index = 0;
  296. int next_poly_index = 0;
  297. while (next_poly_index < Polys.Count()) {
  298. next_poly_index = Process_Material_Run(cur_poly_index);
  299. DX8Wrapper::Set_Index_Buffer(dynamic_ib,0);
  300. DX8Wrapper::Set_Vertex_Buffer(dynamic_vb);
  301. DX8Wrapper::Draw_Triangles( 3*cur_poly_index,
  302. (next_poly_index - cur_poly_index), // poly count
  303. Polys[cur_poly_index].I,
  304. 1 + Polys[next_poly_index-1].K - Polys[cur_poly_index].I);
  305. cur_poly_index = next_poly_index;
  306. }
  307. }
  308. /***********************************************************************************************
  309. * RigidDecalMeshClass::Process_Material_Run -- scans the mesh for material runs *
  310. * *
  311. * This function will install the materials for poly[start_index] and scan forward for *
  312. * the next material change. It will return the start index for the next material change *
  313. * *
  314. * INPUT: *
  315. * *
  316. * OUTPUT: *
  317. * *
  318. * WARNINGS: *
  319. * *
  320. * HISTORY: *
  321. * 2/22/2001 gth : Created. *
  322. *=============================================================================================*/
  323. int RigidDecalMeshClass::Process_Material_Run(int start_index)
  324. {
  325. DX8Wrapper::Set_Texture(0,Textures[start_index]);
  326. DX8Wrapper::Set_Material(VertexMaterials[Polys[start_index].I]);
  327. DX8Wrapper::Set_Shader(Shaders[start_index]);
  328. int next_index = start_index;
  329. while ( (next_index < Polys.Count()) &&
  330. (Textures[next_index] == Textures[start_index]) &&
  331. (Shaders[next_index] == Shaders[start_index]) &&
  332. (VertexMaterials[next_index] == VertexMaterials[start_index]))
  333. {
  334. next_index++;
  335. }
  336. return next_index;
  337. }
  338. /***********************************************************************************************
  339. * RigidDecalMeshClass::Create_Decal -- Generate a new decal *
  340. * *
  341. * INPUT: *
  342. * *
  343. * OUTPUT: *
  344. * *
  345. * WARNINGS: *
  346. * All Decals on a mesh must be generated from the same DecalSystemClass! *
  347. * *
  348. * HISTORY: *
  349. * 1/26/00 gth : Created. *
  350. *=============================================================================================*/
  351. bool RigidDecalMeshClass::Create_Decal
  352. (
  353. DecalGeneratorClass * generator,
  354. const OBBoxClass & localbox,
  355. SimpleDynVecClass<uint32> & apt,
  356. const DynamicVectorClass<Vector3> * world_vertex_locs
  357. )
  358. {
  359. // Since we can't rely on the hardware polygon offset function, I'm physically offsetting
  360. // the decal polygons along the normal of the decal generator. If we could instead rely
  361. // on hardware "polygon offset" we could remove this code and we could make decals non-sorting
  362. #if 0
  363. const float ZBIAS_DISTANCE = 0.01f;
  364. Vector3 zbias_offset;
  365. generator->Get_Transform().Get_Z_Vector(&zbias_offset);
  366. Matrix3D invtm;
  367. Parent->Get_Transform().Get_Orthogonal_Inverse(invtm);
  368. Matrix3D::Rotate_Vector(invtm,zbias_offset,&zbias_offset);
  369. zbias_offset *= ZBIAS_DISTANCE;
  370. #endif
  371. // NOTE: world_vertex_locs/norms should not be set for this class
  372. WWASSERT(world_vertex_locs == 0);
  373. int i,j;
  374. WWASSERT(generator->Peek_Decal_System() == DecalSystem);
  375. /*
  376. ** If any polys were collected, add a new MeshDecalStruct
  377. */
  378. if (apt.Count() == 0) {
  379. return false;
  380. }
  381. DecalStruct newdecal;
  382. newdecal.DecalID = generator->Get_Decal_ID();
  383. newdecal.FaceStartIndex = Polys.Count(); // start faces at the end of the current array
  384. newdecal.FaceCount = 0; // init facecount to zero
  385. newdecal.VertexStartIndex = Verts.Count(); // start vertices at the end of the current array
  386. newdecal.VertexCount = 0; // init vertcount to zero
  387. /*
  388. ** Grab pointers to the parent mesh's components
  389. */
  390. MeshModelClass * model = Parent->Peek_Model();
  391. const Vector3i * src_polys = model->Get_Polygon_Array();
  392. const Vector3 * src_verts = model->Get_Vertex_Array();
  393. const Vector3 * src_vnorms = model->Get_Vertex_Normal_Array();
  394. /*
  395. ** Grab a pointer to the material settings
  396. */
  397. MaterialPassClass * material = generator->Get_Material();
  398. /*
  399. ** Set up the generator for our coordinate system
  400. */
  401. generator->Set_Mesh_Transform(Parent->Get_Transform());
  402. /*
  403. ** Compute the clipping planes
  404. */
  405. PlaneClass planes[4];
  406. Vector3 extent;
  407. Matrix3::Rotate_Vector(localbox.Basis,Vector3(localbox.Extent.X,0,0),&extent);
  408. Vector3 direction(localbox.Basis.Get_X_Vector());
  409. planes[0].Set(-direction,localbox.Center + extent);
  410. planes[1].Set(direction,localbox.Center - extent);
  411. Matrix3::Rotate_Vector(localbox.Basis,Vector3(0,localbox.Extent.Y,0),&extent);
  412. direction.Set(localbox.Basis.Get_Y_Vector());
  413. planes[2].Set(-direction,localbox.Center + extent);
  414. planes[3].Set(direction,localbox.Center - extent);
  415. /*
  416. ** Generate the faces and per-face info
  417. */
  418. bool added_polys = false;
  419. Vector3 pdir = localbox.Basis.Get_Z_Vector();
  420. for (i=0; i<apt.Count(); i++) {
  421. /*
  422. ** check if the polygon is backfacing
  423. */
  424. PlaneClass plane;
  425. model->Compute_Plane(apt[i],&plane);
  426. float dot = Vector3::Dot_Product(plane.N,pdir);
  427. if (dot > generator->Get_Backface_Threshhold()) {
  428. /*
  429. ** Copy src_polys[apt[i]] into our clip polygon
  430. */
  431. _DecalPoly0.Reset();
  432. const Vector3i & poly = src_polys[apt[i]];
  433. for (j=0; j<3; j++) {
  434. _DecalPoly0.Add_Vertex(src_verts[poly[j]] /*+ zbias_offset*/,src_vnorms[poly[j]]);
  435. }
  436. /*
  437. ** Clip against the edges of the bounding box
  438. */
  439. _DecalPoly0.Clip(planes[0],_DecalPoly1);
  440. _DecalPoly1.Clip(planes[1],_DecalPoly0);
  441. _DecalPoly0.Clip(planes[2],_DecalPoly1);
  442. _DecalPoly1.Clip(planes[3],_DecalPoly0);
  443. /*
  444. ** Check if the clipped polygon is empty or degenerate
  445. */
  446. if (_DecalPoly0.Verts.Count() >= 3) {
  447. /*
  448. ** Extract triangles from the clipped polygon
  449. */
  450. int first_vert = Verts.Count();
  451. for (j=1; j<_DecalPoly0.Verts.Count()-1; j++) {
  452. /*
  453. ** Check if this triangle is degenerate (Sutherland-Hodgeman can sometimes create degenerate tris)
  454. */
  455. // TODO
  456. /*
  457. ** Add the triangle, its plane equation, and the per-tri materials
  458. */
  459. added_polys = true;
  460. Polys.Add(Vector3i(first_vert,first_vert + j,first_vert + j + 1));
  461. Shaders.Add(material->Peek_Shader());
  462. Textures.Add(material->Get_Texture()); // Get_Texture gives us a reference...
  463. }
  464. /*
  465. ** Extract verts from the clipped polygon
  466. */
  467. for (j=0; j<_DecalPoly0.Verts.Count(); j++) {
  468. Verts.Add(_DecalPoly0.Verts[j]);
  469. _DecalPoly0.VertNorms[j].Normalize();
  470. VertNorms.Add(_DecalPoly0.VertNorms[j]);
  471. VertexMaterials.Add(material->Get_Material()); // Get_Material gives us a ref.
  472. /*
  473. ** Compute the uv coordinates for this vertex
  474. */
  475. Vector3 stq;
  476. generator->Compute_Texture_Coordinate(Verts[Verts.Count()-1],&stq);
  477. TexCoords.Add(Vector2(stq.X,stq.Y));
  478. }
  479. }
  480. }
  481. }
  482. if (added_polys) {
  483. newdecal.FaceCount = Polys.Count() - newdecal.FaceStartIndex;
  484. newdecal.VertexCount = Verts.Count() - newdecal.VertexStartIndex;
  485. Decals.Add(newdecal);
  486. /*
  487. ** tell the generator that we added a decal
  488. */
  489. generator->Add_Mesh(Parent);
  490. }
  491. material->Release_Ref();
  492. #ifdef WWDEBUG
  493. /*
  494. ** Some paranoid debug code: ensure all tris have valid vertex indices
  495. */
  496. int poly_count = Polys.Count();
  497. int vert_count = Verts.Count();
  498. for (int poly_idx = 0; poly_idx < poly_count; poly_idx++) {
  499. WWASSERT (Polys[poly_idx].I < vert_count);
  500. WWASSERT (Polys[poly_idx].I >= 0);
  501. WWASSERT (Polys[poly_idx].J < vert_count);
  502. WWASSERT (Polys[poly_idx].J >= 0);
  503. WWASSERT (Polys[poly_idx].K < vert_count);
  504. WWASSERT (Polys[poly_idx].K >= 0);
  505. }
  506. #endif
  507. /*
  508. ** Only return true if we actually added a decal
  509. */
  510. return added_polys;
  511. }
  512. /***********************************************************************************************
  513. * RigidDecalMeshClass::Delete_Decal -- Delete a decal *
  514. * *
  515. * INPUT: *
  516. * *
  517. * OUTPUT: *
  518. * *
  519. * WARNINGS: *
  520. * *
  521. * HISTORY: *
  522. * 1/26/00 gth : Created. *
  523. *=============================================================================================*/
  524. bool RigidDecalMeshClass::Delete_Decal(uint32 id)
  525. {
  526. /*
  527. ** Find the MeshDecal which matches the given id
  528. */
  529. int decal_index = -1;
  530. for (int i = 0;i < Decals.Count(); i++) {
  531. if (Decals[i].DecalID == id) {
  532. decal_index = i;
  533. break;
  534. }
  535. }
  536. if (decal_index == -1) {
  537. return false;
  538. }
  539. DecalStruct * decal = &Decals[decal_index];
  540. /*
  541. ** Remove all geometry used by this decal
  542. */
  543. Polys.Delete_Range(decal->FaceStartIndex,decal->FaceCount);
  544. Verts.Delete_Range(decal->VertexStartIndex,decal->VertexCount);
  545. VertNorms.Delete_Range(decal->VertexStartIndex,decal->VertexCount);
  546. TexCoords.Delete_Range(decal->VertexStartIndex,decal->VertexCount);
  547. /*
  548. ** Re-index the remaining triangle vertex indices
  549. */
  550. for (int poly_index = 0; poly_index < Polys.Count(); poly_index++) {
  551. if (Polys[poly_index].I > decal->VertexStartIndex) Polys[poly_index].I -= decal->VertexCount;
  552. if (Polys[poly_index].J > decal->VertexStartIndex) Polys[poly_index].J -= decal->VertexCount;
  553. if (Polys[poly_index].K > decal->VertexStartIndex) Polys[poly_index].K -= decal->VertexCount;
  554. }
  555. /*
  556. ** Remove all materials used by this decal (remember to release refs!)
  557. */
  558. for (int fi=decal->FaceStartIndex; fi<decal->FaceCount; fi++) {
  559. REF_PTR_RELEASE(Textures[fi]);
  560. }
  561. for (int vi=decal->VertexStartIndex; vi<decal->VertexCount; vi++) {
  562. REF_PTR_RELEASE(VertexMaterials[vi]);
  563. }
  564. Shaders.Delete_Range(decal->FaceStartIndex,decal->FaceCount);
  565. Textures.Delete_Range(decal->FaceStartIndex,decal->FaceCount);
  566. VertexMaterials.Delete_Range(decal->VertexStartIndex,decal->VertexCount);
  567. /*
  568. ** Remove MeshDecal and refresh all other decal indices
  569. */
  570. for (int di=decal_index+1; di<Decals.Count(); di++) {
  571. Decals[di].FaceStartIndex -= decal->FaceCount;
  572. Decals[di].VertexStartIndex -= decal->VertexCount;
  573. }
  574. Decals.Delete(decal_index);
  575. #ifdef WWDEBUG
  576. /*
  577. ** Some paranoid debug code: ensure all tris have valid vertex indices
  578. */
  579. int poly_count = Polys.Count();
  580. int vert_count = Verts.Count();
  581. for (int poly_idx = 0; poly_idx < poly_count; poly_idx++) {
  582. WWASSERT (Polys[poly_idx].I < vert_count);
  583. WWASSERT (Polys[poly_idx].I >= 0);
  584. WWASSERT (Polys[poly_idx].J < vert_count);
  585. WWASSERT (Polys[poly_idx].J >= 0);
  586. WWASSERT (Polys[poly_idx].K < vert_count);
  587. WWASSERT (Polys[poly_idx].K >= 0);
  588. }
  589. #endif
  590. return true;
  591. }
  592. /*
  593. ** Temporary Buffers
  594. ** These buffers are used by the skin code for temporary storage of the deformed vertices and
  595. ** vertex normals.
  596. */
  597. static SimpleVecClass<Vector3> _TempVertexBuffer;
  598. static SimpleVecClass<Vector3> _TempNormalBuffer;
  599. /*
  600. ** SkinDecalMeshClass Implementation
  601. */
  602. /***********************************************************************************************
  603. * SkinDecalMeshClass::SkinDecalMeshClass -- Constructor *
  604. * *
  605. * INPUT: *
  606. * *
  607. * OUTPUT: *
  608. * *
  609. * WARNINGS: *
  610. * *
  611. * HISTORY: *
  612. * 1/31/00 NH : Created. *
  613. *=============================================================================================*/
  614. SkinDecalMeshClass::SkinDecalMeshClass(MeshClass * parent, DecalSystemClass * system) :
  615. DecalMeshClass(parent, system)
  616. {
  617. }
  618. /***********************************************************************************************
  619. * SkinDecalMeshClass::~SkinDecalMeshClass -- Destructor *
  620. * *
  621. * INPUT: *
  622. * *
  623. * OUTPUT: *
  624. * *
  625. * WARNINGS: *
  626. * *
  627. * HISTORY: *
  628. * 1/31/00 NH : Created. *
  629. *=============================================================================================*/
  630. SkinDecalMeshClass::~SkinDecalMeshClass(void)
  631. {
  632. int i;
  633. // Notify the system that this decal mesh is being destroyed.
  634. for (i=0; i<Decals.Count(); i++) {
  635. DecalSystem->Decal_Mesh_Destroyed(Decals[i].DecalID,this);
  636. }
  637. // Release all of our references. The memory in the arrays will automatically be
  638. // released by the SimpleDynVecClass...
  639. for (i=0; i<Polys.Count(); i++) {
  640. REF_PTR_RELEASE(Textures[i]);
  641. }
  642. for (i=0; i<ParentVertexIndices.Count(); i++) {
  643. REF_PTR_RELEASE(VertexMaterials[i]);
  644. }
  645. }
  646. /***********************************************************************************************
  647. * SkinDecalMeshClass::Render -- Render the decals *
  648. * *
  649. * INPUT: *
  650. * *
  651. * OUTPUT: *
  652. * *
  653. * WARNINGS: *
  654. * *
  655. * HISTORY: *
  656. * 1/31/00 NH : Created. *
  657. *=============================================================================================*/
  658. void SkinDecalMeshClass::Render(void)
  659. {
  660. if ((Decals.Count() == 0) || (WW3D::Are_Decals_Enabled() == false)) return;
  661. /*
  662. ** Don't allow decals on sorted meshes
  663. */
  664. MeshModelClass * model = Parent->Peek_Model();
  665. if (model->Get_Flag(MeshModelClass::SORT)) {
  666. WWDEBUG_SAY(("ERROR: decals applied to a sorted mesh!\n"));
  667. return;
  668. }
  669. /*
  670. ** Skin decals coordinates are in world space
  671. */
  672. DX8Wrapper::Set_Transform(D3DTS_WORLD,Matrix3D::Identity);
  673. /*
  674. ** Skin decals have to get the deformed vertices of their parent meshes. For this
  675. ** reason, decals on skins is not a very good idea...
  676. */
  677. _TempVertexBuffer.Uninitialised_Grow(model->Get_Vertex_Count());
  678. _TempNormalBuffer.Uninitialised_Grow(model->Get_Vertex_Count());
  679. Parent->Get_Deformed_Vertices(&(_TempVertexBuffer[0]),&(_TempNormalBuffer[0]));
  680. /*
  681. ** Copy the vertices into the dynamic vb
  682. */
  683. DynamicVBAccessClass dynamic_vb(BUFFER_TYPE_DYNAMIC_DX8,dynamic_fvf_type,ParentVertexIndices.Count());
  684. {
  685. DynamicVBAccessClass::WriteLockClass lock(&dynamic_vb);
  686. VertexFormatXYZNDUV2 * vertex = lock.Get_Formatted_Vertex_Array();
  687. for (int i=0; i<ParentVertexIndices.Count(); i++) {
  688. int src_i = ParentVertexIndices[i];
  689. vertex->x = _TempVertexBuffer[src_i].X;
  690. vertex->y = _TempVertexBuffer[src_i].Y;
  691. vertex->z = _TempVertexBuffer[src_i].Z;
  692. vertex->nx = _TempNormalBuffer[src_i].X;
  693. vertex->ny = _TempNormalBuffer[src_i].Y;
  694. vertex->nz = _TempNormalBuffer[src_i].Z;
  695. vertex->diffuse = 0xFFFFFFFF;
  696. vertex->u1 = TexCoords[i].X;
  697. vertex->v1 = TexCoords[i].Y;
  698. vertex->u2 = 0.0f;
  699. vertex->v2 = 0.0f;
  700. vertex++;
  701. }
  702. }
  703. /*
  704. ** Copy the indices into the dynamic ib
  705. */
  706. DynamicIBAccessClass dynamic_ib(BUFFER_TYPE_DYNAMIC_DX8,Polys.Count() * 3);
  707. {
  708. DynamicIBAccessClass::WriteLockClass lock(&dynamic_ib);
  709. unsigned short * indices = lock.Get_Index_Array();
  710. for (int i=0; i < Polys.Count(); i++)
  711. {
  712. indices[i*3 + 0] = (unsigned short)Polys[i].I;
  713. indices[i*3 + 1] = (unsigned short)Polys[i].J;
  714. indices[i*3 + 2] = (unsigned short)Polys[i].K;
  715. }
  716. }
  717. /*
  718. ** Render in runs of constant material settings
  719. */
  720. int cur_poly_index = 0;
  721. int next_poly_index = 0;
  722. while (next_poly_index < Polys.Count()) {
  723. next_poly_index = Process_Material_Run(cur_poly_index);
  724. DX8Wrapper::Set_Index_Buffer(dynamic_ib,0);
  725. DX8Wrapper::Set_Vertex_Buffer(dynamic_vb);
  726. DX8Wrapper::Draw_Triangles(3*cur_poly_index,
  727. (next_poly_index - cur_poly_index), // poly count
  728. Polys[cur_poly_index].I,
  729. 1 + Polys[next_poly_index-1].K - Polys[cur_poly_index].I);
  730. cur_poly_index = next_poly_index;
  731. }
  732. }
  733. /***********************************************************************************************
  734. * SkinDecalMeshClass::Process_Material_Run -- scans the mesh for material runs *
  735. * *
  736. * This function will install the materials for poly[start_index] and scan forward for *
  737. * the next material change. It will return the start index for the next material change *
  738. * *
  739. * INPUT: *
  740. * *
  741. * OUTPUT: *
  742. * *
  743. * WARNINGS: *
  744. * *
  745. * HISTORY: *
  746. * 2/22/2001 gth : Created. *
  747. *=============================================================================================*/
  748. int SkinDecalMeshClass::Process_Material_Run(int start_index)
  749. {
  750. DX8Wrapper::Set_Texture(0,Textures[start_index]);
  751. DX8Wrapper::Set_Material(VertexMaterials[Polys[start_index].I]);
  752. DX8Wrapper::Set_Shader(Shaders[start_index]);
  753. int next_index = start_index;
  754. while ( (next_index < Polys.Count()) &&
  755. (Textures[next_index] == Textures[start_index]) &&
  756. (Shaders[next_index] == Shaders[start_index]) &&
  757. (VertexMaterials[next_index] == VertexMaterials[start_index]))
  758. {
  759. next_index++;
  760. }
  761. return next_index;
  762. }
  763. /***********************************************************************************************
  764. * SkinDecalMeshClass::Create_Decal -- Generate a new decal *
  765. * *
  766. * INPUT: *
  767. * *
  768. * OUTPUT: *
  769. * *
  770. * WARNINGS: *
  771. * All Decals on a mesh must be generated from the same DecalSystemClass! *
  772. * *
  773. * HISTORY: *
  774. * 1/31/00 NH : Created. *
  775. *=============================================================================================*/
  776. bool SkinDecalMeshClass::Create_Decal(DecalGeneratorClass * generator,
  777. const OBBoxClass & localbox, SimpleDynVecClass<uint32> & apt,
  778. const DynamicVectorClass<Vector3> * world_vertex_locs)
  779. {
  780. int i;
  781. WWASSERT(generator->Peek_Decal_System() == DecalSystem);
  782. // The dynamically updated vertex locations are needed - we have no static geometry
  783. WWASSERT(world_vertex_locs);
  784. /*
  785. ** If any polys were collected, add a new MeshDecalStruct
  786. */
  787. if (apt.Count() == 0) {
  788. return false;
  789. }
  790. DecalStruct newdecal;
  791. newdecal.DecalID = generator->Get_Decal_ID();
  792. newdecal.FaceStartIndex = Polys.Count(); // start faces at the end of the current array
  793. newdecal.FaceCount = 0; // init facecount to zero
  794. newdecal.VertexStartIndex = ParentVertexIndices.Count(); // start vertices at the end of the current array
  795. newdecal.VertexCount = 0; // init vertcount to zero
  796. /*
  797. ** Grab pointers to the parent mesh's components
  798. */
  799. MeshModelClass * model = Parent->Peek_Model();
  800. const Vector3i * src_polys = model->Get_Polygon_Array();
  801. /*
  802. ** Grab a pointer to the material settings
  803. */
  804. MaterialPassClass * material = generator->Get_Material();
  805. /*
  806. ** Set up the generator for the world coordinate system (the deformed vertices are in worldspace)
  807. */
  808. generator->Set_Mesh_Transform(Matrix3D::Identity);
  809. /*
  810. ** Generate the faces and per-face info (remember to add-ref's)
  811. ** TODO: rewrite this to take advantage of vertex sharing...
  812. */
  813. int face_size_hint = Polys.Count() + apt.Count();
  814. int first_vert = ParentVertexIndices.Count();
  815. for (i = 0; i < apt.Count(); i++) {
  816. int offset = first_vert + i * 3;
  817. Polys.Add(Vector3i(offset, offset + 1, offset + 2), face_size_hint);
  818. Shaders.Add(material->Peek_Shader(), face_size_hint);
  819. Textures.Add(material->Get_Texture(), face_size_hint); // Get_Texture gives us a reference...
  820. }
  821. /*
  822. ** Copy the vertices and per-vertex info
  823. ** TODO: rewrite this to take advantage of vertex sharing...
  824. */
  825. int vertex_size_hint = ParentVertexIndices.Count() + 3 * apt.Count();
  826. for (i = 0; i < apt.Count(); i++) {
  827. int face_index = apt[i];
  828. for (int vi = 0; vi < 3; vi++) {
  829. /*
  830. ** Copy data for this vertex
  831. */
  832. ParentVertexIndices.Add(src_polys[face_index][vi], vertex_size_hint);
  833. VertexMaterials.Add(material->Get_Material(), vertex_size_hint); // Get_Material gives us a ref.
  834. /*
  835. ** Compute the uv coordinates for this vertex
  836. */
  837. Vector3 stq;
  838. generator->Compute_Texture_Coordinate((*world_vertex_locs)[ParentVertexIndices[ParentVertexIndices.Count() - 1]], &stq);
  839. TexCoords.Add(Vector2(stq.X,stq.Y));
  840. }
  841. }
  842. newdecal.FaceCount = Polys.Count() - newdecal.FaceStartIndex;
  843. newdecal.VertexCount = ParentVertexIndices.Count() - newdecal.VertexStartIndex;
  844. Decals.Add(newdecal);
  845. material->Release_Ref();
  846. /*
  847. ** tell the generator that we added a MeshDecal
  848. */
  849. generator->Add_Mesh(Parent);
  850. #ifdef WWDEBUG
  851. /*
  852. ** Some paranoid debug code: ensure all tris have valid vertex indices
  853. */
  854. int poly_count = Polys.Count();
  855. int vert_count = ParentVertexIndices.Count();
  856. for (int poly_idx = 0; poly_idx < poly_count; poly_idx++) {
  857. WWASSERT (Polys[poly_idx].I < vert_count);
  858. WWASSERT (Polys[poly_idx].I >= 0);
  859. WWASSERT (Polys[poly_idx].J < vert_count);
  860. WWASSERT (Polys[poly_idx].J >= 0);
  861. WWASSERT (Polys[poly_idx].K < vert_count);
  862. WWASSERT (Polys[poly_idx].K >= 0);
  863. }
  864. #endif
  865. // WWDEBUG_SAY(("Decal mesh now has: %d polys\r\n",Polys.Count()));
  866. return true;
  867. }
  868. /***********************************************************************************************
  869. * SkinDecalMeshClass::Delete_Decal -- Delete a decal *
  870. * *
  871. * INPUT: *
  872. * *
  873. * OUTPUT: *
  874. * *
  875. * WARNINGS: *
  876. * *
  877. * HISTORY: *
  878. * 1/31/00 NH : Created. *
  879. *=============================================================================================*/
  880. bool SkinDecalMeshClass::Delete_Decal(uint32 id)
  881. {
  882. /*
  883. ** Find the MeshDecal which matches the given id
  884. */
  885. int decal_index = -1;
  886. for (int i = 0;i < Decals.Count(); i++) {
  887. if (Decals[i].DecalID == id) {
  888. decal_index = i;
  889. break;
  890. }
  891. }
  892. if (decal_index == -1) {
  893. return false;
  894. }
  895. DecalStruct * decal = &Decals[decal_index];
  896. /*
  897. ** Remove all geometry used by this decal
  898. */
  899. Polys.Delete_Range(decal->FaceStartIndex, decal->FaceCount);
  900. ParentVertexIndices.Delete_Range(decal->VertexStartIndex, decal->VertexCount);
  901. TexCoords.Delete_Range(decal->VertexStartIndex, decal->VertexCount);
  902. /*
  903. ** Re-index the remaining triangle vertex indices
  904. */
  905. for (int poly_index = 0; poly_index < Polys.Count(); poly_index++) {
  906. if (Polys[poly_index].I > decal->VertexStartIndex) Polys[poly_index].I -= decal->VertexCount;
  907. if (Polys[poly_index].J > decal->VertexStartIndex) Polys[poly_index].J -= decal->VertexCount;
  908. if (Polys[poly_index].K > decal->VertexStartIndex) Polys[poly_index].K -= decal->VertexCount;
  909. }
  910. /*
  911. ** Remove all materials used by this decal (remember to release refs!)
  912. */
  913. for (int fi = decal->FaceStartIndex; fi < decal->FaceCount; fi++) {
  914. REF_PTR_RELEASE(Textures[fi]);
  915. }
  916. for (int vi=decal->VertexStartIndex; vi<decal->VertexCount; vi++) {
  917. REF_PTR_RELEASE(VertexMaterials[vi]);
  918. }
  919. Shaders.Delete_Range(decal->FaceStartIndex,decal->FaceCount);
  920. Textures.Delete_Range(decal->FaceStartIndex,decal->FaceCount);
  921. VertexMaterials.Delete_Range(decal->VertexStartIndex,decal->VertexCount);
  922. /*
  923. ** Remove MeshDecal and refresh all other decal indices
  924. */
  925. for (int di=decal_index+1; di<Decals.Count(); di++) {
  926. Decals[di].FaceStartIndex -= decal->FaceCount;
  927. Decals[di].VertexStartIndex -= decal->VertexCount;
  928. }
  929. Decals.Delete(decal_index);
  930. #ifdef WWDEBUG
  931. /*
  932. ** Some paranoid debug code: ensure all tris have valid vertex indices
  933. */
  934. int poly_count = Polys.Count();
  935. int vert_count = ParentVertexIndices.Count();
  936. for (int poly_idx = 0; poly_idx < poly_count; poly_idx++) {
  937. WWASSERT (Polys[poly_idx].I < vert_count);
  938. WWASSERT (Polys[poly_idx].I >= 0);
  939. WWASSERT (Polys[poly_idx].J < vert_count);
  940. WWASSERT (Polys[poly_idx].J >= 0);
  941. WWASSERT (Polys[poly_idx].K < vert_count);
  942. WWASSERT (Polys[poly_idx].K >= 0);
  943. }
  944. #endif
  945. return true;
  946. }