hlod.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/hlod.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 5/25/01 1:37p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef HLOD_H
  39. #define HLOD_H
  40. #ifndef ANIMOBJ_H
  41. #include "animobj.h"
  42. #endif
  43. #ifndef VECTOR_H
  44. #include "vector.h"
  45. #endif
  46. #ifndef SNAPPTS_H
  47. #include "snappts.h"
  48. #endif
  49. #ifndef PROTO_H
  50. #include "proto.h"
  51. #endif
  52. #ifndef W3DERR_H
  53. #include "w3derr.h"
  54. #endif
  55. #ifndef __PROXY_H
  56. #include "proxy.h"
  57. #endif
  58. class DistLODClass;
  59. class HModelClass;
  60. class HLodDefClass;
  61. class HModelDefClass;
  62. class ProxyArrayClass;
  63. /*
  64. HLodClass
  65. This is an hierarchical, animatable level-of-detail model.
  66. */
  67. class HLodClass : public Animatable3DObjClass
  68. {
  69. public:
  70. HLodClass(const HLodClass & src);
  71. HLodClass(const char * name,RenderObjClass ** lods,int count);
  72. HLodClass(const HLodDefClass & def);
  73. HLodClass(const HModelDefClass & def);
  74. HLodClass & operator = (const HLodClass &);
  75. virtual ~HLodClass(void);
  76. /////////////////////////////////////////////////////////////////////////////
  77. // Render Object Interface - Cloning and Identification
  78. /////////////////////////////////////////////////////////////////////////////
  79. virtual RenderObjClass * Clone(void) const;
  80. virtual int Class_ID(void) const { return CLASSID_HLOD; }
  81. virtual int Get_Num_Polys(void) const;
  82. /////////////////////////////////////////////////////////////////////////////
  83. // HLod Interface - Editing and information
  84. /////////////////////////////////////////////////////////////////////////////
  85. virtual void Set_Max_Screen_Size(int lod_index, float size);
  86. virtual float Get_Max_Screen_Size(int lod_index) const;
  87. virtual int Get_Lod_Count(void) const;
  88. virtual int Get_Lod_Model_Count (int lod_index) const;
  89. virtual RenderObjClass * Peek_Lod_Model (int lod_index, int model_index) const;
  90. virtual RenderObjClass * Get_Lod_Model (int lod_index, int model_index) const;
  91. virtual int Get_Lod_Model_Bone (int lod_index, int model_index) const;
  92. virtual int Get_Additional_Model_Count(void) const;
  93. virtual RenderObjClass * Peek_Additional_Model (int model_index) const;
  94. virtual RenderObjClass * Get_Additional_Model (int model_index) const;
  95. virtual int Get_Additional_Model_Bone (int model_index) const;
  96. virtual bool Is_NULL_Lod_Included (void) const;
  97. virtual void Include_NULL_Lod (bool include = true);
  98. /////////////////////////////////////////////////////////////////////////////
  99. // Proxy interface
  100. /////////////////////////////////////////////////////////////////////////////
  101. virtual int Get_Proxy_Count (void) const;
  102. virtual bool Get_Proxy (int index, ProxyClass &proxy) const;
  103. /////////////////////////////////////////////////////////////////////////////
  104. // Render Object Interface - Rendering
  105. /////////////////////////////////////////////////////////////////////////////
  106. virtual void Render(RenderInfoClass & rinfo);
  107. virtual void Special_Render(SpecialRenderInfoClass & rinfo);
  108. /////////////////////////////////////////////////////////////////////////////
  109. // Render Object Interface - "Scene Graph"
  110. /////////////////////////////////////////////////////////////////////////////
  111. virtual void Set_Transform(const Matrix3D &m);
  112. virtual void Set_Position(const Vector3 &v);
  113. virtual void Notify_Added(SceneClass * scene);
  114. virtual void Notify_Removed(SceneClass * scene);
  115. virtual int Get_Num_Sub_Objects(void) const;
  116. virtual RenderObjClass * Get_Sub_Object(int index) const;
  117. virtual int Add_Sub_Object(RenderObjClass * subobj);
  118. virtual int Remove_Sub_Object(RenderObjClass * robj);
  119. virtual int Get_Num_Sub_Objects_On_Bone(int boneindex) const;
  120. virtual RenderObjClass * Get_Sub_Object_On_Bone(int index,int boneindex) const;
  121. virtual int Get_Sub_Object_Bone_Index(RenderObjClass * subobj) const;
  122. virtual int Add_Sub_Object_To_Bone(RenderObjClass * subobj,int bone_index);
  123. /////////////////////////////////////////////////////////////////////////////
  124. // Render Object Interface - Hierarchical Animation
  125. /////////////////////////////////////////////////////////////////////////////
  126. virtual void Set_Animation(void);
  127. virtual void Set_Animation( HAnimClass * motion,
  128. float frame, int anim_mode = ANIM_MODE_MANUAL);
  129. virtual void Set_Animation( HAnimClass * motion0,
  130. float frame0,
  131. HAnimClass * motion1,
  132. float frame1,
  133. float percentage);
  134. virtual void Set_Animation( HAnimComboClass * anim_combo);
  135. /////////////////////////////////////////////////////////////////////////////
  136. // Render Object Interface - Collision Detection, Ray Tracing
  137. /////////////////////////////////////////////////////////////////////////////
  138. virtual bool Cast_Ray(RayCollisionTestClass & raytest);
  139. virtual bool Cast_AABox(AABoxCollisionTestClass & boxtest);
  140. virtual bool Cast_OBBox(OBBoxCollisionTestClass & boxtest);
  141. virtual bool Intersect_AABox(AABoxIntersectionTestClass & boxtest);
  142. virtual bool Intersect_OBBox(OBBoxIntersectionTestClass & boxtest);
  143. /////////////////////////////////////////////////////////////////////////////
  144. // Render Object Interface - Predictive LOD
  145. /////////////////////////////////////////////////////////////////////////////
  146. virtual void Prepare_LOD(CameraClass &camera);
  147. virtual void Recalculate_Static_LOD_Factors(void);
  148. virtual void Increment_LOD(void);
  149. virtual void Decrement_LOD(void);
  150. virtual float Get_Cost(void) const;
  151. virtual float Get_Value(void) const;
  152. virtual float Get_Post_Increment_Value(void) const;
  153. virtual void Set_LOD_Level(int lod);
  154. virtual int Get_LOD_Level(void) const;
  155. virtual int Get_LOD_Count(void) const;
  156. virtual void Set_LOD_Bias(float bias);
  157. virtual int Calculate_Cost_Value_Arrays(float screen_area, float *values, float *costs) const;
  158. virtual RenderObjClass * Get_Current_LOD(void);
  159. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  160. // Render Object Interface - Bounding Volumes
  161. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162. virtual const SphereClass & Get_Bounding_Sphere(void) const;
  163. virtual const AABoxClass & Get_Bounding_Box(void) const;
  164. virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
  165. virtual void Get_Obj_Space_Bounding_Box(AABoxClass & box) const;
  166. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  167. // Render Object Interface - Decals
  168. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  169. virtual void Create_Decal(DecalGeneratorClass * generator);
  170. virtual void Delete_Decal(uint32 decal_id);
  171. /////////////////////////////////////////////////////////////////////////////
  172. // Render Object Interface - Attributes, Options, Properties, etc
  173. /////////////////////////////////////////////////////////////////////////////
  174. // virtual void Set_Texture_Reduction_Factor(float trf);
  175. virtual void Scale(float scale);
  176. virtual void Scale(float scalex, float scaley, float scalez) { }
  177. virtual int Get_Num_Snap_Points(void);
  178. virtual void Get_Snap_Point(int index,Vector3 * set);
  179. virtual void Set_Hidden(int onoff);
  180. // (gth) TESTING DYNAMICALLY SWAPPING SKELETONS!
  181. virtual void Set_HTree(HTreeClass * htree);
  182. protected:
  183. HLodClass(void);
  184. void Free(void);
  185. virtual void Update_Sub_Object_Transforms(void);
  186. virtual void Update_Obj_Space_Bounding_Volumes(void);
  187. void add_lod_model(int lod,RenderObjClass * robj,int boneindex);
  188. protected:
  189. class ModelNodeClass
  190. {
  191. public:
  192. RenderObjClass * Model;
  193. int BoneIndex;
  194. bool operator == (const ModelNodeClass & that) { return (Model == that.Model) && (BoneIndex == that.BoneIndex); }
  195. bool operator != (const ModelNodeClass & that) { return !operator == (that); }
  196. };
  197. class ModelArrayClass : public DynamicVectorClass<ModelNodeClass>
  198. {
  199. public:
  200. ModelArrayClass(void) : MaxScreenSize(NO_MAX_SCREEN_SIZE), NonPixelCost(0.0f),
  201. PixelCostPerArea(0.0f), BenefitFactor(0.0f) {}
  202. float MaxScreenSize; // Maximum screen size for this LOD
  203. float NonPixelCost; // Cost heuristics of LODS (w/o per-pixel cost)
  204. float PixelCostPerArea; // PixelCostPerArea * area(normalized) + NonPixelCost = total Cost
  205. float BenefitFactor; // BenefitFactor * area(normalized) = Benefit
  206. };
  207. // Lod Render Objects, basically one of the LOD Models will be rendered. Typically
  208. // each model in an HLodModel will be a mesh or a "simple" HLod (one with a single LOD)
  209. int LodCount;
  210. int CurLod;
  211. ModelArrayClass * Lod;
  212. //
  213. // An animating heirarchy can use a hidden CLASSID_OBBOX mesh to represent its bounding
  214. // box as it animates. This is the sub object index of that mesh (if it exists).
  215. //
  216. int BoundingBoxIndex;
  217. float * Cost; // Cost array (recalculated every frame)
  218. float * Value; // Value array (recalculated every frame)
  219. // Additional Models, these models have been linked to one of the bones in this
  220. // model. They are all always rendered. They can be HLODs themselves in order
  221. // to implement switching on sub models.
  222. // NOTE: This uses ModelArrayClass for convenience, but MaxScreenSize,
  223. // NonPixelCost, PixelCostPerArea, BenefitFactor are not used here.
  224. ModelArrayClass AdditionalModels;
  225. // possible array of snap points.
  226. SnapPointsClass * SnapPoints;
  227. // possible array of proxy objects (names and bone indexes for application defined usage)
  228. ProxyArrayClass * ProxyArray;
  229. // Current LOD Bias (affects recalculation of the Value array)
  230. float LODBias;
  231. };
  232. /*
  233. ** Loaders for HLodClass
  234. */
  235. class HLodLoaderClass : public PrototypeLoaderClass
  236. {
  237. public:
  238. virtual int Chunk_Type (void) { return W3D_CHUNK_HLOD; }
  239. virtual PrototypeClass * Load_W3D(ChunkLoadClass & cload);
  240. };
  241. /**
  242. ** HLodDefClass
  243. ** This description object is generated when reading a W3D_CHUNK_HLOD. It
  244. ** directly describes the contents of an HLod model.
  245. */
  246. class HLodDefClass
  247. {
  248. public:
  249. HLodDefClass(void);
  250. HLodDefClass(HLodClass &src_lod);
  251. ~HLodDefClass(void);
  252. WW3DErrorType Load_W3D(ChunkLoadClass & cload);
  253. WW3DErrorType Save(ChunkSaveClass & csave);
  254. const char * Get_Name(void) const { return Name; }
  255. void Initialize(HLodClass &src_lod);
  256. protected:
  257. /*
  258. ** Serializtion methods
  259. */
  260. WW3DErrorType Save_Header (ChunkSaveClass &csave);
  261. WW3DErrorType Save_Lod_Array (ChunkSaveClass &csave);
  262. WW3DErrorType Save_Aggregate_Array(ChunkSaveClass & csave);
  263. private:
  264. /*
  265. ** SubObjectArrayClass
  266. ** Describes a level-of-detail in an HLod object. Note that this is
  267. ** a render object which will be exploded when the HLod is constructed (its
  268. ** sub-objects, if any, will be placed into the HLod).
  269. */
  270. class SubObjectArrayClass
  271. {
  272. public:
  273. SubObjectArrayClass(void);
  274. ~SubObjectArrayClass(void);
  275. void Reset(void);
  276. void operator = (const SubObjectArrayClass & that);
  277. bool Load_W3D(ChunkLoadClass & cload);
  278. bool Save_W3D(ChunkSaveClass & csave);
  279. float MaxScreenSize;
  280. int ModelCount;
  281. char ** ModelName; // array of model names
  282. int * BoneIndex; // array of bone indices
  283. };
  284. char * Name;
  285. char * HierarchyTreeName;
  286. int LodCount;
  287. SubObjectArrayClass * Lod;
  288. SubObjectArrayClass Aggregates;
  289. ProxyArrayClass * ProxyArray;
  290. void Free(void);
  291. bool read_header(ChunkLoadClass & cload);
  292. bool read_proxy_array(ChunkLoadClass & cload);
  293. friend class HLodClass;
  294. };
  295. /*
  296. ** Prototype for HLod objects
  297. */
  298. class HLodPrototypeClass : public PrototypeClass
  299. {
  300. public:
  301. HLodPrototypeClass( HLodDefClass *def ) { Definition = def; }
  302. virtual ~HLodPrototypeClass(void) { delete Definition; }
  303. virtual const char * Get_Name(void) const { return Definition->Get_Name(); }
  304. virtual int Get_Class_ID(void) const { return RenderObjClass::CLASSID_HLOD; }
  305. virtual RenderObjClass * Create(void);
  306. HLodDefClass * Get_Definition(void) const { return Definition; }
  307. private:
  308. HLodDefClass * Definition;
  309. };
  310. /*
  311. ** Instance of the loaders which the asset manager install
  312. */
  313. extern HLodLoaderClass _HLodLoader;
  314. #endif