ww3d.h 18 KB

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