RenderWorld.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __RENDERWORLD_H__
  21. #define __RENDERWORLD_H__
  22. /*
  23. ===============================================================================
  24. Render World
  25. ===============================================================================
  26. */
  27. #define PROC_FILE_EXT "proc"
  28. #define PROC_FILE_ID "mapProcFile003"
  29. // shader parms
  30. const int SHADERPARM_RED = 0;
  31. const int SHADERPARM_GREEN = 1;
  32. const int SHADERPARM_BLUE = 2;
  33. const int SHADERPARM_ALPHA = 3;
  34. const int SHADERPARM_TIMESCALE = 3;
  35. const int SHADERPARM_TIMEOFFSET = 4;
  36. const int SHADERPARM_DIVERSITY = 5; // random between 0.0 and 1.0 for some effects (muzzle flashes, etc)
  37. const int SHADERPARM_MODE = 7; // for selecting which shader passes to enable
  38. const int SHADERPARM_TIME_OF_DEATH = 7; // for the monster skin-burn-away effect enable and time offset
  39. // model parms
  40. const int SHADERPARM_MD3_FRAME = 8;
  41. const int SHADERPARM_MD3_LASTFRAME = 9;
  42. const int SHADERPARM_MD3_BACKLERP = 10;
  43. const int SHADERPARM_BEAM_END_X = 8; // for _beam models
  44. const int SHADERPARM_BEAM_END_Y = 9;
  45. const int SHADERPARM_BEAM_END_Z = 10;
  46. const int SHADERPARM_BEAM_WIDTH = 11;
  47. const int SHADERPARM_SPRITE_WIDTH = 8;
  48. const int SHADERPARM_SPRITE_HEIGHT = 9;
  49. const int SHADERPARM_PARTICLE_STOPTIME = 8; // don't spawn any more particles after this time
  50. // guis
  51. const int MAX_RENDERENTITY_GUI = 3;
  52. // the renderEntity_s::joints array needs to point at enough memory to store the number of joints rounded up to two for SIMD
  53. ID_INLINE int SIMD_ROUND_JOINTS( int numJoints ) { return ( ( numJoints + 1 ) & ~1 ); }
  54. ID_INLINE void SIMD_INIT_LAST_JOINT( idJointMat * joints, int numJoints ) { if ( numJoints & 1 ) { joints[numJoints] = joints[numJoints - 1]; } }
  55. typedef bool(*deferredEntityCallback_t)( renderEntity_s *, const renderView_s * );
  56. typedef struct renderEntity_s {
  57. idRenderModel * hModel; // this can only be null if callback is set
  58. int entityNum;
  59. int bodyId;
  60. // Entities that are expensive to generate, like skeletal models, can be
  61. // deferred until their bounds are found to be in view, in the frustum
  62. // of a shadowing light that is in view, or contacted by a trace / overlay test.
  63. // This is also used to do visual cueing on items in the view
  64. // The renderView may be NULL if the callback is being issued for a non-view related
  65. // source.
  66. // The callback function should clear renderEntity->callback if it doesn't
  67. // want to be called again next time the entity is referenced (ie, if the
  68. // callback has now made the entity valid until the next updateEntity)
  69. idBounds bounds; // only needs to be set for deferred models and md5s
  70. deferredEntityCallback_t callback;
  71. void * callbackData; // used for whatever the callback wants
  72. // player bodies and possibly player shadows should be suppressed in views from
  73. // that player's eyes, but will show up in mirrors and other subviews
  74. // security cameras could suppress their model in their subviews if we add a way
  75. // of specifying a view number for a remoteRenderMap view
  76. int suppressSurfaceInViewID;
  77. int suppressShadowInViewID;
  78. // world models for the player and weapons will not cast shadows from view weapon
  79. // muzzle flashes
  80. int suppressShadowInLightID;
  81. // if non-zero, the surface and shadow (if it casts one)
  82. // will only show up in the specific view, ie: player weapons
  83. int allowSurfaceInViewID;
  84. // positioning
  85. // axis rotation vectors must be unit length for many
  86. // R_LocalToGlobal functions to work, so don't scale models!
  87. // axis vectors are [0] = forward, [1] = left, [2] = up
  88. idVec3 origin;
  89. idMat3 axis;
  90. // texturing
  91. const idMaterial * customShader; // if non-0, all surfaces will use this
  92. const idMaterial * referenceShader; // used so flares can reference the proper light shader
  93. const idDeclSkin * customSkin; // 0 for no remappings
  94. class idSoundEmitter * referenceSound; // for shader sound tables, allowing effects to vary with sounds
  95. float shaderParms[ MAX_ENTITY_SHADER_PARMS ]; // can be used in any way by shader or model generation
  96. // networking: see WriteGUIToSnapshot / ReadGUIFromSnapshot
  97. class idUserInterface * gui[ MAX_RENDERENTITY_GUI ];
  98. struct renderView_s * remoteRenderView; // any remote camera surfaces will use this
  99. int numJoints;
  100. idJointMat * joints; // array of joints that will modify vertices.
  101. // NULL if non-deformable model. NOT freed by renderer
  102. float modelDepthHack; // squash depth range so particle effects don't clip into walls
  103. // options to override surface shader flags (replace with material parameters?)
  104. bool noSelfShadow; // cast shadows onto other objects,but not self
  105. bool noShadow; // no shadow at all
  106. bool noDynamicInteractions; // don't create any light / shadow interactions after
  107. // the level load is completed. This is a performance hack
  108. // for the gigantic outdoor meshes in the monorail map, so
  109. // all the lights in the moving monorail don't touch the meshes
  110. bool weaponDepthHack; // squash depth range so view weapons don't poke into walls
  111. // this automatically implies noShadow
  112. bool noOverlays; // force no overlays on this model
  113. bool skipMotionBlur; // Mask out this object during motion blur
  114. int forceUpdate; // force an update (NOTE: not a bool to keep this struct a multiple of 4 bytes)
  115. int timeGroup;
  116. int xrayIndex;
  117. } renderEntity_t;
  118. typedef struct renderLight_s {
  119. idMat3 axis; // rotation vectors, must be unit length
  120. idVec3 origin;
  121. // if non-zero, the light will not show up in the specific view,
  122. // which may be used if we want to have slightly different muzzle
  123. // flash lights for the player and other views
  124. int suppressLightInViewID;
  125. // if non-zero, the light will only show up in the specific view
  126. // which can allow player gun gui lights and such to not effect everyone
  127. int allowLightInViewID;
  128. // I am sticking the four bools together so there are no unused gaps in
  129. // the padded structure, which could confuse the memcmp that checks for redundant
  130. // updates
  131. bool forceShadows; // Used to override the material parameters
  132. bool noShadows; // (should we replace this with material parameters on the shader?)
  133. bool noSpecular; // (should we replace this with material parameters on the shader?)
  134. bool pointLight; // otherwise a projection light (should probably invert the sense of this, because points are way more common)
  135. bool parallel; // lightCenter gives the direction to the light at infinity
  136. idVec3 lightRadius; // xyz radius for point lights
  137. idVec3 lightCenter; // offset the lighting direction for shading and
  138. // shadows, relative to origin
  139. // frustum definition for projected lights, all reletive to origin
  140. // FIXME: we should probably have real plane equations here, and offer
  141. // a helper function for conversion from this format
  142. idVec3 target;
  143. idVec3 right;
  144. idVec3 up;
  145. idVec3 start;
  146. idVec3 end;
  147. // Dmap will generate an optimized shadow volume named _prelight_<lightName>
  148. // for the light against all the _area* models in the map. The renderer will
  149. // ignore this value if the light has been moved after initial creation
  150. idRenderModel * prelightModel;
  151. // muzzle flash lights will not cast shadows from player and weapon world models
  152. int lightId;
  153. const idMaterial * shader; // NULL = either lights/defaultPointLight or lights/defaultProjectedLight
  154. float shaderParms[MAX_ENTITY_SHADER_PARMS]; // can be used in any way by shader
  155. idSoundEmitter * referenceSound; // for shader sound tables, allowing effects to vary with sounds
  156. } renderLight_t;
  157. typedef struct renderView_s {
  158. // player views will set this to a non-zero integer for model suppress / allow
  159. // subviews (mirrors, cameras, etc) will always clear it to zero
  160. int viewID;
  161. float fov_x, fov_y; // in degrees
  162. idVec3 vieworg; // has already been adjusted for stereo world seperation
  163. idVec3 vieworg_weapon; // has already been adjusted for stereo world seperation
  164. idMat3 viewaxis; // transformation matrix, view looks down the positive X axis
  165. bool cramZNear; // for cinematics, we want to set ZNear much lower
  166. bool flipProjection;
  167. bool forceUpdate; // for an update
  168. // time in milliseconds for shader effects and other time dependent rendering issues
  169. int time[2];
  170. float shaderParms[MAX_GLOBAL_SHADER_PARMS]; // can be used in any way by shader
  171. const idMaterial *globalMaterial; // used to override everything draw
  172. // the viewEyeBuffer may be of a different polarity than stereoScreenSeparation if the eyes have been swapped
  173. int viewEyeBuffer; // -1 = left eye, 1 = right eye, 0 = monoscopic view or GUI
  174. float stereoScreenSeparation; // projection matrix horizontal offset, positive or negative based on camera eye
  175. } renderView_t;
  176. // exitPortal_t is returned by idRenderWorld::GetPortal()
  177. typedef struct {
  178. int areas[2]; // areas connected by this portal
  179. const idWinding * w; // winding points have counter clockwise ordering seen from areas[0]
  180. int blockingBits; // PS_BLOCK_VIEW, PS_BLOCK_AIR, etc
  181. qhandle_t portalHandle;
  182. } exitPortal_t;
  183. // guiPoint_t is returned by idRenderWorld::GuiTrace()
  184. typedef struct {
  185. float x, y; // 0.0 to 1.0 range if trace hit a gui, otherwise -1
  186. int guiId; // id of gui ( 0, 1, or 2 ) that the trace happened against
  187. } guiPoint_t;
  188. // modelTrace_t is for tracing vs. visual geometry
  189. typedef struct modelTrace_s {
  190. float fraction; // fraction of trace completed
  191. idVec3 point; // end point of trace in global space
  192. idVec3 normal; // hit triangle normal vector in global space
  193. const idMaterial * material; // material of hit surface
  194. const renderEntity_t * entity; // render entity that was hit
  195. int jointNumber; // md5 joint nearest to the hit triangle
  196. } modelTrace_t;
  197. static const int NUM_PORTAL_ATTRIBUTES = 3;
  198. typedef enum {
  199. PS_BLOCK_NONE = 0,
  200. PS_BLOCK_VIEW = 1,
  201. PS_BLOCK_LOCATION = 2, // game map location strings often stop in hallways
  202. PS_BLOCK_AIR = 4, // windows between pressurized and unpresurized areas
  203. PS_BLOCK_ALL = (1<<NUM_PORTAL_ATTRIBUTES)-1
  204. } portalConnection_t;
  205. class idRenderWorld {
  206. public:
  207. virtual ~idRenderWorld() {};
  208. // The same render world can be reinitialized as often as desired
  209. // a NULL or empty mapName will create an empty, single area world
  210. virtual bool InitFromMap( const char *mapName ) = 0;
  211. // This fixes a crash when switching between expansion packs in the same game session
  212. // the modelManager gets reset, which deletes all render models without resetting the localModels list inside renderWorldLocal.
  213. // Now we'll have a hook to reset the list from here.
  214. virtual void ResetLocalRenderModels() = 0;
  215. //-------------- Entity and Light Defs -----------------
  216. // entityDefs and lightDefs are added to a given world to determine
  217. // what will be drawn for a rendered scene. Most update work is defered
  218. // until it is determined that it is actually needed for a given view.
  219. virtual qhandle_t AddEntityDef( const renderEntity_t *re ) = 0;
  220. virtual void UpdateEntityDef( qhandle_t entityHandle, const renderEntity_t *re ) = 0;
  221. virtual void FreeEntityDef( qhandle_t entityHandle ) = 0;
  222. virtual const renderEntity_t *GetRenderEntity( qhandle_t entityHandle ) const = 0;
  223. virtual qhandle_t AddLightDef( const renderLight_t *rlight ) = 0;
  224. virtual void UpdateLightDef( qhandle_t lightHandle, const renderLight_t *rlight ) = 0;
  225. virtual void FreeLightDef( qhandle_t lightHandle ) = 0;
  226. virtual const renderLight_t *GetRenderLight( qhandle_t lightHandle ) const = 0;
  227. // Force the generation of all light / surface interactions at the start of a level
  228. // If this isn't called, they will all be dynamically generated
  229. virtual void GenerateAllInteractions() = 0;
  230. // returns true if this area model needs portal sky to draw
  231. virtual bool CheckAreaForPortalSky( int areaNum ) = 0;
  232. //-------------- Decals and Overlays -----------------
  233. // Creates decals on all world surfaces that the winding projects onto.
  234. // The projection origin should be infront of the winding plane.
  235. // The decals are projected onto world geometry between the winding plane and the projection origin.
  236. // The decals are depth faded from the winding plane to a certain distance infront of the
  237. // winding plane and the same distance from the projection origin towards the winding.
  238. virtual void ProjectDecalOntoWorld( const idFixedWinding &winding, const idVec3 &projectionOrigin, const bool parallel, const float fadeDepth, const idMaterial *material, const int startTime ) = 0;
  239. // Creates decals on static models.
  240. virtual void ProjectDecal( qhandle_t entityHandle, const idFixedWinding &winding, const idVec3 &projectionOrigin, const bool parallel, const float fadeDepth, const idMaterial *material, const int startTime ) = 0;
  241. // Creates overlays on dynamic models.
  242. virtual void ProjectOverlay( qhandle_t entityHandle, const idPlane localTextureAxis[2], const idMaterial *material, const int startTime ) = 0;
  243. // Removes all decals and overlays from the given entity def.
  244. virtual void RemoveDecals( qhandle_t entityHandle ) = 0;
  245. //-------------- Scene Rendering -----------------
  246. // some calls to material functions use the current renderview time when servicing cinematics. this function
  247. // ensures that any parms accessed (such as time) are properly set.
  248. virtual void SetRenderView( const renderView_t *renderView ) = 0;
  249. // rendering a scene may actually render multiple subviews for mirrors and portals, and
  250. // may render composite textures for gui console screens and light projections
  251. // It would also be acceptable to render a scene multiple times, for "rear view mirrors", etc
  252. virtual void RenderScene( const renderView_t *renderView ) = 0;
  253. //-------------- Portal Area Information -----------------
  254. // returns the number of portals
  255. virtual int NumPortals() const = 0;
  256. // returns 0 if no portal contacts the bounds
  257. // This is used by the game to identify portals that are contained
  258. // inside doors, so the connection between areas can be topologically
  259. // terminated when the door shuts.
  260. virtual qhandle_t FindPortal( const idBounds &b ) const = 0;
  261. // doors explicitly close off portals when shut
  262. // multiple bits can be set to block multiple things, ie: ( PS_VIEW | PS_LOCATION | PS_AIR )
  263. virtual void SetPortalState( qhandle_t portal, int blockingBits ) = 0;
  264. virtual int GetPortalState( qhandle_t portal ) = 0;
  265. // returns true only if a chain of portals without the given connection bits set
  266. // exists between the two areas (a door doesn't separate them, etc)
  267. virtual bool AreasAreConnected( int areaNum1, int areaNum2, portalConnection_t connection ) const = 0;
  268. // returns the number of portal areas in a map, so game code can build information
  269. // tables for the different areas
  270. virtual int NumAreas() const = 0;
  271. // Will return -1 if the point is not in an area, otherwise
  272. // it will return 0 <= value < NumAreas()
  273. virtual int PointInArea( const idVec3 &point ) const = 0;
  274. // fills the *areas array with the numbers of the areas the bounds cover
  275. // returns the total number of areas the bounds cover
  276. virtual int BoundsInAreas( const idBounds &bounds, int *areas, int maxAreas ) const = 0;
  277. // Used by the sound system to do area flowing
  278. virtual int NumPortalsInArea( int areaNum ) = 0;
  279. // returns one portal from an area
  280. virtual exitPortal_t GetPortal( int areaNum, int portalNum ) = 0;
  281. //-------------- Tracing -----------------
  282. // Checks a ray trace against any gui surfaces in an entity, returning the
  283. // fraction location of the trace on the gui surface, or -1,-1 if no hit.
  284. // This doesn't do any occlusion testing, simply ignoring non-gui surfaces.
  285. // start / end are in global world coordinates.
  286. virtual guiPoint_t GuiTrace( qhandle_t entityHandle, const idVec3 start, const idVec3 end ) const = 0;
  287. // Traces vs the render model, possibly instantiating a dynamic version, and returns true if something was hit
  288. virtual bool ModelTrace( modelTrace_t &trace, qhandle_t entityHandle, const idVec3 &start, const idVec3 &end, const float radius ) const = 0;
  289. // Traces vs the whole rendered world. FIXME: we need some kind of material flags.
  290. virtual bool Trace( modelTrace_t &trace, const idVec3 &start, const idVec3 &end, const float radius, bool skipDynamic = true, bool skipPlayer = false ) const = 0;
  291. // Traces vs the world model bsp tree.
  292. virtual bool FastWorldTrace( modelTrace_t &trace, const idVec3 &start, const idVec3 &end ) const = 0;
  293. //-------------- Demo Control -----------------
  294. // Writes a loadmap command to the demo, and clears archive counters.
  295. virtual void StartWritingDemo( idDemoFile *demo ) = 0;
  296. virtual void StopWritingDemo() = 0;
  297. // Returns true when demoRenderView has been filled in.
  298. // adds/updates/frees entityDefs and lightDefs based on the current demo file
  299. // and returns the renderView to be used to render this frame.
  300. // a demo file may need to be advanced multiple times if the framerate
  301. // is less than 30hz
  302. // demoTimeOffset will be set if a new map load command was processed before
  303. // the next renderScene
  304. virtual bool ProcessDemoCommand( idDemoFile *readDemo, renderView_t *demoRenderView, int *demoTimeOffset ) = 0;
  305. // this is used to regenerate all interactions ( which is currently only done during influences ), there may be a less
  306. // expensive way to do it
  307. virtual void RegenerateWorld() = 0;
  308. //-------------- Debug Visualization -----------------
  309. // Line drawing for debug visualization
  310. virtual void DebugClearLines( int time ) = 0; // a time of 0 will clear all lines and text
  311. virtual void DebugLine( const idVec4 &color, const idVec3 &start, const idVec3 &end, const int lifetime = 0, const bool depthTest = false ) = 0;
  312. virtual void DebugArrow( const idVec4 &color, const idVec3 &start, const idVec3 &end, int size, const int lifetime = 0 ) = 0;
  313. virtual void DebugWinding( const idVec4 &color, const idWinding &w, const idVec3 &origin, const idMat3 &axis, const int lifetime = 0, const bool depthTest = false ) = 0;
  314. virtual void DebugCircle( const idVec4 &color, const idVec3 &origin, const idVec3 &dir, const float radius, const int numSteps, const int lifetime = 0, const bool depthTest = false ) = 0;
  315. virtual void DebugSphere( const idVec4 &color, const idSphere &sphere, const int lifetime = 0, bool depthTest = false ) = 0;
  316. virtual void DebugBounds( const idVec4 &color, const idBounds &bounds, const idVec3 &org = vec3_origin, const int lifetime = 0 ) = 0;
  317. virtual void DebugBox( const idVec4 &color, const idBox &box, const int lifetime = 0 ) = 0;
  318. virtual void DebugCone( const idVec4 &color, const idVec3 &apex, const idVec3 &dir, float radius1, float radius2, const int lifetime = 0 ) = 0;
  319. virtual void DebugAxis( const idVec3 &origin, const idMat3 &axis ) = 0;
  320. // Polygon drawing for debug visualization.
  321. virtual void DebugClearPolygons( int time ) = 0; // a time of 0 will clear all polygons
  322. virtual void DebugPolygon( const idVec4 &color, const idWinding &winding, const int lifeTime = 0, const bool depthTest = false ) = 0;
  323. // Text drawing for debug visualization.
  324. virtual void DrawText( const char *text, const idVec3 &origin, float scale, const idVec4 &color, const idMat3 &viewAxis, const int align = 1, const int lifetime = 0, bool depthTest = false ) = 0;
  325. };
  326. #endif /* !__RENDERWORLD_H__ */