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.

282 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef MAPOVERLAY_H
  8. #define MAPOVERLAY_H
  9. #pragma once
  10. #include <afxwin.h>
  11. #include "utlvector.h"
  12. #include "MapSideList.h"
  13. class CHelperInfo;
  14. class CMapFace;
  15. class CRender3D;
  16. class CMapView;
  17. class IEditorTexture;
  18. #define OVERLAY_HANDLES_COUNT 4
  19. #define NUM_CLIPFACE_TEXCOORDS 2
  20. #define OVERLAY_TYPE_GENERIC 0x01
  21. #define OVERLAY_TYPE_SHORE 0x02
  22. //=============================================================================
  23. //
  24. // Class Map Overlay
  25. //
  26. class CMapOverlay : public CMapSideList
  27. {
  28. public:
  29. DECLARE_MAPCLASS( CMapOverlay, CMapSideList );
  30. // Construction/Deconstruction.
  31. CMapOverlay();
  32. ~CMapOverlay();
  33. // Factory for building from a list of string parameters.
  34. static CMapClass *CreateMapOverlay( CHelperInfo *pInfo, CMapEntity *pParent );
  35. // Virtual/Interface Implementation.
  36. virtual void PostloadWorld( CMapWorld *pWorld );
  37. virtual CMapClass *Copy( bool bUpdateDependencies );
  38. virtual CMapClass *CopyFrom( CMapClass *pObject, bool bUpdateDependencies );
  39. void CalcBounds( BOOL bFullUpdate = FALSE );
  40. virtual void OnParentKeyChanged( const char* szKey, const char* szValue );
  41. virtual void OnNotifyDependent( CMapClass *pObject, Notify_Dependent_t eNotifyType );
  42. void DoTransform( const VMatrix &matrix );
  43. void OnPaste( CMapClass *pCopy, CMapWorld *pSourceWorld, CMapWorld *pDestWorld,
  44. const CMapObjectList &OriginalList, CMapObjectList &NewList);
  45. void OnClone( CMapClass *pClone, CMapWorld *pWorld,
  46. const CMapObjectList &OriginalList, CMapObjectList &NewList );
  47. void OnUndoRedo( void );
  48. void Render3D( CRender3D *pRender );
  49. // Overlay.
  50. void HandlesReset( void );
  51. bool HandlesHitTest( CMapView *pView, const Vector2D &vPoint );
  52. void HandlesDragTo( Vector &vecImpact, CMapFace *pFace );
  53. void HandleMoveTo( int iHandle, Vector &vecPoint, CMapFace *pFace );
  54. void SetTexCoords( Vector2D vecTexCoords[4] );
  55. void GetHandlePos( int iHandle, Vector &vecPos );
  56. bool IsSelected( void ) { return ( GetSelectionState() == SELECT_NORMAL ); }
  57. void DoClip( void );
  58. void CenterEntity( void );
  59. void GetPlane( cplane_t &plane );
  60. int GetFaceCount( void ) { return m_Faces.Count(); }
  61. CMapFace *GetFace( int iFace ) { return m_Faces.Element( iFace ); }
  62. void SetMaterial( const char *szMaterialName );
  63. void SetMaterial( IEditorTexture *pTexture ) { m_Material.m_pTexture = pTexture; }
  64. IEditorTexture* GetMaterial() { return m_Material.m_pTexture; }
  65. // Creation.
  66. void Basis_Init( CMapFace *pFace );
  67. void Handles_Init( CMapFace *pFace );
  68. void SideList_Init( CMapFace *pFace );
  69. void SideList_AddFace( CMapFace *pFace );
  70. void SetLoaded( bool bLoaded ) { m_bLoaded = bLoaded; }
  71. // Attributes.
  72. inline virtual bool IsVisualElement( void ) { return true; }
  73. inline virtual bool ShouldRenderLast( void ) { return true; }
  74. inline const char* GetDescription() { return ( "Overlay" ); }
  75. void SetOverlayType( unsigned short uiType ) { m_uiFlags |= uiType; }
  76. void ResetOverlayType( unsigned short uiType ) { m_uiFlags &= ~uiType; }
  77. unsigned short GetOverlayType( void ) { return m_uiFlags; }
  78. ChunkFileResult_t SaveDataToVMF( CChunkFile *pFile, CSaveInfo *pSaveInfo );
  79. private:
  80. //=========================================================================
  81. //
  82. // Basis Data
  83. //
  84. struct Basis_t
  85. {
  86. CMapFace *m_pFace; // Index to the face the basis were derived from
  87. Vector m_vecOrigin; // Origin of basis vectors (in plane)
  88. Vector m_vecAxes[3]; // Basis vectors
  89. int m_nAxesFlip[3]; // u, v, n flip values (3 bits x,y,z) - saved off the z components of the first 3 uv points
  90. };
  91. void Basis_Clear( void );
  92. void Basis_SetFace( CMapFace *pFace );
  93. void Basis_UpdateOrigin( void );
  94. void Basis_BuildAxes( void );
  95. void Basis_SetInitialUAxis( Vector const &vecNormal );
  96. bool Basis_IsValid( void );
  97. void Basis_Copy( Basis_t *pSrc, Basis_t *pDst );
  98. void Basis_UpdateParentKey( void );
  99. // Legacy support.
  100. void Basis_BuildFromSideList( void );
  101. void Basis_ToggleAxesFlip( int iAxis, int iComponent );
  102. bool Basis_IsFlipped( int iAxis, int iComponent );
  103. //=========================================================================
  104. //
  105. // Handle Data
  106. //
  107. struct Handles_t
  108. {
  109. int m_iHit; // Index of the selected handle
  110. Vector2D m_vecBasisCoords[OVERLAY_HANDLES_COUNT]; // U,V coordinates of the 4 corners in the editable plane (use basis)
  111. Vector m_vec3D[OVERLAY_HANDLES_COUNT]; // World space handles for snap testing
  112. };
  113. void Handles_Clear( void );
  114. void Handles_Build3D();
  115. void Handles_Render3D( CRender3D *pRender );
  116. void Handles_SurfToOverlayPlane( CMapFace *pFace, Vector const &vSurf, Vector &vPoint );
  117. void Handles_Copy( Handles_t *pSrc, Handles_t *pDst );
  118. void Handles_UpdateParentKey( void );
  119. void Handles_FixOrder();
  120. //=========================================================================
  121. //
  122. // ClipFace Data
  123. //
  124. struct BlendData_t
  125. {
  126. void Init()
  127. {
  128. m_nType = 0;
  129. memset(m_iPoints, 0, sizeof(m_iPoints));
  130. memset(m_flBlends, 0, sizeof(m_flBlends));
  131. }
  132. int m_nType; // type of blend (point, edge, barycentric)
  133. short m_iPoints[3]; // displacement point indices
  134. float m_flBlends[3]; // blending values
  135. };
  136. struct ClipFace_t
  137. {
  138. CMapFace *m_pBuildFace;
  139. int m_nPointCount;
  140. CUtlVector<Vector> m_aPoints;
  141. CUtlVector<Vector> m_aDispPointUVs; // z is always 0 (need to be this way to share functions!)
  142. CUtlVector<Vector2D> m_aTexCoords[NUM_CLIPFACE_TEXCOORDS];
  143. CUtlVector<BlendData_t> m_aBlends;
  144. ClipFace_t()
  145. {
  146. m_pBuildFace = NULL;
  147. m_nPointCount = 0;
  148. }
  149. ~ClipFace_t()
  150. {
  151. m_aPoints.Purge();
  152. m_aDispPointUVs.Purge();
  153. m_aBlends.Purge();
  154. for ( int iCoord = 0; iCoord < NUM_CLIPFACE_TEXCOORDS; ++iCoord )
  155. {
  156. m_aTexCoords[iCoord].Purge();
  157. }
  158. }
  159. };
  160. typedef CUtlVector<ClipFace_t*> ClipFaces_t;
  161. ClipFace_t *ClipFace_Create( int nSize );
  162. void ClipFace_Destroy( ClipFace_t **ppClipFace );
  163. ClipFace_t *ClipFace_Copy( ClipFace_t *pSrc );
  164. void ClipFace_GetBounds( ClipFace_t *pClipFace, Vector &vecMin, Vector &vecMax );
  165. void ClipFace_Clip( ClipFace_t *pClipFace, cplane_t *pClipPlane, float flEpsilon, ClipFace_t **ppFront, ClipFace_t **ppBack );
  166. void ClipFace_ClipBarycentric( ClipFace_t *pClipFace, cplane_t *pClipPlane, float flEpsilon, int iClip, CMapDisp *pDisp, ClipFace_t **ppFront, ClipFace_t **ppBack );
  167. void ClipFace_PreClipDisp( ClipFace_t *pClipFace, CMapDisp *pDisp );
  168. void ClipFace_PostClipDisp( void );
  169. void ClipFace_ResolveBarycentricClip( CMapDisp *pDisp, ClipFace_t *pClipFace, int iClipFacePoint, const Vector2D &vecPointUV, float *pCoefs, int *pTris, Vector2D *pVertsUV );
  170. bool ClipFace_CalcBarycentricCooefs( CMapDisp *pDisp, Vector2D *pVertsUV, const Vector2D &vecPointUV, float *pCoefs );
  171. int ClipFace_GetAxisType( cplane_t *pClipPlane );
  172. void ClipFace_BuildBlend( ClipFace_t *pClipFace, CMapDisp *pDisp, cplane_t *pClipPlane, int iClip, const Vector &vecUV, const Vector &vecPoint );
  173. void ClipFace_CopyBlendFrom( ClipFace_t *pClipFace, BlendData_t *pBlendFrom );
  174. void ClipFace_BuildFacesFromBlendedData( ClipFace_t *pClipFace );
  175. //=========================================================================
  176. //
  177. // Material Functions
  178. //
  179. struct Material_t
  180. {
  181. IEditorTexture *m_pTexture; // material
  182. Vector2D m_vecTextureU; // material starting and ending U
  183. Vector2D m_vecTextureV; // material starting and ending V
  184. };
  185. void Material_Clear( void );
  186. void Material_Copy( Material_t *pSrc, Material_t *pDst );
  187. void Material_TexCoordInit( void );
  188. void Material_UpdateParentKey( void );
  189. //=========================================================================
  190. //
  191. // Clipping
  192. //
  193. void PreClip( void );
  194. void PostClip( void );
  195. void DoClipFace( CMapFace *pFace );
  196. void DoClipDisp( CMapFace *pFace, ClipFace_t *pClippedFace );
  197. void DoClipDispInV( CMapDisp *pDisp, ClipFaces_t &aCurrentFaces );
  198. void DoClipDispInU( CMapDisp *pDisp, ClipFaces_t &aCurrentFaces );
  199. void DoClipDispInUVFromTLToBR( CMapDisp *pDisp, ClipFaces_t &aCurrentFaces );
  200. void DoClipDispInUVFromBLToTR( CMapDisp *pDisp, ClipFaces_t &aCurrentFaces );
  201. void Disp_ClipFragments( CMapDisp *pDisp, ClipFaces_t &aDispFragments );
  202. void Disp_DoClip( CMapDisp *pDisp, ClipFaces_t &aDispFragments, cplane_t &clipPlane, float clipDistStart, int nInterval, int nLoopStart, int nLoopEnd, int nLoopInc );
  203. //==========================================================================
  204. //
  205. // Transform
  206. //
  207. // Utility
  208. void OverlayUVToOverlayPlane( const Vector2D &vecUV, Vector &vecPoint );
  209. void OverlayPlaneToOverlayUV( const Vector &vecPoint, Vector2D &vecUV );
  210. void WorldToOverlayPlane( const Vector &vecWorld, Vector &vecPoint );
  211. void OverlayPlaneToWorld( CMapFace *pFace, const Vector &vecPlane, Vector &vecWorld );
  212. void OverlayPlaneToSurfFromList( const Vector &vecOverlayPoint, Vector &vecSurfPoint );
  213. bool EntityOnSurfFromListToBaseFacePlane( const Vector &vecWorldPoint, Vector &vecBasePoint );
  214. bool BuildEdgePlanes( Vector const *pPoints, int pointCount, cplane_t *pEdgePlanes, int edgePlaneCount );
  215. void UpdateDispBarycentric( void );
  216. void PostModified( void );
  217. void GetTriVerts( CMapDisp *pDisp, const Vector2D &vecSurfUV, int *pTris, Vector2D *pVertsUV );
  218. private:
  219. Basis_t m_Basis; // Overlay Basis Data
  220. Handles_t m_Handles; // Overlay Handle Data
  221. Material_t m_Material; // Overlay Material
  222. ClipFace_t *m_pOverlayFace; // Primary Overlay
  223. ClipFaces_t m_aRenderFaces; // Clipped Face Cache (Render Faces)
  224. unsigned short m_uiFlags; //
  225. bool m_bLoaded;
  226. };
  227. #endif // MAPOVERLAY_H