Counter Strike : Global Offensive Source Code
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.

319 lines
11 KiB

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