shattersystem.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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 : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/shattersystem.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 7/11/01 9:49p $*
  31. * *
  32. * $Revision:: 7 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "shattersystem.h"
  38. #include "assetmgr.h"
  39. #include "mesh.h"
  40. #include "meshmdl.h"
  41. #include "dynamesh.h"
  42. #include "htree.h"
  43. #include "plane.h"
  44. #include "simplevec.h"
  45. #include "wwstring.h"
  46. #include "vp.h"
  47. #include "meshmatdesc.h"
  48. #include <stdlib.h>
  49. /*
  50. ** Debug logging for the shatter system
  51. */
  52. #define SHATTER_DEBUG_LOG_ENABLED 0
  53. #if (SHATTER_DEBUG_LOG_ENABLED)
  54. #define SHATTER_DEBUG_SAY(x) WWDEBUG_SAY(x)
  55. #else
  56. #define SHATTER_DEBUG_SAY(x)
  57. #endif
  58. #define SHATTER_PATTERN_FORMAT "ShatterPlanes%d"
  59. #define BPT_FRONT 0x01
  60. #define BPT_BACK 0x02
  61. #define BPT_ON 0x04
  62. #define BPT_BOTH 0x08
  63. #define BPT_EPSILON 0.0001f
  64. #define BPT_COINCIDENCE_EPSILON 0.000001f
  65. /**
  66. ** Mesh Material Parameters
  67. */
  68. class MeshMtlParamsClass
  69. {
  70. public:
  71. MeshMtlParamsClass(MeshModelClass * model);
  72. ~MeshMtlParamsClass(void);
  73. int PassCount;
  74. /*
  75. ** NOTE: currently I do not fully support stage 1 textures or texture arrays in any pass.
  76. ** The uv coordinates will be computed but the textures will not be set up.
  77. */
  78. unsigned * DCG[MeshMatDescClass::MAX_PASSES];
  79. unsigned * DIG[MeshMatDescClass::MAX_PASSES];
  80. Vector2 * UV[MeshMatDescClass::MAX_PASSES][MeshMatDescClass::MAX_TEX_STAGES];
  81. // Vector3i * UVIndexArray[MeshMatDescClass::MAX_PASSES];
  82. };
  83. /**
  84. ** VertexClass
  85. ** Temporary representation of vertices as they are being clipped
  86. */
  87. class VertexClass
  88. {
  89. public:
  90. VertexClass(void);
  91. VertexClass(const VertexClass & that);
  92. VertexClass & operator = (const VertexClass & that);
  93. int Which_Side(const PlaneClass & plane) const;
  94. Vector3 Position;
  95. Vector3 Normal;
  96. int PassCount;
  97. unsigned DCG[MeshMatDescClass::MAX_PASSES];
  98. unsigned DIG[MeshMatDescClass::MAX_PASSES];
  99. Vector2 TexCoord[MeshMatDescClass::MAX_PASSES][MeshMatDescClass::MAX_TEX_STAGES];
  100. static void Lerp(const VertexClass & v0,const VertexClass & v1,float lerp,VertexClass * res);
  101. static void Intersect_Plane(const VertexClass & v0,const VertexClass & v1,const PlaneClass & plane,VertexClass * res);
  102. };
  103. /**
  104. ** PolygonClass
  105. ** Temporary representation of polygons as they are being clipped
  106. */
  107. class PolygonClass
  108. {
  109. public:
  110. enum { BPT_POLY_MAX_VERTS = 24 };
  111. PolygonClass(void);
  112. PolygonClass(const PolygonClass & that);
  113. PolygonClass(const VertexClass * points, int num);
  114. PolygonClass & operator = (const PolygonClass & that);
  115. // Accessors
  116. const VertexClass & operator[] (int i) const { return Verts[i]; }
  117. VertexClass & operator[] (int i) { return Verts[i]; }
  118. int Get_Vertex_Count(void) const { return NumVerts; }
  119. int Get_Material_ID(void) const { return MaterialId; }
  120. const PlaneClass & Get_Plane(void) const { return Plane; }
  121. void Set_Vertex_Count(int count) { NumVerts = count; }
  122. void Set_Material_Id(int id) { MaterialId = id; }
  123. void Set_Plane(const PlaneClass & plane) { Plane = plane; }
  124. // Operations
  125. void Compute_Plane(void);
  126. int Which_Side(const PlaneClass & plane) const;
  127. void Split(const PlaneClass & plane,PolygonClass & front,PolygonClass & back) const;
  128. bool Is_Degenerate(void);
  129. bool Salvage_Degenerate(void);
  130. public:
  131. int MaterialId;
  132. int NumVerts;
  133. VertexClass Verts[BPT_POLY_MAX_VERTS];
  134. PlaneClass Plane;
  135. };
  136. /**
  137. ** BSPClass
  138. ** Representation of a node in a BSP clipping tree in the shatter system
  139. ** Meshes are "shattered" by clipping them with a fixed BSP tree.
  140. ** The resulting leaf polygons are turned into separate dynamic objects
  141. ** which are thrown around in the world. The BSP tree is constructed
  142. ** using an HTree where each transform in the tree is used as a clipping
  143. ** plane. The plane is defined by the x-y plane of the coordinate system
  144. ** (i.e. z-axis is the normal of the plane, origin is a point on the plane).
  145. ** Leaf nodes of this tree will have two indices. These are indices into
  146. ** the MeshFragments array where they put the polygons in thier front and
  147. ** back half-spaces.
  148. */
  149. class BSPClass
  150. {
  151. public:
  152. BSPClass(HTreeClass * tree,int bone_index,int & leaf_index);
  153. ~BSPClass(void);
  154. const PlaneClass & Get_Plane(void) { return Plane; }
  155. BSPClass * Get_Front_Child(void) { return Front; }
  156. BSPClass * Get_Back_Child(void) { return Back; }
  157. void Clip_Polygon(const PolygonClass & poly);
  158. protected:
  159. void Set_Plane_From_Transform(const Matrix3D & tm);
  160. PlaneClass Plane; // plane equation
  161. BSPClass * Front; // pointers to child clipping planes
  162. BSPClass * Back;
  163. int FrontLeafIndex; // if a leaf node, has two mesh fragment indices
  164. int BackLeafIndex;
  165. };
  166. /***********************************************************************************************
  167. **
  168. ** Static Variables
  169. ** ShatterPatterns - Array of BSP-trees for clipping (shattering) messhes
  170. ** ClippedPolygonPool - Array of polygon arrays, one per leaf.
  171. ** MeshFragments - Array of resultant clipped meshes, one per leaf
  172. ** TmpVertPositions - Workspace for transforming vertex positions
  173. ** TmpVertNormals - Workspace for transforming vertex normals
  174. **
  175. ***********************************************************************************************/
  176. enum { MAX_MESH_FRAGMENTS = 32 };
  177. static SimpleDynVecClass<BSPClass *> ShatterPatterns;
  178. static SimpleDynVecClass<PolygonClass> ClipPools[MAX_MESH_FRAGMENTS];
  179. static SimpleDynVecClass<DynamicMeshClass *> MeshFragments(MAX_MESH_FRAGMENTS);
  180. static SimpleVecClass<Vector3> TmpVertPositions(256);
  181. static SimpleVecClass<Vector3> TmpVertNormals(256);
  182. /***********************************************************************************************
  183. **
  184. ** Local static utility functions
  185. **
  186. ***********************************************************************************************/
  187. static Vector3 * _get_temp_vertex_position_array(int count)
  188. {
  189. if (TmpVertPositions.Length() < count) {
  190. TmpVertPositions.Resize(count);
  191. }
  192. return &(TmpVertPositions[0]);
  193. }
  194. static Vector3 * _get_temp_vertex_normal_array(int count)
  195. {
  196. if (TmpVertNormals.Length() < count) {
  197. TmpVertNormals.Resize(count);
  198. }
  199. return &(TmpVertNormals[0]);
  200. }
  201. /***********************************************************************************************
  202. **
  203. ** MeshMtlParamsClass Implementation
  204. **
  205. ***********************************************************************************************/
  206. MeshMtlParamsClass::MeshMtlParamsClass(MeshModelClass * model)
  207. {
  208. PassCount = model->Get_Pass_Count();
  209. for (int ipass=0; ipass<MeshMatDescClass::MAX_PASSES; ipass++) {
  210. DCG[ipass] = model->Get_DCG_Array(ipass);
  211. DIG[ipass] = model->Get_DIG_Array(ipass);
  212. // UVIndexArray[ipass] = model->Get_UVIndex_Array(ipass,false);
  213. for (int istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  214. UV[ipass][istage] = const_cast<Vector2*>(model->Get_UV_Array(ipass,istage));
  215. }
  216. }
  217. }
  218. MeshMtlParamsClass::~MeshMtlParamsClass(void)
  219. {
  220. }
  221. /***********************************************************************************************
  222. **
  223. ** VertexClass Implementation
  224. **
  225. ***********************************************************************************************/
  226. VertexClass::VertexClass(void) :
  227. Position(0,0,0),
  228. Normal(0,0,1),
  229. PassCount(0)
  230. {
  231. for (int ipass=0; ipass<MeshMatDescClass::MAX_PASSES; ipass++) {
  232. DCG[ipass]=0xffffffff;
  233. DIG[ipass]=0xffffffff;
  234. for (int istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  235. TexCoord[ipass][istage].Set(0,0);
  236. }
  237. }
  238. }
  239. VertexClass::VertexClass(const VertexClass & that)
  240. {
  241. Position = that.Position;
  242. Normal = that.Normal;
  243. PassCount = that.PassCount;
  244. for (int ipass=0; ipass<PassCount; ipass++) {
  245. DCG[ipass] = that.DCG[ipass];
  246. DIG[ipass] = that.DIG[ipass];
  247. for (int istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  248. TexCoord[ipass][istage] = that.TexCoord[ipass][istage];
  249. }
  250. }
  251. }
  252. VertexClass & VertexClass::operator = (const VertexClass & that)
  253. {
  254. if (this != &that) {
  255. Position = that.Position;
  256. Normal = that.Normal;
  257. PassCount = that.PassCount;
  258. for (int ipass=0; ipass<PassCount; ipass++) {
  259. DCG[ipass] = that.DCG[ipass];
  260. DIG[ipass] = that.DIG[ipass];
  261. for (int istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  262. TexCoord[ipass][istage] = that.TexCoord[ipass][istage];
  263. }
  264. }
  265. }
  266. return *this;
  267. }
  268. void VertexClass::Lerp
  269. (
  270. const VertexClass & v0,
  271. const VertexClass & v1,
  272. float lerp,
  273. VertexClass * res
  274. )
  275. {
  276. assert(lerp >= -BPT_EPSILON);
  277. assert(lerp <= 1.0 + BPT_EPSILON);
  278. assert(v0.PassCount==v1.PassCount);
  279. res->PassCount = v0.PassCount;
  280. // interpolate position
  281. Vector3::Lerp(v0.Position,v1.Position,lerp,&(res->Position));
  282. // interpolate normal, renormalize
  283. Vector3::Lerp(v0.Normal,v1.Normal,lerp,&(res->Normal));
  284. res->Normal.Normalize();
  285. // interpolate material properies
  286. for (int ipass=0; ipass<v0.PassCount; ipass++) {
  287. Vector4 dcg_v0=DX8Wrapper::Convert_Color(v0.DCG[ipass]);
  288. Vector4 dcg_v1=DX8Wrapper::Convert_Color(v1.DCG[ipass]);
  289. Vector4 dig_v0=DX8Wrapper::Convert_Color(v0.DIG[ipass]);
  290. Vector4 dig_v1=DX8Wrapper::Convert_Color(v1.DIG[ipass]);
  291. Vector4::Lerp(dcg_v0,dcg_v1,res->DCG[ipass]);
  292. Vector4::Lerp(dig_v0,dig_v1,res->DIG[ipass]);
  293. // Vector4::Lerp(v0.DCG[ipass],v1.DCG[ipass],lerp,&(res->DCG[ipass]));
  294. // Vector4::Lerp(v0.DIG[ipass],v1.DIG[ipass],lerp,&(res->DIG[ipass]));
  295. for (int istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  296. Vector2::Lerp(v0.TexCoord[ipass][istage],v1.TexCoord[ipass][istage],lerp,&(res->TexCoord[ipass][istage]));
  297. }
  298. }
  299. }
  300. int VertexClass::Which_Side(const PlaneClass & plane) const
  301. {
  302. float d = Vector3::Dot_Product(plane.N,Position);
  303. d -= plane.D;
  304. if (d > BPT_EPSILON) {
  305. return BPT_FRONT;
  306. }
  307. if (d < -BPT_EPSILON) {
  308. return BPT_BACK;
  309. }
  310. return BPT_ON;
  311. }
  312. void VertexClass::Intersect_Plane
  313. (
  314. const VertexClass & p0,
  315. const VertexClass & p1,
  316. const PlaneClass & plane,
  317. VertexClass * res
  318. )
  319. {
  320. float alpha = 0.0f;
  321. plane.Compute_Intersection(p0.Position,p1.Position,&alpha);
  322. VertexClass::Lerp(p0,p1,alpha,res);
  323. }
  324. /***********************************************************************************************
  325. **
  326. ** PolygonClass Implementation
  327. **
  328. ***********************************************************************************************/
  329. PolygonClass::PolygonClass(void) :
  330. NumVerts(0)
  331. {
  332. }
  333. PolygonClass::PolygonClass(const PolygonClass & that)
  334. {
  335. NumVerts = that.NumVerts;
  336. for (int i=0;i<NumVerts;i++) {
  337. Verts[i] = that.Verts[i];
  338. }
  339. }
  340. PolygonClass::PolygonClass(const VertexClass * points, int num)
  341. {
  342. NumVerts = num;
  343. for (int i=0; i<NumVerts; i++) {
  344. Verts[i] = points[i];
  345. }
  346. }
  347. PolygonClass & PolygonClass::operator = (const PolygonClass & that)
  348. {
  349. if (this != &that) {
  350. MaterialId = that.MaterialId;
  351. NumVerts = that.NumVerts;
  352. Plane = that.Plane;
  353. for (int i=0;i<NumVerts;i++) {
  354. Verts[i] = that.Verts[i];
  355. }
  356. }
  357. return * this;
  358. }
  359. void PolygonClass::Compute_Plane(void)
  360. {
  361. double nx = 0;
  362. double ny = 0;
  363. double nz = 0;
  364. double ax = 0;
  365. double ay = 0;
  366. double az = 0;
  367. int i,j;
  368. for (i=0; i<NumVerts; i++) {
  369. j = (i+1) % NumVerts;
  370. nx += (double)(Verts[i].Position.Y - Verts[j].Position.Y) * (double)(Verts[i].Position.Z + Verts[j].Position.Z);
  371. ny += (double)(Verts[i].Position.Z - Verts[j].Position.Z) * (double)(Verts[i].Position.X + Verts[j].Position.X);
  372. nz += (double)(Verts[i].Position.X - Verts[j].Position.X) * (double)(Verts[i].Position.Y + Verts[j].Position.Y);
  373. ax += (double)Verts[i].Position.X;
  374. ay += (double)Verts[i].Position.Y;
  375. az += (double)Verts[i].Position.Z;
  376. }
  377. ax /= (double)NumVerts;
  378. ay /= (double)NumVerts;
  379. az /= (double)NumVerts;
  380. double len = WWMath::Sqrt(nx*nx + ny*ny + nz*nz);
  381. nx /= len;
  382. ny /= len;
  383. nz /= len;
  384. Plane.Set(Vector3(nx,ny,nz),Vector3(ax,ay,az));
  385. }
  386. int PolygonClass::Which_Side(const PlaneClass & plane) const
  387. {
  388. int side_mask = 0;
  389. for (int i=0; i<NumVerts;i++) {
  390. side_mask |= Verts[i].Which_Side(plane);
  391. }
  392. // check if all verts are "ON"
  393. if (side_mask == BPT_ON) {
  394. return BPT_ON;
  395. }
  396. // check if all verts are either "ON" or "FRONT"
  397. if ((side_mask & ~(BPT_FRONT | BPT_ON)) == 0) {
  398. return BPT_FRONT;
  399. }
  400. // check if all verts are either "ON" or "BACK"
  401. if ((side_mask & ~(BPT_BACK | BPT_ON)) == 0) {
  402. return BPT_BACK;
  403. }
  404. // otherwise, poly spans the plane.
  405. return BPT_BOTH;
  406. }
  407. void PolygonClass::Split(const PlaneClass & plane,PolygonClass & front,PolygonClass & back) const
  408. {
  409. front = *this;
  410. back = *this;
  411. front.NumVerts = back.NumVerts = 0;
  412. assert(Which_Side(plane) == BPT_BOTH);
  413. VertexClass point;
  414. front.NumVerts = 0;
  415. back.NumVerts = 0;
  416. // find a vertex on one side or the other
  417. int side = BPT_ON;
  418. int i;
  419. for (i = 0; (i < NumVerts) && ((side = Verts[i].Which_Side(plane)) == BPT_ON); i++);
  420. // perform clipping
  421. int iprev = i;
  422. int sideprev = side;
  423. int sidelastdefinite = 0;
  424. i = (i+1) % NumVerts;
  425. for (int j=0; j<NumVerts; j++) {
  426. side = Verts[i].Which_Side(plane);
  427. if (sideprev == BPT_FRONT) {
  428. if (side == BPT_FRONT) {
  429. // Previous vertex was in front of plane and this vertex is in
  430. // front of the plane so we emit this vertex in the front poly
  431. front.Verts[(front.NumVerts)++] = Verts[i];
  432. } else if (side == BPT_ON) {
  433. // Previous vert was in front, this vert is "on" so emit
  434. // the vertex into the front poly.
  435. sidelastdefinite = BPT_FRONT;
  436. front.Verts[(front.NumVerts)++] = Verts[i];
  437. } else { // side == BPT_BACK
  438. // Previous vert was in front, this vert is behind, compute
  439. // the intersection and emit the point in both the front
  440. // and back polys. Then continue the edge into the back halfspace
  441. VertexClass::Intersect_Plane(Verts[iprev],Verts[i],plane,&point);
  442. front.Verts[(front.NumVerts)++] = point;
  443. back.Verts[(back.NumVerts)++] = point;
  444. back.Verts[(back.NumVerts)++] = Verts[i];
  445. }
  446. } else if (sideprev == BPT_BACK) {
  447. if (side == BPT_FRONT) {
  448. // segment is going from the back halfspace to the front halfspace
  449. // compute the intersection and emit it in both polys, then continue
  450. // the edge into the front halfspace.
  451. VertexClass::Intersect_Plane(Verts[iprev],Verts[i],plane,&point);
  452. back.Verts[(back.NumVerts)++] = point;
  453. front.Verts[(front.NumVerts)++] = point;
  454. front.Verts[(front.NumVerts)++] = Verts[i];
  455. } else if (side == BPT_ON) {
  456. // segment went from back halfspace to "on" the plane. Emit
  457. // the vertex into the back poly and remember that we came
  458. // from the back halfspace.
  459. sidelastdefinite = BPT_BACK;
  460. back.Verts[(back.NumVerts)++] = Verts[i];
  461. } else { // side == BPT_BACK
  462. // segment is completely in the back halfspace, just emit the
  463. // vertex into the back poly
  464. back.Verts[(back.NumVerts)++] = Verts[i];
  465. }
  466. } else if (sideprev == BPT_ON) {
  467. if (side == BPT_FRONT) {
  468. // segment is on the plane
  469. if (sidelastdefinite == BPT_BACK) {
  470. front.Verts[(front.NumVerts)++] = Verts[iprev];
  471. }
  472. front.Verts[(front.NumVerts)++] = Verts[i];
  473. } else if (side == BPT_ON) {
  474. if (sidelastdefinite == BPT_FRONT) {
  475. front.Verts[(front.NumVerts)++] = Verts[i];
  476. } else {
  477. back.Verts[(back.NumVerts)++] = Verts[i];
  478. }
  479. } else { // side == BPT_BACK
  480. if (sidelastdefinite == BPT_FRONT) {
  481. back.Verts[(back.NumVerts)++] = Verts[iprev];
  482. }
  483. back.Verts[(back.NumVerts)++] = Verts[i];
  484. }
  485. } else {
  486. WWASSERT_PRINT(0,"PolygonClass::Split : invalid side\n");
  487. }
  488. sideprev = side;
  489. iprev = i;
  490. i = (i+1)%NumVerts;
  491. }
  492. front.Compute_Plane();
  493. back.Compute_Plane();
  494. // check the two polygons
  495. if (front.Is_Degenerate()) {
  496. front.Salvage_Degenerate();
  497. }
  498. if (back.Is_Degenerate()) {
  499. back.Salvage_Degenerate();
  500. }
  501. }
  502. bool PolygonClass::Is_Degenerate(void)
  503. {
  504. int i,j;
  505. if (NumVerts <= 2) {
  506. WWDEBUG_SAY(("Degenerate Poly - fewer than 3 vertices\r\n"));
  507. return true;
  508. }
  509. for (i=0; i<NumVerts; i++) {
  510. for (j = i+1; j < NumVerts; j++) {
  511. float delta = (Verts[i].Position - Verts[j].Position).Length();
  512. if (delta < BPT_COINCIDENCE_EPSILON) {
  513. WWDEBUG_SAY(("Degenerate Poly - coincident vertices!\r\n"));
  514. return true;
  515. }
  516. }
  517. }
  518. for (i=0; i<NumVerts; i++) {
  519. int side = Verts[i].Which_Side(Plane);
  520. if (side != BPT_ON) {
  521. // hmmm, try to recalculate the plane, if it is still bad, then give up
  522. Compute_Plane();
  523. if (Verts[i].Which_Side(Plane) != BPT_ON) {
  524. WWDEBUG_SAY(("Degenerate Poly - invalid plane!\r\n"));
  525. return true;
  526. }
  527. }
  528. }
  529. return false;
  530. }
  531. bool PolygonClass::Salvage_Degenerate(void)
  532. {
  533. /*
  534. ** About all we can do is combine sequential vertices which are co-incident
  535. */
  536. int i = 0;
  537. while (i < NumVerts) {
  538. float delta = (Verts[i].Position - Verts[i+1].Position).Length();
  539. if (delta < BPT_COINCIDENCE_EPSILON) {
  540. for (int j=i+1; j<NumVerts-1; j++) {
  541. Verts[j] = Verts[j+1];
  542. }
  543. NumVerts--;
  544. } else {
  545. i++;
  546. }
  547. }
  548. return !Is_Degenerate();
  549. }
  550. /***********************************************************************************************
  551. **
  552. ** BSPClass Implementation
  553. **
  554. ***********************************************************************************************/
  555. BSPClass::BSPClass(HTreeClass * tree,int bone_index,int & leaf_index) :
  556. Plane(0,0,1,0),
  557. Front(NULL),
  558. Back(NULL),
  559. FrontLeafIndex(-1),
  560. BackLeafIndex(-1)
  561. {
  562. // initialize our plane equation from the transform
  563. Set_Plane_From_Transform(tree->Get_Transform(bone_index));
  564. int front = -1;
  565. int back = -1;
  566. // search for a front and back child of this bone
  567. for (int ibone=0; ibone < tree->Num_Pivots(); ibone++) {
  568. if (tree->Get_Parent_Index(ibone) == bone_index) {
  569. // found a child, now see if it is the front or back child
  570. Vector3 point = tree->Get_Transform(ibone).Get_Translation();
  571. if (Plane.In_Front(point)) {
  572. front = ibone;
  573. } else {
  574. back = ibone;
  575. }
  576. }
  577. }
  578. // Recurse if we have children, otherwise assign leaf indices
  579. if (front != -1) {
  580. Front = W3DNEW BSPClass(tree,front,leaf_index);
  581. } else {
  582. FrontLeafIndex = leaf_index++;
  583. }
  584. if (back != -1) {
  585. Back = W3DNEW BSPClass(tree,back,leaf_index);
  586. } else {
  587. BackLeafIndex = leaf_index++;
  588. }
  589. }
  590. BSPClass::~BSPClass(void)
  591. {
  592. if (Front != NULL) {
  593. delete Front;
  594. }
  595. if (Back != NULL) {
  596. delete Back;
  597. }
  598. Front = Back = NULL;
  599. }
  600. void BSPClass::Set_Plane_From_Transform(const Matrix3D & tm)
  601. {
  602. Plane.Set(tm.Get_Z_Vector(),tm.Get_Translation());
  603. }
  604. void BSPClass::Clip_Polygon(const PolygonClass & polygon)
  605. {
  606. PolygonClass front_poly,back_poly;
  607. front_poly.Set_Vertex_Count(0);
  608. back_poly.Set_Vertex_Count(0);
  609. switch(polygon.Which_Side(Plane))
  610. {
  611. case BPT_FRONT: case BPT_ON:
  612. front_poly = polygon;
  613. break;
  614. case BPT_BACK:
  615. back_poly = polygon;
  616. break;
  617. default:
  618. polygon.Split(Plane,front_poly,back_poly);
  619. break;
  620. };
  621. // Process the front halfspace: Recurse if we have a child clipping plane,
  622. // otherwise add our polygons to our assigned clipping pool
  623. if (Front == NULL) {
  624. // We're a leaf node so put the polygons into the mesh fragment arrays
  625. if (front_poly.Get_Vertex_Count() >= 3) {
  626. ClipPools[FrontLeafIndex].Add(front_poly);
  627. }
  628. } else {
  629. // Pass the polygons to our child for further clipping
  630. if (front_poly.Get_Vertex_Count() >= 3) {
  631. Front->Clip_Polygon(front_poly);
  632. }
  633. }
  634. // Process the back halfspace:
  635. if (Back==NULL) {
  636. if (back_poly.Get_Vertex_Count() >= 3) {
  637. ClipPools[BackLeafIndex].Add(back_poly);
  638. }
  639. } else {
  640. if (back_poly.Get_Vertex_Count() >= 3) {
  641. Back->Clip_Polygon(back_poly);
  642. }
  643. }
  644. }
  645. /***********************************************************************************************
  646. **
  647. ** ShatterSystem Implementation - this is the interface with the outside world
  648. **
  649. ** Transform meshes into space needed for shattering:
  650. ** Vshatter = Mscale-to-unit * Mworld-shatterview * Mobj-world * Vobj
  651. **
  652. ***********************************************************************************************/
  653. void ShatterSystem::Init(void)
  654. {
  655. /*
  656. ** Resize the Mesh Fragment pointer array to handle the maximum number
  657. ** of mesh fragments.
  658. */
  659. MeshFragments.Resize(MAX_MESH_FRAGMENTS);
  660. /*
  661. ** Search for ShatterPattern hierarchy tree objects, beginning with ShatterPattern00
  662. ** Create a BSP structure for each one.
  663. */
  664. StringClass htree_name;
  665. htree_name.Format(SHATTER_PATTERN_FORMAT,0);
  666. if (WW3DAssetManager::Get_Instance()==NULL)
  667. return; // WorldBuilderTool doesn't initialize the asset manager. jba.
  668. #if 1
  669. HTreeClass *htree = NULL;
  670. #else
  671. HTreeClass * htree = WW3DAssetManager::Get_Instance()->Get_HTree(htree_name);
  672. #endif
  673. while (htree != NULL) {
  674. if ((htree->Num_Pivots() > 1) && (htree->Num_Pivots() < MAX_MESH_FRAGMENTS)) {
  675. int leaf_counter = 0;
  676. htree->Base_Update(Matrix3D(1));
  677. ShatterPatterns.Add(W3DNEW BSPClass(htree,1,leaf_counter));
  678. }
  679. /*
  680. ** Try to load the next tree
  681. */
  682. htree_name.Format(SHATTER_PATTERN_FORMAT,ShatterPatterns.Count());
  683. htree = WW3DAssetManager::Get_Instance()->Get_HTree(htree_name);
  684. }
  685. }
  686. void ShatterSystem::Shutdown(void)
  687. {
  688. /*
  689. ** Release all mesh fragments
  690. */
  691. Release_Fragments();
  692. /*
  693. ** Release any loaded BSP trees
  694. */
  695. for (int i=0; i<ShatterPatterns.Count(); i++) {
  696. delete ShatterPatterns[i];
  697. ShatterPatterns[i] = NULL;
  698. }
  699. ShatterPatterns.Delete_All();
  700. }
  701. void ShatterSystem::Shatter_Mesh(MeshClass * mesh,const Vector3 & point,const Vector3 & direction)
  702. {
  703. int ivert,ipoly;
  704. int ipass,istage;
  705. /*
  706. ** Reset the temporary clip arrays
  707. ** Release any old mesh fragment render objects
  708. */
  709. Reset_Clip_Pools();
  710. Release_Fragments();
  711. /*
  712. ** Verify that this mesh meets the criteria for being shattered
  713. ** - it has a maximum of two passes
  714. ** - it uses only one vertex material per pass
  715. ** - it uses only one shader per pass
  716. ** - it uses only one texture per pass
  717. */
  718. MeshModelClass * model = mesh->Get_Model();
  719. if (model->Get_Pass_Count() > MeshMatDescClass::MAX_PASSES) {
  720. WWDEBUG_SAY(("Failed to shatter model: %s. Too many passes (%d)\n",model->Get_Name(),model->Get_Pass_Count()));
  721. REF_PTR_RELEASE(model);
  722. return;
  723. }
  724. for (ipass=0; ipass<model->Get_Pass_Count(); ipass++) {
  725. if (model->Has_Material_Array(ipass) || model->Has_Shader_Array(ipass)) {
  726. WWDEBUG_SAY(("Failed to shatter model: %s. It has shader or material arrays\n",model->Get_Name()));
  727. REF_PTR_RELEASE(model);
  728. return;
  729. }
  730. for (istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  731. if (model->Has_Texture_Array(ipass,istage)) {
  732. WWDEBUG_SAY(("Failed to shatter model: %s. Texture array in pass: %d stage: %d\n",model->Get_Name(),ipass,istage));
  733. REF_PTR_RELEASE(model);
  734. return;
  735. }
  736. }
  737. }
  738. /*
  739. ** Grab a random shatter pattern
  740. */
  741. BSPClass * clipper = ShatterPatterns[rand() % ShatterPatterns.Count()];
  742. /*
  743. ** Compute transforms which take vertices from mesh-space to shatter-space
  744. ** and back again. Transform polygons into "shatter-space", clip, then
  745. ** transform the results back out
  746. **
  747. ** Take vertices from obj-space to shatter space:
  748. ** Vshatter = Mscale-to-unit * Mworld-shatterview * Mobj-world * Vobj
  749. **
  750. ** Clip the polygons to the BSP
  751. ** Vclipped = BSP_CLIP(Vshatter)
  752. **
  753. ** Next, take the verts back to object space:
  754. ** Vobjclip = Inverse(Mscale-to-unit * Mworld-shatter * Mobj-world) * Vclipped
  755. ** = Inv(Mobj-world)*Inv(Mworld-shatter)*Inv(Mscl-to-unit) * Vclipped
  756. **
  757. ** Next, create separate, re-centered meshes
  758. ** Vnewobj = Mold-obj-to-new-obj * Vobjclip
  759. */
  760. /*
  761. ** Object-space to world-space transform
  762. */
  763. Matrix3D Mobj_to_world = mesh->Get_Transform();
  764. /*
  765. ** World-space to shatter-space transform
  766. */
  767. Matrix3D Mshatter_to_world;
  768. Matrix3D Mworld_to_shatter;
  769. Mshatter_to_world.Look_At(point,point+direction,0.0f); // TODO: could put random roll
  770. Mshatter_to_world.Get_Orthogonal_Inverse(Mworld_to_shatter);
  771. /*
  772. ** Final pair of matrices
  773. */
  774. Matrix3D Mobj_to_shatter;
  775. Matrix3D Mshatter_to_obj;
  776. Matrix3D::Multiply(Mworld_to_shatter,Mobj_to_world,&Mobj_to_shatter);
  777. Mobj_to_shatter.Get_Orthogonal_Inverse(Mshatter_to_obj);
  778. /*
  779. ** Scaling matrices. This could be simpler if Matrix3D had a full inverse function.
  780. */
  781. SphereClass sphere;
  782. model->Get_Bounding_Sphere(&sphere);
  783. float scale_factor = 5.0f / sphere.Radius; // mesh scales to 5x shatter pattern.
  784. Matrix3D Mscale_to_shatter(1);
  785. Matrix3D Mscale_from_shatter(1);
  786. Mscale_to_shatter.Scale(scale_factor);
  787. Mscale_from_shatter.Scale(1.0f / scale_factor);
  788. Matrix3D::Multiply(Mscale_to_shatter,Mobj_to_shatter,&Mobj_to_shatter);
  789. Matrix3D::Multiply(Mshatter_to_obj,Mscale_from_shatter,&Mshatter_to_obj);
  790. /*
  791. ** Grab the arrays out of the mesh and transform verts and vnorms
  792. ** into "shatter-space"
  793. */
  794. const Vector3i * polys = model->Get_Polygon_Array();
  795. const Vector3 * src_verts = model->Get_Vertex_Array();
  796. const Vector3 * src_vnorms = model->Get_Vertex_Normal_Array();
  797. Vector3 * verts = _get_temp_vertex_position_array(model->Get_Vertex_Count());
  798. VectorProcessorClass::Transform(verts,src_verts,Mobj_to_shatter,model->Get_Vertex_Count());
  799. /*
  800. ** Build a description of the material parameters for the mesh
  801. */
  802. MeshMtlParamsClass mtl_params(model);
  803. SHATTER_DEBUG_SAY(("****************************************************\n"));
  804. SHATTER_DEBUG_SAY((" Clipping model: %s\n",model->Get_Name()));
  805. SHATTER_DEBUG_SAY(("****************************************************\n"));
  806. /*
  807. ** Pass each polygon of the source model through the BSP clipper
  808. */
  809. for (ipoly=0; ipoly<model->Get_Polygon_Count(); ipoly++) {
  810. /*
  811. ** Set up a PolygonClass for polygon 'i' in the mesh
  812. */
  813. SHATTER_DEBUG_SAY(("Passing polygon %d to clipper.\n",ipoly));
  814. PolygonClass polygon;
  815. for (ivert=0; ivert<3; ivert++) {
  816. int vert_index = polys[ipoly][ivert];
  817. polygon.Verts[ivert].PassCount = mtl_params.PassCount;
  818. polygon.Verts[ivert].Position = verts[vert_index];
  819. polygon.Verts[ivert].Normal = src_vnorms[vert_index];
  820. SHATTER_DEBUG_SAY(("position: %f %f %f\n",verts[vert_index].X,verts[vert_index].Y,verts[vert_index].Z));
  821. SHATTER_DEBUG_SAY(("normal: %f %f %f\n",src_vnorms[vert_index].X,src_vnorms[vert_index].Y,src_vnorms[vert_index].Z));
  822. for (ipass=0; ipass<MeshMatDescClass::MAX_PASSES; ipass++) {
  823. if (mtl_params.DCG[ipass] != NULL) {
  824. polygon.Verts[ivert].DCG[ipass] = mtl_params.DCG[ipass][vert_index];
  825. SHATTER_DEBUG_SAY(("DCG: pass: %d : %f %f %f\n",ipass,mtl_params.DCG[ipass][vert_index].X,mtl_params.DCG[ipass][vert_index].Y,mtl_params.DCG[ipass][vert_index].Z));
  826. }
  827. if (mtl_params.DIG[ipass] != NULL) {
  828. polygon.Verts[ivert].DIG[ipass] = mtl_params.DIG[ipass][vert_index];
  829. SHATTER_DEBUG_SAY(("DIG: pass: %d : %f %f %f\n",ipass,mtl_params.DIG[ipass][vert_index].X,mtl_params.DIG[ipass][vert_index].Y,mtl_params.DIG[ipass][vert_index].Z));
  830. }
  831. for (istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  832. if (mtl_params.UV[ipass][istage] != NULL) {
  833. polygon.Verts[ivert].TexCoord[ipass][istage] = mtl_params.UV[ipass][istage][vert_index];
  834. SHATTER_DEBUG_SAY(("UV pass: %d stage: %d: %f %f\n",ipass,istage,mtl_params.UV[ipass][istage][vert_index].X,mtl_params.UV[ipass][istage][vert_index].Y));
  835. }
  836. }
  837. /* if (mtl_params.UVIndexArray[ipass] != NULL) {
  838. int uv_index = mtl_params.UVIndexArray[ipass][ipoly][ivert];
  839. polygon.Verts[ivert].TexCoord[ipass][0] = mtl_params.UV[ipass][0][uv_index];
  840. SHATTER_DEBUG_SAY(("Per-Face UV pass: %d: %f %f\n",ipass,polygon.Verts[ivert].TexCoord[ipass][0].X,polygon.Verts[ivert].TexCoord[ipass][0].Y));
  841. }
  842. */ }
  843. }
  844. polygon.Set_Vertex_Count(3);
  845. polygon.Compute_Plane();
  846. /*
  847. ** Pass it through the BSP
  848. */
  849. clipper->Clip_Polygon(polygon);
  850. }
  851. /*
  852. ** convert the clipped polygons into meshes
  853. */
  854. Process_Clip_Pools(Mshatter_to_obj,mesh,mtl_params);
  855. /*
  856. ** release resources
  857. */
  858. REF_PTR_RELEASE(model);
  859. }
  860. int ShatterSystem::Get_Fragment_Count(void)
  861. {
  862. return MeshFragments.Count();
  863. }
  864. RenderObjClass * ShatterSystem::Get_Fragment(int fragment_index)
  865. {
  866. if (MeshFragments[fragment_index] != NULL) {
  867. MeshFragments[fragment_index]->Add_Ref();
  868. }
  869. return MeshFragments[fragment_index];
  870. }
  871. RenderObjClass * ShatterSystem::Peek_Fragment(int fragment_index)
  872. {
  873. return MeshFragments[fragment_index];
  874. }
  875. void ShatterSystem::Release_Fragments(void)
  876. {
  877. // release any ref's to render objects
  878. for (int i=0; i<MeshFragments.Count(); i++) {
  879. REF_PTR_RELEASE(MeshFragments[i]);
  880. }
  881. // reset array but don't resize
  882. MeshFragments.Delete_All(false);
  883. }
  884. void ShatterSystem::Reset_Clip_Pools(void)
  885. {
  886. for (int i=0; i<MAX_MESH_FRAGMENTS; i++) {
  887. // reset array but don't resize
  888. ClipPools[i].Delete_All(false);
  889. }
  890. }
  891. void ShatterSystem::Process_Clip_Pools
  892. (
  893. const Matrix3D & Mshatter_to_mesh,
  894. MeshClass * mesh,
  895. MeshMtlParamsClass & mtl_params
  896. )
  897. {
  898. /*
  899. ** Release any render objects we currently have and reset the array count
  900. */
  901. Release_Fragments();
  902. /*
  903. ** Grab the model
  904. */
  905. MeshModelClass * model = mesh->Get_Model();
  906. WWASSERT(model != NULL);
  907. /*
  908. ** Loop over all ClipPools and build a mesh for any that contain polygons
  909. */
  910. for (int ipool=0; ipool<MAX_MESH_FRAGMENTS; ipool++) {
  911. if (ClipPools[ipool].Count() > 0) {
  912. int ivert,ipoly,ipass,istage;
  913. /*
  914. ** Count the verts and polys
  915. */
  916. int pcount = 0;
  917. int vcount = 0;
  918. for (ipoly=0;ipoly<ClipPools[ipool].Count();ipoly++) {
  919. int poly_vert_count = ClipPools[ipool][ipoly].Get_Vertex_Count();
  920. vcount += poly_vert_count;
  921. pcount += poly_vert_count-2;
  922. }
  923. SHATTER_DEBUG_SAY(("****************************************************\n"));
  924. SHATTER_DEBUG_SAY((" Reassembling fragment %d of model: %s\n",ipool,model->Get_Name()));
  925. SHATTER_DEBUG_SAY((" polycount = %d vertexcount = %d\n",pcount,vcount));
  926. SHATTER_DEBUG_SAY(("****************************************************\n"));
  927. /*
  928. ** Create the new mesh, install materials
  929. */
  930. DynamicMeshClass * new_mesh = NEW_REF(DynamicMeshClass,(pcount,vcount));
  931. MaterialInfoClass * matinfo = NEW_REF(MaterialInfoClass,());
  932. if (model->Get_Flag(MeshModelClass::SORT)) {
  933. new_mesh->Enable_Sort();
  934. }
  935. new_mesh->Set_Pass_Count(mtl_params.PassCount);
  936. bool has_textures = false;
  937. for (ipass=0; ipass<model->Get_Pass_Count(); ipass++) {
  938. if (model->Peek_Single_Material(ipass) != NULL) {
  939. matinfo->Add_Vertex_Material(model->Peek_Single_Material(ipass));
  940. }
  941. if (model->Peek_Single_Texture(ipass) != NULL) {
  942. matinfo->Add_Texture(model->Peek_Single_Texture(ipass));
  943. has_textures = true;
  944. }
  945. }
  946. new_mesh->Set_Material_Info(matinfo);
  947. for (ipass=0; ipass<model->Get_Pass_Count(); ipass++) {
  948. new_mesh->Set_Vertex_Material(model->Peek_Single_Material(ipass),false,ipass);
  949. new_mesh->Set_Shader(model->Get_Single_Shader(ipass),ipass);
  950. TextureClass * tex = model->Peek_Single_Texture(ipass,0);
  951. if (tex != NULL) {
  952. new_mesh->Set_Texture(tex,true,ipass);
  953. }
  954. }
  955. REF_PTR_RELEASE(matinfo);
  956. /*
  957. ** Add the polygons and vertices to the mesh, transform the vertices
  958. ** back into the original mesh's coordinate system as we do this
  959. */
  960. for (ipoly=0; ipoly<ClipPools[ipool].Count(); ipoly++) {
  961. PolygonClass & poly = ClipPools[ipool][ipoly];
  962. new_mesh->Begin_Tri_Fan();
  963. SHATTER_DEBUG_SAY(("Begin Tri Fan\n"));
  964. for(ivert=0; ivert<poly.Get_Vertex_Count(); ivert++) {
  965. Vector3 pos,norm;
  966. VertexClass & vert = poly[ivert];
  967. Matrix3D::Transform_Vector(Mshatter_to_mesh,vert.Position,&pos);
  968. norm = vert.Normal;
  969. SHATTER_DEBUG_SAY(("Begin Vertex:\n"));
  970. new_mesh->Begin_Vertex();
  971. SHATTER_DEBUG_SAY(("postion: %f %f %f\n",pos.X,pos.Y,pos.Z));
  972. new_mesh->Location_Inline(pos);
  973. new_mesh->Normal(norm);
  974. for (ipass=0; ipass<mtl_params.PassCount; ipass++) {
  975. unsigned mycolor=0;
  976. /*
  977. ** If there were vertex colors for this pass in the original mesh, then
  978. ** copy the color out of the vertex into the new mesh.
  979. */
  980. if (mtl_params.DCG[ipass] != NULL) {
  981. SHATTER_DEBUG_SAY(("DCG: pass:%d: %f %f %f\n",ipass,vert.DCG[ipass].X,vert.DCG[ipass].Y,vert.DCG[ipass].Z));
  982. /* OLD CODE
  983. new_mesh->DCG(Vector3(vert.DCG[ipass].X,vert.DCG[ipass].Y,vert.DCG[ipass].Z),ipass);
  984. new_mesh->Alpha(vert.DCG[ipass].W,ipass);
  985. */
  986. mycolor=vert.DCG[ipass];
  987. }
  988. if (mtl_params.DIG[ipass] != NULL) {
  989. SHATTER_DEBUG_SAY(("DIG: pass:%d: %f %f %f\n",ipass,vert.DIG[ipass].X,vert.DIG[ipass].Y,vert.DIG[ipass].Z));
  990. Vector4 mc=DX8Wrapper::Convert_Color(mycolor);
  991. Vector4 dc=DX8Wrapper::Convert_Color(vert.DIG[ipass]);
  992. mc=Vector4(mc.X*dc.X,mc.Y*dc.Y,mc.Z*dc.Z,mc.W);
  993. mycolor=DX8Wrapper::Convert_Color(mc);
  994. }
  995. new_mesh->Color(mycolor);
  996. /*
  997. ** If there were UV coordinates in the original mesh for either stage,
  998. ** then copy the vertex's uv's into into the new mesh.
  999. */
  1000. for (istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  1001. if (mtl_params.UV[ipass][istage] != NULL) {
  1002. SHATTER_DEBUG_SAY(("UV: pass:%d stage: %d: %f %f\n",ipass,istage,vert.TexCoord[ipass][istage].X,vert.TexCoord[ipass][istage].Y));
  1003. new_mesh->UV(vert.TexCoord[ipass][istage]);
  1004. }
  1005. }
  1006. }
  1007. new_mesh->End_Vertex();
  1008. /*
  1009. ** Set the texture for each pass and stage.
  1010. ** TODO: support texture arrays?
  1011. ** TODO: support stage 1 textures
  1012. */
  1013. for (ipass=0; ipass<mtl_params.PassCount; ipass++) {
  1014. TextureClass * tex = model->Peek_Single_Texture(ipass,0);
  1015. if (tex != NULL) {
  1016. new_mesh->Set_Texture(tex,true,ipass);
  1017. }
  1018. }
  1019. }
  1020. new_mesh->End_Tri_Fan();
  1021. }
  1022. /*
  1023. ** Offset all vertices so that the bounding box center is 0,0,0 and
  1024. ** record that offset into the transform of the mesh
  1025. */
  1026. new_mesh->Set_Dirty_Bounds();
  1027. Vector3 center = new_mesh->Get_Bounding_Box().Center;
  1028. new_mesh->Translate_Vertices(-center);
  1029. Matrix3D tm(1);// = mesh->Get_Transform();
  1030. tm.Translate(center);
  1031. Matrix3D::Multiply(tm,mesh->Get_Transform(),&tm);
  1032. new_mesh->Set_Transform(tm);
  1033. /*
  1034. ** We gave it good vertex normals so clear their dirty flag
  1035. */
  1036. new_mesh->Set_Dirty_Bounds();
  1037. new_mesh->Set_Dirty_Planes();
  1038. new_mesh->Clear_Dirty_Vertex_Normals();
  1039. /*
  1040. ** Install it in the mesh fragment pool, transferring our reference
  1041. ** to the fragment array...
  1042. */
  1043. MeshFragments.Add(new_mesh);
  1044. }
  1045. }
  1046. REF_PTR_RELEASE(model);
  1047. }