decalmsh.cpp 45 KB

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