sphereobj.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/sphereobj.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 11/24/01 6:17p $*
  29. * *
  30. * $Revision:: 23 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * SphereRenderObjClass::SphereRenderObjClass -- Constructor *
  35. * SphereRenderObjClass::SphereRenderObjClass -- Constructor - init from a definition *
  36. * SphereRenderObjClass::SphereRenderObjClass -- Copy constructor *
  37. * SphereRenderObjClass::operator -- assignment operator *
  38. * SphereRenderObjClass::Get_Num_Polys -- returns number of polygons *
  39. * SphereRenderObjClass::Get_Name -- returns name *
  40. * SphereRenderObjClass::Set_Name -- sets the name *
  41. * SphereRenderObjClass::render_Sphere -- submits the Sphere to the GERD *
  42. * SphereRenderObjClass::vis_render_Sphere -- submits Sphere to the GERD for VIS *
  43. * SphereRenderObjClass::SphereRenderObjClass -- constructor *
  44. * SphereRenderObjClass::SphereRenderObjClass -- Constructor - init from a definition *
  45. * SphereRenderObjClass::SphereRenderObjClass -- copy constructor *
  46. * SphereRenderObjClass::SphereRenderObjClass -- Constructor from a wwmath aaSphere *
  47. * SphereRenderObjClass::operator -- assignment operator *
  48. * SphereRenderObjClass::Clone -- clones the Sphere *
  49. * SphereRenderObjClass::Class_ID -- returns the class-id for AASphere's *
  50. * SphereRenderObjClass::Render -- render this Sphere *
  51. * SphereRenderObjClass::Special_Render -- special render this Sphere (vis) *
  52. * SphereRenderObjClass::Set_Transform -- set the transform for this Sphere *
  53. * SphereRenderObjClass::Set_Position -- Set the position of this Sphere *
  54. * SphereRenderObjClass::update_cached_Sphere -- update the world-space version of this Spher*
  55. * SphereRenderObjClass::Cast_Ray -- cast a ray against this Sphere *
  56. * SphereRenderObjClass::Cast_AASphere -- cast an AASphere against this Sphere *
  57. * SphereRenderObjClass::Cast_OBSphere -- cast an OBSphere against this Sphere *
  58. * SphereRenderObjClass::Intersect_AASphere -- intersect this Sphere with an AASphere *
  59. * SphereRenderObjClass::Intersect_OBSphere -- Intersect this Sphere with an OBSphere *
  60. * SphereRenderObjClass::Get_Obj_Space_Bounding_Sphere -- return the object-space bounding sp*
  61. * SphereRenderObjClass::Get_Obj_Space_Bounding_Sphere -- returns the obj-space bounding Sphe*
  62. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  63. #include "sphereobj.h"
  64. #include "w3d_util.h"
  65. #include "wwdebug.h"
  66. #include "vertmaterial.h"
  67. #include "ww3d.h"
  68. #include "chunkio.h"
  69. #include "rinfo.h"
  70. #include "coltest.h"
  71. #include "inttest.h"
  72. #include "matrix3.h"
  73. #include "wwmath.h"
  74. #include "assetmgr.h"
  75. #include "wwstring.h"
  76. #include "camera.h"
  77. #include "statistics.h"
  78. #include "dx8wrapper.h"
  79. #include "dx8vertexbuffer.h"
  80. #include "dx8indexbuffer.h"
  81. #include "sortingrenderer.h"
  82. #include "visrasterizer.h"
  83. #define SPHERE_NUM_LOD (6)
  84. #define SPHERE_LOWEST_LOD (4)
  85. #define SPHERE_HIGHEST_LOD (16)
  86. #define STATIC_SORT_SPHERES 1
  87. #define SPHERE_SORT_LEVEL 1
  88. static bool Sphere_Array_Valid = false;
  89. SphereMeshClass SphereMeshArray[SPHERE_NUM_LOD];
  90. /*
  91. ** SphereRenderObjClass Implementation
  92. */
  93. /***********************************************************************************************
  94. * SphereRenderObjClass::SphereRenderObjClass -- Constructor *
  95. * *
  96. * INPUT: *
  97. * *
  98. * OUTPUT: *
  99. * *
  100. * WARNINGS: *
  101. * *
  102. * HISTORY: *
  103. * 1/19/00 gth : Created. *
  104. *=============================================================================================*/
  105. SphereRenderObjClass::SphereRenderObjClass(void)
  106. : anim_time (0.0F),
  107. IsAnimating (true),
  108. AnimDuration (0.0F),
  109. SphereMaterial (NULL),
  110. SphereTexture (NULL),
  111. Color (0.75F,0.75F,0.75F),
  112. Scale (1, 1, 1),
  113. Alpha (1.0F),
  114. Flags(USE_ALPHA_VECTOR),
  115. ObjSpaceCenter (0, 0, 0),
  116. ObjSpaceExtent (1, 1, 1),
  117. CurrentLOD (SPHERE_NUM_LOD - 1)
  118. {
  119. Generate_Shared_Mesh_Arrays ( Vector );
  120. Orientation.Make_Identity();
  121. memset(Name,0,sizeof(Name));
  122. Init_Material ();
  123. #if (STATIC_SORT_SPHERES)
  124. // (gth) testing whether we can get away without poly-sorting rings and spheres
  125. Set_Sort_Level(SPHERE_SORT_LEVEL);
  126. #endif
  127. }
  128. /***********************************************************************************************
  129. * SphereRenderObjClass::SphereRenderObjClass -- Constructor - init from a definition *
  130. * *
  131. * INPUT: *
  132. * *
  133. * OUTPUT: *
  134. * *
  135. * WARNINGS: *
  136. * *
  137. * HISTORY: *
  138. * 1/19/00 gth : Created. *
  139. *=============================================================================================*/
  140. SphereRenderObjClass::SphereRenderObjClass(const W3dSphereStruct & def)
  141. : anim_time (0.0F),
  142. IsAnimating (true),
  143. AnimDuration (0.0F),
  144. SphereMaterial (NULL),
  145. SphereTexture (NULL),
  146. Color (0.75F,0.75F,0.75F),
  147. Scale (1, 1, 1),
  148. Alpha (1.0F),
  149. Flags(USE_ALPHA_VECTOR),
  150. ObjSpaceCenter (0, 0, 0),
  151. ObjSpaceExtent (1, 1, 1),
  152. CurrentLOD (SPHERE_NUM_LOD - 1)
  153. {
  154. Generate_Shared_Mesh_Arrays ( Vector );
  155. Init_Material ();
  156. Orientation.Make_Identity();
  157. //
  158. // Initialize from the defintion
  159. //
  160. Set_Name(def.Name);
  161. Set_Local_Center_Extent ( Vector3 (def.Center.X, def.Center.Y, def.Center.Z),
  162. Vector3 (def.Extent.X, def.Extent.Y, def.Extent.Z));
  163. if (def.TextureName[0] != 0) {
  164. SphereTexture = WW3DAssetManager::Get_Instance ()->Get_Texture (def.TextureName);
  165. }
  166. #if (STATIC_SORT_SPHERES)
  167. // (gth) testing whether we can get away without poly-sorting rings and spheres
  168. Set_Sort_Level(SPHERE_SORT_LEVEL);
  169. #endif
  170. }
  171. /***********************************************************************************************
  172. * SphereRenderObjClass::SphereRenderObjClass -- Copy constructor *
  173. * *
  174. * INPUT: *
  175. * *
  176. * OUTPUT: *
  177. * *
  178. * WARNINGS: *
  179. * *
  180. * HISTORY: *
  181. * 1/19/00 gth : Created. *
  182. *=============================================================================================*/
  183. SphereRenderObjClass::SphereRenderObjClass(const SphereRenderObjClass & src)
  184. : anim_time (0.0F),
  185. IsAnimating (true),
  186. AnimDuration (0.0F),
  187. SphereMaterial (NULL),
  188. SphereTexture (NULL),
  189. Color (0.75F,0.75F,0.75F),
  190. Scale (1, 1, 1),
  191. Alpha (1.0F),
  192. Flags(USE_ALPHA_VECTOR),
  193. ObjSpaceCenter (0, 0, 0),
  194. ObjSpaceExtent (1, 1, 1),
  195. CurrentLOD (SPHERE_NUM_LOD - 1)
  196. {
  197. Generate_Shared_Mesh_Arrays ( Vector );
  198. Init_Material ();
  199. Orientation.Make_Identity();
  200. *this = src;
  201. #if (STATIC_SORT_SPHERES)
  202. // (gth) testing whether we can get away without poly-sorting rings and spheres
  203. Set_Sort_Level(SPHERE_SORT_LEVEL);
  204. #endif
  205. }
  206. SphereRenderObjClass::~SphereRenderObjClass()
  207. {
  208. REF_PTR_RELEASE(SphereMaterial);
  209. } // destructor
  210. /***********************************************************************************************
  211. * SphereRenderObjClass::operator -- assignment operator *
  212. * *
  213. * INPUT: *
  214. * *
  215. * OUTPUT: *
  216. * *
  217. * WARNINGS: *
  218. * *
  219. * HISTORY: *
  220. * 1/19/00 gth : Created. *
  221. *=============================================================================================*/
  222. SphereRenderObjClass & SphereRenderObjClass::operator = (const SphereRenderObjClass & that)
  223. {
  224. if (this != &that) {
  225. RenderObjClass::operator = (that);
  226. Set_Name(that.Get_Name());
  227. Color = that.Color;
  228. Alpha = that.Alpha;
  229. Scale = that.Scale;
  230. Vector = that.Vector;
  231. Flags = that.Flags;
  232. Orientation = that.Orientation;
  233. SphereShader = that.SphereShader;
  234. CachedBox = that.CachedBox;
  235. anim_time = that.anim_time;
  236. AnimDuration = that.AnimDuration;
  237. ObjSpaceCenter = that.ObjSpaceCenter;
  238. ObjSpaceExtent = that.ObjSpaceExtent;
  239. ColorChannel = that.ColorChannel;
  240. AlphaChannel = that.AlphaChannel;
  241. ScaleChannel = that.ScaleChannel;
  242. VectorChannel = that.VectorChannel;
  243. Set_Texture (that.SphereTexture);
  244. }
  245. return *this;
  246. }
  247. /***********************************************************************************************
  248. * SphereRenderObjClass::Generate_Shared_Mesh_Arrays -- Generates mesh LOD arrays. *
  249. * *
  250. * INPUT: *
  251. * *
  252. * OUTPUT: *
  253. * *
  254. * WARNINGS: *
  255. * *
  256. * HISTORY: *
  257. * 03/08/00 pds : Created. *
  258. *=============================================================================================*/
  259. void SphereRenderObjClass::Generate_Shared_Mesh_Arrays (const AlphaVectorStruct &alphavector)
  260. {
  261. // Generate shared Mesh Arrays
  262. if (!Sphere_Array_Valid) {
  263. float size = SPHERE_LOWEST_LOD;
  264. float step = (SPHERE_HIGHEST_LOD - SPHERE_LOWEST_LOD);
  265. step /= SPHERE_NUM_LOD;
  266. for(int i=0; i < SPHERE_NUM_LOD; i++) {
  267. SphereMeshArray[i].Generate(1.0f, size, size);
  268. size+=step;
  269. SphereMeshArray[i].Set_Alpha_Vector( alphavector, false, false );
  270. }
  271. Sphere_Array_Valid = true;
  272. }
  273. return ;
  274. }
  275. /***********************************************************************************************
  276. * SphereRenderObjClass::Init_Material -- Sets up the material and default shader for the sphere.*
  277. * *
  278. * INPUT: *
  279. * *
  280. * OUTPUT: *
  281. * *
  282. * WARNINGS: *
  283. * *
  284. * HISTORY: *
  285. * 03/08/00 pds : Created. *
  286. *=============================================================================================*/
  287. void SphereRenderObjClass::Init_Material (void)
  288. {
  289. REF_PTR_RELEASE (SphereMaterial);
  290. SphereMaterial = NEW_REF(VertexMaterialClass,());
  291. SphereMaterial->Set_Ambient(0,0,0);
  292. SphereMaterial->Set_Diffuse(0,0,0);
  293. SphereMaterial->Set_Specular(0,0,0);
  294. SphereMaterial->Set_Emissive(1,1,1);
  295. SphereMaterial->Set_Opacity(0.25f);
  296. SphereMaterial->Set_Shininess(0.0f);
  297. SphereMaterial->Set_Lighting(true);
  298. // Texturing, zbuffer, primary gradient, alpha blending
  299. SphereShader = ShaderClass::_PresetAlphaShader;
  300. } // Init_Material
  301. /***********************************************************************************************
  302. * SphereRenderObjClass::Get_Num_Polys -- returns number of polygons *
  303. * *
  304. * INPUT: *
  305. * *
  306. * OUTPUT: *
  307. * *
  308. * WARNINGS: *
  309. * *
  310. * HISTORY: *
  311. * 1/19/00 gth : Created. *
  312. *=============================================================================================*/
  313. int SphereRenderObjClass::Get_Num_Polys(void) const
  314. {
  315. return SphereMeshArray[ CurrentLOD ].Get_Num_Polys();
  316. }
  317. /***********************************************************************************************
  318. * SphereRenderObjClass::Set_Texture *
  319. * *
  320. * INPUT: *
  321. * *
  322. * OUTPUT: *
  323. * *
  324. * WARNINGS: *
  325. * *
  326. * HISTORY: *
  327. * 1/19/00 gth : Created. *
  328. *=============================================================================================*/
  329. void SphereRenderObjClass::Set_Texture(TextureClass *tf)
  330. {
  331. REF_PTR_SET(SphereTexture,tf);
  332. }
  333. /***********************************************************************************************
  334. * SphereRenderObjClass::Get_Name -- returns name *
  335. * *
  336. * INPUT: *
  337. * *
  338. * OUTPUT: *
  339. * *
  340. * WARNINGS: *
  341. * *
  342. * HISTORY: *
  343. * 1/19/00 gth : Created. *
  344. *=============================================================================================*/
  345. const char * SphereRenderObjClass::Get_Name(void) const
  346. {
  347. return Name;
  348. }
  349. /***********************************************************************************************
  350. * SphereRenderObjClass::Set_Name -- sets the name *
  351. * *
  352. * INPUT: *
  353. * *
  354. * OUTPUT: *
  355. * *
  356. * WARNINGS: *
  357. * *
  358. * HISTORY: *
  359. * 1/19/00 gth : Created. *
  360. *=============================================================================================*/
  361. void SphereRenderObjClass::Set_Name(const char * name)
  362. {
  363. WWASSERT(name != NULL);
  364. WWASSERT(strlen(name) < 2*W3D_NAME_LEN);
  365. strcpy(Name,name);
  366. }
  367. /***********************************************************************************************
  368. * SphereRenderObjClass::render_sphere *
  369. * *
  370. * INPUT: *
  371. * *
  372. * OUTPUT: *
  373. * *
  374. * WARNINGS: *
  375. * *
  376. * HISTORY: *
  377. * 3/01/00 jga : Created. *
  378. * 2/19/01 HY : upgraded to DX8 *
  379. *=============================================================================================*/
  380. void SphereRenderObjClass::render_sphere()
  381. {
  382. SphereMeshClass & mesh = SphereMeshArray[ CurrentLOD ];
  383. if (SphereTexture) {
  384. SphereShader.Set_Texturing (ShaderClass::TEXTURING_ENABLE);
  385. } else {
  386. SphereShader.Set_Texturing (ShaderClass::TEXTURING_DISABLE);
  387. }
  388. DX8Wrapper::Set_Shader(SphereShader);
  389. DX8Wrapper::Set_Texture(0,SphereTexture);
  390. DX8Wrapper::Set_Material(SphereMaterial);
  391. DynamicVBAccessClass vb(BUFFER_TYPE_DYNAMIC_SORTING,dynamic_fvf_type,mesh.Vertex_ct);
  392. {
  393. DynamicVBAccessClass::WriteLockClass Lock(&vb);
  394. VertexFormatXYZNDUV2 *vb = Lock.Get_Formatted_Vertex_Array();
  395. for (int i=0; i<mesh.Vertex_ct; i++)
  396. {
  397. vb->x = mesh.vtx[i].X;
  398. vb->y = mesh.vtx[i].Y;
  399. vb->z = mesh.vtx[i].Z;
  400. vb->nx = mesh.vtx_normal[i].X; // may not need this!
  401. vb->ny = mesh.vtx_normal[i].Y;
  402. vb->nz = mesh.vtx_normal[i].Z;
  403. if (Flags & USE_ALPHA_VECTOR) {
  404. vb->diffuse = DX8Wrapper::Convert_Color(mesh.dcg[i]);
  405. } else {
  406. vb->diffuse = 0xFFFFFFFF; // TODO could combine the material color with this and turn off lighting
  407. }
  408. if (SphereTexture) {
  409. vb->u1 = mesh.vtx_uv[i].X;
  410. vb->v1 = mesh.vtx_uv[i].Y;
  411. }
  412. vb++;
  413. }
  414. }
  415. DynamicIBAccessClass ib(BUFFER_TYPE_DYNAMIC_SORTING,mesh.face_ct*3);
  416. {
  417. DynamicIBAccessClass::WriteLockClass Lock(&ib);
  418. unsigned short *mem=Lock.Get_Index_Array();
  419. for (int i=0; i<mesh.face_ct; i++)
  420. {
  421. mem[3*i]=mesh.tri_poly[i].I;
  422. mem[3*i+1]=mesh.tri_poly[i].J;
  423. mem[3*i+2]=mesh.tri_poly[i].K;
  424. }
  425. }
  426. DX8Wrapper::Set_Vertex_Buffer(vb);
  427. DX8Wrapper::Set_Index_Buffer(ib,0);
  428. #if (STATIC_SORT_SPHERES)
  429. DX8Wrapper::Draw_Triangles(0,mesh.face_ct,0,mesh.Vertex_ct);
  430. #else
  431. SortingRendererClass::Insert_Triangles(
  432. Get_Bounding_Sphere(),
  433. 0,
  434. mesh.face_ct,
  435. 0,
  436. mesh.Vertex_ct);
  437. #endif
  438. } // render_sphere
  439. /***********************************************************************************************
  440. * SphereRenderObjClass::vis_render_sphere -- submits box to the GERD for VIS *
  441. * *
  442. * this renders the sphere with the specified VIS-ID. *
  443. * *
  444. * INPUT: *
  445. * *
  446. * OUTPUT: *
  447. * *
  448. * WARNINGS: *
  449. * *
  450. * HISTORY: *
  451. * 1/19/00 gth : Created. *
  452. *=============================================================================================*/
  453. void SphereRenderObjClass::vis_render_sphere(SpecialRenderInfoClass & rinfo,const Vector3 & center,const Vector3 & extent)
  454. {
  455. } // vis_render_sphere
  456. /***********************************************************************************************
  457. * SphereRenderObjClass::Clone -- clones the box *
  458. * *
  459. * INPUT: *
  460. * *
  461. * OUTPUT: *
  462. * *
  463. * WARNINGS: *
  464. * *
  465. * HISTORY: *
  466. * 1/19/00 gth : Created. *
  467. *=============================================================================================*/
  468. RenderObjClass * SphereRenderObjClass::Clone(void) const
  469. {
  470. return new SphereRenderObjClass(*this);
  471. }
  472. /***********************************************************************************************
  473. * SphereRenderObjClass::Class_ID -- returns the class-id for sphere's *
  474. * *
  475. * INPUT: *
  476. * *
  477. * OUTPUT: *
  478. * *
  479. * WARNINGS: *
  480. * *
  481. * HISTORY: *
  482. * 1/19/00 gth : Created. *
  483. *=============================================================================================*/
  484. int SphereRenderObjClass::Class_ID(void) const
  485. {
  486. return RenderObjClass::CLASSID_SPHERE;
  487. }
  488. /***********************************************************************************************
  489. * SphereRenderObjClass::Render -- render this box *
  490. * *
  491. * INPUT: *
  492. * *
  493. * OUTPUT: *
  494. * *
  495. * WARNINGS: *
  496. * *
  497. * HISTORY: *
  498. * 1/19/00 gth : Created. *
  499. *=============================================================================================*/
  500. void SphereRenderObjClass::Render(RenderInfoClass & rinfo)
  501. {
  502. if (Is_Not_Hidden_At_All() == false) {
  503. return;
  504. }
  505. // If static sort lists are enabled and this mesh has a sort level, put it on the list instead
  506. // of rendering it.
  507. unsigned int sort_level = (unsigned int)Get_Sort_Level();
  508. if (WW3D::Are_Static_Sort_Lists_Enabled() && sort_level != SORT_LEVEL_NONE) {
  509. WW3D::Add_To_Static_Sort_List(this, sort_level);
  510. } else {
  511. // Process texture reductions:
  512. // if (SphereTexture) SphereTexture->Process_Reduction();
  513. // Determine LOD
  514. float screen_size = Get_Screen_Size(rinfo.Camera);
  515. // This code assumes that screen_size returns back the percentage of the screen, 0.0 = 0%, 1.0f = 100%
  516. // Currently it will use Linear interpolation over 0.0 to 1.0, with the SPHERE_NUM_LOD
  517. // to set the CurrentLOD
  518. screen_size = sqrtf(screen_size);
  519. float lod = screen_size * ((float) SPHERE_NUM_LOD);
  520. int lod_int = lod;
  521. lod-=lod_int;
  522. if (lod >= 0.5f) lod_int++;
  523. if (lod_int < 0) lod_int = 0;
  524. if (lod_int >= SPHERE_NUM_LOD) lod_int = SPHERE_NUM_LOD-1;
  525. CurrentLOD = lod_int;
  526. // End LOD Determination
  527. Matrix3D temp = Transform;
  528. // Do Time Based Animation
  529. animate();
  530. // Scale
  531. Vector3 real_scale;
  532. real_scale.X = ObjSpaceExtent.X * Scale.X;
  533. real_scale.Y = ObjSpaceExtent.Y * Scale.Y;
  534. real_scale.Z = ObjSpaceExtent.Z * Scale.Z;
  535. temp.Scale(real_scale);
  536. //
  537. // Configure the alpha
  538. //
  539. bool is_additive = (SphereShader.Get_Dst_Blend_Func () == ShaderClass::DSTBLEND_ONE);
  540. if (is_additive) {
  541. SphereMaterial->Set_Emissive (Alpha * Color);
  542. } else {
  543. SphereMaterial->Set_Opacity (Alpha);
  544. SphereMaterial->Set_Emissive (Color);
  545. }
  546. // If using Alpha Vector, check to see if it needs updated
  547. if (Flags & USE_ALPHA_VECTOR) {
  548. bool use_inverse = false;
  549. if (Flags & USE_INVERSE_ALPHA) {
  550. use_inverse = true;
  551. }
  552. SphereMeshArray[CurrentLOD].Set_Alpha_Vector( Vector, use_inverse, is_additive );
  553. }
  554. // Camera Align
  555. if (Flags & USE_CAMERA_ALIGN) {
  556. Matrix4 view,ident(true);
  557. DX8Wrapper::Get_Transform(D3DTS_VIEW,view);
  558. Vector4 wpos(Transform[0][3],Transform[1][3],Transform[2][3],1);
  559. Vector4 cpos;
  560. Matrix4::Transform_Vector(view,wpos,&cpos);
  561. Matrix3D tm(0.0f, 1.0f, 0.0f, cpos.X,
  562. 0.0f, 0.0f, 1.0f, cpos.Y,
  563. 1.0f, 0.0f, 0.0f, cpos.Z);
  564. tm.Scale(real_scale);
  565. DX8Wrapper::Set_Transform(D3DTS_WORLD,ident);
  566. DX8Wrapper::Set_Transform(D3DTS_VIEW,tm);
  567. render_sphere();
  568. DX8Wrapper::Set_Transform(D3DTS_VIEW,view);
  569. } else {
  570. DX8Wrapper::Set_Transform(D3DTS_WORLD,temp);
  571. render_sphere();
  572. }
  573. }
  574. }
  575. /***********************************************************************************************
  576. * SphereRenderObjClass::Special_Render -- special render this box (vis) *
  577. * *
  578. * INPUT: *
  579. * *
  580. * OUTPUT: *
  581. * *
  582. * WARNINGS: *
  583. * *
  584. * HISTORY: *
  585. * 1/19/00 gth : Created. *
  586. *=============================================================================================*/
  587. void SphereRenderObjClass::Special_Render(SpecialRenderInfoClass & rinfo)
  588. {
  589. Matrix3D temp(1);
  590. temp.Translate(Transform.Get_Translation());
  591. if (rinfo.RenderType == SpecialRenderInfoClass::RENDER_VIS) {
  592. WWASSERT(rinfo.VisRasterizer != NULL);
  593. rinfo.VisRasterizer->Set_Model_Transform(temp);
  594. vis_render_sphere(rinfo,ObjSpaceCenter,ObjSpaceExtent);
  595. }
  596. }
  597. /***********************************************************************************************
  598. * SphereRenderObjClass::Set_Transform -- set the transform for this box *
  599. * *
  600. * INPUT: *
  601. * *
  602. * OUTPUT: *
  603. * *
  604. * WARNINGS: *
  605. * *
  606. * HISTORY: *
  607. * 1/19/00 gth : Created. *
  608. *=============================================================================================*/
  609. void SphereRenderObjClass::Set_Transform(const Matrix3D &m)
  610. {
  611. RenderObjClass::Set_Transform(m);
  612. update_cached_box();
  613. }
  614. /***********************************************************************************************
  615. * SphereRenderObjClass::Set_Position -- Set the position of this box *
  616. * *
  617. * INPUT: *
  618. * *
  619. * OUTPUT: *
  620. * *
  621. * WARNINGS: *
  622. * *
  623. * HISTORY: *
  624. * 1/19/00 gth : Created. *
  625. *=============================================================================================*/
  626. void SphereRenderObjClass::Set_Position(const Vector3 &v)
  627. {
  628. RenderObjClass::Set_Position(v);
  629. update_cached_box();
  630. }
  631. /***********************************************************************************************
  632. * SphereRenderObjClass::update_cached_box -- update the world-space version of this box *
  633. * *
  634. * INPUT: *
  635. * *
  636. * OUTPUT: *
  637. * *
  638. * WARNINGS: *
  639. * *
  640. * HISTORY: *
  641. * 1/19/00 gth : Created. *
  642. *=============================================================================================*/
  643. void SphereRenderObjClass::update_cached_box(void)
  644. {
  645. CachedBox.Center = Transform.Get_Translation() + ObjSpaceCenter;
  646. CachedBox.Extent = ObjSpaceExtent;
  647. }
  648. /***********************************************************************************************
  649. * SphereRenderObjClass::Get_Obj_Space_Bounding_Sphere -- return the object-space bounding sphe *
  650. * *
  651. * INPUT: *
  652. * *
  653. * OUTPUT: *
  654. * *
  655. * WARNINGS: *
  656. * *
  657. * HISTORY: *
  658. * 1/19/00 gth : Created. *
  659. *=============================================================================================*/
  660. void SphereRenderObjClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  661. {
  662. sphere.Init(ObjSpaceCenter,ObjSpaceExtent.Length());
  663. }
  664. /***********************************************************************************************
  665. * SphereRenderObjClass::Get_Obj_Space_Bounding_Box -- returns the obj-space bounding box *
  666. * *
  667. * INPUT: *
  668. * *
  669. * OUTPUT: *
  670. * *
  671. * WARNINGS: *
  672. * *
  673. * HISTORY: *
  674. * 1/19/00 gth : Created. *
  675. *=============================================================================================*/
  676. void SphereRenderObjClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  677. {
  678. box.Init(ObjSpaceCenter,ObjSpaceExtent);
  679. }
  680. /***********************************************************************************************
  681. * SphereRenderObjClass::Update_Cached_Bounding_Volumes -- Updates world-space bounding volumes *
  682. * *
  683. * INPUT: *
  684. * *
  685. * OUTPUT: *
  686. * *
  687. * WARNINGS: *
  688. * *
  689. * HISTORY: *
  690. * 1/19/00 gth : Created. *
  691. *=============================================================================================*/
  692. void SphereRenderObjClass::Update_Cached_Bounding_Volumes(void) const
  693. {
  694. CachedBoundingBox.Extent.X = ObjSpaceExtent.X * Scale.X;
  695. CachedBoundingBox.Extent.Y = ObjSpaceExtent.Y * Scale.Y;
  696. CachedBoundingBox.Extent.Z = ObjSpaceExtent.Z * Scale.Z;
  697. CachedBoundingSphere.Center = CachedBoundingBox.Center = Get_Position() + ObjSpaceCenter;
  698. CachedBoundingSphere.Radius = CachedBoundingBox.Extent.Length();
  699. Validate_Cached_Bounding_Volumes();
  700. }
  701. /***********************************************************************************************
  702. * SphereRenderObjClass::Get_Default_Color - get the default (or first frame) value *
  703. * *
  704. * INPUT: *
  705. * *
  706. * OUTPUT: *
  707. * *
  708. * WARNINGS: *
  709. * *
  710. * HISTORY: *
  711. * 3/13/2000 pds : Created. *
  712. *=============================================================================================*/
  713. Vector3 SphereRenderObjClass::Get_Default_Color(void) const
  714. {
  715. Vector3 value;
  716. if (ColorChannel.Get_Key_Count () > 0) {
  717. value = ColorChannel.Get_Key (0).Get_Value ();
  718. } else {
  719. value = Color;
  720. }
  721. return value;
  722. }
  723. /***********************************************************************************************
  724. * SphereRenderObjClass::Get_Default_Alpha - get the default (or first frame) value *
  725. * *
  726. * INPUT: *
  727. * *
  728. * OUTPUT: *
  729. * *
  730. * WARNINGS: *
  731. * *
  732. * HISTORY: *
  733. * 3/13/2000 pds : Created. *
  734. *=============================================================================================*/
  735. float SphereRenderObjClass::Get_Default_Alpha(void) const
  736. {
  737. float value;
  738. if (AlphaChannel.Get_Key_Count () > 0) {
  739. value = AlphaChannel.Get_Key (0).Get_Value ();
  740. } else {
  741. value = Alpha;
  742. }
  743. return value;
  744. }
  745. /***********************************************************************************************
  746. * SphereRenderObjClass::Get_Default_Scale - get the default (or first frame) value *
  747. * *
  748. * INPUT: *
  749. * *
  750. * OUTPUT: *
  751. * *
  752. * WARNINGS: *
  753. * *
  754. * HISTORY: *
  755. * 3/13/2000 pds : Created. *
  756. *=============================================================================================*/
  757. Vector3 SphereRenderObjClass::Get_Default_Scale(void) const
  758. {
  759. Vector3 value;
  760. if (ScaleChannel.Get_Key_Count () > 0) {
  761. value = ScaleChannel.Get_Key (0).Get_Value ();
  762. } else {
  763. value = Scale;
  764. }
  765. return value;
  766. }
  767. /***********************************************************************************************
  768. * SphereRenderObjClass::Get_Default_Vector - get the default (or first frame) value *
  769. * *
  770. * INPUT: *
  771. * *
  772. * OUTPUT: *
  773. * *
  774. * WARNINGS: *
  775. * *
  776. * HISTORY: *
  777. * 3/13/2000 pds : Created. *
  778. *=============================================================================================*/
  779. AlphaVectorStruct SphereRenderObjClass::Get_Default_Vector(void) const
  780. {
  781. AlphaVectorStruct value;
  782. if (VectorChannel.Get_Key_Count () > 0) {
  783. value = VectorChannel.Get_Key (0).Get_Value ();
  784. } else {
  785. value = Vector;
  786. }
  787. return value;
  788. }
  789. /***********************************************************************************************
  790. * SphereRenderObjClass::Update_On_Visibilty -- Either starts or stops the animation based on visibility*
  791. * *
  792. * INPUT: *
  793. * *
  794. * OUTPUT: *
  795. * *
  796. * WARNINGS: *
  797. * *
  798. * HISTORY: *
  799. * 4/04/00 pds : Created. *
  800. *=============================================================================================*/
  801. void SphereRenderObjClass::Update_On_Visibilty(void)
  802. {
  803. // Simply start or stop the animation based on
  804. // the visibility state of the primitive.
  805. if (Is_Not_Hidden_At_All () && Is_Animating () == false) {
  806. Start_Animating ();
  807. } else if ((Is_Not_Hidden_At_All () == false) && Is_Animating ()) {
  808. Stop_Animating ();
  809. }
  810. return ;
  811. }
  812. /***********************************************************************************************
  813. * SphereRenderObjClass::animate -- Update Current Display state *
  814. * *
  815. * INPUT: *
  816. * *
  817. * OUTPUT: *
  818. * *
  819. * WARNINGS: *
  820. * *
  821. * HISTORY: *
  822. * 3/07/00 jga : Created. *
  823. *=============================================================================================*/
  824. void SphereRenderObjClass::animate (void)
  825. {
  826. if (Is_Animating ()) {
  827. if ( ColorChannel.Get_Key_Count () > 0 ||
  828. AlphaChannel.Get_Key_Count () > 0 ||
  829. ScaleChannel.Get_Key_Count () > 0 ||
  830. VectorChannel.Get_Key_Count () > 0)
  831. {
  832. //
  833. // Convert from milliseconds to seconds and normalize the time
  834. //
  835. if (AnimDuration > 0) {
  836. float frametime = WW3D::Get_Frame_Time();
  837. frametime = (frametime * 0.001F) / AnimDuration;
  838. anim_time += frametime;
  839. } else {
  840. anim_time = 1.0F;
  841. }
  842. WWASSERT (anim_time >= 0.0F);
  843. if ((Flags & USE_ANIMATION_LOOP) && anim_time > 1.0F) {
  844. anim_time -= 1.0F;
  845. }
  846. if (ColorChannel.Get_Key_Count () > 0) {
  847. Color = ColorChannel.Evaluate (anim_time);
  848. }
  849. if (AlphaChannel.Get_Key_Count () > 0) {
  850. Alpha = AlphaChannel.Evaluate (anim_time);
  851. }
  852. if (ScaleChannel.Get_Key_Count () > 0) {
  853. Scale = ScaleChannel.Evaluate (anim_time);
  854. Update_Cached_Bounding_Volumes ();
  855. }
  856. if (VectorChannel.Get_Key_Count () > 0) {
  857. Vector = VectorChannel.Evaluate (anim_time);
  858. }
  859. }
  860. }
  861. return ;
  862. } // animate
  863. /*
  864. ** SphereLoaderClass Implementation
  865. */
  866. PrototypeClass * SphereLoaderClass::Load_W3D(ChunkLoadClass & cload)
  867. {
  868. SpherePrototypeClass *prototype = new SpherePrototypeClass;
  869. prototype->Load (cload);
  870. return prototype;
  871. }
  872. /*
  873. ** SpherePrototypeClass Implementation
  874. */
  875. SpherePrototypeClass::SpherePrototypeClass (void)
  876. {
  877. ::memset (&Definition, 0, sizeof (Definition));
  878. return ;
  879. }
  880. SpherePrototypeClass::SpherePrototypeClass(SphereRenderObjClass *sphere)
  881. {
  882. ::memset (&Definition, 0, sizeof (Definition));
  883. ::strcpy (Definition.Name, sphere->Get_Name ());
  884. Definition.DefaultAlpha = sphere->Get_Default_Alpha ();
  885. Definition.AnimDuration = sphere->AnimDuration;
  886. Definition.Attributes = sphere->Get_Flags ();
  887. Definition.DefaultVector = sphere->Get_Default_Vector ();
  888. Vector3 def_color = sphere->Get_Default_Color ();
  889. Vector3 def_scale = sphere->Get_Default_Scale ();
  890. W3dUtilityClass::Convert_Vector (def_color, &Definition.DefaultColor);
  891. W3dUtilityClass::Convert_Vector (def_scale, &Definition.DefaultScale);
  892. W3dUtilityClass::Convert_Vector (sphere->Get_Box ().Center, &Definition.Center);
  893. W3dUtilityClass::Convert_Vector (sphere->Get_Box ().Extent, &Definition.Extent);
  894. W3dUtilityClass::Convert_Shader (sphere->SphereShader, &Definition.Shader);
  895. //
  896. // Determine the texture name for this sphere
  897. //
  898. if (sphere->SphereTexture != NULL) {
  899. StringClass name = sphere->SphereTexture->Get_Full_Path();
  900. const char *filename = ::strrchr (name, '\\');
  901. if (filename != NULL) {
  902. filename ++;
  903. } else {
  904. filename = name;
  905. }
  906. ::strcpy (Definition.TextureName, filename);
  907. }
  908. //
  909. // Save the animateable channels
  910. //
  911. ColorChannel = sphere->Peek_Color_Channel ();
  912. AlphaChannel = sphere->Peek_Alpha_Channel ();
  913. ScaleChannel = sphere->Peek_Scale_Channel ();
  914. VectorChannel = sphere->Peek_Vector_Channel ();
  915. return ;
  916. }
  917. SpherePrototypeClass::~SpherePrototypeClass (void)
  918. {
  919. return ;
  920. }
  921. enum
  922. {
  923. CHUNKID_SPHERE_DEF = 1,
  924. CHUNKID_COLOR_CHANNEL,
  925. CHUNKID_ALPHA_CHANNEL,
  926. CHUNKID_SCALE_CHANNEL,
  927. CHUNKID_VECTOR_CHANNEL
  928. };
  929. bool SpherePrototypeClass::Load (ChunkLoadClass &cload)
  930. {
  931. ColorChannel.Reset ();
  932. AlphaChannel.Reset ();
  933. ScaleChannel.Reset ();
  934. VectorChannel.Reset ();
  935. while (cload.Open_Chunk ()) {
  936. switch (cload.Cur_Chunk_ID ()) {
  937. case CHUNKID_SPHERE_DEF:
  938. cload.Read (&Definition, sizeof (Definition));
  939. break;
  940. case CHUNKID_COLOR_CHANNEL:
  941. ColorChannel.Load (cload);
  942. break;
  943. case CHUNKID_ALPHA_CHANNEL:
  944. AlphaChannel.Load (cload);
  945. break;
  946. case CHUNKID_SCALE_CHANNEL:
  947. ScaleChannel.Load (cload);
  948. break;
  949. case CHUNKID_VECTOR_CHANNEL:
  950. VectorChannel.Load (cload);
  951. break;
  952. }
  953. cload.Close_Chunk ();
  954. }
  955. return true;
  956. }
  957. bool SpherePrototypeClass::Save (ChunkSaveClass &csave)
  958. {
  959. csave.Begin_Chunk (W3D_CHUNK_SPHERE);
  960. csave.Begin_Chunk (CHUNKID_SPHERE_DEF);
  961. csave.Write (&Definition, sizeof (Definition));
  962. csave.End_Chunk ();
  963. if (ColorChannel.Get_Key_Count () > 0) {
  964. csave.Begin_Chunk (CHUNKID_COLOR_CHANNEL);
  965. ColorChannel.Save (csave);
  966. csave.End_Chunk ();
  967. }
  968. if (AlphaChannel.Get_Key_Count () > 0) {
  969. csave.Begin_Chunk (CHUNKID_ALPHA_CHANNEL);
  970. AlphaChannel.Save (csave);
  971. csave.End_Chunk ();
  972. }
  973. if (ScaleChannel.Get_Key_Count () > 0) {
  974. csave.Begin_Chunk (CHUNKID_SCALE_CHANNEL);
  975. ScaleChannel.Save (csave);
  976. csave.End_Chunk ();
  977. }
  978. if (VectorChannel.Get_Key_Count () > 0) {
  979. csave.Begin_Chunk (CHUNKID_VECTOR_CHANNEL);
  980. VectorChannel.Save (csave);
  981. csave.End_Chunk ();
  982. }
  983. csave.End_Chunk ();
  984. return true;
  985. }
  986. const char * SpherePrototypeClass::Get_Name(void) const
  987. {
  988. return Definition.Name;
  989. }
  990. int SpherePrototypeClass::Get_Class_ID(void) const
  991. {
  992. return RenderObjClass::CLASSID_SPHERE;
  993. }
  994. RenderObjClass * SpherePrototypeClass::Create(void)
  995. {
  996. //
  997. // Create the new render object
  998. //
  999. SphereRenderObjClass *sphere = new SphereRenderObjClass (Definition);
  1000. //
  1001. // Configure the sphere
  1002. //
  1003. W3dUtilityClass::Convert_Shader (Definition.Shader, &sphere->SphereShader);
  1004. if (WW3DAssetManager::Get_Instance()->Get_Activate_Fog_On_Load()) {
  1005. sphere->SphereShader.Enable_Fog ("SpherePrototypeClass");
  1006. }
  1007. W3dUtilityClass::Convert_Vector (Definition.DefaultColor, &sphere->Color);
  1008. W3dUtilityClass::Convert_Vector (Definition.DefaultScale, &sphere->Scale);
  1009. sphere->Vector = Definition.DefaultVector;
  1010. sphere->Set_Animation_Duration (Definition.AnimDuration);
  1011. sphere->Alpha = Definition.DefaultAlpha;
  1012. sphere->Set_Flags (Definition.Attributes);
  1013. //
  1014. // Initialize the render object with the keyframe arrays
  1015. //
  1016. sphere->Set_Color_Channel (ColorChannel);
  1017. sphere->Set_Alpha_Channel (AlphaChannel);
  1018. sphere->Set_Scale_Channel (ScaleChannel);
  1019. sphere->Set_Vector_Channel (VectorChannel);
  1020. return sphere;
  1021. }
  1022. /*
  1023. ** Global instance of the box loader
  1024. */
  1025. SphereLoaderClass _SphereLoader;
  1026. //
  1027. // Vertices are ordered as such
  1028. // North Pole, stack 1, stack 2, stack x..., SouthPole
  1029. //
  1030. /***********************************************************************************************
  1031. * SphereMeshClass::SphereMeshClass -- Constructor for SphereMesh Geometry *
  1032. * *
  1033. * INPUT: *
  1034. * *
  1035. * OUTPUT: *
  1036. * *
  1037. * WARNINGS: *
  1038. * *
  1039. * HISTORY: *
  1040. * 3/07/00 jga : Created. *
  1041. *=============================================================================================*/
  1042. SphereMeshClass::SphereMeshClass(float radius, int slices, int stacks):
  1043. Radius(radius),
  1044. Slices(slices),
  1045. Stacks(stacks),
  1046. Vertex_ct(0),
  1047. vtx(NULL),
  1048. vtx_normal(NULL),
  1049. vtx_uv(NULL),
  1050. strip_ct(0),
  1051. strip_size(0),
  1052. strips(NULL),
  1053. fan_ct(0),
  1054. fan_size(0),
  1055. fans(NULL),
  1056. face_ct(0),
  1057. tri_poly(NULL),
  1058. inverse_alpha(false)
  1059. {
  1060. // compute # of vertices
  1061. // 1st 2 vertices, represent, north, and south pole (Y - axis)
  1062. Generate(radius, slices, stacks);
  1063. } // SphereMesh Constructor
  1064. /***********************************************************************************************
  1065. * SphereMeshClass::SphereMeshClass -- Constructor for SphereMesh Geometry *
  1066. * *
  1067. * INPUT: *
  1068. * *
  1069. * OUTPUT: *
  1070. * *
  1071. * WARNINGS: *
  1072. * *
  1073. * HISTORY: *
  1074. * 3/07/00 jga : Created. *
  1075. *=============================================================================================*/
  1076. SphereMeshClass::SphereMeshClass(void):
  1077. Radius(0.0f),
  1078. Slices(0),
  1079. Stacks(0),
  1080. Vertex_ct(0),
  1081. vtx(NULL),
  1082. vtx_normal(NULL),
  1083. vtx_uv(NULL),
  1084. strip_ct(0),
  1085. strip_size(0),
  1086. strips(NULL),
  1087. fan_ct(0),
  1088. fan_size(0),
  1089. fans(NULL),
  1090. face_ct(0),
  1091. tri_poly(NULL),
  1092. inverse_alpha(false)
  1093. {
  1094. } // Empty SphereMesh Constructor
  1095. /***********************************************************************************************
  1096. * SphereMeshClass::Set_Alpha_Vector -- Unit Direction Vector, for Alpha hole effects *
  1097. * *
  1098. * INPUT: vector4 v ; Unit Vector pointing into the sphere in object space *
  1099. * .W = Intensity ; Percent Effect *
  1100. * *
  1101. * OUTPUT: *
  1102. * *
  1103. * WARNINGS: *
  1104. * *
  1105. * HISTORY: *
  1106. * 3/07/00 jga : Created. *
  1107. *=============================================================================================*/
  1108. void SphereMeshClass::Set_Alpha_Vector (const AlphaVectorStruct &v, bool inverse, bool is_additive, bool force)
  1109. {
  1110. //
  1111. // Exit if there is nothing to do...
  1112. //
  1113. if ( alpha_vector == v &&
  1114. inverse == inverse_alpha &&
  1115. is_additive == IsAdditive &&
  1116. !force)
  1117. {
  1118. return ;
  1119. }
  1120. inverse_alpha = inverse;
  1121. alpha_vector = v;
  1122. float Intensity = v.intensity;
  1123. assert(Intensity >= 0.0f);
  1124. Vector3 vec = alpha_vector.angle.Rotate_Vector (Vector3 (1, 0, 0));
  1125. if (inverse_alpha) {
  1126. for (int idx=0; idx < Vertex_ct; idx++) {
  1127. float temp;
  1128. temp = vec * vtx_normal[idx];
  1129. temp*= Intensity;
  1130. temp = fabs(temp);
  1131. if (temp > 1.0f) temp = 1.0f;
  1132. Set_DCG (is_additive, idx, temp);
  1133. }
  1134. } else {
  1135. for (int idx=0; idx < Vertex_ct; idx++) {
  1136. float temp;
  1137. temp = vec * vtx_normal[idx];
  1138. temp*= Intensity;
  1139. temp = fabs(temp);
  1140. if (temp > 1.0f) temp = 1.0f;
  1141. Set_DCG (is_additive, idx, 1.0F - temp);
  1142. }
  1143. }
  1144. return ;
  1145. } // Set_Alpha_Vector
  1146. /***********************************************************************************************
  1147. * SphereMeshClass::Generate -- Alloc Memory, and Generate Geometry, for the SphereMesh *
  1148. * *
  1149. * INPUT: *
  1150. * *
  1151. * OUTPUT: *
  1152. * *
  1153. * WARNINGS: *
  1154. * *
  1155. * HISTORY: *
  1156. * 3/07/00 jga : Created. *
  1157. *=============================================================================================*/
  1158. void SphereMeshClass::Generate(float radius, int slices, int stacks)
  1159. {
  1160. // compute # of vertices
  1161. // 1st 2 vertices, represent, north, and south pole (Y - axis)
  1162. Free();
  1163. Slices = slices;
  1164. Stacks = stacks;
  1165. Radius = radius;
  1166. face_ct = (Slices * Stacks * 2);
  1167. Vertex_ct = ((Slices+1) * Stacks) + 2;
  1168. vtx = new Vector3[Vertex_ct];
  1169. vtx_normal = new Vector3[Vertex_ct];
  1170. vtx_uv = new Vector2[Vertex_ct];
  1171. dcg = new Vector4[Vertex_ct];
  1172. Matrix3 mat;
  1173. Vector3 vec(0.0f, 0.0f, radius);
  1174. Vector3 *veclist = vtx;
  1175. Vector2 *uv = vtx_uv;
  1176. *veclist = vec; // assign north pole
  1177. veclist++;
  1178. // Generate Vertex UV coordinates
  1179. //
  1180. // 0,0 _____ 1,0
  1181. // Texture maps UV's look something like this |_____|
  1182. // 0,1 1,1
  1183. //
  1184. //
  1185. uv->U = 0.5f; uv->V = 0.0f; // assign uv for north pole
  1186. uv++;
  1187. for (stacks = 0; stacks < Stacks; stacks++) {
  1188. float stackstep = ((float)stacks + 1) / ((float) Stacks+1);
  1189. float XAxisAngle = WWMATH_PI * stackstep;
  1190. for (slices = 0; slices <= Slices; slices++) {
  1191. float slicestep = ((float)slices) / ((float)Slices);
  1192. float YAxisAngle = (WWMATH_PI * 2.0f) * slicestep;
  1193. mat.Make_Identity();
  1194. mat.Rotate_Z( YAxisAngle );
  1195. mat.Rotate_X( XAxisAngle );
  1196. *veclist = mat * vec;
  1197. veclist++;
  1198. // update UV
  1199. uv->U = slicestep;
  1200. uv->V = stackstep;
  1201. uv++;
  1202. } // for slices
  1203. } // for stacks
  1204. // Assign vertex for south pole;
  1205. *veclist = -1.0f * vec;
  1206. uv->U = 0.5f; uv->V = 1.0f; // uv for south pole
  1207. // Generate Vertex Normals
  1208. Vector3 *src = vtx;
  1209. Vector3 *dst = vtx_normal;
  1210. for(int idx = 0; idx < Vertex_ct; idx++) {
  1211. Vector3 temp;
  1212. temp = src[idx];
  1213. temp.Normalize();
  1214. dst[idx] = temp;
  1215. }
  1216. // Generate Fans for North + south pole
  1217. fan_ct = 2;
  1218. fan_size = Slices+2;
  1219. fans = new int[fan_size * fan_ct];
  1220. // Do Fan #1
  1221. for (int ct = 0; ct < fan_size; ct++) {
  1222. fans[ct] = ct;
  1223. }
  1224. //fans[ct] = 1;
  1225. // Do Fan #2
  1226. int vtx_idx = Vertex_ct - 1;
  1227. for (ct = fan_size; ct < (fan_size * 2); ct++) {
  1228. fans[ct] = vtx_idx;
  1229. vtx_idx--;
  1230. }
  1231. //fans[ct] = Vertex_ct - 2;
  1232. // Generate Strips, for the inbetween stacks
  1233. strip_size = ((Slices+1) * 2);
  1234. strip_ct = Stacks - 1;
  1235. if (strip_ct) {
  1236. strips = new int[strip_size * strip_ct];
  1237. for (stacks = 0; stacks < strip_ct; stacks++) {
  1238. // outer loop for each stack
  1239. int store_idx = stacks * strip_size;
  1240. int base_vtx = 1 + (stacks * (Slices+1));
  1241. int cur_vtx = base_vtx;
  1242. for(ct = 0; ct <= Slices; ct++) {
  1243. strips[store_idx] = cur_vtx + (Slices+1);
  1244. strips[store_idx+1] = cur_vtx;
  1245. cur_vtx++;
  1246. store_idx+=2;
  1247. }
  1248. // Last, special case +2
  1249. //strips[store_idx] = base_vtx + Slices;
  1250. //strips[store_idx+1] = base_vtx;
  1251. }
  1252. }
  1253. // Generate Tri-Poly Indices
  1254. tri_poly = new TriIndex[face_ct]; // 3 indices per triangle
  1255. TriIndex *out = tri_poly;
  1256. int *in;
  1257. for (stacks = 0; stacks < strip_ct; stacks++) {
  1258. in = &strips[strip_size * stacks];
  1259. // Strip to Poly Function
  1260. // IJK
  1261. for(int fidx=0; fidx < (strip_size - 2); fidx++) {
  1262. out->I = in[0];
  1263. out->J = in[1];
  1264. out->K = in[2];
  1265. out++;
  1266. in++;
  1267. fidx++;
  1268. if (fidx >= (strip_size-2)) break;
  1269. out->I = in[0];
  1270. out->J = in[2];
  1271. out->K = in[1];
  1272. out++;
  1273. in++;
  1274. }
  1275. // End Strip to Poly Function
  1276. }
  1277. // Fans to Poly Function
  1278. for(slices = 0; slices < fan_ct; slices++) {
  1279. in = &fans[fan_size * slices];
  1280. int *base_idx = in;
  1281. for (int fidx=0; fidx < (fan_size - 2); fidx++) {
  1282. out->I = base_idx[0];
  1283. out->J = in[2];
  1284. out->K = in[1];
  1285. in++;
  1286. out++;
  1287. }
  1288. }
  1289. // Make Sure ptr is where I expect it to be
  1290. WWASSERT(((int)out) == ((int)(tri_poly + face_ct)));
  1291. //
  1292. // Fill in the DCG array
  1293. //
  1294. Set_Alpha_Vector (alpha_vector, inverse_alpha, IsAdditive, true);
  1295. return ;
  1296. } // Generate
  1297. /***********************************************************************************************
  1298. * SphereMeshClass::~SphereMeshClass -- Destructor *
  1299. * *
  1300. * WARNINGS: *
  1301. * *
  1302. * HISTORY: *
  1303. * 3/07/00 jga : Created. *
  1304. *=============================================================================================*/
  1305. SphereMeshClass::~SphereMeshClass(void)
  1306. {
  1307. Free();
  1308. } // Destructor
  1309. /***********************************************************************************************
  1310. * SphereMeshClass::Free Memory used by geometry for the SphereMesh *
  1311. * *
  1312. * INPUT: *
  1313. * *
  1314. * OUTPUT: *
  1315. * *
  1316. * WARNINGS: *
  1317. * *
  1318. * HISTORY: *
  1319. * 3/07/00 jga : Created. *
  1320. *=============================================================================================*/
  1321. void SphereMeshClass::Free(void)
  1322. {
  1323. if (vtx) delete [] vtx;
  1324. if (vtx_normal) delete [] vtx_normal;
  1325. if (vtx_uv) delete [] vtx_uv;
  1326. if (dcg) delete [] dcg;
  1327. if (strips) delete [] strips;
  1328. if (fans) delete [] fans;
  1329. if (tri_poly) delete [] tri_poly;
  1330. vtx = NULL;
  1331. vtx_normal = NULL;
  1332. vtx_uv = NULL;
  1333. dcg = NULL;
  1334. strips = NULL;
  1335. fans = NULL;
  1336. tri_poly = NULL;
  1337. }
  1338. // EOF - sphereobj.cpp