texproject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/texproject.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Kenny Mitchell *
  29. * *
  30. * $Modtime:: 06/26/02 4:04p $*
  31. * *
  32. * $Revision:: 8 $*
  33. * *
  34. * 06/27/02 KM Render to shadow buffer texture support *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #if defined(_MSC_VER)
  39. #pragma once
  40. #endif
  41. #ifndef TEXPROJECT_H
  42. #define TEXPROJECT_H
  43. #include "always.h"
  44. #include "matrix3d.h"
  45. #include "matrix4.h"
  46. #include "obbox.h"
  47. #include "matpass.h"
  48. #include "matrixmapper.h"
  49. #include "cullsys.h"
  50. #include "multilist.h"
  51. #include "projector.h"
  52. class SpecialRenderInfoClass;
  53. class RenderObjClass;
  54. class MaterialPassClass;
  55. class SurfaceClass;
  56. class TextureClass;
  57. class ZTextureClass;
  58. /**
  59. ** TexProjectClass
  60. ** This class is used to project textures onto object in the world. It contains
  61. ** a pointer to the texture to be projected, a bounding volume for the projection,
  62. ** and a material pass which can perform the projection.
  63. **
  64. ** Design Goals:
  65. ** - Texture Projectors should be easy to place in the world and move around.
  66. ** solution: Store the bounding volume and any other parameters in a coordinate
  67. ** system local to the projector. Update the world-space cache whenever needed.
  68. ** The automatically generated shadow texture projectors are a special case subset
  69. ** of the general texture projection code. For this reason, a local bounding
  70. ** volume will be stored and its world-space equivalent will be updated whenever
  71. ** needed.
  72. **
  73. ** - Texture projectors need to be compatible with the culling systems.
  74. ** solution: inherit the Cullable interface.
  75. **
  76. ** - Texture projectors need to be quickly pulled in and out of many lists during
  77. ** the rendering process.
  78. ** solution: list system similar to PhysListClass? I really need to templatize this.
  79. ** - done: MultiListClass!
  80. **
  81. ** - Dynamic Texture projectors need to be updated in stages. Update/recompute only the
  82. ** bounding volume first, then defer computing the actual texture until it is
  83. ** determined that the volume falls into the frustum and is applied to at least one
  84. ** object.
  85. ** solution: Code the bounding volume/projection paramter generation separate from
  86. ** the texture generation code. A derived texture projection object. Let texture
  87. ** projectors know about the object they are projecting so that they can have that
  88. ** object rendered from the desired viewpoint. Need a 'Dirty' flag and a pointer to
  89. ** a 'parent' object. Perhaps separate this code into a ShadowProjectClass since shadows
  90. ** are the only case I can think of that need the ability to re-render their 'parent'
  91. ** object. Everything else should be a static texture-map either generated 'artistically'
  92. ** or generated off-line.
  93. **
  94. */
  95. class TexProjectClass : public ProjectorClass, public CullableClass, public MultiListObjectClass
  96. {
  97. public:
  98. TexProjectClass(void);
  99. virtual ~TexProjectClass(void);
  100. /*
  101. ** Material settings
  102. */
  103. void Set_Texture_Size(int size);
  104. int Get_Texture_Size(void);
  105. void Init_Multiplicative(void);
  106. void Init_Additive(void);
  107. void Set_Intensity(float intensity,bool immediate = false); // 1.0 = on 100%, 0.0 = 'off'
  108. float Get_Intensity(void);
  109. bool Is_Intensity_Zero(void);
  110. void Set_Attenuation(float attenuation); // 1.0 = on, 0.0 = off
  111. float Get_Attenuation(void);
  112. void Enable_Attenuation(bool onoff);
  113. bool Is_Attenuation_Enabled(void);
  114. MaterialPassClass * Peek_Material_Pass(void);
  115. /*
  116. ** Options
  117. */
  118. void Enable_Affect_Dynamic_Objects(bool onoff) { Set_Flag(AFFECT_DYNAMIC_OBJS,onoff); }
  119. bool Is_Affect_Dynamic_Objects_Enabled(void) { return Get_Flag(AFFECT_DYNAMIC_OBJS); }
  120. void Enable_Affect_Static_Objects(bool onoff) { Set_Flag(AFFECT_STATIC_OBJS,onoff); }
  121. bool Is_Affect_Static_Objects_Enabled(void) { return Get_Flag(AFFECT_STATIC_OBJS); }
  122. void Enable_Depth_Gradient(bool onoff);
  123. bool Is_Depth_Gradient_Enabled(bool onoff);
  124. /*
  125. ** Manual initialization of a TexProjectClass
  126. ** 1 - call ProjectorClass::Set_Transform
  127. ** 2 - call Set_Projection_Perspective -or- Set_Projection_Ortho
  128. ** 3 - call Set_Texture.
  129. */
  130. virtual void Set_Perspective_Projection(float hfov,float vfov,float znear,float zfar);
  131. virtual void Set_Ortho_Projection(float xmin,float xmax,float ymin,float ymax,float znear,float zfar);
  132. void Set_Texture(TextureClass * texture);
  133. TextureClass * Get_Texture(void) const;
  134. TextureClass * Peek_Texture(void) const;
  135. void Set_DepthStencilBuffer(ZTextureClass* ztex);
  136. ZTextureClass* Get_DepthStencilBuffer() const;
  137. ZTextureClass* Peek_DepthStencilBuffer() const;
  138. /*
  139. ** Automatic initialization of a TexProjectClass.
  140. ** First set up your projection parameters, give the projector a render target, then call Compute_Texture
  141. */
  142. bool Compute_Perspective_Projection(RenderObjClass * obj,const Vector3 & lightpos,float znear=-1.0f,float zfar=-1.0f);
  143. bool Compute_Perspective_Projection(const AABoxClass & obj_box,const Matrix3D & tm,const Vector3 & lightpos,float znear=-1.0f,float zfar=-1.0f);
  144. bool Compute_Ortho_Projection(RenderObjClass * obj,const Vector3 & lightdir,float znear=-1.0f,float zfar=-1.0f);
  145. bool Compute_Ortho_Projection(const AABoxClass & obj_box,const Matrix3D & tm,const Vector3 & lightdir,float znear=-1.0f,float zfar=-1.0f);
  146. bool Needs_Render_Target(void);
  147. void Set_Render_Target(TextureClass* render_target, ZTextureClass* ztarget=NULL);
  148. TextureClass* Peek_Render_Target(TextureClass** rtarget=NULL, ZTextureClass** ztarget=NULL);
  149. bool Compute_Texture(RenderObjClass * model,SpecialRenderInfoClass * context);
  150. /*
  151. ** Prep for rendering, called by the scene prior to usage.
  152. */
  153. virtual void Pre_Render_Update(const Matrix3D & camera);
  154. /*
  155. ** virtual interface for getting the pointer of the object that generated this shadow.
  156. ** defaults to returning NULL. This is implemented by some derived classes and used by
  157. ** the system to prevent a projection from being applied to the object that generated
  158. ** the projection...
  159. ** (gth) feels kludgy, this got a little messy when I moved this code into WW3D from WWPhys
  160. */
  161. virtual void * Get_Projection_Object_ID(void) const { return NULL; }
  162. protected:
  163. void Set_Flag(uint32 flag,bool onoff);
  164. bool Get_Flag(uint32 flag) const;
  165. virtual void Update_WS_Bounding_Volume(void);
  166. void Configure_Camera(CameraClass & camera);
  167. enum FlagsType
  168. {
  169. PERSPECTIVE = 0x00000001, // PERSPECTIVE or ORTHO
  170. ADDITIVE = 0x00000002, // ADDITIVE or MULTIPLICATIVE
  171. TEXTURE_DIRTY = 0x00000004, // used by derived DynTexProjectClass
  172. VOLATILE = 0x00000008, // this is a volatile texture.
  173. ATTENUATE = 0x00000010, // this projector should be attenuated based on distance to viewer
  174. AFFECT_DYNAMIC_OBJS = 0x00000020, // this projector affects dynamic objects
  175. AFFECT_STATIC_OBJS = 0x00000040, // this projector affects static objects
  176. USE_DEPTH_GRADIENT = 0x00000080, // fade the projection as a function of depth
  177. HAS_RENDER_TARGET = 0x00000100, // the texture we have can be a render target
  178. SIZE_MASK = 0xFFF00000, // desired texture size stored in upper 3 nibbles
  179. SIZE_SHIFT = 20,
  180. DEFAULT_FLAGS = ATTENUATE | AFFECT_DYNAMIC_OBJS | AFFECT_STATIC_OBJS
  181. };
  182. uint32 Flags;
  183. /*
  184. ** Intensity Controls
  185. */
  186. float DesiredIntensity; // last input desired intensity.
  187. float Intensity; // basic shadow intensity. value between 0 and 1
  188. float Attenuation; // attenuation factor based on distance from camera. value between 0 and 1
  189. /*
  190. ** Material pass to be added to any object that intersects the volume
  191. */
  192. MaterialPassClass * MaterialPass;
  193. MatrixMapperClass * Mapper1;
  194. TextureClass * RenderTarget;
  195. ZTextureClass* DepthStencilTarget;
  196. /*
  197. ** I have to remember all of these values so that I can properly initialize a CameraClass
  198. ** when we do render-to-texture. Some day we will merge CameraClass and ProjectorClass.
  199. */
  200. float HFov; // horizontal fov (for perspective mode)
  201. float VFov; // vertical fov (for perspective mode)
  202. float XMin; // left x clip plane (for ortho)
  203. float XMax; // right x clip plane (for ortho)
  204. float YMin; // bottom y clip plane (for ortho)
  205. float YMax; // top y clip plane (for ortho)
  206. float ZNear,ZFar; // z clip planes (for both modes)
  207. };
  208. /*
  209. ** Texture Projector Lists
  210. */
  211. typedef RefMultiListClass<TexProjectClass> TexProjListClass;
  212. typedef RefMultiListIterator<TexProjectClass> TexProjListIterator;
  213. #endif