dazzle.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #if defined(_MSC_VER)
  19. #pragma once
  20. #endif
  21. #ifndef DAZZLE_H
  22. #define DAZZLE_H
  23. #include "always.h"
  24. #include "vector3.h"
  25. #include "matrix3d.h"
  26. #include "rendobj.h"
  27. #include "wwstring.h"
  28. #include "proto.h"
  29. #include "w3derr.h"
  30. #include "shader.h"
  31. #include "matrix4.h"
  32. class CameraClass;
  33. class DazzleVisibilityClass;
  34. struct VertexFormatXYZNDUV2;
  35. class DazzleInitClass
  36. {
  37. public:
  38. unsigned type;
  39. bool use_camera_translation;
  40. StringClass primary_texture_name;
  41. StringClass secondary_texture_name;
  42. StringClass lensflare_name;
  43. float halo_intensity;
  44. float halo_scale_x;
  45. float halo_scale_y;
  46. float dazzle_size_pow;
  47. float dazzle_intensity_pow;
  48. float dazzle_intensity;
  49. float dazzle_area;
  50. float dazzle_direction_area;
  51. Vector3 dazzle_direction;
  52. Vector3 dazzle_test_color;
  53. Vector3 dazzle_color;
  54. Vector3 halo_color;
  55. float dazzle_scale_x;
  56. float dazzle_scale_y;
  57. float fadeout_start;
  58. float fadeout_end;
  59. float size_optimization_limit;
  60. float history_weight;
  61. float radius;
  62. float blink_period;
  63. float blink_on_time;
  64. };
  65. class LensflareInitClass
  66. {
  67. public:
  68. LensflareInitClass()
  69. :
  70. flare_locations(0),
  71. flare_sizes(0),
  72. flare_colors(0),
  73. flare_uv(0)
  74. {
  75. }
  76. LensflareInitClass(const LensflareInitClass& lic)
  77. :
  78. type(lic.type),
  79. texture_name(lic.texture_name),
  80. flare_count(lic.flare_count),
  81. flare_locations(0),
  82. flare_sizes(0),
  83. flare_colors(0),
  84. flare_uv(0)
  85. {
  86. if (flare_count) {
  87. flare_locations=new float[flare_count];
  88. memcpy(flare_locations,lic.flare_locations,sizeof(float)*flare_count);
  89. flare_sizes=new float[flare_count];
  90. memcpy(flare_sizes,lic.flare_sizes,sizeof(float)*flare_count);
  91. flare_colors=new Vector3[flare_count];
  92. memcpy(flare_colors,lic.flare_colors,sizeof(Vector3)*flare_count);
  93. flare_uv=new Vector4[flare_count];
  94. memcpy(flare_uv,lic.flare_uv,sizeof(Vector4)*flare_count);
  95. }
  96. }
  97. ~LensflareInitClass()
  98. {
  99. delete[] flare_locations;
  100. delete[] flare_sizes;
  101. delete[] flare_colors;
  102. delete[] flare_uv;
  103. }
  104. unsigned type;
  105. StringClass texture_name;
  106. int flare_count;
  107. float* flare_locations;
  108. float* flare_sizes;
  109. Vector3* flare_colors;
  110. Vector4* flare_uv;
  111. };
  112. class DazzleRenderObjClass;
  113. class DazzleLayerClass;
  114. class DazzleTypeClass
  115. {
  116. friend DazzleRenderObjClass;
  117. friend DazzleLayerClass;
  118. TextureClass* primary_texture;
  119. TextureClass* secondary_texture;
  120. DazzleInitClass ic;
  121. float fadeout_end_sqr;
  122. float fadeout_start_sqr;
  123. StringClass name;
  124. unsigned dazzle_test_color_integer;
  125. unsigned dazzle_test_mask_integer;
  126. unsigned lensflare_id;
  127. ShaderClass dazzle_shader;
  128. ShaderClass halo_shader;
  129. float radius;
  130. DazzleTypeClass(const DazzleInitClass& is);
  131. virtual ~DazzleTypeClass();
  132. public:
  133. virtual void Calculate_Intensities(
  134. float& dazzle_intensity,
  135. float& dazzle_size,
  136. float& halo_intensity,
  137. const Vector3& camera_dir,
  138. const Vector3& dazzle_dir,
  139. float distance) const;
  140. void Set_Dazzle_Shader(const ShaderClass& s); // Set shader for the dazzle type
  141. void Set_Halo_Shader(const ShaderClass& s); // Set shader for the dazzle type
  142. TextureClass* Get_Dazzle_Texture();
  143. TextureClass* Get_Halo_Texture();
  144. };
  145. // The DazzleLayerClass is for all the dazzles being rendered with a given
  146. // group of camera settings: for example, different scenes may use different
  147. // z-buffer settings and in such a case each scene should have a dazzle layer
  148. // associated with it. (In some special cases a scene may have more than one
  149. // dazzle layer). A dazzle layer contains visible and invisible lists for
  150. // each dazzle type. During rendering each dazzle is put on the correct list
  151. // (a "current dazzle layer" static variable is set before rendering the
  152. // appropriate scenes to ensure this). After all scenes are rendered, the
  153. // dazzle layers are rendered one by one with the correct camera settings.
  154. // NOTE: dazzle layers must be constructed AFTER all the dazzle types have
  155. // been initialized, since the constructor needs to know how many dazzle types
  156. // there are.
  157. class DazzleLayerClass {
  158. friend DazzleRenderObjClass;
  159. public:
  160. DazzleLayerClass(void);
  161. ~DazzleLayerClass(void);
  162. // Render all dazzles in this layer (DazzleRenderObj::Render() only sets visibility)
  163. void Render(CameraClass* camera);
  164. private:
  165. virtual int Get_Visible_Item_Count(unsigned int type) const; // Return visible item count
  166. // virtual void Get_Visible_Item_Locations(unsigned int type, Vector3* locations) const; // Copy locations of visible items to buffer
  167. virtual void Clear_Visible_List(unsigned int type);
  168. // We have an array of visible lists (one for each dazzle type).
  169. DazzleRenderObjClass** visible_lists;
  170. };
  171. class LensflareTypeClass
  172. {
  173. friend DazzleLayerClass;
  174. friend DazzleRenderObjClass;
  175. TextureClass* texture;
  176. LensflareInitClass lic;
  177. StringClass name;
  178. LensflareTypeClass(const LensflareInitClass& is);
  179. virtual ~LensflareTypeClass();
  180. public:
  181. TextureClass* Get_Texture();
  182. void Generate_Vertex_Buffers(
  183. VertexFormatXYZNDUV2* vertex,
  184. int& vertex_count,
  185. float screen_x_scale,
  186. float screen_y_scale,
  187. float dazzle_intensity,
  188. const Vector4& transformed_location);
  189. void Render_Arrays(
  190. const Vector4* vertex_coordinates,
  191. const Vector2* uv_coordinates,
  192. const Vector3* color,
  193. int vertex_count,
  194. int halo_vertex_count,
  195. const Vector2* texture_coordinates);
  196. };
  197. class INIClass;
  198. class DazzleRenderObjClass : public RenderObjClass
  199. {
  200. friend DazzleLayerClass;
  201. DazzleRenderObjClass * succ;
  202. unsigned type;
  203. float current_dazzle_intensity;
  204. float current_dazzle_size;
  205. float current_halo_intensity;
  206. float current_distance;
  207. Vector4 transformed_loc;
  208. Vector3 current_vloc;
  209. Vector3 current_dir;
  210. Vector3 dazzle_color;
  211. Vector3 halo_color;
  212. float lensflare_intensity;
  213. float visibility;
  214. bool on_list; // This is used to avoid insterting a dazzle into a list twice.
  215. float radius; // Used to cast rays against
  216. unsigned int creation_time;
  217. static bool _dazzle_rendering_enabled;
  218. // static void Draw_Debug_Dazzle(int idx);
  219. void vis_render_dazzle(SpecialRenderInfoClass & rinfo);
  220. void Render_Dazzle(CameraClass* camera);
  221. public:
  222. DazzleRenderObjClass(unsigned type);
  223. DazzleRenderObjClass(const char* type_name);
  224. DazzleRenderObjClass(const DazzleRenderObjClass & src);
  225. DazzleRenderObjClass & operator = (const DazzleRenderObjClass &);
  226. DazzleRenderObjClass* Succ() { return succ; }
  227. const DazzleRenderObjClass* Succ() const { return succ; }
  228. /////////////////////////////////////////////////////////////////////////////
  229. // Render Object Interface
  230. /////////////////////////////////////////////////////////////////////////////
  231. virtual RenderObjClass * Clone(void) const;
  232. virtual int Class_ID(void) const { return CLASSID_DAZZLE; }
  233. virtual void Render(RenderInfoClass & rinfo);
  234. virtual void Special_Render(SpecialRenderInfoClass & rinfo);
  235. virtual void Set_Transform(const Matrix3D &m);
  236. virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
  237. virtual void Get_Obj_Space_Bounding_Box(AABoxClass & box) const;
  238. virtual void Scale(float scale) { radius*=scale; };
  239. void Set_Dazzle_Color(const Vector3& col) { dazzle_color=col; }
  240. void Set_Halo_Color(const Vector3& col) { halo_color=col; }
  241. void Set_Lensflare_Intensity (float intensity) {lensflare_intensity=intensity;}
  242. unsigned int Get_Dazzle_Type(void) { return type; }
  243. // Usually, a DazzleRenderObj adds itself to the appropriate visible list
  244. // (determined by the current layer) when it is rendered. This does not
  245. // work for dazzles with "camera transform off", since they are located in
  246. // camera space and the standard worldspace visibility algo will give
  247. // unpredictable results for them (they may never have a Render() call).
  248. // So for these dazzles, you need to call Set_Layer() after constructing
  249. // them (this is instead of putting them in a scene). This function adds
  250. // the dazzle to the appropriate visible list. NOTE: It is also called
  251. // internally by the Render() function.
  252. void Set_Layer(DazzleLayerClass *layer);
  253. // Persistant object save-load interface
  254. // Dazzles save their "dazzle-type" and transform
  255. virtual const PersistFactoryClass & Get_Factory (void) const;
  256. // Set the static "current layer" variable. This variable is used in the
  257. // Render() call so that the dazzle knows which list to add itself to if
  258. // it is visible. This function must be called before rendering the
  259. // scene(s) in which the dazzles are present.
  260. static void Set_Current_Dazzle_Layer(DazzleLayerClass *layer);
  261. static void Init_Type(const DazzleInitClass& i);
  262. static void Init_Lensflare(const LensflareInitClass& i);
  263. static void Init_From_INI(const INIClass* ini);
  264. static unsigned Get_Type_ID(const char* name); // Return the ID of type with given name, or INT_MAX if failed
  265. static const char * Get_Type_Name(unsigned int id); // Return the name of the type with the given ID
  266. static DazzleTypeClass* Get_Type_Class(unsigned id); // Return dazzle type class pointer, or NULL if not found
  267. // The pointer is NOT refcounted - all types are deinitialised
  268. // when exiting the level.
  269. static unsigned Get_Lensflare_ID(const char* name); // Return the ID of lensflare with given name, or INT_MAX if failed
  270. static LensflareTypeClass* Get_Lensflare_Class(unsigned id); // Return lensflare type class pointer, or NULL if not found
  271. static void Deinit();
  272. // Install a class derived from DazzleVisibilityClass to add app-specific
  273. // visibility determination. The default behavior will ask the scene which
  274. // the dazzle is a member of to compute its visibility.
  275. static void Install_Dazzle_Visibility_Handler(const DazzleVisibilityClass * visibility_handler);
  276. // Globally disable/enable dazzle rendering
  277. static void Enable_Dazzle_Rendering(bool onoff) { _dazzle_rendering_enabled = onoff; }
  278. static bool Is_Dazzle_Rendering_Enabled(void) { return _dazzle_rendering_enabled; }
  279. };
  280. /**
  281. ** DazzleVisibilityClass
  282. ** The user should derive a class from DazzleVisibilityClass and implement an app-specific
  283. ** dazzle visibility test. Renegade will use ray-casting to determine visibility. The
  284. ** default visibility handler will query the scene which the dazzle is contained in.
  285. */
  286. class DazzleVisibilityClass
  287. {
  288. public:
  289. virtual float Compute_Dazzle_Visibility( RenderInfoClass & rinfo,
  290. DazzleRenderObjClass * dazzle,
  291. const Vector3 & point) const;
  292. };
  293. /**
  294. ** DazzlePrototypeClass
  295. ** This description object is generated when reading a W3D_CHUNK_DAZZLE. It stores the
  296. ** information needed to construct a particular instance of a dazzle. Prototypes are
  297. ** stored in the asset manager and used to construct render objects when needed.
  298. */
  299. class DazzlePrototypeClass : public PrototypeClass
  300. {
  301. public:
  302. DazzlePrototypeClass(void) : DazzleType(0) { }
  303. virtual ~DazzlePrototypeClass(void) { }
  304. virtual const char * Get_Name(void) const { return Name; }
  305. virtual int Get_Class_ID(void) const { return RenderObjClass::CLASSID_DAZZLE; }
  306. virtual RenderObjClass * Create(void);
  307. WW3DErrorType Load_W3D(ChunkLoadClass & cload);
  308. private:
  309. StringClass Name;
  310. int DazzleType;
  311. };
  312. /**
  313. ** DazzleLoaderClass
  314. ** An instance of this class is registered with the asset manager and handles loading W3D_CHUNK_DAZZLE.
  315. ** It creates DazzlePrototypes from the data in the chunk.
  316. */
  317. class DazzleLoaderClass : public PrototypeLoaderClass
  318. {
  319. public:
  320. DazzleLoaderClass(void) { }
  321. ~DazzleLoaderClass(void) { }
  322. virtual int Chunk_Type(void) { return W3D_CHUNK_DAZZLE; }
  323. virtual PrototypeClass * Load_W3D(ChunkLoadClass & cload);
  324. };
  325. extern DazzleLoaderClass _DazzleLoader;
  326. #endif