shattersystem.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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 : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/shattersystem.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 12/03/01 4:57p $*
  31. * *
  32. * $Revision:: 11 $*
  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. if (ShatterPatterns.Count() == 0) {
  704. return ;
  705. }
  706. int ivert,ipoly;
  707. int ipass,istage;
  708. /*
  709. ** Reset the temporary clip arrays
  710. ** Release any old mesh fragment render objects
  711. */
  712. Reset_Clip_Pools();
  713. Release_Fragments();
  714. /*
  715. ** Verify that this mesh meets the criteria for being shattered
  716. ** - it has a maximum of two passes
  717. ** - it uses only one vertex material per pass
  718. ** - it uses only one shader per pass
  719. ** - it uses only one texture per pass
  720. */
  721. MeshModelClass * model = mesh->Get_Model();
  722. if (model->Get_Pass_Count() > MeshMatDescClass::MAX_PASSES) {
  723. WWDEBUG_SAY(("Failed to shatter model: %s. Too many passes (%d)\n",model->Get_Name(),model->Get_Pass_Count()));
  724. REF_PTR_RELEASE(model);
  725. return;
  726. }
  727. for (ipass=0; ipass<model->Get_Pass_Count(); ipass++) {
  728. if (model->Has_Material_Array(ipass) || model->Has_Shader_Array(ipass)) {
  729. WWDEBUG_SAY(("Failed to shatter model: %s. It has shader or material arrays\n",model->Get_Name()));
  730. REF_PTR_RELEASE(model);
  731. return;
  732. }
  733. for (istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  734. if (model->Has_Texture_Array(ipass,istage)) {
  735. WWDEBUG_SAY(("Failed to shatter model: %s. Texture array in pass: %d stage: %d\n",model->Get_Name(),ipass,istage));
  736. REF_PTR_RELEASE(model);
  737. return;
  738. }
  739. }
  740. }
  741. /*
  742. ** Grab a random shatter pattern
  743. */
  744. BSPClass * clipper = ShatterPatterns[rand() % ShatterPatterns.Count()];
  745. /*
  746. ** Compute transforms which take vertices from mesh-space to shatter-space
  747. ** and back again. Transform polygons into "shatter-space", clip, then
  748. ** transform the results back out
  749. **
  750. ** Take vertices from obj-space to shatter space:
  751. ** Vshatter = Mscale-to-unit * Mworld-shatterview * Mobj-world * Vobj
  752. **
  753. ** Clip the polygons to the BSP
  754. ** Vclipped = BSP_CLIP(Vshatter)
  755. **
  756. ** Next, take the verts back to object space:
  757. ** Vobjclip = Inverse(Mscale-to-unit * Mworld-shatter * Mobj-world) * Vclipped
  758. ** = Inv(Mobj-world)*Inv(Mworld-shatter)*Inv(Mscl-to-unit) * Vclipped
  759. **
  760. ** Next, create separate, re-centered meshes
  761. ** Vnewobj = Mold-obj-to-new-obj * Vobjclip
  762. */
  763. /*
  764. ** Object-space to world-space transform
  765. */
  766. Matrix3D Mobj_to_world = mesh->Get_Transform();
  767. /*
  768. ** World-space to shatter-space transform
  769. */
  770. Matrix3D Mshatter_to_world;
  771. Matrix3D Mworld_to_shatter;
  772. Mshatter_to_world.Look_At(point,point+direction,0.0f); // TODO: could put random roll
  773. Mshatter_to_world.Get_Orthogonal_Inverse(Mworld_to_shatter);
  774. /*
  775. ** Final pair of matrices
  776. */
  777. Matrix3D Mobj_to_shatter;
  778. Matrix3D Mshatter_to_obj;
  779. Matrix3D::Multiply(Mworld_to_shatter,Mobj_to_world,&Mobj_to_shatter);
  780. Mobj_to_shatter.Get_Orthogonal_Inverse(Mshatter_to_obj);
  781. /*
  782. ** Scaling matrices. This could be simpler if Matrix3D had a full inverse function.
  783. */
  784. SphereClass sphere;
  785. model->Get_Bounding_Sphere(&sphere);
  786. float scale_factor = 5.0f / sphere.Radius; // mesh scales to 5x shatter pattern.
  787. Matrix3D Mscale_to_shatter(1);
  788. Matrix3D Mscale_from_shatter(1);
  789. Mscale_to_shatter.Scale(scale_factor);
  790. Mscale_from_shatter.Scale(1.0f / scale_factor);
  791. Matrix3D::Multiply(Mscale_to_shatter,Mobj_to_shatter,&Mobj_to_shatter);
  792. Matrix3D::Multiply(Mshatter_to_obj,Mscale_from_shatter,&Mshatter_to_obj);
  793. /*
  794. ** Grab the arrays out of the mesh and transform verts and vnorms
  795. ** into "shatter-space"
  796. */
  797. const TriIndex * polys = model->Get_Polygon_Array();
  798. const Vector3 * src_verts = model->Get_Vertex_Array();
  799. const Vector3 * src_vnorms = model->Get_Vertex_Normal_Array();
  800. Vector3 * verts = _get_temp_vertex_position_array(model->Get_Vertex_Count());
  801. VectorProcessorClass::Transform(verts,src_verts,Mobj_to_shatter,model->Get_Vertex_Count());
  802. /*
  803. ** Build a description of the material parameters for the mesh
  804. */
  805. MeshMtlParamsClass mtl_params(model);
  806. SHATTER_DEBUG_SAY(("****************************************************\n"));
  807. SHATTER_DEBUG_SAY((" Clipping model: %s\n",model->Get_Name()));
  808. SHATTER_DEBUG_SAY(("****************************************************\n"));
  809. /*
  810. ** Pass each polygon of the source model through the BSP clipper
  811. */
  812. for (ipoly=0; ipoly<model->Get_Polygon_Count(); ipoly++) {
  813. /*
  814. ** Set up a PolygonClass for polygon 'i' in the mesh
  815. */
  816. SHATTER_DEBUG_SAY(("Passing polygon %d to clipper.\n",ipoly));
  817. PolygonClass polygon;
  818. for (ivert=0; ivert<3; ivert++) {
  819. int vert_index = polys[ipoly][ivert];
  820. polygon.Verts[ivert].PassCount = mtl_params.PassCount;
  821. polygon.Verts[ivert].Position = verts[vert_index];
  822. polygon.Verts[ivert].Normal = src_vnorms[vert_index];
  823. SHATTER_DEBUG_SAY(("position: %f %f %f\n",verts[vert_index].X,verts[vert_index].Y,verts[vert_index].Z));
  824. SHATTER_DEBUG_SAY(("normal: %f %f %f\n",src_vnorms[vert_index].X,src_vnorms[vert_index].Y,src_vnorms[vert_index].Z));
  825. for (ipass=0; ipass<MeshMatDescClass::MAX_PASSES; ipass++) {
  826. if (mtl_params.DCG[ipass] != NULL) {
  827. polygon.Verts[ivert].DCG[ipass] = mtl_params.DCG[ipass][vert_index];
  828. 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));
  829. }
  830. if (mtl_params.DIG[ipass] != NULL) {
  831. polygon.Verts[ivert].DIG[ipass] = mtl_params.DIG[ipass][vert_index];
  832. 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));
  833. }
  834. for (istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  835. if (mtl_params.UV[ipass][istage] != NULL) {
  836. polygon.Verts[ivert].TexCoord[ipass][istage] = mtl_params.UV[ipass][istage][vert_index];
  837. 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));
  838. }
  839. }
  840. /* if (mtl_params.UVIndexArray[ipass] != NULL) {
  841. int uv_index = mtl_params.UVIndexArray[ipass][ipoly][ivert];
  842. polygon.Verts[ivert].TexCoord[ipass][0] = mtl_params.UV[ipass][0][uv_index];
  843. 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));
  844. }
  845. */ }
  846. }
  847. polygon.Set_Vertex_Count(3);
  848. polygon.Compute_Plane();
  849. /*
  850. ** Pass it through the BSP
  851. */
  852. clipper->Clip_Polygon(polygon);
  853. }
  854. /*
  855. ** convert the clipped polygons into meshes
  856. */
  857. Process_Clip_Pools(Mshatter_to_obj,mesh,mtl_params);
  858. /*
  859. ** release resources
  860. */
  861. REF_PTR_RELEASE(model);
  862. }
  863. int ShatterSystem::Get_Fragment_Count(void)
  864. {
  865. return MeshFragments.Count();
  866. }
  867. RenderObjClass * ShatterSystem::Get_Fragment(int fragment_index)
  868. {
  869. if (MeshFragments[fragment_index] != NULL) {
  870. MeshFragments[fragment_index]->Add_Ref();
  871. }
  872. return MeshFragments[fragment_index];
  873. }
  874. RenderObjClass * ShatterSystem::Peek_Fragment(int fragment_index)
  875. {
  876. return MeshFragments[fragment_index];
  877. }
  878. void ShatterSystem::Release_Fragments(void)
  879. {
  880. // release any ref's to render objects
  881. for (int i=0; i<MeshFragments.Count(); i++) {
  882. REF_PTR_RELEASE(MeshFragments[i]);
  883. }
  884. // reset array but don't resize
  885. MeshFragments.Delete_All(false);
  886. }
  887. void ShatterSystem::Reset_Clip_Pools(void)
  888. {
  889. for (int i=0; i<MAX_MESH_FRAGMENTS; i++) {
  890. // reset array but don't resize
  891. ClipPools[i].Delete_All(false);
  892. }
  893. }
  894. void ShatterSystem::Process_Clip_Pools
  895. (
  896. const Matrix3D & Mshatter_to_mesh,
  897. MeshClass * mesh,
  898. MeshMtlParamsClass & mtl_params
  899. )
  900. {
  901. /*
  902. ** Release any render objects we currently have and reset the array count
  903. */
  904. Release_Fragments();
  905. /*
  906. ** Grab the model
  907. */
  908. MeshModelClass * model = mesh->Get_Model();
  909. WWASSERT(model != NULL);
  910. /*
  911. ** Loop over all ClipPools and build a mesh for any that contain polygons
  912. */
  913. for (int ipool=0; ipool<MAX_MESH_FRAGMENTS; ipool++) {
  914. if (ClipPools[ipool].Count() > 0) {
  915. int ivert,ipoly,ipass,istage;
  916. /*
  917. ** Count the verts and polys
  918. */
  919. int pcount = 0;
  920. int vcount = 0;
  921. for (ipoly=0;ipoly<ClipPools[ipool].Count();ipoly++) {
  922. int poly_vert_count = ClipPools[ipool][ipoly].Get_Vertex_Count();
  923. vcount += poly_vert_count;
  924. pcount += poly_vert_count-2;
  925. }
  926. SHATTER_DEBUG_SAY(("****************************************************\n"));
  927. SHATTER_DEBUG_SAY((" Reassembling fragment %d of model: %s\n",ipool,model->Get_Name()));
  928. SHATTER_DEBUG_SAY((" polycount = %d vertexcount = %d\n",pcount,vcount));
  929. SHATTER_DEBUG_SAY(("****************************************************\n"));
  930. /*
  931. ** Create the new mesh, install materials
  932. */
  933. DynamicMeshClass * new_mesh = NEW_REF(DynamicMeshClass,(pcount,vcount));
  934. MaterialInfoClass * matinfo = NEW_REF(MaterialInfoClass,());
  935. if (model->Get_Flag(MeshModelClass::SORT)) {
  936. new_mesh->Enable_Sort();
  937. }
  938. new_mesh->Set_Pass_Count(mtl_params.PassCount);
  939. bool has_textures = false;
  940. for (ipass=0; ipass<model->Get_Pass_Count(); ipass++) {
  941. if (model->Peek_Single_Material(ipass) != NULL) {
  942. matinfo->Add_Vertex_Material(model->Peek_Single_Material(ipass));
  943. }
  944. for (int istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  945. if (model->Peek_Single_Texture(ipass,istage) != NULL) {
  946. matinfo->Add_Texture(model->Peek_Single_Texture(ipass,istage));
  947. has_textures = true;
  948. }
  949. }
  950. }
  951. new_mesh->Set_Material_Info(matinfo);
  952. for (ipass=0; ipass<model->Get_Pass_Count(); ipass++) {
  953. new_mesh->Set_Vertex_Material(model->Peek_Single_Material(ipass),false,ipass);
  954. new_mesh->Set_Shader(model->Get_Single_Shader(ipass),ipass);
  955. for (istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  956. TextureClass * tex = model->Peek_Single_Texture(ipass,istage);
  957. if (tex != NULL) {
  958. new_mesh->Peek_Model()->Set_Single_Texture(tex,ipass,istage);
  959. }
  960. }
  961. }
  962. REF_PTR_RELEASE(matinfo);
  963. /*
  964. ** Add the polygons and vertices to the mesh, transform the vertices
  965. ** back into the original mesh's coordinate system as we do this
  966. */
  967. for (ipoly=0; ipoly<ClipPools[ipool].Count(); ipoly++) {
  968. PolygonClass & poly = ClipPools[ipool][ipoly];
  969. new_mesh->Begin_Tri_Fan();
  970. SHATTER_DEBUG_SAY(("Begin Tri Fan\n"));
  971. for(ivert=0; ivert<poly.Get_Vertex_Count(); ivert++) {
  972. Vector3 pos,norm;
  973. VertexClass & vert = poly[ivert];
  974. Matrix3D::Transform_Vector(Mshatter_to_mesh,vert.Position,&pos);
  975. norm = vert.Normal;
  976. SHATTER_DEBUG_SAY(("Begin Vertex:\n"));
  977. new_mesh->Begin_Vertex();
  978. SHATTER_DEBUG_SAY(("postion: %f %f %f\n",pos.X,pos.Y,pos.Z));
  979. new_mesh->Location_Inline(pos);
  980. new_mesh->Normal(norm);
  981. for (ipass=0; ipass<mtl_params.PassCount; ipass++) {
  982. unsigned mycolor=0;
  983. /*
  984. ** If there were vertex colors for this pass in the original mesh, then
  985. ** copy the color out of the vertex into the new mesh.
  986. */
  987. if (mtl_params.DCG[ipass] != NULL) {
  988. SHATTER_DEBUG_SAY(("DCG: pass:%d: %f %f %f\n",ipass,vert.DCG[ipass].X,vert.DCG[ipass].Y,vert.DCG[ipass].Z));
  989. /* OLD CODE
  990. new_mesh->DCG(Vector3(vert.DCG[ipass].X,vert.DCG[ipass].Y,vert.DCG[ipass].Z),ipass);
  991. new_mesh->Alpha(vert.DCG[ipass].W,ipass);
  992. */
  993. mycolor=vert.DCG[ipass];
  994. }
  995. // HY- Multiplying DIG with DCG as in meshmdlio
  996. if (mtl_params.DIG[ipass] != NULL) {
  997. SHATTER_DEBUG_SAY(("DIG: pass:%d: %f %f %f\n",ipass,vert.DIG[ipass].X,vert.DIG[ipass].Y,vert.DIG[ipass].Z));
  998. Vector4 mc=DX8Wrapper::Convert_Color(mycolor);
  999. Vector4 dc=DX8Wrapper::Convert_Color(vert.DIG[ipass]);
  1000. mc=Vector4(mc.X*dc.X,mc.Y*dc.Y,mc.Z*dc.Z,mc.W);
  1001. mycolor=DX8Wrapper::Convert_Color(mc);
  1002. }
  1003. new_mesh->Color(mycolor);
  1004. /*
  1005. ** If there were UV coordinates in the original mesh for either stage,
  1006. ** then copy the vertex's uv's into into the new mesh.
  1007. */
  1008. // #pragma MESSAGE("HY- Naty, will dynamesh support multiple stages of UV?")
  1009. for (istage=0; istage<MeshMatDescClass::MAX_TEX_STAGES; istage++) {
  1010. if (mtl_params.UV[ipass][istage] != NULL) {
  1011. SHATTER_DEBUG_SAY(("UV: pass:%d stage: %d: %f %f\n",ipass,istage,vert.TexCoord[ipass][istage].X,vert.TexCoord[ipass][istage].Y));
  1012. new_mesh->UV(vert.TexCoord[ipass][istage],istage);
  1013. }
  1014. }
  1015. }
  1016. new_mesh->End_Vertex();
  1017. }
  1018. new_mesh->End_Tri_Fan();
  1019. }
  1020. /*
  1021. ** Offset all vertices so that the bounding box center is 0,0,0 and
  1022. ** record that offset into the transform of the mesh
  1023. */
  1024. new_mesh->Set_Dirty_Bounds();
  1025. Vector3 center = new_mesh->Get_Bounding_Box().Center;
  1026. new_mesh->Translate_Vertices(-center);
  1027. Matrix3D tm(1);// = mesh->Get_Transform();
  1028. tm.Translate(center);
  1029. Matrix3D::Multiply(tm,mesh->Get_Transform(),&tm);
  1030. new_mesh->Set_Transform(tm);
  1031. /*
  1032. ** We gave it good vertex normals so clear their dirty flag
  1033. */
  1034. new_mesh->Set_Dirty_Bounds();
  1035. new_mesh->Set_Dirty_Planes();
  1036. new_mesh->Clear_Dirty_Vertex_Normals();
  1037. /*
  1038. ** Install it in the mesh fragment pool, transferring our reference
  1039. ** to the fragment array...
  1040. */
  1041. MeshFragments.Add(new_mesh);
  1042. }
  1043. }
  1044. REF_PTR_RELEASE(model);
  1045. }