meshgeometry.cpp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /VSS_Sync/ww3d2/meshgeometry.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Vss_sync $*
  29. * *
  30. * $Modtime:: 8/29/01 7:29p $*
  31. * *
  32. * $Revision:: 13 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * MeshGeometryClass::MeshGeometryClass -- Constructor *
  37. * MeshGeometryClass::MeshGeometryClass -- Copy Constructor *
  38. * -- assignment operator *
  39. * MeshGeometryClass::~MeshGeometryClass -- destructor *
  40. * MeshGeometryClass::Reset_Geometry -- releases current resources and allocates space if ne *
  41. * MeshGeometryClass::Get_Name -- returns the name *
  42. * MeshGeometryClass::Set_Name -- set the name of this model *
  43. * MeshGeometryClass::Get_User_Text -- get the user-text buffer *
  44. * MeshGeometryClass::Set_User_Text -- set the user text buffer *
  45. * MeshGeometryClass::Get_Bounding_Box -- get the bounding box *
  46. * MeshGeometryClass::Get_Bounding_Sphere -- get the bounding sphere *
  47. * MeshGeometryClass::Generate_Rigid_APT -- generate active polygon table *
  48. * MeshGeometryClass::Generate_Skin_APT -- generate an active polygon table *
  49. * MeshGeometryClass::Contains -- test if the mesh contains the given point *
  50. * MeshGeometryClass::Cast_Ray -- compute a ray intersection with this mesh *
  51. * MeshGeometryClass::Cast_AABox -- cast an AABox against this mesh *
  52. * MeshGeometryClass::Cast_OBBox -- Cast an obbox against this mesh *
  53. * MeshGeometryClass::Intersect_OBBox -- test for intersection with the given OBBox *
  54. * MeshGeometryClass::Cast_World_Space_AABox -- test for intersection with a worldspace AABox*
  55. * MeshGeometryClass::cast_semi_infinite_axis_aligned_ray -- casts an axis aligned ray *
  56. * MeshGeometryClass::cast_aabox_identity -- aligned aabox test *
  57. * MeshGeometryClass::cast_aabox_z90 -- aabox test which is rotated about z by 90 *
  58. * MeshGeometryClass::cast_aabox_z180 -- aabox test which is rotated about z by 180 *
  59. * MeshGeometryClass::cast_aabox_z270 -- aabox test which is rotated about z by 270 *
  60. * MeshGeometryClass::cast_ray_brute_force -- brute force ray-cast *
  61. * MeshGeometryClass::cast_aabox_brute_force -- brute force aabox cast *
  62. * MeshGeometryClass::cast_obbox_brute_force -- brute force obbox cast *
  63. * MeshGeometryClass::intersect_obbox_brute_force -- brute force intersection check *
  64. * MeshGeometryClass::Compute_Plane_Equations -- Recalculates the plane equations *
  65. * MeshGeometryClass::Compute_Vertex_Normals -- recompute the vertex normals *
  66. * MeshGeometryClass::Compute_Bounds -- recomputes the bounding volumes *
  67. * MeshGeometryClass::get_vert_normals -- get the vertex normal array *
  68. * MeshGeometryClass::Get_Vertex_Normal_Array -- validates and returns the vertex normal arr *
  69. * MeshGeometryClass::get_planes -- get the plane array memory (internal) *
  70. * MeshGeometryClass::Get_Plane_Array -- validates and returns the array of plane equations *
  71. * MeshGeometryClass::Compute_Plane -- compute the plane equation of a single poly *
  72. * MeshGeometryClass::Generate_Culling_Tree -- Generate an AABTree for this mesh *
  73. * MeshGeometryClass::Load_W3D -- Load a mesh from a w3d *
  74. * MeshGeometryClass::read_chunks -- read w3d chunks *
  75. * MeshGeometryClass::read_vertices -- read the vertex chunk from a W3D file *
  76. * MeshGeometryClass::read_vertex_normals -- read the vertex normals chunk from a w3d file *
  77. * MeshGeometryClass::read_triangles -- read the triangles chunk from a w3d file *
  78. * MeshGeometryClass::read_user_text -- read the user text chunk from a w3d file *
  79. * MeshGeometryClass::read_vertex_influences -- read the vertex influences chunk from a w3d *
  80. * MeshGeometryClass::read_vertex_shade_indices -- read the vertex shade indices chunk *
  81. * MeshGeometryClass::read_aabtree -- read the AABTree chunk from a w3d file *
  82. * MeshGeometryClass::Generate_APT -- generate an apt for a box and view direction *
  83. * MeshGeometryClass::Generate_Rigid_APT -- generate an apt using backface culling *
  84. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  85. #include "meshgeometry.h"
  86. #include "aabtree.h"
  87. #include "chunkio.h"
  88. #include "aabox.h"
  89. #include "obbox.h"
  90. #include "sphere.h"
  91. #include "plane.h"
  92. #include "wwdebug.h"
  93. #include "wwmemlog.h"
  94. #include "w3d_file.h"
  95. #include "vp.h"
  96. #if (OPTIMIZE_PLANEEQ_RAM)
  97. static SimpleVecClass<Vector4> _PlaneEQArray(1024);
  98. #endif
  99. #if (OPTIMIZE_VNORM_RAM)
  100. static SimpleVecClass<Vector3> _VNormArray(1024);
  101. #endif
  102. /***********************************************************************************************
  103. * MeshGeometryClass::MeshGeometryClass -- Constructor *
  104. * *
  105. * INPUT: *
  106. * *
  107. * OUTPUT: *
  108. * *
  109. * WARNINGS: *
  110. * *
  111. * HISTORY: *
  112. * 11/9/2000 gth : Created. *
  113. *=============================================================================================*/
  114. MeshGeometryClass::MeshGeometryClass(void) :
  115. MeshName(NULL),
  116. UserText(NULL),
  117. Flags(0),
  118. SortLevel(SORT_LEVEL_NONE),
  119. W3dAttributes(0),
  120. PolyCount(0),
  121. VertexCount(0),
  122. Poly(NULL),
  123. PolySurfaceType(NULL),
  124. Vertex(NULL),
  125. VertexNorm(NULL),
  126. PlaneEq(NULL),
  127. VertexShadeIdx(NULL),
  128. VertexBoneLink(NULL),
  129. BoundBoxMin(0,0,0),
  130. BoundBoxMax(1,1,1),
  131. BoundSphereCenter(0,0,0),
  132. BoundSphereRadius(1),
  133. CullTree(NULL)
  134. {
  135. }
  136. /***********************************************************************************************
  137. * MeshGeometryClass::MeshGeometryClass -- Copy Constructor *
  138. * *
  139. * INPUT: *
  140. * *
  141. * OUTPUT: *
  142. * *
  143. * WARNINGS: *
  144. * *
  145. * HISTORY: *
  146. * 11/9/2000 gth : Created. *
  147. *=============================================================================================*/
  148. MeshGeometryClass::MeshGeometryClass(const MeshGeometryClass & that) :
  149. MeshName(NULL),
  150. UserText(NULL),
  151. Flags(0),
  152. SortLevel(SORT_LEVEL_NONE),
  153. W3dAttributes(0),
  154. PolyCount(0),
  155. VertexCount(0),
  156. Poly(NULL),
  157. PolySurfaceType(NULL),
  158. Vertex(NULL),
  159. VertexNorm(NULL),
  160. PlaneEq(NULL),
  161. VertexShadeIdx(NULL),
  162. VertexBoneLink(NULL),
  163. BoundBoxMin(0,0,0),
  164. BoundBoxMax(1,1,1),
  165. BoundSphereCenter(0,0,0),
  166. BoundSphereRadius(1),
  167. CullTree(NULL)
  168. {
  169. *this = that;
  170. }
  171. /***********************************************************************************************
  172. * -- assignment operator *
  173. * *
  174. * INPUT: *
  175. * *
  176. * OUTPUT: *
  177. * *
  178. * WARNINGS: *
  179. * *
  180. * HISTORY: *
  181. * 11/9/2000 gth : Created. *
  182. *=============================================================================================*/
  183. MeshGeometryClass & MeshGeometryClass::operator = (const MeshGeometryClass & that)
  184. {
  185. if (this != &that) {
  186. Flags = that.Flags;
  187. SortLevel = that.SortLevel;
  188. W3dAttributes = that.W3dAttributes;
  189. PolyCount = that.PolyCount;
  190. VertexCount = that.VertexCount;
  191. BoundBoxMin = that.BoundBoxMin;
  192. BoundBoxMax = that.BoundBoxMax;
  193. BoundSphereCenter = that.BoundSphereCenter;
  194. BoundSphereRadius = that.BoundSphereRadius;
  195. REF_PTR_SET(MeshName,that.MeshName);
  196. REF_PTR_SET(UserText,that.UserText);
  197. REF_PTR_SET(Poly,that.Poly);
  198. REF_PTR_SET(PolySurfaceType,that.PolySurfaceType);
  199. REF_PTR_SET(Vertex,that.Vertex);
  200. REF_PTR_SET(VertexNorm,that.VertexNorm);
  201. REF_PTR_SET(PlaneEq,that.PlaneEq);
  202. REF_PTR_SET(VertexShadeIdx,that.VertexShadeIdx);
  203. REF_PTR_SET(VertexBoneLink,that.VertexBoneLink);
  204. REF_PTR_SET(CullTree,that.CullTree);
  205. }
  206. return * this;
  207. }
  208. /***********************************************************************************************
  209. * MeshGeometryClass::~MeshGeometryClass -- destructor *
  210. * *
  211. * INPUT: *
  212. * *
  213. * OUTPUT: *
  214. * *
  215. * WARNINGS: *
  216. * *
  217. * HISTORY: *
  218. * 11/9/2000 gth : Created. *
  219. *=============================================================================================*/
  220. MeshGeometryClass::~MeshGeometryClass(void)
  221. {
  222. Reset_Geometry(0,0);
  223. }
  224. /***********************************************************************************************
  225. * MeshGeometryClass::Reset_Geometry -- releases current resources and allocates space if need *
  226. * *
  227. * INPUT: *
  228. * *
  229. * OUTPUT: *
  230. * *
  231. * WARNINGS: *
  232. * *
  233. * HISTORY: *
  234. * 11/9/2000 gth : Created. *
  235. *=============================================================================================*/
  236. void MeshGeometryClass::Reset_Geometry(int polycount,int vertcount)
  237. {
  238. // Release everything we have and reset to initial state
  239. Flags = 0;
  240. PolyCount = 0;
  241. VertexCount = 0;
  242. SortLevel = SORT_LEVEL_NONE;
  243. REF_PTR_RELEASE(MeshName);
  244. REF_PTR_RELEASE(UserText);
  245. REF_PTR_RELEASE(Poly);
  246. REF_PTR_RELEASE(PolySurfaceType);
  247. REF_PTR_RELEASE(Vertex);
  248. REF_PTR_RELEASE(VertexNorm);
  249. REF_PTR_RELEASE(PlaneEq);
  250. REF_PTR_RELEASE(VertexShadeIdx);
  251. REF_PTR_RELEASE(VertexBoneLink);
  252. REF_PTR_RELEASE(CullTree);
  253. PolyCount = polycount;
  254. VertexCount = vertcount;
  255. // allocate new geometry arrays
  256. if ((polycount != 0) && (vertcount != 0)) {
  257. Poly = NEW_REF(ShareBufferClass<Vector3i>,(PolyCount, "MeshGeometryClass::Poly"));
  258. PolySurfaceType = NEW_REF(ShareBufferClass<uint8>,(PolyCount, "MeshGeometryClass::PolySurfaceType"));
  259. Vertex = NEW_REF(ShareBufferClass<Vector3>,(VertexCount, "MeshGeometryClass::Vertex"));
  260. Poly->Clear();
  261. PolySurfaceType->Clear();
  262. Vertex->Clear();
  263. #if (!OPTIMIZE_VNORM_RAM)
  264. VertexNorm = NEW_REF(ShareBufferClass<Vector3>,(VertexCount, "MeshGeometryClass::VertexNorm"));
  265. VertexNorm->Clear();
  266. #endif
  267. }
  268. return ;
  269. }
  270. /***********************************************************************************************
  271. * MeshGeometryClass::Get_Name -- returns the name *
  272. * *
  273. * INPUT: *
  274. * *
  275. * OUTPUT: *
  276. * *
  277. * WARNINGS: *
  278. * *
  279. * HISTORY: *
  280. * 11/9/2000 gth : Created. *
  281. *=============================================================================================*/
  282. const char * MeshGeometryClass::Get_Name(void) const
  283. {
  284. if (MeshName) {
  285. return MeshName->Get_Array();
  286. }
  287. return NULL;
  288. }
  289. /***********************************************************************************************
  290. * MeshGeometryClass::Set_Name -- set the name of this model *
  291. * *
  292. * INPUT: *
  293. * *
  294. * OUTPUT: *
  295. * *
  296. * WARNINGS: *
  297. * *
  298. * HISTORY: *
  299. * 11/9/2000 gth : Created. *
  300. *=============================================================================================*/
  301. void MeshGeometryClass::Set_Name(const char * newname)
  302. {
  303. if (MeshName) {
  304. MeshName->Release_Ref();
  305. }
  306. if (newname) {
  307. MeshName = NEW_REF(ShareBufferClass<char>,(strlen(newname)+1, "MeshGeometryClass::MeshName"));
  308. strcpy(MeshName->Get_Array(),newname);
  309. }
  310. }
  311. /***********************************************************************************************
  312. * MeshGeometryClass::Get_User_Text -- get the user-text buffer *
  313. * *
  314. * INPUT: *
  315. * *
  316. * OUTPUT: *
  317. * *
  318. * WARNINGS: *
  319. * *
  320. * HISTORY: *
  321. * 11/9/2000 gth : Created. *
  322. *=============================================================================================*/
  323. const char * MeshGeometryClass::Get_User_Text(void)
  324. {
  325. if (UserText) {
  326. return UserText->Get_Array();
  327. }
  328. return NULL;
  329. }
  330. /***********************************************************************************************
  331. * MeshGeometryClass::Set_User_Text -- set the user text buffer *
  332. * *
  333. * INPUT: *
  334. * *
  335. * OUTPUT: *
  336. * *
  337. * WARNINGS: *
  338. * *
  339. * HISTORY: *
  340. * 11/9/2000 gth : Created. *
  341. *=============================================================================================*/
  342. void MeshGeometryClass::Set_User_Text(char * usertext)
  343. {
  344. if (UserText) {
  345. UserText->Release_Ref();
  346. }
  347. if (usertext) {
  348. UserText = NEW_REF(ShareBufferClass<char>,(strlen(usertext)+1, "MeshGeometryClass::UserText"));
  349. strcpy(UserText->Get_Array(),usertext);
  350. }
  351. }
  352. /***********************************************************************************************
  353. * MeshGeometryClass::Get_Bounding_Box -- get the bounding box *
  354. * *
  355. * INPUT: *
  356. * *
  357. * OUTPUT: *
  358. * *
  359. * WARNINGS: *
  360. * *
  361. * HISTORY: *
  362. * 11/9/2000 gth : Created. *
  363. *=============================================================================================*/
  364. void MeshGeometryClass::Get_Bounding_Box(AABoxClass * set_box)
  365. {
  366. WWASSERT(set_box != NULL);
  367. set_box->Center = (BoundBoxMax + BoundBoxMin) * 0.5f;
  368. set_box->Extent = (BoundBoxMax - BoundBoxMin) * 0.5f;
  369. }
  370. /***********************************************************************************************
  371. * MeshGeometryClass::Get_Bounding_Sphere -- get the bounding sphere *
  372. * *
  373. * INPUT: *
  374. * *
  375. * OUTPUT: *
  376. * *
  377. * WARNINGS: *
  378. * *
  379. * HISTORY: *
  380. * 11/9/2000 gth : Created. *
  381. *=============================================================================================*/
  382. void MeshGeometryClass::Get_Bounding_Sphere(SphereClass * set_sphere)
  383. {
  384. WWASSERT(set_sphere != NULL);
  385. set_sphere->Center = BoundSphereCenter;
  386. set_sphere->Radius = BoundSphereRadius;
  387. }
  388. /***********************************************************************************************
  389. * MeshGeometryClass::Generate_Rigid_APT -- generate an apt using backface culling *
  390. * *
  391. * INPUT: *
  392. * *
  393. * OUTPUT: *
  394. * *
  395. * WARNINGS: *
  396. * *
  397. * HISTORY: *
  398. * 5/10/2001 gth : Created. *
  399. *=============================================================================================*/
  400. void MeshGeometryClass::Generate_Rigid_APT(const Vector3 & view_dir, SimpleDynVecClass<uint32> & apt)
  401. {
  402. const Vector3 * loc = Get_Vertex_Array();
  403. const Vector4 * norms = Get_Plane_Array();
  404. const Vector3i * polys = Get_Polygon_Array();
  405. TriClass tri;
  406. for (int poly_counter = 0; poly_counter < PolyCount; poly_counter++) {
  407. tri.V[0] = &(loc[ polys[poly_counter][0] ]);
  408. tri.V[1] = &(loc[ polys[poly_counter][1] ]);
  409. tri.V[2] = &(loc[ polys[poly_counter][2] ]);
  410. tri.N = (Vector3*)&(norms[poly_counter]);
  411. if (Vector3::Dot_Product(*tri.N,view_dir) < 0.0f) {
  412. apt.Add(poly_counter);
  413. }
  414. }
  415. }
  416. /***********************************************************************************************
  417. * MeshGeometryClass::Generate_Rigid_APT -- generate active polygon table *
  418. * *
  419. * This function will cull the mesh against the specified volume *
  420. * *
  421. * INPUT: *
  422. * *
  423. * OUTPUT: *
  424. * *
  425. * WARNINGS: *
  426. * *
  427. * HISTORY: *
  428. * 11/9/2000 gth : Created. *
  429. *=============================================================================================*/
  430. void MeshGeometryClass::Generate_Rigid_APT(const OBBoxClass & local_box, SimpleDynVecClass<uint32> & apt)
  431. {
  432. if (CullTree != NULL) {
  433. CullTree->Generate_APT(local_box, apt);
  434. } else {
  435. // Beware, this is gonna be expensive!
  436. const Vector3 * loc = Get_Vertex_Array();
  437. const Vector4 * norms = Get_Plane_Array();
  438. const Vector3i * polys = Get_Polygon_Array();
  439. TriClass tri;
  440. for (int poly_counter = 0; poly_counter < PolyCount; poly_counter++) {
  441. tri.V[0] = &(loc[ polys[poly_counter][0] ]);
  442. tri.V[1] = &(loc[ polys[poly_counter][1] ]);
  443. tri.V[2] = &(loc[ polys[poly_counter][2] ]);
  444. tri.N = (Vector3*)&(norms[poly_counter]);
  445. if (CollisionMath::Intersection_Test(local_box, tri)) {;
  446. apt.Add(poly_counter);
  447. }
  448. }
  449. }
  450. }
  451. /***********************************************************************************************
  452. * MeshGeometryClass::Generate_APT -- generate an apt for a box and view direction *
  453. * *
  454. * INPUT: *
  455. * *
  456. * OUTPUT: *
  457. * *
  458. * WARNINGS: *
  459. * *
  460. * HISTORY: *
  461. * 5/10/2001 gth : Created. *
  462. *=============================================================================================*/
  463. void MeshGeometryClass::Generate_Rigid_APT(const OBBoxClass & local_box,const Vector3 & viewdir,SimpleDynVecClass<uint32> & apt)
  464. {
  465. if (CullTree != NULL) {
  466. CullTree->Generate_APT(local_box, viewdir,apt);
  467. } else {
  468. // Beware, this is gonna be expensive!
  469. const Vector3 * loc = Get_Vertex_Array();
  470. const Vector4 * norms = Get_Plane_Array();
  471. const Vector3i * polys = Get_Polygon_Array();
  472. TriClass tri;
  473. for (int poly_counter = 0; poly_counter < PolyCount; poly_counter++) {
  474. tri.V[0] = &(loc[ polys[poly_counter][0] ]);
  475. tri.V[1] = &(loc[ polys[poly_counter][1] ]);
  476. tri.V[2] = &(loc[ polys[poly_counter][2] ]);
  477. tri.N = (Vector3*)&(norms[poly_counter]);
  478. if (Vector3::Dot_Product(*tri.N,viewdir) < 0.0f) {
  479. if (CollisionMath::Intersection_Test(local_box,tri)) {
  480. apt.Add(poly_counter);
  481. }
  482. }
  483. }
  484. }
  485. }
  486. /***********************************************************************************************
  487. * MeshGeometryClass::Generate_Skin_APT -- generate an active polygon table *
  488. * *
  489. * This function culls the mesh against the specified volume *
  490. * *
  491. * INPUT: *
  492. * *
  493. * OUTPUT: *
  494. * *
  495. * WARNINGS: *
  496. * *
  497. * HISTORY: *
  498. * 11/9/2000 gth : Created. *
  499. *=============================================================================================*/
  500. void MeshGeometryClass::Generate_Skin_APT(const OBBoxClass & world_box, SimpleDynVecClass<uint32> & apt, const Vector3 *world_vertex_locs)
  501. {
  502. WWASSERT(world_vertex_locs);
  503. // Beware, this is gonna be expensive!
  504. const Vector3i * polys = Get_Polygon_Array();
  505. TriClass tri;
  506. for (int poly_counter=0; poly_counter < PolyCount; poly_counter++) {
  507. tri.V[0] = &(world_vertex_locs[ polys[poly_counter][0] ]);
  508. tri.V[1] = &(world_vertex_locs[ polys[poly_counter][1] ]);
  509. tri.V[2] = &(world_vertex_locs[ polys[poly_counter][2] ]);
  510. // We would have to do a non-trivial amount of computation to get the triangle normal
  511. // (since this is a skin we have no valid plane equations) and we know that N is not used
  512. // in the Intersection_Test, so we set it to a dummy value.
  513. static const Vector3 dummy_vec(0.0f, 0.0f, 1.0f);
  514. tri.N = &dummy_vec;
  515. if (CollisionMath::Intersection_Test(world_box,tri)) {;
  516. apt.Add(poly_counter);
  517. }
  518. }
  519. }
  520. /***********************************************************************************************
  521. * MeshGeometryClass::Contains -- test if the mesh contains the given point *
  522. * *
  523. * INPUT: *
  524. * *
  525. * OUTPUT: *
  526. * *
  527. * WARNINGS: *
  528. * *
  529. * HISTORY: *
  530. * 11/9/2000 gth : Created. *
  531. *=============================================================================================*/
  532. bool MeshGeometryClass::Contains(const Vector3 &point)
  533. {
  534. // To determine whether the mesh contains a point, we will use the parity method (cast a
  535. // semi-infinite ray from the point and check the number of intersections with the mesh.
  536. // An even number of intersections means the point is outside the mesh, and an odd number
  537. // means the point is inside the mesh. Since we can cast the ray in any direction we will
  538. // use axis-aligned rays for speed.
  539. // The method fails if the ray goes through any triangle edge or corner. For robustness
  540. // we will cast rays in all six axis-aligned directions, and take a majority vote. We will
  541. // significantly reduce the weighting of rays which go through corners/edges.
  542. // Also, if any raycast reports that the point is embedded in a triangle, we will stop and
  543. // report that the point is contained in the mesh (using the normal algorithm in this case
  544. // could lead to errors).
  545. float yes = 0.0f; // weighted sum of rays indicating the point is contained in the mesh
  546. float no = 0.0f; // weighted sum of rays indicating the point is not contained in the mesh
  547. for (int axis_dir = 0; axis_dir < 6; axis_dir++) {
  548. unsigned char flags = TRI_RAYCAST_FLAG_NONE;
  549. int intersections = cast_semi_infinite_axis_aligned_ray(point, axis_dir, flags);
  550. if (flags & TRI_RAYCAST_FLAG_START_IN_TRI) return true;
  551. float weight = flags & TRI_RAYCAST_FLAG_HIT_EDGE ? 0.1f : 1.0f;
  552. if (intersections & 0x01) {
  553. yes += weight;
  554. } else {
  555. no += weight;
  556. }
  557. }
  558. return yes > no;
  559. }
  560. /***********************************************************************************************
  561. * MeshGeometryClass::Cast_Ray -- compute a ray intersection with this mesh *
  562. * *
  563. * INPUT: *
  564. * *
  565. * OUTPUT: *
  566. * *
  567. * WARNINGS: *
  568. * *
  569. * HISTORY: *
  570. * 3/1/2001 NH : Created. *
  571. *=============================================================================================*/
  572. bool MeshGeometryClass::Cast_Ray(RayCollisionTestClass & raytest)
  573. {
  574. bool hit = false;
  575. if (CullTree) {
  576. hit = CullTree->Cast_Ray(raytest);
  577. } else {
  578. hit = cast_ray_brute_force(raytest);
  579. }
  580. return hit;
  581. }
  582. /***********************************************************************************************
  583. * MeshGeometryClass::Cast_AABox -- cast an AABox against this mesh *
  584. * *
  585. * INPUT: *
  586. * *
  587. * OUTPUT: *
  588. * *
  589. * WARNINGS: *
  590. * *
  591. * HISTORY: *
  592. * 3/1/2001 NH : Created. *
  593. *=============================================================================================*/
  594. bool MeshGeometryClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
  595. {
  596. bool hit = false;
  597. if (CullTree) {
  598. hit = CullTree->Cast_AABox(boxtest);
  599. } else {
  600. hit = cast_aabox_brute_force(boxtest);
  601. }
  602. return hit;
  603. }
  604. /***********************************************************************************************
  605. * MeshGeometryClass::Cast_OBBox -- Cast an obbox against this mesh *
  606. * *
  607. * INPUT: *
  608. * *
  609. * OUTPUT: *
  610. * *
  611. * WARNINGS: *
  612. * *
  613. * HISTORY: *
  614. * 3/1/2001 NH : Created. *
  615. *=============================================================================================*/
  616. bool MeshGeometryClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
  617. {
  618. bool hit = false;
  619. if (CullTree) {
  620. hit = CullTree->Cast_OBBox(boxtest);
  621. } else {
  622. hit = cast_obbox_brute_force(boxtest);
  623. }
  624. return hit;
  625. }
  626. /***********************************************************************************************
  627. * MeshGeometryClass::Intersect_OBBox -- test for intersection with the given OBBox *
  628. * *
  629. * INPUT: *
  630. * *
  631. * OUTPUT: *
  632. * *
  633. * WARNINGS: *
  634. * *
  635. * HISTORY: *
  636. * 3/1/2001 NH : Created. *
  637. *=============================================================================================*/
  638. bool MeshGeometryClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
  639. {
  640. bool hit = false;
  641. if (CullTree) {
  642. hit = CullTree->Intersect_OBBox(boxtest);
  643. } else {
  644. hit = intersect_obbox_brute_force(boxtest);
  645. }
  646. return hit;
  647. }
  648. /***********************************************************************************************
  649. * MeshGeometryClass::Cast_World_Space_AABox -- test for intersection with a worldspace AABox*
  650. * *
  651. * INPUT: *
  652. * *
  653. * OUTPUT: *
  654. * *
  655. * WARNINGS: *
  656. * *
  657. * HISTORY: *
  658. * 3/1/2001 NH : Created. *
  659. *=============================================================================================*/
  660. bool MeshGeometryClass::Cast_World_Space_AABox(AABoxCollisionTestClass & boxtest, const Matrix3D &transform)
  661. {
  662. /*
  663. ** Attempt to classify the transform:
  664. ** NOTE: This code assumes that the matrix is orthogonal! Much code in W3D assumes
  665. ** orthogonal matrices, if that convention is broken this code is also broken.
  666. ** Basically what I'm doing here is doing the minimum number of compares needed
  667. ** to identify a transform which is a 90 degree rotation about the z axis.
  668. ** TODO: cache some transform flags somewhere to reduce the number of times
  669. ** that these compares are done
  670. */
  671. bool hit = false;
  672. if ((transform[0][0] == 1.0f) && (transform[1][1] == 1.0f)) {
  673. hit = cast_aabox_identity(boxtest,-transform.Get_Translation());
  674. } else if ((transform[0][1] == -1.0f) && (transform[1][0] == 1.0f)) {
  675. // this mesh has been rotated 90 degrees about z
  676. hit = cast_aabox_z90(boxtest,-transform.Get_Translation());
  677. } else if ((transform[0][0] == -1.0f) && (transform[1][1] == -1.0f)) {
  678. // this mesh has been rotated 180
  679. hit = cast_aabox_z180(boxtest,-transform.Get_Translation());
  680. } else if ((transform[0][1] == 1.0f) && (transform[1][0] == -1.0f)) {
  681. // this mesh has been rotated 270
  682. hit = cast_aabox_z270(boxtest,-transform.Get_Translation());
  683. } else {
  684. /*
  685. ** Ok, we fell through to here, that means there is a more general
  686. ** transform on this mesh. In this case, I create a new test which
  687. ** is an oriented box test in the coordinate system of the mesh and cast it.
  688. */
  689. Matrix3D world_to_obj;
  690. transform.Get_Orthogonal_Inverse(world_to_obj);
  691. OBBoxCollisionTestClass obbox(boxtest, world_to_obj);
  692. if (CullTree) {
  693. hit = CullTree->Cast_OBBox(obbox);
  694. } else {
  695. hit = cast_obbox_brute_force(obbox);
  696. }
  697. /*
  698. ** now, we must transform the results of the test back to the original
  699. ** coordinate system.
  700. */
  701. if (hit) {
  702. Matrix3D::Rotate_Vector(transform, obbox.Result->Normal, &(obbox.Result->Normal));
  703. if (boxtest.Result->ComputeContactPoint) {
  704. Matrix3D::Transform_Vector(transform, obbox.Result->ContactPoint, &(obbox.Result->ContactPoint));
  705. }
  706. }
  707. }
  708. return hit;
  709. }
  710. /***********************************************************************************************
  711. * MeshGeometryClass::cast_semi_infinite_axis_aligned_ray -- casts an axis aligned ray *
  712. * *
  713. * This function is used by the 'Contains' function *
  714. * *
  715. * INPUT: *
  716. * *
  717. * OUTPUT: *
  718. * *
  719. * WARNINGS: *
  720. * *
  721. * HISTORY: *
  722. * 11/9/2000 gth : Created. *
  723. *=============================================================================================*/
  724. int MeshGeometryClass::cast_semi_infinite_axis_aligned_ray(const Vector3 & start_point, int axis_dir,
  725. unsigned char & flags)
  726. {
  727. int count = 0;
  728. if (CullTree) {
  729. count = CullTree->Cast_Semi_Infinite_Axis_Aligned_Ray(start_point, axis_dir, flags);
  730. } else {
  731. const Vector3 * loc = Get_Vertex_Array();
  732. const Vector4 * plane = Get_Plane_Array();
  733. const Vector3i * polyverts = Get_Polygon_Array();
  734. // These tables translate between the axis_dir representation (which is an integer in which 0
  735. // indicates a ray along the positive x axis, 1 along the negative x axis, 2 the positive y
  736. // axis, 3 negative y axis, 4 positive z axis, 5 negative z axis) and a four-integer
  737. // representation (axis_r is the axis number - 0, 1 or 2 - of the axis along which the ray is
  738. // cast; axis_1 and axis_2 are the axis numbers of the other two axes; direction is 0 for
  739. // negative and 1 for positive direction of the ray).
  740. static const int axis_r[6] = { 0, 0, 1, 1, 2, 2 };
  741. static const int axis_1[6] = { 1, 1, 2, 2, 0, 0 };
  742. static const int axis_2[6] = { 2, 2, 0, 0, 1, 1 };
  743. static const int direction[6] = { 1, 0, 1, 0, 1, 0 };
  744. WWASSERT(axis_dir >= 0);
  745. WWASSERT(axis_dir < 6);
  746. // The functions called after this point will 'or' bits into this variable, so it needs to
  747. // be initialized here to TRI_RAYCAST_FLAG_NONE.
  748. flags = TRI_RAYCAST_FLAG_NONE;
  749. /*
  750. ** Loop over each polygon
  751. */
  752. int poly_count = Get_Polygon_Count();
  753. for (int poly_counter=0; poly_counter < poly_count; poly_counter++) {
  754. const Vector3 &v0 = loc[ polyverts[poly_counter][0] ];
  755. const Vector3 &v1 = loc[ polyverts[poly_counter][1] ];
  756. const Vector3 &v2 = loc[ polyverts[poly_counter][2] ];
  757. const Vector4 &tri_plane = plane[poly_counter];
  758. // Since (int)true is defined as 1, and (int)false as 0:
  759. count += (unsigned int)Cast_Semi_Infinite_Axis_Aligned_Ray_To_Triangle(v0, v1, v2,
  760. tri_plane, start_point, axis_r[axis_dir], axis_1[axis_dir], axis_2[axis_dir],
  761. direction[axis_dir], flags);
  762. }
  763. }
  764. return count;
  765. }
  766. /***********************************************************************************************
  767. * MeshGeometryClass::cast_aabox_identity -- aligned aabox test *
  768. * *
  769. * INPUT: *
  770. * *
  771. * OUTPUT: *
  772. * *
  773. * WARNINGS: *
  774. * *
  775. * HISTORY: *
  776. * 3/1/2001 NH : Created. *
  777. *=============================================================================================*/
  778. bool MeshGeometryClass::cast_aabox_identity(AABoxCollisionTestClass & boxtest, const Vector3 & translation)
  779. {
  780. // transform the test into the mesh's coordinate system
  781. AABoxCollisionTestClass newbox(boxtest);
  782. newbox.Translate(translation);
  783. // cast the box against the mesh
  784. if (CullTree) {
  785. return CullTree->Cast_AABox(newbox);
  786. } else {
  787. return cast_aabox_brute_force(newbox);
  788. }
  789. }
  790. /***********************************************************************************************
  791. * MeshGeometryClass::cast_aabox_z90 -- aabox test which is rotated about z by 90 *
  792. * *
  793. * INPUT: *
  794. * *
  795. * OUTPUT: *
  796. * *
  797. * WARNINGS: *
  798. * *
  799. * HISTORY: *
  800. * 3/1/2001 NH : Created. *
  801. *=============================================================================================*/
  802. bool MeshGeometryClass::cast_aabox_z90(AABoxCollisionTestClass & boxtest, const Vector3 & translation)
  803. {
  804. // transform the test into the mesh's coordinate system
  805. AABoxCollisionTestClass newbox(boxtest);
  806. newbox.Translate(translation);
  807. newbox.Rotate(AABoxCollisionTestClass::ROTATE_Z270);
  808. // cast the box against the mesh, using culling if possible
  809. bool hit;
  810. if (CullTree) {
  811. hit = CullTree->Cast_AABox(newbox);
  812. } else {
  813. hit = cast_aabox_brute_force(newbox);
  814. }
  815. // if we hit something, we need to rotate the normal back out of the mesh coordinate system
  816. if (hit) {
  817. // rotating the normal by 90 degrees about Z
  818. float tmp = boxtest.Result->Normal.X;
  819. boxtest.Result->Normal.X = -boxtest.Result->Normal.Y;
  820. boxtest.Result->Normal.Y = tmp;
  821. }
  822. return hit;
  823. }
  824. /***********************************************************************************************
  825. * MeshGeometryClass::cast_aabox_z180 -- aabox test which is rotated about z by 180 *
  826. * *
  827. * INPUT: *
  828. * *
  829. * OUTPUT: *
  830. * *
  831. * WARNINGS: *
  832. * *
  833. * HISTORY: *
  834. * 3/1/2001 NH : Created. *
  835. *=============================================================================================*/
  836. bool MeshGeometryClass::cast_aabox_z180(AABoxCollisionTestClass & boxtest, const Vector3 & translation)
  837. {
  838. // transform the test into the meshes coordinate system
  839. AABoxCollisionTestClass newbox(boxtest);
  840. newbox.Translate(translation);
  841. newbox.Rotate(AABoxCollisionTestClass::ROTATE_Z180);
  842. // cast the box against the mesh, using culling if possible
  843. bool hit;
  844. if (CullTree) {
  845. hit = CullTree->Cast_AABox(newbox);
  846. } else {
  847. hit = cast_aabox_brute_force(newbox);
  848. }
  849. // if we hit something, we need to rotate the normal back out of the mesh coordinate system
  850. if (hit) {
  851. // rotating the normal by 180 degrees about Z
  852. boxtest.Result->Normal.X = -boxtest.Result->Normal.X;
  853. boxtest.Result->Normal.Y = -boxtest.Result->Normal.Y;
  854. }
  855. return hit;
  856. }
  857. /***********************************************************************************************
  858. * MeshGeometryClass::cast_aabox_z270 -- aabox test which is rotated about z by 270 *
  859. * *
  860. * INPUT: *
  861. * *
  862. * OUTPUT: *
  863. * *
  864. * WARNINGS: *
  865. * *
  866. * HISTORY: *
  867. * 3/1/2001 NH : Created. *
  868. *=============================================================================================*/
  869. bool MeshGeometryClass::cast_aabox_z270(AABoxCollisionTestClass & boxtest, const Vector3 & translation)
  870. {
  871. // transform the test into the mesh's coordinate system
  872. AABoxCollisionTestClass newbox(boxtest);
  873. newbox.Translate(translation);
  874. newbox.Rotate(AABoxCollisionTestClass::ROTATE_Z90);
  875. // cast the box against the mesh, using culling if possible
  876. bool hit;
  877. if (CullTree) {
  878. hit = CullTree->Cast_AABox(newbox);
  879. } else {
  880. hit = cast_aabox_brute_force(newbox);
  881. }
  882. // if we hit something, we need to rotate the normal back out of the mesh coordinate system
  883. if (hit) {
  884. // rotating the normal by 270 degrees about Z
  885. float tmp = boxtest.Result->Normal.X;
  886. boxtest.Result->Normal.X = boxtest.Result->Normal.Y;
  887. boxtest.Result->Normal.Y = -tmp;
  888. }
  889. return hit;
  890. }
  891. /***********************************************************************************************
  892. * MeshGeometryClass::intersect_obbox_brute_force -- brute force intersection check *
  893. * *
  894. * This function gets used if the mesh does not have a CullTree *
  895. * *
  896. * INPUT: *
  897. * *
  898. * OUTPUT: *
  899. * *
  900. * WARNINGS: *
  901. * *
  902. * HISTORY: *
  903. * 3/1/2001 NH : Created. *
  904. *=============================================================================================*/
  905. bool MeshGeometryClass::intersect_obbox_brute_force(OBBoxIntersectionTestClass & localtest)
  906. {
  907. TriClass tri;
  908. const Vector3 * loc = Get_Vertex_Array();
  909. const Vector3i * polyverts = Get_Polygon_Array();
  910. #ifndef COMPUTE_NORMALS
  911. const Vector4 * norms = Get_Plane_Array();
  912. #endif
  913. /*
  914. ** Loop over each polygon
  915. */
  916. for (int srtri=0; srtri < Get_Polygon_Count(); srtri++) {
  917. tri.V[0] = &(loc[ polyverts[srtri][0] ]);
  918. tri.V[1] = &(loc[ polyverts[srtri][1] ]);
  919. tri.V[2] = &(loc[ polyverts[srtri][2] ]);
  920. #ifdef COMPUTE_NORMALS
  921. static Vector3 _normal;
  922. tri.N = &_normal;
  923. tri.Compute_Normal();
  924. #else
  925. tri.N = (Vector3 *)&(norms[srtri]);
  926. #endif
  927. if (CollisionMath::Intersection_Test(localtest.Box, tri)) {
  928. return true;
  929. }
  930. }
  931. return false;
  932. }
  933. /***********************************************************************************************
  934. * MeshGeometryClass::cast_ray_brute_force -- brute force ray-cast *
  935. * *
  936. * This function gets used if the mesh does not have a CullTree *
  937. * *
  938. * INPUT: *
  939. * *
  940. * OUTPUT: *
  941. * *
  942. * WARNINGS: *
  943. * *
  944. * HISTORY: *
  945. * 3/1/2001 NH : Created. *
  946. *=============================================================================================*/
  947. bool MeshGeometryClass::cast_ray_brute_force(RayCollisionTestClass & raytest)
  948. {
  949. int srtri;
  950. TriClass tri;
  951. const Vector3 * loc = Get_Vertex_Array();
  952. const Vector3i * polyverts = Get_Polygon_Array();
  953. #ifndef COMPUTE_NORMALS
  954. const Vector4 * norms = Get_Plane_Array();
  955. #endif
  956. /*
  957. ** Loop over each polygon
  958. */
  959. bool hit = false;
  960. for (srtri=0; srtri < Get_Polygon_Count(); srtri++) {
  961. // TODO: find a better way to do this?
  962. tri.V[0] = &(loc[ polyverts[srtri][0] ]);
  963. tri.V[1] = &(loc[ polyverts[srtri][1] ]);
  964. tri.V[2] = &(loc[ polyverts[srtri][2] ]);
  965. #ifdef COMPUTE_NORMALS
  966. static Vector3 _normal;
  967. tri.N = &_normal;
  968. tri.Compute_Normal();
  969. #else
  970. tri.N = (Vector3 *)&(norms[srtri]);
  971. #endif
  972. hit = hit | CollisionMath::Collide(raytest.Ray, tri, raytest.Result);
  973. if (hit) {
  974. raytest.Result->SurfaceType = Get_Poly_Surface_Type (srtri);
  975. }
  976. if (raytest.Result->StartBad) return true;
  977. }
  978. return hit;
  979. }
  980. /***********************************************************************************************
  981. * MeshGeometryClass::cast_aabox_brute_force -- brute force aabox cast *
  982. * *
  983. * This function gets used only if the mesh doesn't have a CullTree *
  984. * *
  985. * INPUT: *
  986. * *
  987. * OUTPUT: *
  988. * *
  989. * WARNINGS: *
  990. * *
  991. * HISTORY: *
  992. * 3/1/2001 NH : Created. *
  993. *=============================================================================================*/
  994. bool MeshGeometryClass::cast_aabox_brute_force(AABoxCollisionTestClass & boxtest)
  995. {
  996. /*
  997. ** Loop over each polygon
  998. */
  999. TriClass tri;
  1000. int polyhit = -1;
  1001. const Vector3 * loc = Get_Vertex_Array();
  1002. const Vector3i * polyverts = Get_Polygon_Array();
  1003. #ifndef COMPUTE_NORMALS
  1004. const Vector4 * norms = Get_Plane_Array();
  1005. #endif
  1006. for (int srtri = 0; srtri < Get_Polygon_Count(); srtri++) {
  1007. tri.V[0] = &(loc[ polyverts[srtri][0] ]);
  1008. tri.V[1] = &(loc[ polyverts[srtri][1] ]);
  1009. tri.V[2] = &(loc[ polyverts[srtri][2] ]);
  1010. #ifdef COMPUTE_NORMALS
  1011. static Vector3 _normal;
  1012. tri.N = &_normal;
  1013. tri.Compute_Normal();
  1014. #else
  1015. tri.N = (Vector3 *)&(norms[srtri]);
  1016. #endif
  1017. if (CollisionMath::Collide(boxtest.Box, boxtest.Move, tri, boxtest.Result)) {
  1018. polyhit = srtri;
  1019. }
  1020. if (boxtest.Result->StartBad) {
  1021. return true;
  1022. }
  1023. }
  1024. if (polyhit != -1) {
  1025. boxtest.Result->SurfaceType = Get_Poly_Surface_Type(polyhit);
  1026. return true;
  1027. }
  1028. return false;
  1029. }
  1030. /***********************************************************************************************
  1031. * MeshGeometryClass::cast_obbox_brute_force -- brute force obbox cast *
  1032. * *
  1033. * This function gets used only if the mesh doesn't have a CullTree *
  1034. * *
  1035. * INPUT: *
  1036. * *
  1037. * OUTPUT: *
  1038. * *
  1039. * WARNINGS: *
  1040. * *
  1041. * HISTORY: *
  1042. * 3/1/2001 NH : Created. *
  1043. *=============================================================================================*/
  1044. bool MeshGeometryClass::cast_obbox_brute_force(OBBoxCollisionTestClass & boxtest)
  1045. {
  1046. /*
  1047. ** Loop over each polygon
  1048. */
  1049. TriClass tri;
  1050. int polyhit = -1;
  1051. const Vector3 * loc = Get_Vertex_Array();
  1052. const Vector3i * polyverts = Get_Polygon_Array();
  1053. #ifndef COMPUTE_NORMALS
  1054. const Vector4 * norms = Get_Plane_Array();
  1055. #endif
  1056. for (int srtri = 0; srtri < Get_Polygon_Count(); srtri++) {
  1057. tri.V[0] = &(loc[ polyverts[srtri][0] ]);
  1058. tri.V[1] = &(loc[ polyverts[srtri][1] ]);
  1059. tri.V[2] = &(loc[ polyverts[srtri][2] ]);
  1060. #ifdef COMPUTE_NORMALS
  1061. static Vector3 _normal;
  1062. tri.N = &_normal;
  1063. tri.Compute_Normal();
  1064. #else
  1065. tri.N = (Vector3 *)&(norms[srtri]);
  1066. #endif
  1067. if (CollisionMath::Collide(boxtest.Box, boxtest.Move, tri, Vector3(0,0,0), boxtest.Result)) {
  1068. polyhit = srtri;
  1069. }
  1070. if (boxtest.Result->StartBad) {
  1071. return true;
  1072. }
  1073. }
  1074. if (polyhit != -1) {
  1075. boxtest.Result->SurfaceType = Get_Poly_Surface_Type(polyhit);
  1076. return true;
  1077. }
  1078. return false;
  1079. }
  1080. /***********************************************************************************************
  1081. * MeshGeometryClass::Compute_Plane_Equations -- Recalculates the plane equations *
  1082. * *
  1083. * INPUT: *
  1084. * *
  1085. * OUTPUT: *
  1086. * *
  1087. * WARNINGS: *
  1088. * This function should not normally be needed during run-time. *
  1089. * The array pointer must point to enough memory to hold a plane equation per polygon *
  1090. * *
  1091. * HISTORY: *
  1092. * 11/9/2000 gth : Created. *
  1093. *=============================================================================================*/
  1094. void MeshGeometryClass::Compute_Plane_Equations(Vector4 * peq)
  1095. {
  1096. WWASSERT(peq!=NULL);
  1097. Vector3i * poly = Poly->Get_Array();
  1098. Vector3 * vert = Vertex->Get_Array();
  1099. for(int pidx = 0; pidx < PolyCount; pidx++)
  1100. {
  1101. Vector3 a,b,normal;
  1102. const Vector3 & p0= vert[poly[pidx][0]];
  1103. Vector3::Subtract(vert[poly[pidx][1]],p0,&a);
  1104. Vector3::Subtract(vert[poly[pidx][2]],p0,&b);
  1105. Vector3::Cross_Product(a,b,&normal);
  1106. normal.Normalize();
  1107. peq[pidx].Set( normal.X, normal.Y, normal.Z, -(Vector3::Dot_Product(p0,normal)) );
  1108. }
  1109. Set_Flag(DIRTY_PLANES,false);
  1110. }
  1111. /***********************************************************************************************
  1112. * MeshGeometryClass::Compute_Vertex_Normals -- recompute the vertex normals *
  1113. * *
  1114. * INPUT: *
  1115. * *
  1116. * OUTPUT: *
  1117. * *
  1118. * WARNINGS: *
  1119. * This function should not normally be needed during run-time. *
  1120. * The array pointer must point to an array of Vector3's of size NumVerts *
  1121. * *
  1122. * HISTORY: *
  1123. * 11/9/2000 gth : Created. *
  1124. *=============================================================================================*/
  1125. void MeshGeometryClass::Compute_Vertex_Normals(Vector3 * vnorm)
  1126. {
  1127. WWASSERT(vnorm != NULL);
  1128. if ((PolyCount == 0)|| (VertexCount == 0)) {
  1129. return;
  1130. }
  1131. const Vector4 * peq = Get_Plane_Array();
  1132. Vector3i * poly = Poly->Get_Array();
  1133. const uint32 * shadeIx = Get_Vertex_Shade_Index_Array(false);
  1134. // Two cases, with or without vertex shade indices. The vertex shade indices
  1135. // implicitly contain the smoothing groups information from the original mesh.
  1136. // In their abscesnce, the entire mesh is smoothed.
  1137. if (!shadeIx) {
  1138. VectorProcessorClass::Clear(vnorm, VertexCount);
  1139. for(int pidx = 0; pidx < PolyCount; pidx++) {
  1140. vnorm[poly[pidx].I].X += peq[pidx].X;
  1141. vnorm[poly[pidx].I].Y += peq[pidx].Y;
  1142. vnorm[poly[pidx].I].Z += peq[pidx].Z;
  1143. vnorm[poly[pidx].J].X += peq[pidx].X;
  1144. vnorm[poly[pidx].J].Y += peq[pidx].Y;
  1145. vnorm[poly[pidx].J].Z += peq[pidx].Z;
  1146. vnorm[poly[pidx].K].X += peq[pidx].X;
  1147. vnorm[poly[pidx].K].Y += peq[pidx].Y;
  1148. vnorm[poly[pidx].K].Z += peq[pidx].Z;
  1149. }
  1150. } else {
  1151. VectorProcessorClass::Clear (vnorm, VertexCount);
  1152. for (int pidx = 0; pidx < PolyCount; pidx++) {
  1153. vnorm[shadeIx[poly[pidx].I]].X += peq[pidx].X;
  1154. vnorm[shadeIx[poly[pidx].I]].Y += peq[pidx].Y;
  1155. vnorm[shadeIx[poly[pidx].I]].Z += peq[pidx].Z;
  1156. vnorm[shadeIx[poly[pidx].J]].X += peq[pidx].X;
  1157. vnorm[shadeIx[poly[pidx].J]].Y += peq[pidx].Y;
  1158. vnorm[shadeIx[poly[pidx].J]].Z += peq[pidx].Z;
  1159. vnorm[shadeIx[poly[pidx].K]].X += peq[pidx].X;
  1160. vnorm[shadeIx[poly[pidx].K]].Y += peq[pidx].Y;
  1161. vnorm[shadeIx[poly[pidx].K]].Z += peq[pidx].Z;
  1162. }
  1163. // normalize the "master" vertex normals and copy the smoothed ones
  1164. // (note: we always encounter the "master" ones first)
  1165. for (unsigned vidx = 0; vidx < (unsigned)VertexCount; vidx ++) {
  1166. if (shadeIx[vidx] == vidx) {
  1167. vnorm[vidx].Normalize();
  1168. } else {
  1169. vnorm[vidx] = vnorm[shadeIx[vidx]];
  1170. }
  1171. }
  1172. }
  1173. VectorProcessorClass::Normalize(vnorm, VertexCount);
  1174. Set_Flag(DIRTY_VNORMALS,false);
  1175. }
  1176. /***********************************************************************************************
  1177. * MeshGeometryClass::Compute_Bounds -- recomputes the bounding volumes *
  1178. * *
  1179. * INPUT: *
  1180. * *
  1181. * OUTPUT: *
  1182. * *
  1183. * WARNINGS: *
  1184. * This function should not normally be needed during run-time. *
  1185. * *
  1186. * HISTORY: *
  1187. * 11/9/2000 gth : Created. *
  1188. *=============================================================================================*/
  1189. void MeshGeometryClass::Compute_Bounds(Vector3 * verts)
  1190. {
  1191. BoundBoxMin.Set(0,0,0);
  1192. BoundBoxMax.Set(0,0,0);
  1193. BoundSphereCenter.Set(0,0,0);
  1194. BoundSphereRadius = 0.0;
  1195. if (VertexCount == 0) {
  1196. return;
  1197. }
  1198. // find bounding box minimum and maximum
  1199. if (verts == NULL) {
  1200. verts = Vertex->Get_Array();
  1201. }
  1202. VectorProcessorClass::MinMax(verts,BoundBoxMin,BoundBoxMax,VertexCount);
  1203. // calculate the bounding sphere
  1204. BoundSphereCenter = (BoundBoxMin + BoundBoxMax)/2.0f;
  1205. BoundSphereRadius = (float)(BoundBoxMax-BoundSphereCenter).Length2();
  1206. BoundSphereRadius = ((float)sqrt(BoundSphereRadius))*1.00001f;
  1207. Set_Flag(DIRTY_BOUNDS,false);
  1208. }
  1209. /***********************************************************************************************
  1210. * MeshGeometryClass::get_vert_normals -- get the vertex normal array *
  1211. * *
  1212. * INPUT: *
  1213. * *
  1214. * OUTPUT: *
  1215. * *
  1216. * WARNINGS: *
  1217. * *
  1218. * HISTORY: *
  1219. * 6/14/2001 gth : Created. *
  1220. *=============================================================================================*/
  1221. Vector3 * MeshGeometryClass::get_vert_normals(void)
  1222. {
  1223. #if (OPTIMIZE_VNORM_RAM)
  1224. _VNormArray.Uninitialised_Grow(VertexCount);
  1225. return &(_VNormArray[0]);
  1226. #else
  1227. WWASSERT(VertexNorm);
  1228. return VertexNorm->Get_Array();
  1229. #endif
  1230. }
  1231. /***********************************************************************************************
  1232. * MeshGeometryClass::Get_Vertex_Normal_Array -- validates and returns the vertex normal array *
  1233. * *
  1234. * INPUT: *
  1235. * *
  1236. * OUTPUT: *
  1237. * *
  1238. * WARNINGS: *
  1239. * *
  1240. * HISTORY: *
  1241. * 6/14/2001 gth : Created. *
  1242. *=============================================================================================*/
  1243. const Vector3 * MeshGeometryClass::Get_Vertex_Normal_Array(void)
  1244. {
  1245. #if (OPTIMIZE_VNORM_RAM)
  1246. Compute_Vertex_Normals(get_vert_normals());
  1247. return get_vert_normals();
  1248. #else
  1249. if (Get_Flag(DIRTY_VNORMALS)) {
  1250. Compute_Vertex_Normals(get_vert_normals());
  1251. }
  1252. return get_vert_normals();
  1253. #endif
  1254. }
  1255. /***********************************************************************************************
  1256. * MeshGeometryClass::get_planes -- get the plane array memory (internal) *
  1257. * *
  1258. * INPUT: *
  1259. * *
  1260. * OUTPUT: *
  1261. * *
  1262. * WARNINGS: *
  1263. * *
  1264. * HISTORY: *
  1265. * 6/14/2001 gth : Created. *
  1266. *=============================================================================================*/
  1267. Vector4 * MeshGeometryClass::get_planes(bool create)
  1268. {
  1269. #if (OPTIMIZE_PLANEEQ_RAM)
  1270. _PlaneEQArray.Uninitialised_Grow(PolyCount);
  1271. return &(_PlaneEQArray[0]);
  1272. #else
  1273. if (create && !PlaneEq) {
  1274. PlaneEq = NEW_REF(ShareBufferClass<Vector4>,(PolyCount, "MeshGeometryClass::PlaneEq"));
  1275. }
  1276. if (PlaneEq) {
  1277. return PlaneEq->Get_Array();
  1278. }
  1279. return NULL;
  1280. #endif
  1281. }
  1282. /***********************************************************************************************
  1283. * MeshGeometryClass::Get_Plane_Array -- validates and returns the array of plane equations *
  1284. * *
  1285. * INPUT: *
  1286. * *
  1287. * OUTPUT: *
  1288. * *
  1289. * WARNINGS: *
  1290. * *
  1291. * HISTORY: *
  1292. * 6/14/2001 gth : Created. *
  1293. *=============================================================================================*/
  1294. const Vector4 * MeshGeometryClass::Get_Plane_Array(bool create)
  1295. {
  1296. #if (OPTIMIZE_PLANEEQ_RAM)
  1297. Vector4 * planes = get_planes(create);
  1298. Compute_Plane_Equations(planes);
  1299. return planes;
  1300. #else
  1301. Vector4 * planes = get_planes(create);
  1302. if (planes && Get_Flag(DIRTY_PLANES)) {
  1303. Compute_Plane_Equations(planes);
  1304. }
  1305. return planes;
  1306. #endif
  1307. }
  1308. /***********************************************************************************************
  1309. * MeshGeometryClass::Compute_Plane -- compute the plane equation of a single poly *
  1310. * *
  1311. * INPUT: *
  1312. * *
  1313. * OUTPUT: *
  1314. * *
  1315. * WARNINGS: *
  1316. * *
  1317. * HISTORY: *
  1318. * 6/14/2001 gth : Created. *
  1319. *=============================================================================================*/
  1320. void MeshGeometryClass::Compute_Plane(int pidx,PlaneClass * set_plane) const
  1321. {
  1322. WWASSERT(pidx >= 0);
  1323. WWASSERT(pidx < PolyCount);
  1324. Vector3i & poly = Poly->Get_Array()[pidx];
  1325. Vector3 * verts = Vertex->Get_Array();
  1326. set_plane->Set(verts[poly.I],verts[poly.J],verts[poly.K]);
  1327. }
  1328. /***********************************************************************************************
  1329. * MeshGeometryClass::Generate_Culling_Tree -- Generate an AABTree for this mesh *
  1330. * *
  1331. * INPUT: *
  1332. * *
  1333. * OUTPUT: *
  1334. * *
  1335. * WARNINGS: *
  1336. * This function should not normally be needed during run-time. *
  1337. * *
  1338. * HISTORY: *
  1339. * 11/9/2000 gth : Created. *
  1340. *=============================================================================================*/
  1341. void MeshGeometryClass::Generate_Culling_Tree(void)
  1342. {
  1343. WWMEMLOG(MEM_CULLINGDATA);
  1344. {
  1345. AABTreeBuilderClass builder;
  1346. builder.Build_AABTree(PolyCount,Poly->Get_Array(),VertexCount,Vertex->Get_Array());
  1347. CullTree = NEW_REF(AABTreeClass,(&builder));
  1348. CullTree->Set_Mesh(this);
  1349. }
  1350. }
  1351. /***********************************************************************************************
  1352. * MeshGeometryClass::Load_W3D -- Load a mesh from a w3d file *
  1353. * *
  1354. * This function will extract just the geometry information from a W3D mesh. It must *
  1355. * be completely replaced by any derived classes that want to handle all of the chunks. *
  1356. * *
  1357. * INPUT: *
  1358. * *
  1359. * OUTPUT: *
  1360. * *
  1361. * WARNINGS: *
  1362. * *
  1363. * HISTORY: *
  1364. * 11/9/2000 gth : Created. *
  1365. *=============================================================================================*/
  1366. WW3DErrorType MeshGeometryClass::Load_W3D(ChunkLoadClass & cload)
  1367. {
  1368. /*
  1369. ** This function will initialize this MeshGeometryClass from the contents of a W3D file.
  1370. ** Note that derived classes need to completely replace this function; only re-using the individual
  1371. ** chunk handling functions.
  1372. */
  1373. /*
  1374. ** Open the first chunk, it should be the mesh header
  1375. */
  1376. cload.Open_Chunk();
  1377. if (cload.Cur_Chunk_ID() != W3D_CHUNK_MESH_HEADER3) {
  1378. WWDEBUG_SAY(("Old format mesh mesh, no longer supported.\n"));
  1379. goto Error;
  1380. }
  1381. W3dMeshHeader3Struct header;
  1382. if (cload.Read(&header,sizeof(W3dMeshHeader3Struct)) != sizeof(W3dMeshHeader3Struct)) {
  1383. goto Error;
  1384. }
  1385. cload.Close_Chunk();
  1386. /*
  1387. ** Process the header
  1388. */
  1389. char * tmpname;
  1390. int namelen;
  1391. Reset_Geometry(header.NumTris,header.NumVertices);
  1392. namelen = strlen(header.ContainerName);
  1393. namelen += strlen(header.MeshName);
  1394. namelen += 2;
  1395. W3dAttributes = header.Attributes;
  1396. SortLevel = header.SortLevel;
  1397. tmpname = W3DNEWARRAY char[namelen];
  1398. memset(tmpname,0,namelen);
  1399. if (strlen(header.ContainerName) > 0) {
  1400. strcpy(tmpname,header.ContainerName);
  1401. strcat(tmpname,".");
  1402. }
  1403. strcat(tmpname,header.MeshName);
  1404. Set_Name(tmpname);
  1405. delete[] tmpname;
  1406. tmpname = NULL;
  1407. /*
  1408. ** Set Bounding Info
  1409. */
  1410. BoundBoxMin.Set(header.Min.X,header.Min.Y,header.Min.Z);
  1411. BoundBoxMax.Set(header.Max.X,header.Max.Y,header.Max.Z);
  1412. BoundSphereCenter.Set(header.SphCenter.X,header.SphCenter.Y,header.SphCenter.Z);
  1413. BoundSphereRadius = header.SphRadius;
  1414. /*
  1415. ** Flags
  1416. */
  1417. if (header.Version >= W3D_MAKE_VERSION(4,1)) {
  1418. int geometry_type = header.Attributes & W3D_MESH_FLAG_GEOMETRY_TYPE_MASK;
  1419. switch (geometry_type)
  1420. {
  1421. case W3D_MESH_FLAG_GEOMETRY_TYPE_NORMAL:
  1422. break;
  1423. case W3D_MESH_FLAG_GEOMETRY_TYPE_CAMERA_ALIGNED:
  1424. Set_Flag(ALIGNED,true);
  1425. break;
  1426. case W3D_MESH_FLAG_GEOMETRY_TYPE_CAMERA_ORIENTED:
  1427. Set_Flag(ORIENTED,true);
  1428. break;
  1429. case W3D_MESH_FLAG_GEOMETRY_TYPE_SKIN:
  1430. Set_Flag(SKIN,true);
  1431. break;
  1432. }
  1433. }
  1434. if (header.Attributes & W3D_MESH_FLAG_TWO_SIDED) {
  1435. Set_Flag(TWO_SIDED,true);
  1436. }
  1437. if (header.Attributes & W3D_MESH_FLAG_CAST_SHADOW) {
  1438. Set_Flag(CAST_SHADOW,true);
  1439. }
  1440. read_chunks(cload);
  1441. /*
  1442. ** If this is a pre-3.0 mesh and it has vertex influences,
  1443. ** fixup the bone indices to account for the new root node
  1444. */
  1445. if ((header.Version < W3D_MAKE_VERSION(3,0)) && (Get_Flag(SKIN))) {
  1446. uint16 * links = get_bone_links();
  1447. WWASSERT(links);
  1448. for (int bi = 0; bi < Get_Vertex_Count(); bi++) {
  1449. links[bi] += 1;
  1450. }
  1451. }
  1452. /*
  1453. ** If this mesh is collideable and no AABTree was in the file, generate one now
  1454. */
  1455. if ( (((W3dAttributes & W3D_MESH_FLAG_COLLISION_TYPE_MASK) >> W3D_MESH_FLAG_COLLISION_TYPE_SHIFT) != 0) &&
  1456. (CullTree == NULL))
  1457. {
  1458. Generate_Culling_Tree();
  1459. }
  1460. return WW3D_ERROR_OK;
  1461. Error:
  1462. return WW3D_ERROR_LOAD_FAILED;
  1463. }
  1464. /***********************************************************************************************
  1465. * MeshGeometryClass::read_chunks -- read w3d chunks *
  1466. * *
  1467. * Again, derived classes must replace this function with one that handles all of their *
  1468. * chunks in addition to calling to this class for the individual chunks that it handles. *
  1469. * *
  1470. * INPUT: *
  1471. * *
  1472. * OUTPUT: *
  1473. * *
  1474. * WARNINGS: *
  1475. * *
  1476. * HISTORY: *
  1477. * 11/9/2000 gth : Created. *
  1478. *=============================================================================================*/
  1479. WW3DErrorType MeshGeometryClass::read_chunks(ChunkLoadClass & cload)
  1480. {
  1481. /*
  1482. ** Read in the chunk header
  1483. ** If there are no more chunks within the mesh chunk,
  1484. ** we are done.
  1485. */
  1486. while (cload.Open_Chunk()) {
  1487. /*
  1488. ** Process the chunk
  1489. */
  1490. WW3DErrorType error = WW3D_ERROR_OK;
  1491. switch (cload.Cur_Chunk_ID()) {
  1492. case W3D_CHUNK_VERTICES:
  1493. error = read_vertices(cload);
  1494. break;
  1495. case W3D_CHUNK_SURRENDER_NORMALS:
  1496. case W3D_CHUNK_VERTEX_NORMALS:
  1497. error = read_vertex_normals(cload);
  1498. break;
  1499. case W3D_CHUNK_TRIANGLES:
  1500. error = read_triangles(cload);
  1501. break;
  1502. case W3D_CHUNK_MESH_USER_TEXT:
  1503. error = read_user_text(cload);
  1504. break;
  1505. case W3D_CHUNK_VERTEX_INFLUENCES:
  1506. error = read_vertex_influences(cload);
  1507. break;
  1508. case W3D_CHUNK_VERTEX_SHADE_INDICES:
  1509. error = read_vertex_shade_indices(cload);
  1510. break;
  1511. case W3D_CHUNK_AABTREE:
  1512. read_aabtree(cload);
  1513. break;
  1514. default:
  1515. break;
  1516. }
  1517. cload.Close_Chunk();
  1518. if (error != WW3D_ERROR_OK) {
  1519. return error;
  1520. }
  1521. }
  1522. return WW3D_ERROR_OK;
  1523. }
  1524. /***********************************************************************************************
  1525. * MeshGeometryClass::read_vertices -- read the vertex chunk from a W3D file *
  1526. * *
  1527. * INPUT: *
  1528. * *
  1529. * OUTPUT: *
  1530. * *
  1531. * WARNINGS: *
  1532. * *
  1533. * HISTORY: *
  1534. * 11/9/2000 gth : Created. *
  1535. *=============================================================================================*/
  1536. WW3DErrorType MeshGeometryClass::read_vertices(ChunkLoadClass & cload)
  1537. {
  1538. W3dVectorStruct vert;
  1539. Vector3 * loc = Vertex->Get_Array();
  1540. assert(loc);
  1541. for (int i=0; i<Get_Vertex_Count(); i++) {
  1542. if (cload.Read(&vert,sizeof(W3dVectorStruct)) != sizeof(W3dVectorStruct)) {
  1543. return WW3D_ERROR_LOAD_FAILED;
  1544. }
  1545. loc[i].X = vert.X;
  1546. loc[i].Y = vert.Y;
  1547. loc[i].Z = vert.Z;
  1548. }
  1549. return WW3D_ERROR_OK;
  1550. }
  1551. /***********************************************************************************************
  1552. * MeshGeometryClass::read_vertex_normals -- read the vertex normals chunk from a w3d file *
  1553. * *
  1554. * INPUT: *
  1555. * *
  1556. * OUTPUT: *
  1557. * *
  1558. * WARNINGS: *
  1559. * *
  1560. * HISTORY: *
  1561. * 11/9/2000 gth : Created. *
  1562. *=============================================================================================*/
  1563. WW3DErrorType MeshGeometryClass::read_vertex_normals(ChunkLoadClass & cload)
  1564. {
  1565. W3dVectorStruct norm;
  1566. Vector3 * mdlnorms = get_vert_normals();
  1567. WWASSERT(mdlnorms);
  1568. for (int i=0; i<VertexCount; i++) {
  1569. if (cload.Read(&norm,sizeof(W3dVectorStruct)) != sizeof(W3dVectorStruct)) {
  1570. return WW3D_ERROR_LOAD_FAILED;
  1571. }
  1572. mdlnorms[i].Set(norm.X,norm.Y,norm.Z);
  1573. }
  1574. return WW3D_ERROR_OK;
  1575. }
  1576. /***********************************************************************************************
  1577. * MeshGeometryClass::read_triangles -- read the triangles chunk from a w3d file *
  1578. * *
  1579. * INPUT: *
  1580. * *
  1581. * OUTPUT: *
  1582. * *
  1583. * WARNINGS: *
  1584. * *
  1585. * HISTORY: *
  1586. * 11/9/2000 gth : Created. *
  1587. *=============================================================================================*/
  1588. WW3DErrorType MeshGeometryClass::read_triangles(ChunkLoadClass & cload)
  1589. {
  1590. W3dTriStruct tri;
  1591. // cache pointers to various arrays in the surrender mesh
  1592. Vector3i * vi = get_polys();
  1593. Set_Flag(DIRTY_PLANES,false);
  1594. Vector4 * peq = get_planes();
  1595. uint8 * surface_types = Get_Poly_Surface_Type_Array();
  1596. // read in each polygon one by one
  1597. for (int i=0; i<Get_Polygon_Count(); i++) {
  1598. if (cload.Read(&tri,sizeof(W3dTriStruct)) != sizeof(W3dTriStruct)) {
  1599. return WW3D_ERROR_LOAD_FAILED;
  1600. }
  1601. // set the vertex indices
  1602. vi[i].I = tri.Vindex[0];
  1603. vi[i].J = tri.Vindex[1];
  1604. vi[i].K = tri.Vindex[2];
  1605. // set the normal
  1606. peq[i].X = tri.Normal.X;
  1607. peq[i].Y = tri.Normal.Y;
  1608. peq[i].Z = tri.Normal.Z;
  1609. peq[i].W = -tri.Dist;
  1610. // set the surface type
  1611. WWASSERT(tri.Attributes < 256);
  1612. surface_types[i] = (uint8)(tri.Attributes);
  1613. }
  1614. return WW3D_ERROR_OK;
  1615. }
  1616. /***********************************************************************************************
  1617. * MeshGeometryClass::read_user_text -- read the user text chunk from a w3d file *
  1618. * *
  1619. * INPUT: *
  1620. * *
  1621. * OUTPUT: *
  1622. * *
  1623. * WARNINGS: *
  1624. * *
  1625. * HISTORY: *
  1626. * 11/9/2000 gth : Created. *
  1627. *=============================================================================================*/
  1628. WW3DErrorType MeshGeometryClass::read_user_text(ChunkLoadClass & cload)
  1629. {
  1630. unsigned int textlen = cload.Cur_Chunk_Length();
  1631. /*
  1632. ** This shouldn't happen but if there are more than one
  1633. ** USER_TEXT chunks in the mesh file, store only the first
  1634. ** one. I am assuming that if the UserText buffer is not
  1635. ** NULL, then a previous user text chunk has been read in...
  1636. */
  1637. if (UserText != NULL) {
  1638. return WW3D_ERROR_OK;
  1639. }
  1640. /*
  1641. ** Allocate the buffer and read in the text
  1642. */
  1643. UserText = NEW_REF(ShareBufferClass<char>,(textlen, "MeshGeometryClass::UserText"));
  1644. if (cload.Read(UserText->Get_Array(),textlen) != textlen) {
  1645. return WW3D_ERROR_LOAD_FAILED;
  1646. }
  1647. return WW3D_ERROR_OK;
  1648. }
  1649. /***********************************************************************************************
  1650. * MeshGeometryClass::read_vertex_influences -- read the vertex influences chunk from a w3d fi *
  1651. * *
  1652. * INPUT: *
  1653. * *
  1654. * OUTPUT: *
  1655. * *
  1656. * WARNINGS: *
  1657. * *
  1658. * HISTORY: *
  1659. * 11/9/2000 gth : Created. *
  1660. *=============================================================================================*/
  1661. WW3DErrorType MeshGeometryClass::read_vertex_influences(ChunkLoadClass & cload)
  1662. {
  1663. W3dVertInfStruct vinf;
  1664. uint16 * links = get_bone_links(true);
  1665. WWASSERT(links);
  1666. for (int i=0; i<Get_Vertex_Count(); i++) {
  1667. if (cload.Read(&vinf,sizeof(W3dVertInfStruct)) != sizeof(W3dVertInfStruct)) {
  1668. return WW3D_ERROR_LOAD_FAILED;
  1669. }
  1670. links[i] = vinf.BoneIdx;
  1671. }
  1672. Set_Flag(SKIN,true);
  1673. return WW3D_ERROR_OK;
  1674. }
  1675. /***********************************************************************************************
  1676. * MeshGeometryClass::read_vertex_shade_indices -- read the vertex shade indices chunk *
  1677. * *
  1678. * INPUT: *
  1679. * *
  1680. * OUTPUT: *
  1681. * *
  1682. * WARNINGS: *
  1683. * *
  1684. * HISTORY: *
  1685. * 11/9/2000 gth : Created. *
  1686. *=============================================================================================*/
  1687. WW3DErrorType MeshGeometryClass::read_vertex_shade_indices(ChunkLoadClass & cload)
  1688. {
  1689. uint32 * shade_index = get_shade_indices(true);
  1690. uint32 si;
  1691. for (int i=0; i<Get_Vertex_Count(); i++) {
  1692. if (cload.Read(&si,sizeof(uint32)) != sizeof(uint32)) {
  1693. return WW3D_ERROR_LOAD_FAILED;
  1694. }
  1695. shade_index[i] = si;
  1696. }
  1697. return WW3D_ERROR_OK;
  1698. }
  1699. /***********************************************************************************************
  1700. * MeshGeometryClass::read_aabtree -- read the AABTree chunk from a w3d file *
  1701. * *
  1702. * INPUT: *
  1703. * *
  1704. * OUTPUT: *
  1705. * *
  1706. * WARNINGS: *
  1707. * *
  1708. * HISTORY: *
  1709. * 11/9/2000 gth : Created. *
  1710. *=============================================================================================*/
  1711. WW3DErrorType MeshGeometryClass::read_aabtree(ChunkLoadClass &cload)
  1712. {
  1713. REF_PTR_RELEASE(CullTree);
  1714. CullTree = NEW_REF(AABTreeClass,());
  1715. CullTree->Load_W3D(cload);
  1716. CullTree->Set_Mesh(this);
  1717. return (WW3D_ERROR_OK);
  1718. }
  1719. void MeshGeometryClass::Scale(const Vector3 &sc)
  1720. {
  1721. WWASSERT(Vertex);
  1722. Vector3 * vert = Vertex->Get_Array();
  1723. for (int i=0;i<VertexCount; i++) {
  1724. vert[i].X *= sc.X;
  1725. vert[i].Y *= sc.Y;
  1726. vert[i].Z *= sc.Z;
  1727. }
  1728. BoundBoxMin.Scale(sc);
  1729. BoundBoxMax.Scale(sc);
  1730. BoundSphereCenter.Scale(sc);
  1731. float max;
  1732. max = (sc.X > sc.Y) ? sc.X : sc.Y;
  1733. max = (max > sc.Z) ? max : sc.Z;
  1734. BoundSphereRadius *= max;
  1735. // If scaling uniformly normals are OK:
  1736. if (sc.X != sc.Y || sc.Y != sc.Z) {
  1737. Set_Flag(DIRTY_VNORMALS,true);
  1738. }
  1739. // pnormals are plane equations...
  1740. Set_Flag(DIRTY_PLANES,true);
  1741. }