ringobj.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/ringobj.h $*
  25. * *
  26. * Author:: Jason Andersen *
  27. * *
  28. * $Modtime:: 3/23/01 7:02p $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef RINGOBJ_H
  39. #define RINGOBJ_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 "quat.h"
  47. #include "vector3.h"
  48. #include "vector2.h"
  49. #include "prim_anim.h"
  50. class VertexMaterialClass;
  51. typedef LERPAnimationChannelClass<Vector3> RingColorChannelClass;
  52. typedef LERPAnimationChannelClass<float> RingAlphaChannelClass;
  53. typedef LERPAnimationChannelClass<Vector2> RingScaleChannelClass;
  54. /**
  55. ** W3dRingStruct
  56. ** This structure is used to define a ring object in the W3D file format.
  57. */
  58. struct W3dRingStruct
  59. {
  60. uint32 Version; // file format version
  61. uint32 Attributes; // box attributes (above #define's)
  62. char Name[2*W3D_NAME_LEN]; // name is in the form <containername>.<boxname>
  63. W3dVectorStruct Center; // center of the box
  64. W3dVectorStruct Extent; // extent of the box
  65. float AnimDuration; // Animation time (in seconds)
  66. W3dVectorStruct DefaultColor;
  67. float DefaultAlpha;
  68. Vector2 DefaultInnerScale;
  69. Vector2 DefaultOuterScale;
  70. Vector2 InnerExtent;
  71. Vector2 OuterExtent;
  72. char TextureName[2*W3D_NAME_LEN];
  73. W3dShaderStruct Shader;
  74. int TextureTileCount;
  75. //
  76. // Note this structure is followed by a series of
  77. // W3dRingKeyArrayStruct structures which define the
  78. // variable set of keyframes for each channel
  79. };
  80. // Note: RING_NUM_LOD does not include the NULL LOD.
  81. #define RING_NUM_LOD (20)
  82. #define RING_LOWEST_LOD (10)
  83. #define RING_HIGHEST_LOD (50)
  84. /*
  85. ** RingRenderObjClass: Procedurally generated render rings
  86. **
  87. */
  88. class RingRenderObjClass : public RenderObjClass
  89. {
  90. public:
  91. // These are bit masks, so they should enum 1,2,4,8,10,20,40 etc...
  92. enum RingFlags {
  93. USE_CAMERA_ALIGN = 0x00000001,
  94. USE_ANIMATION_LOOP= 0x00000002,
  95. };
  96. RingRenderObjClass(void);
  97. RingRenderObjClass(const W3dRingStruct & def);
  98. RingRenderObjClass(const RingRenderObjClass & src);
  99. RingRenderObjClass & operator = (const RingRenderObjClass &);
  100. ~RingRenderObjClass(void);
  101. /////////////////////////////////////////////////////////////////////////////
  102. // Render Object Interface
  103. /////////////////////////////////////////////////////////////////////////////
  104. virtual RenderObjClass * Clone(void) const;
  105. virtual int Class_ID(void) const;
  106. virtual void Render(RenderInfoClass & rinfo);
  107. virtual void Special_Render(SpecialRenderInfoClass & rinfo);
  108. virtual void Set_Transform(const Matrix3D &m);
  109. virtual void Set_Position(const Vector3 &v);
  110. virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
  111. virtual void Get_Obj_Space_Bounding_Box(AABoxClass & box) const;
  112. virtual void Prepare_LOD(CameraClass &camera);
  113. virtual void Increment_LOD(void);
  114. virtual void Decrement_LOD(void);
  115. virtual float Get_Cost(void) const;
  116. virtual float Get_Value(void) const;
  117. virtual float Get_Post_Increment_Value(void) const;
  118. virtual void Set_LOD_Level(int lod);
  119. virtual int Get_LOD_Level(void) const;
  120. virtual int Get_LOD_Count(void) const;
  121. virtual void Set_LOD_Bias(float bias) { LODBias = MAX(bias, 0.0f); }
  122. virtual int Calculate_Cost_Value_Arrays(float screen_area, float *values, float *costs) const;
  123. virtual void Scale(float scale);
  124. virtual void Scale(float scalex, float scaley, float scalez);
  125. virtual void Set_Hidden(int onoff) { RenderObjClass::Set_Hidden (onoff); Update_On_Visibilty (); }
  126. virtual void Set_Visible(int onoff) { RenderObjClass::Set_Visible (onoff); Update_On_Visibilty (); }
  127. virtual void Set_Animation_Hidden(int onoff) { RenderObjClass::Set_Animation_Hidden (onoff); Update_On_Visibilty (); }
  128. virtual void Set_Force_Visible(int onoff) { RenderObjClass::Set_Force_Visible (onoff); Update_On_Visibilty (); }
  129. const AABoxClass & Get_Box(void);
  130. virtual int Get_Num_Polys(void) const;
  131. virtual const char *Get_Name(void) const;
  132. virtual void Set_Name(const char * name);
  133. unsigned int Get_Flags(void) {return Flags;}
  134. void Set_Flags(unsigned int flags) { Flags = flags; }
  135. void Set_Flag(unsigned int flag, bool onoff) { Flags &= (~flag); if (onoff) Flags |= flag; }
  136. // Animation access
  137. bool Is_Animating (void) { return IsAnimating; }
  138. void Start_Animating (void) { IsAnimating = true; anim_time = 0; }
  139. void Stop_Animating (void) { IsAnimating = false; anim_time = 0; }
  140. // Texture tiling access
  141. int Get_Texture_Tiling (void) const { return TextureTileCount; }
  142. void Set_Texture_Tiling (int count) { TextureTileCount = count; }
  143. // Current state access
  144. void Set_Color(const Vector3 & color) { Color = color; }
  145. void Set_Alpha(float alpha) { Alpha = alpha; }
  146. void Set_Inner_Scale(const Vector2 & scale) { InnerScale = scale; }
  147. void Set_Outer_Scale(const Vector2 & scale) { OuterScale = scale; }
  148. const Vector3 & Get_Color(void) const { return Color; }
  149. float Get_Alpha(void) const { return Alpha; }
  150. const Vector2 & Get_Inner_Scale(void) const { return InnerScale; }
  151. const Vector2 & Get_Outer_Scale(void) const { return OuterScale; }
  152. Vector3 Get_Default_Color(void) const;
  153. float Get_Default_Alpha(void) const;
  154. Vector2 Get_Default_Inner_Scale(void) const;
  155. Vector2 Get_Default_Outer_Scale(void) const;
  156. // Size/position methods
  157. const Vector2 & Get_Inner_Extent (void) const { return InnerExtent; }
  158. const Vector2 & Get_Outer_Extent (void) const { return OuterExtent; }
  159. void Set_Inner_Extent (const Vector2 &extent);
  160. void Set_Outer_Extent (const Vector2 &extent);
  161. void Set_Local_Center_Extent(const Vector3 & center,const Vector3 & extent);
  162. void Set_Local_Min_Max(const Vector3 & min,const Vector3 & max);
  163. // Texture access
  164. void Set_Texture(TextureClass *tf);
  165. TextureClass * Peek_Texture(void) { return RingTexture; }
  166. ShaderClass & Get_Shader(void) { return RingShader; }
  167. void Set_Shader(ShaderClass &shader) { RingShader = shader; }
  168. // Animation Control
  169. float Get_Animation_Duration(void) const { return AnimDuration; }
  170. void Set_Animation_Duration(float time) { AnimDuration = time; Restart_Animation(); }
  171. void Restart_Animation (void) { anim_time = 0; }
  172. // Animatable channel access
  173. RingColorChannelClass & Get_Color_Channel (void) { return ColorChannel; }
  174. const RingColorChannelClass & Peek_Color_Channel (void) { return ColorChannel; }
  175. RingAlphaChannelClass & Get_Alpha_Channel (void) { return AlphaChannel; }
  176. const RingAlphaChannelClass & Peek_Alpha_Channel (void) { return AlphaChannel; }
  177. RingScaleChannelClass & Get_Inner_Scale_Channel (void) { return InnerScaleChannel; }
  178. const RingScaleChannelClass & Peek_Inner_Scale_Channel (void) { return InnerScaleChannel; }
  179. RingScaleChannelClass & Get_Outer_Scale_Channel (void) { return OuterScaleChannel; }
  180. const RingScaleChannelClass & Peek_Outer_Scale_Channel (void) { return OuterScaleChannel; }
  181. void Set_Color_Channel (const RingColorChannelClass &data) { ColorChannel = data; }
  182. void Set_Alpha_Channel (const RingAlphaChannelClass &data) { AlphaChannel = data; }
  183. void Set_Inner_Scale_Channel (const RingScaleChannelClass &data) { InnerScaleChannel = data; }
  184. void Set_Outer_Scale_Channel (const RingScaleChannelClass &data) { OuterScaleChannel = data; }
  185. protected:
  186. virtual void update_cached_box(void);
  187. void Update_On_Visibilty(void);
  188. // Initialization stuff
  189. void Init_Material (void);
  190. static void Generate_Shared_Mesh_Arrays (void);
  191. // Animation Stuff
  192. void animate(void); // animation update function
  193. float anim_time; // what time in seconds are we in the animation
  194. float AnimDuration;
  195. bool IsAnimating;
  196. // LOD Stuff
  197. void calculate_value_array(float screen_area, float *values) const;
  198. float LODBias;
  199. int CurrentLOD;
  200. float Value[RING_NUM_LOD + 2];// Value array needs # of LODs + 1 (RING_NUM_LOD doesn't include null LOD)
  201. RingColorChannelClass ColorChannel;
  202. RingAlphaChannelClass AlphaChannel;
  203. RingScaleChannelClass InnerScaleChannel;
  204. RingScaleChannelClass OuterScaleChannel;
  205. void update_mesh_data(const Vector3 & center,const Vector3 & extent);
  206. void render_ring(RenderInfoClass & rinfo,const Vector3 & center,const Vector3 & extent);
  207. void vis_render_ring(SpecialRenderInfoClass & rinfo,const Vector3 & center,const Vector3 & extent);
  208. char Name[2*W3D_NAME_LEN];
  209. Vector3 ObjSpaceCenter;
  210. Vector3 ObjSpaceExtent;
  211. Vector2 InnerExtent;
  212. Vector2 OuterExtent;
  213. int TextureTileCount;
  214. // Current State
  215. Vector3 Color;
  216. float Alpha;
  217. Vector2 InnerScale;
  218. Vector2 OuterScale;
  219. // Flags
  220. unsigned int Flags;
  221. VertexMaterialClass *RingMaterial;
  222. ShaderClass RingShader;
  223. TextureClass *RingTexture;
  224. AABoxClass CachedBox;
  225. // Friend classes
  226. friend class RingPrototypeClass;
  227. };
  228. inline void RingRenderObjClass::Set_Inner_Extent (const Vector2 &extent)
  229. {
  230. InnerExtent = extent;
  231. }
  232. inline void RingRenderObjClass::Set_Outer_Extent (const Vector2 &extent)
  233. {
  234. OuterExtent = extent;
  235. ObjSpaceExtent.X = extent.X;
  236. ObjSpaceExtent.Y = extent.Y;
  237. ObjSpaceExtent.Z = 0;
  238. update_cached_box();
  239. }
  240. inline void RingRenderObjClass::Set_Local_Center_Extent(const Vector3 & center,const Vector3 & extent)
  241. {
  242. ObjSpaceCenter = center;
  243. ObjSpaceExtent = extent;
  244. update_cached_box();
  245. }
  246. inline void RingRenderObjClass::Set_Local_Min_Max(const Vector3 & min,const Vector3 & max)
  247. {
  248. ObjSpaceCenter = (max + min) / 2.0f;
  249. ObjSpaceExtent = (max - min) / 2.0f;
  250. update_cached_box();
  251. }
  252. inline const AABoxClass & RingRenderObjClass::Get_Box(void)
  253. {
  254. Validate_Transform();
  255. update_cached_box();
  256. return CachedBox;
  257. }
  258. /*
  259. ** Loader for rings
  260. */
  261. class RingLoaderClass : public PrototypeLoaderClass
  262. {
  263. public:
  264. virtual int Chunk_Type (void) { return W3D_CHUNK_RING; }
  265. virtual PrototypeClass * Load_W3D(ChunkLoadClass & cload);
  266. };
  267. /*
  268. ** Prototype for Ring objects
  269. */
  270. class RingPrototypeClass : public W3DMPO, public PrototypeClass
  271. {
  272. W3DMPO_GLUE(RingPrototypeClass)
  273. public:
  274. RingPrototypeClass (void);
  275. RingPrototypeClass (RingRenderObjClass *ring);
  276. virtual const char * Get_Name(void) const;
  277. virtual int Get_Class_ID(void) const;
  278. virtual RenderObjClass * Create(void);
  279. virtual void DeleteSelf() { delete this; }
  280. bool Load (ChunkLoadClass &cload);
  281. bool Save (ChunkSaveClass &csave);
  282. protected:
  283. ~RingPrototypeClass (void);
  284. private:
  285. W3dRingStruct Definition;
  286. RingColorChannelClass ColorChannel;
  287. RingAlphaChannelClass AlphaChannel;
  288. RingScaleChannelClass InnerScaleChannel;
  289. RingScaleChannelClass OuterScaleChannel;
  290. };
  291. /*
  292. ** Instance of the loader which the asset manager installs
  293. */
  294. extern RingLoaderClass _RingLoader;
  295. #endif // RINGOBJ_H
  296. // EOF - ringobj,h