ww3d.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/ww3d.h $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 1/02/02 4:17p $*
  29. * *
  30. * $Revision:: 42 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef WW3D_H
  39. #define WW3D_H
  40. #include "always.h"
  41. #include "vector3.h"
  42. #include "layer.h"
  43. #include "w3derr.h"
  44. #include "robjlist.h"
  45. class SceneClass;
  46. class CameraClass;
  47. class ShaderClass;
  48. class DX8Wrapper;
  49. struct RenderStatistics;
  50. class FrameGrabClass;
  51. class VertexMaterialClass;
  52. class ExtraMaterialPassClass;
  53. class RenderInfoClass;
  54. class RenderDeviceDescClass;
  55. class StringClass;
  56. class LightEnvironmentClass;
  57. class MaterialPassClass;
  58. #define MESH_RENDER_SNAPSHOT_ENABLED
  59. #define SNAPSHOT_SAY(x) if (WW3D::Is_Snapshot_Activated()) { WWDEBUG_SAY(x); }
  60. //#define SNAPSHOT_SAY(x)
  61. /**
  62. ** WW3D
  63. **
  64. ** This is the collection of static functions and data which initialize and
  65. ** control the behavior of the WW3D library.
  66. */
  67. class WW3D
  68. {
  69. public:
  70. enum PrelitModeEnum {
  71. PRELIT_MODE_VERTEX,
  72. PRELIT_MODE_LIGHTMAP_MULTI_PASS,
  73. PRELIT_MODE_LIGHTMAP_MULTI_TEXTURE
  74. };
  75. enum MeshDrawModeEnum {
  76. MESH_DRAW_MODE_OLD,
  77. MESH_DRAW_MODE_NEW,
  78. MESH_DRAW_MODE_DEBUG_DRAW,
  79. MESH_DRAW_MODE_DEBUG_CLIP,
  80. MESH_DRAW_MODE_DEBUG_BOX,
  81. MESH_DRAW_MODE_NONE,
  82. MESH_DRAW_MODE_DX8_ONLY
  83. };
  84. enum NPatchesGapFillingModeEnum {
  85. NPATCHES_GAP_FILLING_DISABLED,
  86. NPATCHES_GAP_FILLING_ENABLED,
  87. NPATCHES_GAP_FILLING_FORCE
  88. };
  89. static WW3DErrorType Init(void * hwnd, char *defaultpal = NULL, bool lite = false);
  90. static WW3DErrorType Shutdown(void);
  91. static bool Is_Initted(void) { return IsInitted; }
  92. static const int Get_Render_Device_Count(void);
  93. static const char * Get_Render_Device_Name(int device_index);
  94. static const RenderDeviceDescClass & Get_Render_Device_Desc(int device = -1);
  95. static int Get_Render_Device(void);
  96. static WW3DErrorType Set_Render_Device( int dev=-1, int resx=-1, int resy=-1, int bits=-1, int windowed=-1, bool resize_window = false );
  97. static WW3DErrorType Set_Render_Device( const char *dev_name, int resx=-1, int resy=-1, int bits=-1, int windowed=-1, bool resize_window = false );
  98. static WW3DErrorType Set_Next_Render_Device(void);
  99. static WW3DErrorType Set_Any_Render_Device( void );
  100. static void Get_Pixel_Center(float &x, float &y);
  101. static void Get_Render_Target_Resolution(int & set_w,int & set_h,int & get_bits,bool & get_windowed);
  102. static void Get_Device_Resolution(int & set_w,int & set_h,int & get_bits,bool & get_windowed);
  103. static WW3DErrorType Set_Device_Resolution(int w=-1,int h=-1,int bits=-1,int windowed=-1, bool resize_window=false );
  104. static bool Is_Windowed( void );
  105. static WW3DErrorType Toggle_Windowed ( void );
  106. static void Set_Window( void *hwnd );
  107. static void * Get_Window( void );
  108. static WW3DErrorType On_Activate_App( void );
  109. static WW3DErrorType On_Deactivate_App( void );
  110. static WW3DErrorType Registry_Save_Render_Device( const char * sub_key );
  111. static WW3DErrorType Registry_Save_Render_Device( const char * sub_key, int device, int width, int height, int depth, bool windowed, int texture_depth );
  112. static WW3DErrorType Registry_Load_Render_Device( const char * sub_key, bool resize_window = false );
  113. static bool Registry_Load_Render_Device( const char * sub_key, char *device, int device_len, int &width, int &height, int &depth, int &windowed, int& texture_depth);
  114. // 0 = bilinear, 1 = trilinear, 2 = anisotropic
  115. static void Set_Texture_Filter(int filter);
  116. static int Get_Texture_Filter() { return TextureFilter; }
  117. /*
  118. ** Rendering functions
  119. ** Each frame should be bracketed by a Begin_Render and End_Render call. Between these two calls you will
  120. ** normally render scenes. The render function which accepts a single render object is implemented for
  121. ** special cases like generating a shadow texture for an object. Basically this function will have the
  122. ** entire scene rendering overhead.
  123. */
  124. static WW3DErrorType Begin_Render(bool clear = false,bool clearz = true,const Vector3 & color = Vector3(0,0,0), void(*network_callback)(void) = NULL);
  125. static WW3DErrorType Render(const LayerListClass & layerlist);
  126. static WW3DErrorType Render(const LayerClass & layer);
  127. static WW3DErrorType Render(SceneClass * scene,CameraClass * cam,bool clear = false,bool clearz = false,const Vector3 & color = Vector3(0,0,0));
  128. static WW3DErrorType Render(RenderObjClass & obj,RenderInfoClass & rinfo);
  129. static void Flush(RenderInfoClass & rinfo); // NOTE: "normal" usage should *NEVER* require the user to call this function
  130. static WW3DErrorType End_Render(bool flip_frame = true);
  131. static void Flip_To_Primary(void);
  132. /*
  133. ** Timing
  134. ** By calling the Sync function, the application can move the ww3d library time forward. This
  135. ** will control things like animated uv-offset mappers and render object animations.
  136. */
  137. static void Sync( unsigned int sync_time );
  138. static unsigned int Get_Sync_Time(void) { return SyncTime; }
  139. static unsigned int Get_Frame_Time(void) { return SyncTime - PreviousSyncTime; }
  140. static unsigned int Get_Frame_Count(void) { return FrameCount; }
  141. static unsigned int Get_Last_Frame_Poly_Count(void);
  142. static unsigned int Get_Last_Frame_Vertex_Count(void);
  143. /*
  144. ** Screen/Movie capturing
  145. ** These functions allow you to create screenshots and movies.
  146. */
  147. static void Make_Screen_Shot( const char * filename = "ScreenShot");
  148. static void Start_Movie_Capture( const char * filename_base = "Movie", float frame_rate = 15);
  149. static void Stop_Movie_Capture( void);
  150. static void Toggle_Movie_Capture( const char * filename_base = "Movie", float frame_rate = 15);
  151. static void Start_Single_Frame_Movie_Capture(const char *filename_base = "Frames");
  152. static void Capture_Next_Movie_Frame();
  153. static void Update_Movie_Capture( void);
  154. static float Get_Movie_Capture_Frame_Rate( void);
  155. static void Pause_Movie(bool mode);
  156. static bool Is_Movie_Paused();
  157. static bool Is_Recording_Next_Frame();
  158. static bool Is_Movie_Ready();
  159. /*
  160. ** Set_Ext_Swap_Interval - how many vertical retraces to wait before flipping frames
  161. ** Get_Ext_Swap_Interval - what is our current setting for the swap interval?
  162. */
  163. static void Set_Ext_Swap_Interval(long swap);
  164. static long Get_Ext_Swap_Interval(void);
  165. /*
  166. ** Texture Reduction - all currently loaded textures can be de-resed on the fly
  167. ** by passing in a non-unit value to Set_Texture_Reduction. Passing in 2 causes
  168. ** all textures to be half their normal resolution. Passing in 3 causes them to
  169. ** be cut in half twice, etc
  170. */
  171. static void Set_Texture_Reduction( int value );
  172. static int Get_Texture_Reduction( void );
  173. static void _Invalidate_Mesh_Cache();
  174. static void _Invalidate_Textures();
  175. static void Set_Thumbnail_Enabled(bool b) { ThumbnailEnabled=b; }
  176. static bool Get_Thumbnail_Enabled() { return ThumbnailEnabled; }
  177. static void Enable_Sorting(bool onoff);
  178. static bool Is_Sorting_Enabled(void) { return IsSortingEnabled; }
  179. static void Set_Screen_UV_Bias( bool onoff ) { IsScreenUVBiased = onoff; }
  180. static bool Is_Screen_UV_Biased( void ) { return IsScreenUVBiased; }
  181. static void Set_Collision_Box_Display_Mask(int mask);
  182. static int Get_Collision_Box_Display_Mask(void);
  183. static void Set_Default_Native_Screen_Size(float dnss) { DefaultNativeScreenSize = dnss; }
  184. static float Get_Default_Native_Screen_Size(void) { return DefaultNativeScreenSize; }
  185. static void Normalize_Coordinates(int x, int y, float &fx, float &fy); // convert pixel coordinates to 0..1 screen coordinates
  186. static VertexMaterialClass * Peek_Default_Debug_Material(void);
  187. static ShaderClass Peek_Default_Debug_Shader(void);
  188. static ShaderClass Peek_Backface_Debug_Shader(void);
  189. static ShaderClass Peek_Lightmap_Debug_Shader(void);
  190. static void Set_Prelit_Mode (PrelitModeEnum mode) { PrelitMode = mode; }
  191. static PrelitModeEnum Get_Prelit_Mode () { return (PrelitMode); }
  192. static bool Supports_Prelit_Mode (PrelitModeEnum mode) { return (true); }
  193. static void Expose_Prelit (bool onoff) { ExposePrelit = onoff; }
  194. static bool Expose_Prelit () { return (ExposePrelit); }
  195. static void Set_Texture_Bitdepth(int bitdepth);
  196. static int Get_Texture_Bitdepth();
  197. static void Set_Mesh_Draw_Mode (MeshDrawModeEnum mode) { MeshDrawMode = mode; }
  198. static MeshDrawModeEnum Get_Mesh_Draw_Mode () { return (MeshDrawMode); }
  199. static void Set_NPatches_Gap_Filling_Mode (NPatchesGapFillingModeEnum mode);
  200. static NPatchesGapFillingModeEnum Get_NPatches_Gap_Filling_Mode () { return (NPatchesGapFillingMode); }
  201. static void Set_NPatches_Level(unsigned level);
  202. static unsigned Get_NPatches_Level() { return NPatchesLevel; }
  203. static void Enable_Texturing(bool b);
  204. static bool Is_Texturing_Enabled() { return IsTexturingEnabled; }
  205. static int Get_Last_Frame_Memory_Allocation_Count() { return LastFrameMemoryAllocations; }
  206. static int Get_Last_Frame_Memory_Free_Count() { return LastFrameMemoryFrees; }
  207. /*
  208. ** Decal control
  209. ** These global settings can control whether decals are rendered at all and
  210. ** at what distance to stop rendering/creating decals
  211. */
  212. static void Enable_Decals(bool onoff) { AreDecalsEnabled = onoff; }
  213. static bool Are_Decals_Enabled(void) { return AreDecalsEnabled; }
  214. static void Set_Decal_Rejection_Distance(float d) { DecalRejectionDistance = d; }
  215. static float Get_Decal_Rejection_Distance(void) { return DecalRejectionDistance; }
  216. /*
  217. ** Static sort lists. The ability to temporarily set a different static
  218. ** sort list from the default one and a min/max sort list range is for
  219. ** specialised uses (such as pipctuire-in-picture windows which need to
  220. ** sort at a certain sort level). After this override is called, the
  221. ** default sort list must be restored.
  222. */
  223. static void Enable_Static_Sort_Lists(bool onoff) { AreStaticSortListsEnabled = onoff; }
  224. static bool Are_Static_Sort_Lists_Enabled(void) { return AreStaticSortListsEnabled; }
  225. static void Enable_Munge_Sort_On_Load(bool onoff) { MungeSortOnLoad=onoff; }
  226. static bool Is_Munge_Sort_On_Load_Enabled(void) { return MungeSortOnLoad; }
  227. static void Add_To_Static_Sort_List(RenderObjClass *robj, unsigned int sort_level);
  228. static void Render_And_Clear_Static_Sort_Lists(RenderInfoClass & rinfo);
  229. static void Override_Current_Static_Sort_Lists(RefRenderObjListClass *sort_list, unsigned int min_sort, unsigned int max_sort);
  230. static void Reset_Current_Static_Sort_Lists_To_Default(void);
  231. static bool Is_Snapshot_Activated() { return SnapshotActivated; }
  232. static void Activate_Snapshot(bool b) { SnapshotActivated=b; }
  233. // These clock all the time under user control, and are used to update
  234. // Stats.UserStat* when performance sampling is enabled.
  235. static long UserStat0;
  236. static long UserStat1;
  237. static long UserStat2;
  238. private:
  239. enum
  240. {
  241. DEFAULT_RESOLUTION_WIDTH = 640,
  242. DEFAULT_RESOLUTION_HEIGHT = 480,
  243. DEFAULT_BIT_DEPTH = 16
  244. };
  245. static void Read_Gerd_Render_Device_Description(RenderDeviceDescClass &desc);
  246. static void Update_Pixel_Center(void);
  247. static void Allocate_Debug_Resources(void);
  248. static void Release_Debug_Resources(void);
  249. // Timing info:
  250. // The absolute synchronized frame time (in milliseconds) supplied by the
  251. // application at the start of every frame. Note that wraparound cases
  252. // etc. need to be considered.
  253. static unsigned int SyncTime;
  254. // The previously set absolute sync time - this is used to get the interval between
  255. // the most recently set sync time and the previous one. Assuming the
  256. // application sets sync time at the start of every frame, this represents
  257. // the frame interval.
  258. static unsigned int PreviousSyncTime;
  259. static float PixelCenterX;
  260. static float PixelCenterY;
  261. static bool IsInitted;
  262. static bool IsRendering;
  263. static bool IsCapturing;
  264. static bool IsSortingEnabled;
  265. static bool IsScreenUVBiased;
  266. static bool IsBackfaceDebugEnabled;
  267. static bool AreDecalsEnabled;
  268. static float DecalRejectionDistance;
  269. static bool AreStaticSortListsEnabled;
  270. static bool MungeSortOnLoad;
  271. static FrameGrabClass * Movie;
  272. static bool PauseRecord;
  273. static bool RecordNextFrame;
  274. static int FrameCount;
  275. static VertexMaterialClass * DefaultDebugMaterial;
  276. static VertexMaterialClass * BackfaceDebugMaterial;
  277. static ShaderClass DefaultDebugShader;
  278. static ShaderClass LightmapDebugShader;
  279. static PrelitModeEnum PrelitMode;
  280. static bool ExposePrelit;
  281. static int TextureFilter;
  282. static bool SnapshotActivated;
  283. static bool ThumbnailEnabled;
  284. static MeshDrawModeEnum MeshDrawMode;
  285. static NPatchesGapFillingModeEnum NPatchesGapFillingMode;
  286. static unsigned NPatchesLevel;
  287. static bool IsTexturingEnabled;
  288. static bool Lite;
  289. // This is the default native screen size which will be set for each
  290. // RenderObject on construction. The native screen size is the screen size
  291. // at which the object was designed to be viewed, and it is used in the
  292. // texture resizing algorithm (may be used in future for other things).
  293. // If the default is overriden, it will usually be in the asset manager
  294. // post-load callback.
  295. static float DefaultNativeScreenSize;
  296. // For meshes which have a static sorting order. These will get drawn
  297. // after opaque meshes and before normally sorted meshes. The 'current'
  298. // pointer is so the application can temporarily set a different set of
  299. // static sort lists to be used temporarily. This and the min/max sort
  300. // levels are for specialised uses.
  301. static RefRenderObjListClass *DefaultStaticSortLists;
  302. static RefRenderObjListClass *CurrentStaticSortLists;
  303. static unsigned int MinStaticSortLevel;
  304. static unsigned int MaxStaticSortLevel;
  305. // Memory allocation statistics
  306. static int LastFrameMemoryAllocations;
  307. static int LastFrameMemoryFrees;
  308. };
  309. /*
  310. ** RenderStatistics
  311. ** This struct holds the results of a performance sampling. The WW3D object returns
  312. ** its statistics packaged up in one of these structures.
  313. */
  314. struct RenderStatistics
  315. {
  316. // General statistics
  317. double ElapsedSeconds;
  318. int FramesRendered;
  319. // Geometry engine statistics
  320. double TrianglesReceived;
  321. double TrianglesSubmitted;
  322. double TrianglesSorted;
  323. double VerticesReceived;
  324. double VerticesSubmitted;
  325. // State change statistics
  326. double ViewStateChanges;
  327. double DrawStateChanges;
  328. double TextureChanges;
  329. double TextureParameterChanges;
  330. double TexturesCreated;
  331. double PaletteChanges;
  332. double ShaderChanges;
  333. double DrawCommands;
  334. double TrianglesClipRemoved;
  335. double TrianglesClipCreated;
  336. double DeviceDriverCalls;
  337. // Rendering device statistics
  338. double TextureTransfers;
  339. double PixelsDrawn;
  340. double PixelsRejected;
  341. // Surface cache statistics
  342. long Hits;
  343. long Misses;
  344. long Insertions;
  345. long Removals;
  346. long MemUsed;
  347. long MaxMemory;
  348. // User stats (can be used to see how often a function is called, etc.)
  349. long UserStat0;
  350. long UserStat1;
  351. long UserStat2;
  352. };
  353. #endif