sphereobj.cpp 74 KB

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