sphereobj.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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.h $*
  25. * *
  26. * Author:: Jason Andersen *
  27. * *
  28. * $Modtime:: 11/24/01 6:21p $*
  29. * *
  30. * $Revision:: 7 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef SPHEREOBJ_H
  39. #define SPHEREOBJ_H
  40. #include "always.h"
  41. #include "rendobj.h"
  42. #include "w3d_file.h"
  43. #include "shader.h"
  44. #include "proto.h"
  45. #include "obbox.h"
  46. #include "vector3i.h"
  47. #include "quat.h"
  48. #include "prim_anim.h"
  49. #include "meshgeometry.h"
  50. class TextureClass;
  51. struct AlphaVectorStruct
  52. {
  53. AlphaVectorStruct (void)
  54. : angle (true),
  55. intensity (1.0F) { }
  56. AlphaVectorStruct (const AlphaVectorStruct &src) { *this = src; }
  57. bool operator== (const AlphaVectorStruct &src) { return (angle.X == src.angle.X) && (angle.Y == src.angle.Y) && (angle.Z == src.angle.Z) && (intensity == src.intensity); }
  58. bool operator!= (const AlphaVectorStruct &src) { return ! operator== (src); }
  59. const AlphaVectorStruct & operator= (const AlphaVectorStruct &src) { angle = src.angle; intensity = src.intensity; return *this; }
  60. Quaternion angle;
  61. float intensity;
  62. };
  63. class AlphaVectorChannel : public PrimitiveAnimationChannelClass<AlphaVectorStruct>
  64. {
  65. public:
  66. AlphaVectorStruct Evaluate (float time)
  67. {
  68. int key_count = m_Data.Count ();
  69. AlphaVectorStruct value = m_Data[key_count - 1].Get_Value ();
  70. //
  71. // Don't interpolate past the last keyframe
  72. //
  73. if (time < m_Data[key_count - 1].Get_Time ()) {
  74. // Check to see if the last key index is valid
  75. if (time < m_Data[m_LastIndex].Get_Time ()) {
  76. m_LastIndex = 0;
  77. }
  78. KeyClass *key1 = &m_Data[m_LastIndex];
  79. KeyClass *key2 = &m_Data[key_count - 1];
  80. //
  81. // Search, using last_key as our starting point
  82. //
  83. for (int keyidx = m_LastIndex; keyidx < (key_count - 1); keyidx ++) {
  84. if (time < m_Data[keyidx+1].Get_Time ()) {
  85. key1 = &m_Data[keyidx];
  86. key2 = &m_Data[keyidx+1];
  87. m_LastIndex = keyidx;
  88. break;
  89. }
  90. }
  91. // Calculate the linear percent between the two keys
  92. float percent = (time - key1->Get_Time ()) / (key2->Get_Time () - key1->Get_Time ());
  93. // Slerp the quaternions and lerp the intensities
  94. value.intensity = (key1->Get_Value ().intensity + (key2->Get_Value ().intensity - key1->Get_Value ().intensity) * percent);
  95. Fast_Slerp (value.angle, key1->Get_Value ().angle, key2->Get_Value ().angle, percent);
  96. }
  97. return value;
  98. }
  99. };
  100. struct W3dSphereStruct
  101. {
  102. uint32 Version; // file format version
  103. uint32 Attributes; // sphere attributes (above #define's)
  104. char Name[2*W3D_NAME_LEN]; // name is in the form <containername>.<spherename>
  105. W3dVectorStruct Center; // center of the sphere
  106. W3dVectorStruct Extent; // extent of the sphere
  107. float AnimDuration; // Animation time (in seconds)
  108. W3dVectorStruct DefaultColor;
  109. float DefaultAlpha;
  110. W3dVectorStruct DefaultScale;
  111. AlphaVectorStruct DefaultVector;
  112. char TextureName[2*W3D_NAME_LEN];
  113. W3dShaderStruct Shader;
  114. //
  115. // Note this structure is followed by a series of
  116. // W3dSphereKeyArrayStruct structures which define the
  117. // variable set of keyframes for each channel.
  118. //
  119. };
  120. typedef LERPAnimationChannelClass<Vector3> SphereColorChannelClass;
  121. typedef LERPAnimationChannelClass<float> SphereAlphaChannelClass;
  122. typedef LERPAnimationChannelClass<Vector3> SphereScaleChannelClass;
  123. typedef AlphaVectorChannel SphereVectorChannelClass;
  124. //typedef AlphaVectorChannel ;
  125. class VertexMaterialClass;
  126. class SphereMeshClass
  127. {
  128. friend class SphereRenderObjClass;
  129. public:
  130. // Constructor
  131. SphereMeshClass(void);
  132. SphereMeshClass(float radius, int slices, int stacks);
  133. // Destructor
  134. ~SphereMeshClass(void);
  135. void Generate (float radius, int slices, int stacks);
  136. int Get_Num_Polys (void) { return face_ct; };
  137. void Set_Alpha_Vector (const AlphaVectorStruct &v, bool inverse, bool is_additive, bool force = false);
  138. private:
  139. void Set_DCG (bool is_additive, int index, float value);
  140. void Free(void);
  141. float Radius;
  142. int Slices;
  143. int Stacks;
  144. int face_ct; // # of faces
  145. int Vertex_ct; // vertex count
  146. Vector3 *vtx; // array of vertices
  147. Vector3 *vtx_normal; // array of vertex normals
  148. Vector2 *vtx_uv; // array of vertex uv coordinates
  149. Vector4 *dcg;
  150. AlphaVectorStruct alpha_vector; // vector last used to computer vtx_alpha array
  151. bool inverse_alpha; // inverse alpha, or not
  152. bool IsAdditive;
  153. int strip_ct; // number of strips
  154. int strip_size; // size of each strip
  155. int *strips; // array of vertex indices for each strip (# stripbs x sizeof strip)
  156. int fan_ct; // number of fans
  157. int fan_size; // size of each fan
  158. int *fans; // array of vertex indices for each fan (# of fans by fan_size)
  159. TriIndex *tri_poly; // array of triangle poly's, vertex indices (can be discard if switched to strips + fans)
  160. };
  161. inline void
  162. SphereMeshClass::Set_DCG (bool is_additive, int index, float value)
  163. {
  164. if (is_additive) {
  165. dcg[index].X = value;
  166. dcg[index].Y = value;
  167. dcg[index].Z = value;
  168. dcg[index].W = 0;
  169. } else {
  170. dcg[index].X = 1.0F;
  171. dcg[index].Y = 1.0F;
  172. dcg[index].Z = 1.0F;
  173. dcg[index].W = value;
  174. }
  175. return ;
  176. }
  177. /*
  178. ** SphereRenderObjClass: Procedurally generated render spheres
  179. **
  180. */
  181. class SphereRenderObjClass : public RenderObjClass
  182. {
  183. public:
  184. // These are bit masks, so they should enum 1,2,4,8,10,20,40 etc...
  185. enum SphereFlags {
  186. USE_ALPHA_VECTOR = 0x00000001,
  187. USE_CAMERA_ALIGN = 0x00000002,
  188. USE_INVERSE_ALPHA = 0x00000004,
  189. USE_ANIMATION_LOOP= 0x00000008,
  190. };
  191. SphereRenderObjClass(void);
  192. SphereRenderObjClass(const W3dSphereStruct & def);
  193. SphereRenderObjClass(const SphereRenderObjClass & src);
  194. SphereRenderObjClass & operator = (const SphereRenderObjClass &);
  195. ~SphereRenderObjClass(void);
  196. /////////////////////////////////////////////////////////////////////////////
  197. // Render Object Interface
  198. /////////////////////////////////////////////////////////////////////////////
  199. virtual RenderObjClass * Clone(void) const;
  200. virtual int Class_ID(void) const;
  201. virtual void Render(RenderInfoClass & rinfo);
  202. virtual void Special_Render(SpecialRenderInfoClass & rinfo);
  203. virtual void Set_Transform(const Matrix3D &m);
  204. virtual void Set_Position(const Vector3 &v);
  205. virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
  206. virtual void Get_Obj_Space_Bounding_Box(AABoxClass & aabox) const;
  207. virtual void Set_Hidden(int onoff) { RenderObjClass::Set_Hidden (onoff); Update_On_Visibilty (); }
  208. virtual void Set_Visible(int onoff) { RenderObjClass::Set_Visible (onoff); Update_On_Visibilty (); }
  209. virtual void Set_Animation_Hidden(int onoff) { RenderObjClass::Set_Animation_Hidden (onoff); Update_On_Visibilty (); }
  210. virtual void Set_Force_Visible(int onoff) { RenderObjClass::Set_Force_Visible (onoff); Update_On_Visibilty (); }
  211. const AABoxClass & Get_Box(void);
  212. virtual int Get_Num_Polys(void) const;
  213. virtual const char * Get_Name(void) const;
  214. virtual void Set_Name(const char * name);
  215. unsigned int Get_Flags(void) { return Flags; }
  216. void Set_Flags(unsigned int flags) { Flags = flags; }
  217. void Set_Flag(unsigned int flag, bool onoff) { Flags &= (~flag); if (onoff) Flags |= flag; }
  218. // Animation access
  219. bool Is_Animating (void) { return IsAnimating; }
  220. void Start_Animating (void) { IsAnimating = true; anim_time = 0; }
  221. void Stop_Animating (void) { IsAnimating = false; anim_time = 0; }
  222. // Current state access
  223. void Set_Color(const Vector3 & color) { Color = color; }
  224. void Set_Alpha(float alpha) { Alpha = alpha; }
  225. void Set_Scale(const Vector3 & scale) { Scale = scale; }
  226. void Set_Vector(const AlphaVectorStruct &v) { Vector = v; }
  227. const Vector3 & Get_Color(void) const { return Color; }
  228. float Get_Alpha(void) const { return Alpha; }
  229. const Vector3 & Get_Scale(void) const { return Scale; }
  230. const AlphaVectorStruct & Get_Vector(void) const { return Vector; }
  231. Vector3 Get_Default_Color(void) const;
  232. float Get_Default_Alpha(void) const;
  233. Vector3 Get_Default_Scale(void) const;
  234. AlphaVectorStruct Get_Default_Vector(void) const;
  235. // Size/position methods
  236. void Set_Extent (const Vector3 &extent);
  237. void Set_Local_Center_Extent(const Vector3 & center,const Vector3 & extent);
  238. void Set_Local_Min_Max(const Vector3 & min,const Vector3 & max);
  239. // Texture access
  240. void Set_Texture(TextureClass *tf);
  241. TextureClass * Peek_Texture(void) {return SphereTexture;}
  242. ShaderClass & Get_Shader(void) {return SphereShader;}
  243. void Set_Shader(ShaderClass &shader) {SphereShader=shader;}
  244. // Animation control
  245. float Get_Animation_Duration (void) const { return AnimDuration; }
  246. void Set_Animation_Duration (float time) { AnimDuration = time; Restart_Animation (); }
  247. void Restart_Animation (void) { anim_time = 0; }
  248. // Animatable channel access
  249. SphereColorChannelClass & Get_Color_Channel (void) { return ColorChannel; }
  250. const SphereColorChannelClass & Peek_Color_Channel (void) { return ColorChannel; }
  251. SphereAlphaChannelClass & Get_Alpha_Channel (void) { return AlphaChannel; }
  252. const SphereAlphaChannelClass & Peek_Alpha_Channel (void) { return AlphaChannel; }
  253. SphereScaleChannelClass & Get_Scale_Channel (void) { return ScaleChannel; }
  254. const SphereScaleChannelClass & Peek_Scale_Channel (void) { return ScaleChannel; }
  255. SphereVectorChannelClass & Get_Vector_Channel (void) { return VectorChannel; }
  256. const SphereVectorChannelClass & Peek_Vector_Channel (void) { return VectorChannel; }
  257. void Set_Color_Channel (const SphereColorChannelClass &data) { ColorChannel = data; }
  258. void Set_Alpha_Channel (const SphereAlphaChannelClass &data) { AlphaChannel = data; }
  259. void Set_Scale_Channel (const SphereScaleChannelClass &data) { ScaleChannel = data; }
  260. void Set_Vector_Channel (const SphereVectorChannelClass &data) { VectorChannel = data; }
  261. protected:
  262. virtual void update_cached_box(void);
  263. virtual void Update_Cached_Bounding_Volumes(void) const;
  264. void Update_On_Visibilty(void);
  265. // Initialization stuff
  266. void Init_Material (void);
  267. static void Generate_Shared_Mesh_Arrays (const AlphaVectorStruct &alphavector);
  268. // Animation Stuff
  269. void animate(void); // animation update function
  270. float anim_time; // what time in seconds are we in the animation
  271. float AnimDuration;
  272. bool IsAnimating;
  273. SphereColorChannelClass ColorChannel;
  274. SphereAlphaChannelClass AlphaChannel;
  275. SphereScaleChannelClass ScaleChannel;
  276. SphereVectorChannelClass VectorChannel;
  277. void update_mesh_data(const Vector3 & center,const Vector3 & extent);
  278. void render_sphere();
  279. void vis_render_sphere(SpecialRenderInfoClass & rinfo,const Vector3 & center,const Vector3 & extent);
  280. char Name[2*W3D_NAME_LEN];
  281. Vector3 ObjSpaceCenter;
  282. Vector3 ObjSpaceExtent;
  283. int CurrentLOD;
  284. // Current State
  285. Vector3 Color;
  286. float Alpha;
  287. Vector3 Scale;
  288. AlphaVectorStruct Vector;
  289. Quaternion Orientation;
  290. // Flags
  291. unsigned int Flags;
  292. VertexMaterialClass *SphereMaterial;
  293. ShaderClass SphereShader;
  294. TextureClass *SphereTexture;
  295. AABoxClass CachedBox;
  296. // Friend classes
  297. friend class SpherePrototypeClass;
  298. };
  299. inline void SphereRenderObjClass::Set_Extent (const Vector3 &extent)
  300. {
  301. ObjSpaceExtent = extent;
  302. update_cached_box();
  303. Update_Cached_Bounding_Volumes();
  304. }
  305. inline void SphereRenderObjClass::Set_Local_Center_Extent(const Vector3 & center,const Vector3 & extent)
  306. {
  307. ObjSpaceCenter = center;
  308. ObjSpaceExtent = extent;
  309. update_cached_box();
  310. }
  311. inline void SphereRenderObjClass::Set_Local_Min_Max(const Vector3 & min,const Vector3 & max)
  312. {
  313. ObjSpaceCenter = (max + min) / 2.0f;
  314. ObjSpaceExtent = (max - min) / 2.0f;
  315. update_cached_box();
  316. }
  317. inline const AABoxClass & SphereRenderObjClass::Get_Box(void)
  318. {
  319. Validate_Transform();
  320. update_cached_box();
  321. return CachedBox;
  322. }
  323. /*
  324. ** Loader for spheres
  325. */
  326. class SphereLoaderClass : public PrototypeLoaderClass
  327. {
  328. public:
  329. virtual int Chunk_Type (void) { return W3D_CHUNK_SPHERE; }
  330. virtual PrototypeClass * Load_W3D(ChunkLoadClass & cload);
  331. };
  332. /*
  333. ** Prototype for Sphere objects
  334. */
  335. class SpherePrototypeClass : public PrototypeClass
  336. {
  337. public:
  338. SpherePrototypeClass (void);
  339. SpherePrototypeClass (SphereRenderObjClass *sphere);
  340. ~SpherePrototypeClass (void);
  341. virtual const char * Get_Name(void) const;
  342. virtual int Get_Class_ID(void) const;
  343. virtual RenderObjClass * Create(void);
  344. bool Load (ChunkLoadClass &cload);
  345. bool Save (ChunkSaveClass &csave);
  346. private:
  347. W3dSphereStruct Definition;
  348. SphereColorChannelClass ColorChannel;
  349. SphereAlphaChannelClass AlphaChannel;
  350. SphereScaleChannelClass ScaleChannel;
  351. SphereVectorChannelClass VectorChannel;
  352. };
  353. /*
  354. ** Instance of the loader which the asset manager installs
  355. */
  356. extern SphereLoaderClass _SphereLoader;
  357. #endif // SPHEREOBJ_H
  358. // EOF - sphereobj,h