decalmsh.cpp 45 KB

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