Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
9.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef RENDER3DMS_H
  8. #define RENDER3DMS_H
  9. #pragma once
  10. #include "Render.h"
  11. #include "mathlib/Vector4D.h"
  12. #include "utlpriorityqueue.h"
  13. #include "mapclass.h"
  14. #include "lpreview_thread.h"
  15. #include "shaderapi/ishaderapi.h"
  16. //
  17. // Size of the buffer used for picking. See glSelectBuffer for documention on
  18. // the contents of the selection buffer.
  19. //
  20. #define SELECTION_BUFFER_SIZE 50
  21. //
  22. // Size of the texture cache. THis is the maximum number of unique textures that
  23. // a map can refer to and still render properly in the editor.
  24. //
  25. #define TEXTURE_CACHE_SIZE 2048
  26. //
  27. // Maximum number of objects that can be kept in the list of objects to render last.
  28. //
  29. #define MAX_RENDER_LAST_OBJECTS 256
  30. //
  31. // Maximum number of hits that can be returned by ObjectsAt.
  32. //
  33. #define MAX_PICK_HITS 512
  34. class BoundBox;
  35. class CCamera;
  36. class CCullTreeNode;
  37. class CMapClass;
  38. class CMapDoc;
  39. class CMapFace;
  40. class CMapInstance;
  41. class CMapWorld;
  42. class IMaterial;
  43. class IMaterialVar;
  44. template< class T, class A >
  45. class CUtlVector;
  46. enum Visibility_t;
  47. enum SelectionState_t;
  48. typedef struct TranslucentObjects_s {
  49. float depth;
  50. CMapAtom *object;
  51. bool m_bInstanceSelected;
  52. TInstanceState m_InstanceState;
  53. } TranslucentObjects_t;
  54. enum RenderState_t
  55. {
  56. RENDER_CENTER_CROSSHAIR, // Whether to draw the crosshair in the center of the view.
  57. RENDER_GRID, // Whether to draw a projected grid onto solid faces.
  58. RENDER_FILTER_TEXTURES, // Whether to filter textures.
  59. RENDER_POLYGON_OFFSET_FILL, // Whether to offset filled polygons (for decals)
  60. RENDER_POLYGON_OFFSET_LINE, // Whether to offset line polygons (for wireframe selections)
  61. RENDER_REVERSE_SELECTION, // Driver issue fix - whether to return the largest (rather than smallest) Z value when picking
  62. };
  63. //
  64. // Render state information set via RenderEnable:
  65. //
  66. typedef struct
  67. {
  68. bool bCenterCrosshair; // Whether to render the center crosshair.
  69. bool bDrawGrid; // Whether to render the grid.
  70. float fGridSpacing; // Grid spacing in world units.
  71. float fGridDistance; // Maximum distance from camera to draw grid.
  72. bool bFilterTextures; // Whether to filter textures.
  73. bool bReverseSelection; // Driver issue fix - whether to return the largest (rather than smallest) Z value when picking
  74. } RenderStateInfo_t;
  75. static inline bool RenderingModeIsTextured(EditorRenderMode_t mode)
  76. {
  77. return (
  78. (mode==RENDER_MODE_TEXTURED) ||
  79. (mode==RENDER_MODE_TEXTURED_SHADED) ||
  80. (mode==RENDER_MODE_LIGHT_PREVIEW_RAYTRACED) ||
  81. (mode==RENDER_MODE_LIGHT_PREVIEW2) );
  82. }
  83. //
  84. // Picking state information used when called from ObjectsAt.
  85. //
  86. typedef struct
  87. {
  88. bool bPicking; // Whether we are rendering in pick mode or not.
  89. unsigned int m_nFlags; // flags
  90. float fX; // Leftmost coordinate of pick rectangle, passed in by caller.
  91. float fY; // Topmost coordinate of pick rectangle, passed in by caller.
  92. float fWidth; // Width of pick rectangle, passed in by caller.
  93. float fHeight; // Height of pick rectangle, passed in by caller.
  94. HitInfo_t *pHitsDest; // Final array in which to place pick hits, passed in by caller.
  95. int nMaxHits; // Maximum number of hits to place in the 'pHits' array, passed in by caller, must be <= MAX_PICK_HITS.
  96. HitInfo_t Hits[MAX_PICK_HITS]; // Temporary array in which to place unsorted pick hits.
  97. int nNumHits; // Number of hits so far in this pick (number of hits in 'Hits' array).
  98. unsigned int uSelectionBuffer[SELECTION_BUFFER_SIZE];
  99. unsigned int uLastZ;
  100. } PickInfo_t;
  101. typedef struct
  102. {
  103. IEditorTexture *pTexture; // Pointer to the texture object that implements this texture.
  104. int nTextureID; // Unique ID of this texture across all renderers.
  105. unsigned int uTexture; // The texture name as returned by OpenGL when the texture was uploaded in this renderer.
  106. } TextureCache_t;
  107. typedef struct
  108. {
  109. HINSTANCE hInstance;
  110. int iCmdShow;
  111. HWND hWnd;
  112. HDC hDC;
  113. bool bActive;
  114. bool bFullScreen;
  115. ATOM wndclass;
  116. WNDPROC wndproc;
  117. bool bChangeBPP;
  118. bool bAllowSoft;
  119. char *szCmdLine;
  120. int argc;
  121. char **argv;
  122. int iResCount;
  123. int iVidMode;
  124. } MatWinData_t;
  125. class CRender3D : public CRender
  126. {
  127. public:
  128. // Constructor / Destructor.
  129. CRender3D(void);
  130. virtual ~CRender3D(void);
  131. // Initialization & shutdown functions.
  132. void ShutDown(void);
  133. float GetElapsedTime(void);
  134. float GetGridDistance(void);
  135. float GetGridSize(void);
  136. bool DeferRendering() const { return m_DeferRendering; }
  137. bool IsEnabled(RenderState_t eRenderState);
  138. bool IsPicking(void);
  139. virtual bool IsInLightingPreview();
  140. virtual void SetInLightingPreview( bool bLightingPreview );
  141. // Operations.
  142. float LightPlane(Vector& Normal);
  143. void UncacheAllTextures();
  144. bool SetView( CMapView *pView );
  145. void StartRenderFrame(void);
  146. void EndRenderFrame(void);
  147. virtual void PushInstanceData( CMapInstance *pInstanceClass, Vector &InstanceOrigin, QAngle &InstanceAngles );
  148. virtual void PopInstanceData( void );
  149. void ResetFocus();
  150. // Picking functions.
  151. void BeginRenderHitTarget(CMapAtom *pObject, unsigned int uHandle = 0);
  152. void EndRenderHitTarget(void);
  153. void Render(void);
  154. void RenderEnable(RenderState_t eRenderState, bool bEnable);
  155. void RenderCrossHair();
  156. virtual void RenderWireframeBox(const Vector &Mins, const Vector &Maxs, unsigned char chRed, unsigned char chGreen, unsigned char chBlue);
  157. void RenderBox(const Vector &Mins, const Vector &Maxs, unsigned char chRed, unsigned char chGreen, unsigned char chBlue, SelectionState_t eBoxSelectionState);
  158. void RenderArrow(Vector const &vStartPt, Vector const &vEndPt, unsigned char chRed, unsigned char chGreen, unsigned char chBlue);
  159. void RenderCone(Vector const &vBasePt, Vector const &vTipPt, float fRadius, int nSlices,
  160. unsigned char chRed, unsigned char chGreen, unsigned char chBlue );
  161. void RenderSphere(Vector const &vCenter, float flRadius, int nTheta, int nPhi,
  162. unsigned char chRed, unsigned char chGreen, unsigned char chBlue );
  163. void RenderWireframeSphere(Vector const &vCenter, float flRadius, int nTheta, int nPhi,
  164. unsigned char chRed, unsigned char chGreen, unsigned char chBlue );
  165. void RenderInstanceMapClass( CMapInstance *pInstanceClass, CMapClass *pMapClass, Vector &InstanceOrigin, QAngle &InstanceAngles );
  166. int ObjectsAt( float x, float y, float fWidth, float fHeight, HitInfo_t *pObjects, int nMaxObjects, unsigned nFlags = 0 );
  167. void DebugHook1(void *pData = NULL);
  168. void DebugHook2(void *pData = NULL);
  169. // indicates we need to render an overlay pass...
  170. bool NeedsOverlay() const;
  171. void BuildLightList( CUtlVector<CLightingPreviewLightDescription> *pList ) const;
  172. void SendLightList(); // send lighting list to lighting preview thread
  173. void SendShadowTriangles();
  174. void AddTranslucentDeferredRendering( CMapPoint *pMapPoint );
  175. protected:
  176. inline void DispatchRender3D(CMapClass *pMapClass);
  177. // Rendering functions.
  178. void RenderMapClass(CMapClass *pMapClass);
  179. void RenderInstanceMapClass_r(CMapClass *pMapClass);
  180. void RenderNode(CCullTreeNode *pNode, bool bForce);
  181. void RenderOverlayElements(void);
  182. void RenderTool(void);
  183. void RenderTree( CMapWorld *pWorld );
  184. void RenderPointsAndPortals(void);
  185. void RenderWorldAxes();
  186. void RenderTranslucentObjects( void );
  187. // Utility functions.
  188. void Preload(CMapClass *pParent);
  189. Visibility_t IsBoxVisible(Vector const &BoxMins, Vector const &BoxMaxs);
  190. // Frustum methods
  191. void ComputeFrustumRenderGeometry(CCamera * pCamera);
  192. void RenderFrustum();
  193. float m_fFrameRate; // Framerate in frames per second, calculated once per second.
  194. int m_nFramesThisSample; // Number of frames rendered in the current sample period.
  195. DWORD m_dwTimeLastSample; // Time when the framerate was last calculated.
  196. DWORD m_dwTimeLastFrame; // The time when the previous frame was rendered.
  197. float m_fTimeElapsed; // Milliseconds elapsed since the last frame was rendered.
  198. // context for the last bitmap we sent to lighting preview for ray tracing. we do not send if
  199. // nothing happens, even if we end up re-rendering
  200. Vector m_LastLPreviewCameraPos;
  201. float m_fLastLPreviewAngles[3]; // y,p,r
  202. float m_fLastLPreviewZoom;
  203. int m_nLastLPreviewWidth;
  204. int m_nLastLPreviewHeight;
  205. Vector4D m_FrustumPlanes[6]; // Plane normals and constants for the current view frustum.
  206. MatWinData_t m_WinData; // Defines our render window parameters.
  207. PickInfo_t m_Pick; // Contains information used when rendering in pick mode.
  208. RenderStateInfo_t m_RenderState; // Render state set via RenderEnable.
  209. bool m_bDroppedCamera; // Whether we have dropped the camera for debugging.
  210. bool m_DeferRendering; // Used when we want to sort lovely opaque objects
  211. bool m_TranslucentSortRendering; // Used when we want to sort translucent objects
  212. CCamera *m_pDropCamera; // Dropped camera to use for debugging.
  213. CUtlPriorityQueue<TranslucentObjects_t> m_TranslucentRenderObjects; // List of objects to render after all the other objects.
  214. IMaterial* m_pVertexColor[2]; // for selecting actual textures
  215. bool m_bLightingPreview;
  216. // for debugging... render the view frustum
  217. #ifdef _DEBUG
  218. Vector m_FrustumRenderPoint[8];
  219. bool m_bRenderFrustum;
  220. bool m_bRecomputeFrustumRenderGeometry;
  221. #endif
  222. };
  223. #endif // RENDER3DGL_H