decalsys.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/decalsys.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 6/29/01 11:30a $*
  31. * *
  32. * $Revision:: 6 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef DECALSYS_H
  41. #define DECALSYS_H
  42. #include "always.h"
  43. #include "matrix3d.h"
  44. #include "matrix4.h"
  45. #include "obbox.h"
  46. #include "robjlist.h"
  47. #include "matpass.h"
  48. #include "projector.h"
  49. class DecalGeneratorClass;
  50. class DecalMeshClass;
  51. /**
  52. ** DecalSystemClass
  53. ** This is a class that manages creation and destruction of decals in the system. It is
  54. ** meant to be over-ridden for game-specific behaviors.
  55. **
  56. ** Sample Code:
  57. ** 1 - Create the generator. The system gives it a unique id and gives you a clean decal generator
  58. **
  59. ** DecalGeneratorClass * gen = DecalSystem->Lock_Decal_Generator();
  60. ** gen->Set_Transform(tm);
  61. ** gen->Set_Projection(proj_tm);
  62. ** gen->Set_Bounds(OBBox);
  63. **
  64. ** 2 - Apply the generator to any objects that you want it to affect. It wont do anything if it
  65. ** does not overlap any polygons in those objects
  66. **
  67. ** Scene->Collect_Objects(gen->Get_Bounding_Box(),objectlist);
  68. ** for (iterator.First(objectlist); !iterator.Is_Done(); iterator.Next()) {
  69. ** iterator.Peek_Object()->Create_Decal(gen);
  70. ** }
  71. **
  72. ** 3 - Release the generator back to the system. At this point, the system may record which meshes
  73. ** actually generated extra decal polygons for future removal. All of this information will be
  74. ** tied together with the unique 'decal ID' that was assigned to the generator.
  75. **
  76. ** DecalSystem->Unlock_Decal_Generator(gen);
  77. **
  78. */
  79. class DecalSystemClass
  80. {
  81. public:
  82. DecalSystemClass(void);
  83. virtual ~DecalSystemClass(void);
  84. /*
  85. ** Create and release DecalGenerators. Note that this is the point at which the
  86. ** decal system can track "logical" decals. The generator will keep an internal list
  87. ** of all of the render objects which generated decals which you should copy if you
  88. ** want to track them (e.g. if you want to cap the maximum number of active decals and
  89. ** kill the old ones...)
  90. */
  91. virtual DecalGeneratorClass * Lock_Decal_Generator(void);
  92. virtual void Unlock_Decal_Generator(DecalGeneratorClass * generator);
  93. /*
  94. ** When a decal-mesh is destroyed, it must inform the DecalSystem. Otherwise, systems
  95. ** which track decals can get dangling pointers.
  96. */
  97. virtual void Decal_Mesh_Destroyed(uint32 decal_id,DecalMeshClass * mesh) { }
  98. protected:
  99. /*
  100. ** This generates the decal ID when a generator is created. This decal system reroutes this
  101. ** to Generate_Unique_Global_Decal_Id(), but other decal systems may use a different method.
  102. */
  103. virtual uint32 Generate_Decal_Id(void) { return Generate_Unique_Global_Decal_Id(); }
  104. /*
  105. ** Unique ID generation for decals. Not all decal systems have to use
  106. ** this method of generating ids. Some may wish to assign the id as the
  107. ** array index of the logical id or use some other aritrary method.
  108. */
  109. static uint32 Generate_Unique_Global_Decal_Id(void);
  110. static uint32 DecalIDGenerator;
  111. };
  112. /**
  113. ** DecalGeneratorClass
  114. ** This class encapsulates the information needed to generate a decal. It also tracks
  115. ** what meshes actually used it to generate new decal polygons.
  116. */
  117. class DecalGeneratorClass : public ProjectorClass
  118. {
  119. public:
  120. /*
  121. ** All meshes that actually generate decal polygons should register themselves in the
  122. ** list. Then when the decal generation is finished, this list can be copied so that
  123. ** we can come back to those meshes and remove the decals if we want to.
  124. */
  125. void Add_Mesh(RenderObjClass * mesh);
  126. NonRefRenderObjListClass & Get_Mesh_List(void);
  127. /*
  128. ** Decal generator parameters.
  129. ** see ProjectorClass for control over the coordinate system, projection, etc
  130. */
  131. uint32 Get_Decal_ID(void) { return DecalID; }
  132. DecalSystemClass * Peek_Decal_System(void) { return System; }
  133. /*
  134. ** Backface rejection thresh-hold. The dot-product between the projection vector and
  135. ** the normal of each polygon is taken, if the result is greater than this value the polygon
  136. ** is accepted into the decal. Set it to -1 if you want to accept all polygons.
  137. */
  138. void Set_Backface_Threshhold(float val) { BackfaceVal = val; }
  139. float Get_Backface_Threshhold(void) { return BackfaceVal; }
  140. /*
  141. ** Normally, decals are not generated on translucent meshes. This is due to the "floating
  142. ** decals" that you can get on things like trees. The user can override this behavior
  143. ** through the following interface.
  144. */
  145. void Apply_To_Translucent_Meshes(bool onoff) { ApplyToTranslucentMeshes = onoff; }
  146. bool Is_Applied_To_Translucent_Meshes(void) { return ApplyToTranslucentMeshes; }
  147. /*
  148. ** Material parameters: just grab a pointer the material pass and modify it.
  149. ** Remember to release your ref to it when you are done.
  150. */
  151. MaterialPassClass * Get_Material(void) { WWASSERT(Material != NULL); Material->Add_Ref(); return Material; }
  152. /*
  153. ** Decal generation support. Call Set_Mesh_Transform for the mesh you want to add
  154. ** a decal to. Then for each vertex, you can call 'Compute_Texture_Coordinate'
  155. */
  156. void Set_Mesh_Transform(const Matrix3D & tm);
  157. protected:
  158. DecalGeneratorClass(uint32 id,DecalSystemClass * system);
  159. ~DecalGeneratorClass(void);
  160. /*
  161. ** Logical Decal ID, DecalSystem that this generator is tied to
  162. */
  163. DecalSystemClass * System;
  164. uint32 DecalID; // unique ID generated by the DecalSystem
  165. /*
  166. ** Backface Threshhold, Translucent mesh decal enable
  167. */
  168. float BackfaceVal;
  169. bool ApplyToTranslucentMeshes;
  170. /*
  171. ** Material settings
  172. */
  173. MaterialPassClass * Material; // material settings for the decal
  174. /*
  175. ** Results, list of the meshes which actually generated decal polygons for this logical decal.
  176. */
  177. NonRefRenderObjListClass MeshList;
  178. friend class DecalSystemClass;
  179. };
  180. /**
  181. ** MultiFixedPoolDecalSystemClass: This is for decal systems which use several fixed-size decal
  182. ** pools (If more than one pool is used, it is usually to have separate pools for several
  183. ** different classes of decals). Note that here the decal IDs are a combination of the pool and
  184. ** slot ids. (decal ids only have to be unique within a given decal system)
  185. */
  186. class MultiFixedPoolDecalSystemClass : public DecalSystemClass
  187. {
  188. public:
  189. MultiFixedPoolDecalSystemClass(uint32 num_pools, const uint32 *pool_sizes);
  190. MultiFixedPoolDecalSystemClass(const MultiFixedPoolDecalSystemClass & that);
  191. virtual ~MultiFixedPoolDecalSystemClass(void);
  192. // This clears the slot in addition to locking the generator, thus preventing any decal id
  193. // collisions (since any decal previously in that slot will have the same id as the new one).
  194. virtual DecalGeneratorClass * Lock_Decal_Generator(void);
  195. // This will register the decal in the system in the appropriate pool and slot (determined by
  196. // the generator's pool and slot ids), removing any decal which may have been there before.
  197. virtual void Unlock_Decal_Generator(DecalGeneratorClass * generator);
  198. // This notifies the system that a mesh which has decals on it was destroyed - therefore we
  199. // need to remove the mesh from our list to avoid dangling pointers.
  200. virtual void Decal_Mesh_Destroyed(uint32 id,DecalMeshClass * mesh);
  201. // Not part of the DecalSystemClass interface - this function removes any decal currently in
  202. // the given slot in the given pool.
  203. void Clear_Decal_Slot(uint32 pool_id, uint32 slot_id);
  204. // This one removes all decals in a given pool.
  205. void Clear_Pool(uint32 pool_id);
  206. // And this one removes all decals in the system.
  207. void Clear_All_Decals(void);
  208. protected:
  209. /*
  210. ** This generates the decal ID when a generator is created. This decal system generates the
  211. ** decal ID from a pool ID and slot ID which are part of the state of the system so someone
  212. ** can set them before calling Lock_Decal_Generator() (which is where this function is called).
  213. ** We do it this way to avoid needing to override Lock_Decal_Generator().
  214. */
  215. virtual uint32 Generate_Decal_Id(void) { return encode_decal_id(Generator_PoolID, Generator_SlotID); }
  216. uint32 Generator_PoolID; // These should be set before calling Lock_Decal_Generator()
  217. uint32 Generator_SlotID; // These should be set before calling Lock_Decal_Generator()
  218. class LogicalDecalClass;
  219. // Get a reference to the logical decal at the given pool and slot id (performs range chacking)
  220. LogicalDecalClass & find_logical_decal(uint32 pool_id, uint32 slot_id);
  221. // Get a reference to the logical decal with the given decal id
  222. LogicalDecalClass & find_logical_decal(uint32 decal_id);
  223. // The decal ids are formed so that the upper 16 bits are equal to the pool id and the lower
  224. // 16 bits are equal to the slot id.
  225. static uint32 encode_decal_id(uint32 pool_id, uint32 slot_id) { return (slot_id & 0xFFFF) | (pool_id << 16); }
  226. static void decode_decal_id(uint32 decal_id, uint32 & pool_id, uint32 & slot_id) { slot_id = decal_id & 0xFFFF; pool_id = decal_id >> 16; }
  227. // A class to store the meshes to which the decal has been applied (so that they can be removed when needed)
  228. class LogicalDecalClass
  229. {
  230. public:
  231. LogicalDecalClass(void);
  232. ~LogicalDecalClass(void);
  233. void Set(DecalGeneratorClass * generator);
  234. // Just clears any existing logical decal information, leaving the decal empty.
  235. void Clear(uint32 decal_id);
  236. NonRefRenderObjListClass MeshList;
  237. };
  238. class LogicalDecalPoolClass
  239. {
  240. public:
  241. LogicalDecalPoolClass(void);
  242. ~LogicalDecalPoolClass(void);
  243. void Initialize(uint32 size);
  244. LogicalDecalClass * Array;
  245. uint32 Size;
  246. };
  247. LogicalDecalPoolClass * Pools;
  248. uint32 PoolCount;
  249. };
  250. #endif //DECALSYS_H