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.

314 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef RENDER_H
  8. #define RENDER_H
  9. #pragma once
  10. #include "Color.h"
  11. #include "utlstack.h"
  12. #include "hammer_mathlib.h"
  13. #include "MaterialSystem\imesh.h"
  14. class IMaterial;
  15. struct DrawModelInfo_t;
  16. class CCoreDispInfo;
  17. class CMapView;
  18. class CCamera;
  19. class CMapAtom;
  20. class IEditorTexture;
  21. class CMapClass;
  22. class CMapInstance;
  23. typedef unsigned short MDLHandle_t;
  24. #define CAMERA_FRONT_PLANE_DISTANCE 8.0f
  25. #define CAMERA_HORIZONTAL_FOV 90.0f
  26. //
  27. // Colors for selected faces and edges. Kinda hacky; should probably be elsewhere.
  28. //
  29. #define SELECT_FACE_RED 220
  30. #define SELECT_FACE_GREEN 0
  31. #define SELECT_FACE_BLUE 0
  32. #define SELECT_EDGE_RED 255
  33. #define SELECT_EDGE_GREEN 255
  34. #define SELECT_EDGE_BLUE 0
  35. inline void SelectFaceColor( Color &pColor )
  36. {
  37. pColor[0] = SELECT_FACE_RED;
  38. pColor[1] = SELECT_FACE_GREEN;
  39. pColor[2] = SELECT_FACE_BLUE;
  40. }
  41. inline void SelectEdgeColor( Color &pColor )
  42. {
  43. pColor[0] = SELECT_EDGE_RED;
  44. pColor[1] = SELECT_EDGE_GREEN;
  45. pColor[2] = SELECT_EDGE_BLUE;
  46. }
  47. inline void InstanceColor( Color &pColor, bool bSelected )
  48. {
  49. if ( bSelected )
  50. {
  51. pColor[ 0 ] = 192;
  52. pColor[ 1 ] = 128;
  53. pColor[ 2 ] = 0;
  54. pColor[ 3 ] = 192;
  55. }
  56. else
  57. {
  58. pColor[ 0 ] = 128;
  59. pColor[ 1 ] = 128;
  60. pColor[ 2 ] = 0;
  61. pColor[ 3 ] = 192;
  62. }
  63. }
  64. enum EditorRenderMode_t
  65. {
  66. RENDER_MODE_NONE = 0, // dont render anything
  67. RENDER_MODE_EXTERN, // other system is using material system
  68. RENDER_MODE_DEFAULT, // select default material
  69. RENDER_MODE_CURRENT, // the current render mode
  70. RENDER_MODE_WIREFRAME, // wire frame mode
  71. RENDER_MODE_FLAT, // flat solid colors
  72. RENDER_MODE_FLAT_NOZ, // flat solid colors, ignore Z
  73. RENDER_MODE_FLAT_NOCULL, // flat solid colors, no backface culling
  74. RENDER_MODE_DOTTED, // flat colored dotted, ignore Z
  75. RENDER_MODE_TRANSLUCENT_FLAT,
  76. RENDER_MODE_TEXTURED,
  77. RENDER_MODE_LIGHTMAP_GRID,
  78. RENDER_MODE_SELECTION_OVERLAY,
  79. RENDER_MODE_SMOOTHING_GROUP,
  80. RENDER_MODE_TEXTURED_SHADED,
  81. RENDER_MODE_LIGHT_PREVIEW2,
  82. RENDER_MODE_LIGHT_PREVIEW_RAYTRACED,
  83. RENDER_MODE_INSTANCE_OVERLAY,
  84. };
  85. enum InstanceRenderingState_t
  86. {
  87. INSTANCE_STATE_OFF, // normal rendering
  88. INSTANCE_STATE_ON, // will be tinted as an instance
  89. INSTANCE_STACE_SELECTED // will be tinted as a selected instance
  90. };
  91. typedef struct SInstanceState
  92. {
  93. CMapInstance *m_pInstanceClass; // the func_instance entity
  94. Vector m_InstanceOrigin; // the origin offset of instance rendering
  95. QAngle m_InstanceAngles; // the rotation of the instance rendering
  96. VMatrix m_InstanceMatrix; // matrix of the origin and rotation of rendering
  97. VMatrix m_InstanceRenderMatrix; // matrix of the current camera transform
  98. bool m_bIsEditable;
  99. CMapInstance *m_pTopInstanceClass;
  100. } TInstanceState;
  101. #define STENCIL_AS_CALLS 1
  102. class CRender
  103. {
  104. public:
  105. CRender(void);
  106. virtual ~CRender(void);
  107. enum
  108. { TEXT_SINGLELINE = 0x1, // put all of the text on one line
  109. TEXT_MULTILINE = 0x2, // the text is written on multiple lines
  110. TEXT_JUSTIFY_BOTTOM = 0x4, // default
  111. TEXT_JUSTIFY_TOP = 0x8,
  112. TEXT_JUSTIFY_RIGHT = 0x10, // default
  113. TEXT_JUSTIFY_LEFT = 0x20,
  114. TEXT_JUSTIFY_HORZ_CENTER = 0x40,
  115. TEXT_JUSTIFY_VERT_CENTER = 0x80,
  116. TEXT_CLEAR_BACKGROUND = 0x100
  117. }; // clear the background behind the text
  118. enum
  119. { HANDLE_NONE = 0,
  120. HANDLE_SQUARE,
  121. HANDLE_CIRCLE,
  122. HANDLE_DIAMOND,
  123. HANDLE_CROSS
  124. };
  125. // map view setup
  126. virtual bool SetView( CMapView *pView );
  127. CMapView *GetView() { return m_pView; }
  128. CCamera *GetCamera();// { return m_pView->GetCamera(); }
  129. bool IsActiveView();
  130. // begin/end single render frame, sets up camera etc
  131. virtual void StartRenderFrame();
  132. virtual void EndRenderFrame();
  133. int GetRenderFrame() { return m_nFrameCount; }
  134. // switch rendering to client space coordinates (horz,vert,ignore)
  135. // render is in world space mode by default
  136. bool BeginClientSpace(void);
  137. void EndClientSpace(void);
  138. bool IsInClientSpace() { return m_bIsClientSpace; }
  139. void BeginLocalTransfrom( const VMatrix &matrix, bool MultiplyCurrent = false );
  140. void EndLocalTransfrom();
  141. bool IsInLocalTransformMode();
  142. void GetLocalTranform( VMatrix &matrix );
  143. void SetTextColor( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255 );
  144. void SetDrawColor( unsigned char r, unsigned char g, unsigned char b );
  145. void SetDrawColor( const Color &color );
  146. void GetDrawColor( Color &color );
  147. void SetHandleColor( unsigned char r, unsigned char g, unsigned char b );
  148. void SetHandleStyle( int size, int type );
  149. void SetDefaultRenderMode(EditorRenderMode_t eRenderMode);
  150. void BindTexture(IEditorTexture *pTexture);
  151. void BindMaterial( IMaterial *pMaterial );
  152. virtual void SetRenderMode( EditorRenderMode_t eRenderMode, bool force = false);
  153. inline EditorRenderMode_t GetCurrentRenderMode() { return m_eCurrentRenderMode; }
  154. inline EditorRenderMode_t GetDefaultRenderMode() { return m_eDefaultRenderMode; }
  155. void PushRenderMode( EditorRenderMode_t eRenderMode );
  156. void PopRenderMode();
  157. // drawing primitives
  158. void DrawPoint( const Vector &vPoint );
  159. void DrawLine( const Vector &vStart, const Vector &vEnd );
  160. virtual void DrawBox( const Vector &vMins, const Vector &vMaxs, bool bFill = false );
  161. void DrawBoxExt( const Vector &vCenter, float extend, bool bFill = false );
  162. void DrawSphere( const Vector &vCenter, int nRadius );
  163. void DrawCircle( const Vector &vCenter, const Vector &vNormal, float flRadius, int nSegments );
  164. void DrawPolyLine( int nPoints, const Vector *Points );
  165. void DrawText( const char *text, int x, int y, int nFlags ); // Uses pixel coordinates
  166. void DrawText( const char *text, const Vector2D &vPos, int nOffsetX, int nOffsetY, int nFlags ); // Uses "world" coordinates
  167. void DrawHandle( const Vector &vPoint, const Vector2D *vOffset = NULL );
  168. void DrawHandles( int nPoints, const Vector *Points );
  169. void DrawArrow( Vector const &vStart, Vector const &vEnd );
  170. void DrawPlane( const Vector &p0, const Vector &p1, const Vector &p2, const Vector &p3, bool bFill = false );
  171. // drawing complex objects
  172. void DrawModel( DrawModelInfo_t* pInfo, matrix3x4_t *pBoneToWorld, const Vector &vOrigin, float fAlpha = 1, bool bWireframe = false );
  173. void DrawDisplacement( CCoreDispInfo *pDisp );
  174. void DrawCollisionModel( MDLHandle_t mdlHandle, const VMatrix &mViewMatrix );
  175. //
  176. // helper funtions
  177. //
  178. void TransformPoint( Vector2D &vClient, const Vector& vWorld );
  179. void TransformNormal( Vector2D &vClient, const Vector& vWorld );
  180. void GetViewForward( Vector &ViewForward ) const;
  181. void GetViewRight( Vector &ViewRight ) const;
  182. void GetViewUp( Vector &ViewUp ) const;
  183. void PrepareInstanceStencil( void );
  184. void DrawInstanceStencil( void );
  185. void PushInstanceRendering( InstanceRenderingState_t State );
  186. void PopInstanceRendering( void );
  187. void SetInstanceRendering( InstanceRenderingState_t State );
  188. void SetInstanceRendering( bool InstanceRendering ) { m_bInstanceRendering = InstanceRendering; }
  189. virtual void PushInstanceData( CMapInstance *pInstanceClass, Vector &InstanceOrigin, QAngle &InstanceAngles );
  190. virtual void PopInstanceData( void );
  191. bool GetInstanceRendering( void ) { return m_bInstanceRendering; }
  192. CMapInstance *GetInstanceClass( void ) { return m_CurrentInstanceState.m_pInstanceClass; }
  193. void GetInstanceMatrix( VMatrix &Matrix ) { Matrix = m_CurrentInstanceState.m_InstanceMatrix; }
  194. Vector GetInstanceOrigin( void ) { return m_CurrentInstanceState.m_InstanceOrigin; }
  195. QAngle GetInstanceAngle( void ) { return m_CurrentInstanceState.m_InstanceAngles; }
  196. void TransformInstanceVector( Vector &In, Vector &Out ) { m_CurrentInstanceState.m_InstanceMatrix.V3Mul( In, Out ); }
  197. void RotateInstanceVector( Vector &In, Vector &Out ) { VectorRotate( In, m_CurrentInstanceState.m_InstanceMatrix.As3x4(), Out ); }
  198. void TransformInstanceAABB( Vector &InMins, Vector &InMaxs, Vector &OutMins, Vector &OutMaxs ) { TransformAABB( m_CurrentInstanceState.m_InstanceMatrix.As3x4(), InMins, InMaxs, OutMins, OutMaxs ); }
  199. protected:
  200. bool GetRequiredMaterial( const char *pName, IMaterial* &pMaterial );
  201. void UpdateStudioRenderConfig( bool bFlat, bool bWireframe );
  202. // client space helper functions:
  203. void DrawFilledRect( Vector2D& pt1, Vector2D& pt2, unsigned char *pColor, bool bBorder );
  204. void DrawCross( Vector2D& pt1, Vector2D& pt2, unsigned char *pColor );
  205. void DrawCircle( Vector2D &vCenter, float fRadius, int nSegments, unsigned char *pColor );
  206. void DrawRect( Vector2D& pt1, Vector2D& pt2, unsigned char *pColor );
  207. protected:
  208. CMapView *m_pView;
  209. unsigned long m_DefaultFont;
  210. bool m_bIsClientSpace;
  211. bool m_bIsLocalTransform;
  212. CUtlVector< VMatrix > m_LocalMatrix;
  213. VMatrix m_OrthoMatrix;
  214. // Meshbuilder used for drawing
  215. IMesh* m_pMesh;
  216. CMeshBuilder meshBuilder;
  217. // colors
  218. Color m_DrawColor; // current draw/fill color
  219. Color m_TextColor; // current text color
  220. Color m_HandleColor; // current text color
  221. // handle styles
  222. int m_nHandleSize;
  223. int m_nHandleType;
  224. // frame count
  225. int m_nFrameCount; // increases each setup camera
  226. bool m_bIsRendering;
  227. bool m_bIsRenderingIntoVGUI;
  228. // materials
  229. IMaterial* m_pCurrentMaterial; // The currently bound material
  230. IMaterial* m_pBoundMaterial; // a material given from external caller
  231. int m_nDecalMode; // 0 or 1
  232. IMaterial* m_pWireframe[2]; // default wireframe material
  233. IMaterial* m_pFlat[2]; // default flat material
  234. IMaterial* m_pDotted[2]; // default dotted material
  235. IMaterial* m_pFlatNoZ[2]; // default flat material, ignore Z
  236. IMaterial* m_pFlatNoCull[2]; // default flat material, no backface cull
  237. IMaterial* m_pTranslucentFlat[2]; // default translucent flat material
  238. IMaterial* m_pLightmapGrid[2]; // default lightmap grid material
  239. IMaterial* m_pSelectionOverlay[2]; // for selecting actual textures
  240. // render modes
  241. EditorRenderMode_t m_eCurrentRenderMode; // Current render mode setting - Wireframe, flat, or textured.
  242. EditorRenderMode_t m_eDefaultRenderMode; // Default render mode - Wireframe, flat, or textured.
  243. CUtlStack<EditorRenderMode_t> m_RenderModeStack;
  244. // instance
  245. int m_nInstanceCount; // increases each time an instance is drawn regardless of view
  246. bool m_bInstanceRendering; // if true, we are rendering an instance
  247. VMatrix m_CurrentMatrix; // matrix of the current transforms
  248. int m_InstanceSelectionDepth;
  249. TInstanceState m_CurrentInstanceState; // the current instance state ( with current transforms )
  250. CUtlVector< TInstanceState > m_InstanceState; // the instance state stack
  251. #ifndef STENCIL_AS_CALLS
  252. ShaderStencilState_t m_ShaderStencilState;
  253. #endif // STENCIL_AS_CALLS
  254. int m_nNumInstancesRendered; // number of instances rendered that impacted the stencil buffer
  255. CUtlVector< InstanceRenderingState_t > m_InstanceRenderingState; // the instance rendering state stack
  256. };
  257. #endif // RENDER_H