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.

2749 lines
86 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include <stdafx.h>
  7. #include "MapOverlay.h"
  8. #include "MapFace.h"
  9. #include "MapSolid.h"
  10. #include "MapWorld.h"
  11. #include "MainFrm.h"
  12. #include "GlobalFunctions.h"
  13. #include "MapDoc.h"
  14. #include "TextureSystem.h"
  15. #include "Material.h"
  16. #include "materialsystem/imesh.h"
  17. #include "Box3D.h"
  18. #include "MapDefs.h"
  19. #include "CollisionUtils.h"
  20. #include "MapSideList.h"
  21. #include "MapDisp.h"
  22. #include "ToolManager.h"
  23. #include "objectproperties.h"
  24. #include "ChunkFile.h"
  25. #include "mapview.h"
  26. #include "options.h"
  27. // memdbgon must be the last include file in a .cpp file!!!
  28. #include <tier0/memdbgon.h>
  29. IMPLEMENT_MAPCLASS( CMapOverlay )
  30. #define OVERLAY_INITSIZE 25.0f // x2
  31. #define OVERLAY_BASIS_U 0
  32. #define OVERLAY_BASIS_V 1
  33. #define OVERLAY_BASIS_NORMAL 2
  34. #define OVERLAY_HANDLES_COUNT 4
  35. #define OVERLAY_WORLDSPACE_EPSILON 0.03125f
  36. #define OVERLAY_DISPSPACE_EPSILON 0.000001f
  37. #define OVERLAY_BARYCENTRIC_EPSILON 0.001f
  38. #define OVERLAY_BLENDTYPE_VERT 0
  39. #define OVERLAY_BLENDTYPE_EDGE 1
  40. #define OVERLAY_BLENDTYPE_BARY 2
  41. #define OVERLAY_ANGLE0 1
  42. #define OVERLAY_ANGLE45 2
  43. #define OVERLAY_ANGLE90 3
  44. #define OVERLAY_ANGLE135 4
  45. #define OVERLAY_INVALID_VALUE -99999.9f
  46. //=============================================================================
  47. //
  48. // Basis Functions
  49. //
  50. //-----------------------------------------------------------------------------
  51. // Purpose: Initialize the basis data.
  52. //-----------------------------------------------------------------------------
  53. void CMapOverlay::Basis_Clear( void )
  54. {
  55. m_Basis.m_pFace = NULL;
  56. m_Basis.m_vecOrigin.Init();
  57. for( int iAxis = 0; iAxis < 3; iAxis++ )
  58. {
  59. m_Basis.m_vecAxes[iAxis].Init( OVERLAY_INVALID_VALUE, OVERLAY_INVALID_VALUE, OVERLAY_INVALID_VALUE );
  60. m_Basis.m_nAxesFlip[iAxis] = 0;
  61. }
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Build the overlay basis given an entity and base face (CMapFace).
  65. //-----------------------------------------------------------------------------
  66. void CMapOverlay::Basis_Init( CMapFace *pFace )
  67. {
  68. // Valid face?
  69. Assert( pFace != NULL );
  70. if( !pFace )
  71. return;
  72. // Set the face the basis are derived from.
  73. Basis_SetFace( pFace );
  74. // Set the basis origin.
  75. Basis_UpdateOrigin();
  76. // Setup the basis axes.
  77. Basis_BuildAxes();
  78. // Initialize the texture coordinates - based on basis.
  79. Material_TexCoordInit();
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Purpose:
  83. //-----------------------------------------------------------------------------
  84. void CMapOverlay::Basis_UpdateOrigin( void )
  85. {
  86. CMapEntity *pEntity = static_cast<CMapEntity*>( GetParent() );
  87. if ( pEntity )
  88. {
  89. Vector vecEntityOrigin;
  90. pEntity->GetOrigin( vecEntityOrigin );
  91. Vector vecPoint( 0.0f, 0.0f, 0.0f );
  92. if ( !EntityOnSurfFromListToBaseFacePlane( vecEntityOrigin, vecPoint ) )
  93. {
  94. vecPoint = vecEntityOrigin;
  95. }
  96. m_Basis.m_vecOrigin = vecPoint;
  97. }
  98. // Update the property box.
  99. Basis_UpdateParentKey();
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose:
  103. //-----------------------------------------------------------------------------
  104. void CMapOverlay::Basis_BuildAxes( void )
  105. {
  106. // Valid face?
  107. if( !m_Basis.m_pFace )
  108. return;
  109. // Build the basis axes.
  110. Vector vecFaceNormal;
  111. m_Basis.m_pFace->GetFaceNormal( vecFaceNormal );
  112. VectorNormalize( vecFaceNormal );
  113. VectorCopy( vecFaceNormal, m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL] );
  114. Basis_SetInitialUAxis( vecFaceNormal );
  115. m_Basis.m_vecAxes[OVERLAY_BASIS_V] = m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].Cross( m_Basis.m_vecAxes[OVERLAY_BASIS_U] );
  116. VectorNormalize( m_Basis.m_vecAxes[OVERLAY_BASIS_V] );
  117. m_Basis.m_vecAxes[OVERLAY_BASIS_U] = m_Basis.m_vecAxes[OVERLAY_BASIS_V].Cross( m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL] );
  118. VectorNormalize( m_Basis.m_vecAxes[OVERLAY_BASIS_U] );
  119. // Flip uvn axes?
  120. for ( int iAxis = 0; iAxis < 3; ++iAxis )
  121. {
  122. for ( int iComp = 0; iComp < 3; ++iComp )
  123. {
  124. if ( Basis_IsFlipped( iAxis, iComp ) )
  125. {
  126. m_Basis.m_vecAxes[iAxis][iComp] = -m_Basis.m_vecAxes[iAxis][iComp];
  127. }
  128. }
  129. }
  130. Basis_UpdateParentKey();
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose: A basis building helper function that finds the best guess u-axis
  134. // given a base face (CMapFace) normal.
  135. // Input: vecNormal - the base face normal
  136. //-----------------------------------------------------------------------------
  137. void CMapOverlay::Basis_SetInitialUAxis( Vector const &vecNormal )
  138. {
  139. // Find the major vector component.
  140. int nMajorAxis = 0;
  141. float flAxisValue = vecNormal[0];
  142. if ( FloatMakePositive( vecNormal[1] ) > FloatMakePositive( flAxisValue ) )
  143. {
  144. nMajorAxis = 1;
  145. flAxisValue = vecNormal[1];
  146. }
  147. if ( FloatMakePositive( vecNormal[2] ) > FloatMakePositive( flAxisValue ) )
  148. {
  149. nMajorAxis = 2;
  150. }
  151. if ( ( nMajorAxis == 1 ) || ( nMajorAxis == 2 ) )
  152. {
  153. m_Basis.m_vecAxes[OVERLAY_BASIS_U].Init( 1.0f, 0.0f, 0.0f );
  154. }
  155. else
  156. {
  157. m_Basis.m_vecAxes[OVERLAY_BASIS_U].Init( 0.0f, 1.0f, 0.0f );
  158. }
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Purpose:
  162. //-----------------------------------------------------------------------------
  163. bool CMapOverlay::Basis_IsValid( void )
  164. {
  165. for ( int iBasis = 0; iBasis < 3; ++iBasis )
  166. {
  167. for ( int iAxis = 0; iAxis < 3; ++iAxis )
  168. {
  169. if ( m_Basis.m_vecAxes[iBasis][iAxis] == OVERLAY_INVALID_VALUE )
  170. return false;
  171. }
  172. }
  173. return true;
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Purpose:
  177. //-----------------------------------------------------------------------------
  178. void CMapOverlay::Basis_SetFace( CMapFace *pFace )
  179. {
  180. // Verify face.
  181. if ( !pFace )
  182. return;
  183. m_Basis.m_pFace = pFace;
  184. }
  185. //-----------------------------------------------------------------------------
  186. // Purpose: Copy the basis data from the source into the destination.
  187. // Input: pSrc - the basis source data
  188. // pDst (Output) - destination for the basis data
  189. //-----------------------------------------------------------------------------
  190. void CMapOverlay::Basis_Copy( Basis_t *pSrc, Basis_t *pDst )
  191. {
  192. pDst->m_pFace = pSrc->m_pFace;
  193. pDst->m_vecOrigin = pSrc->m_vecOrigin;
  194. for ( int iAxis = 0; iAxis < 3; iAxis++ )
  195. {
  196. pDst->m_vecAxes[iAxis] = pSrc->m_vecAxes[iAxis];
  197. pDst->m_nAxesFlip[iAxis] = pSrc->m_nAxesFlip[iAxis];
  198. }
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Purpose:
  202. //-----------------------------------------------------------------------------
  203. void CMapOverlay::Basis_UpdateParentKey( void )
  204. {
  205. char szValue[80];
  206. CMapEntity *pEntity = ( CMapEntity* )GetParent();
  207. if ( pEntity )
  208. {
  209. sprintf( szValue, "%g %g %g", m_Basis.m_vecOrigin.x, m_Basis.m_vecOrigin.y, m_Basis.m_vecOrigin.z );
  210. pEntity->NotifyChildKeyChanged( this, "BasisOrigin", szValue );
  211. sprintf( szValue, "%g %g %g", m_Basis.m_vecAxes[OVERLAY_BASIS_U].x, m_Basis.m_vecAxes[OVERLAY_BASIS_U].y, m_Basis.m_vecAxes[OVERLAY_BASIS_U].z );
  212. pEntity->NotifyChildKeyChanged( this, "BasisU", szValue );
  213. sprintf( szValue, "%g %g %g", m_Basis.m_vecAxes[OVERLAY_BASIS_V].x, m_Basis.m_vecAxes[OVERLAY_BASIS_V].y, m_Basis.m_vecAxes[OVERLAY_BASIS_V].z );
  214. pEntity->NotifyChildKeyChanged( this, "BasisV", szValue );
  215. sprintf( szValue, "%g %g %g", m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].x, m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].y, m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].z );
  216. pEntity->NotifyChildKeyChanged( this, "BasisNormal", szValue );
  217. }
  218. }
  219. //=============================================================================
  220. //
  221. // Basis - Legacy support!
  222. //
  223. //-----------------------------------------------------------------------------
  224. // Purpose:
  225. //-----------------------------------------------------------------------------
  226. void CMapOverlay::Basis_BuildFromSideList( void )
  227. {
  228. // Initialization (don't have or couldn't find the basis face)
  229. if ( m_Faces.Count() > 0 )
  230. {
  231. Basis_Init( m_Faces.Element( 0 ) );
  232. }
  233. else
  234. {
  235. m_Basis.m_pFace = NULL;
  236. }
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Purpose:
  240. // Input: iAxis - 0, 1, 2 (u, v, n)
  241. // iComponet - 0, 1, 2 (x, y, z)
  242. //-----------------------------------------------------------------------------
  243. void CMapOverlay::Basis_ToggleAxesFlip( int iAxis, int iComponent )
  244. {
  245. if ( iAxis < 0 || iAxis > 2 || iComponent < 0 || iComponent > 2 )
  246. return;
  247. int nValue = ( 1 << iComponent );
  248. m_Basis.m_nAxesFlip[iAxis] ^= nValue;
  249. }
  250. //-----------------------------------------------------------------------------
  251. // Purpose:
  252. //-----------------------------------------------------------------------------
  253. bool CMapOverlay::Basis_IsFlipped( int iAxis, int iComponent )
  254. {
  255. if ( iAxis < 0 || iAxis > 2 || iComponent < 0 || iComponent > 2 )
  256. return false;
  257. int nValue = ( 1 << iComponent );
  258. return ( ( m_Basis.m_nAxesFlip[iAxis] & nValue ) != 0 );
  259. }
  260. //=============================================================================
  261. //
  262. // Handles Functions
  263. //
  264. //-----------------------------------------------------------------------------
  265. // Purpose:
  266. //-----------------------------------------------------------------------------
  267. void CMapOverlay::Handles_Clear( void )
  268. {
  269. m_Handles.m_iHit = -1;
  270. for ( int iHandle = 0; iHandle < OVERLAY_HANDLES_COUNT; iHandle++ )
  271. {
  272. m_Handles.m_vec3D[iHandle].Init();
  273. }
  274. m_Handles.m_vecBasisCoords[0].Init( -OVERLAY_INITSIZE, -OVERLAY_INITSIZE );
  275. m_Handles.m_vecBasisCoords[1].Init( -OVERLAY_INITSIZE, OVERLAY_INITSIZE );
  276. m_Handles.m_vecBasisCoords[2].Init( OVERLAY_INITSIZE, OVERLAY_INITSIZE );
  277. m_Handles.m_vecBasisCoords[3].Init( OVERLAY_INITSIZE, -OVERLAY_INITSIZE );
  278. }
  279. //-----------------------------------------------------------------------------
  280. // Purpose:
  281. //-----------------------------------------------------------------------------
  282. void CMapOverlay::Handles_Init( CMapFace *pFace )
  283. {
  284. IEditorTexture *pTexture = g_Textures.FindActiveTexture( GetDefaultTextureName() );
  285. int nWidth = pTexture->GetImageWidth();
  286. int nHeight = pTexture->GetImageHeight();
  287. // Half-height (width) and 1/4 scale
  288. int nWidthHalf = nWidth / 8;
  289. int nHeightHalf = nHeight / 8;
  290. m_Handles.m_vecBasisCoords[0].Init( -nWidthHalf, -nHeightHalf );
  291. m_Handles.m_vecBasisCoords[1].Init( -nWidthHalf, nHeightHalf );
  292. m_Handles.m_vecBasisCoords[2].Init( nWidthHalf, nHeightHalf );
  293. m_Handles.m_vecBasisCoords[3].Init( nWidthHalf, -nHeightHalf );
  294. Handles_Build3D();
  295. Handles_UpdateParentKey();
  296. }
  297. //-----------------------------------------------------------------------------
  298. // Purpose:
  299. //-----------------------------------------------------------------------------
  300. void CMapOverlay::Handles_Build3D( void )
  301. {
  302. // Verify that we have a valid basis to build the handles from.
  303. if ( !Basis_IsValid() )
  304. return;
  305. for ( int iHandle = 0; iHandle < OVERLAY_HANDLES_COUNT; iHandle++ )
  306. {
  307. Vector vecHandle;
  308. OverlayUVToOverlayPlane( m_Handles.m_vecBasisCoords[iHandle], vecHandle );
  309. OverlayPlaneToSurfFromList( vecHandle, m_Handles.m_vec3D[iHandle] );
  310. }
  311. Handles_FixOrder();
  312. }
  313. //-----------------------------------------------------------------------------
  314. // Purpose:
  315. //-----------------------------------------------------------------------------
  316. void CMapOverlay::Handles_Render3D( CRender3D *pRender )
  317. {
  318. // Set the render mode to "flat."
  319. pRender->PushRenderMode( RENDER_MODE_FLAT );
  320. // Set the color, should be based on selection.
  321. unsigned char ucColor[4];
  322. ucColor[0] = ucColor[1] = ucColor[2] = ucColor[3] = 255;
  323. unsigned char ucSelectColor[4];
  324. ucSelectColor[0] = ucSelectColor[3] = 255;
  325. ucSelectColor[1] = ucSelectColor[2] = 0;
  326. pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_SQUARE );
  327. for ( int iHandle = 0; iHandle < OVERLAY_HANDLES_COUNT; iHandle++ )
  328. {
  329. pRender->BeginRenderHitTarget( this, iHandle );
  330. if ( m_Handles.m_iHit == iHandle )
  331. {
  332. pRender->SetHandleColor( ucSelectColor[0], ucSelectColor[1], ucSelectColor[2] );
  333. }
  334. else
  335. {
  336. pRender->SetHandleColor( ucColor[0], ucColor[1], ucColor[2] );
  337. }
  338. pRender->DrawHandle( m_Handles.m_vec3D[iHandle] );
  339. pRender->EndRenderHitTarget();
  340. }
  341. pRender->PopRenderMode();
  342. }
  343. //-----------------------------------------------------------------------------
  344. // Purpose:
  345. //-----------------------------------------------------------------------------
  346. void CMapOverlay::Handles_SurfToOverlayPlane( CMapFace *pFace, Vector const &vecSurf, Vector &vecPoint )
  347. {
  348. Vector vecWorld;
  349. if ( pFace->HasDisp() )
  350. {
  351. EditDispHandle_t handle = pFace->GetDisp();
  352. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  353. pDisp->SurfToBaseFacePlane( vecSurf, vecWorld );
  354. }
  355. else
  356. {
  357. vecWorld = vecSurf;
  358. }
  359. WorldToOverlayPlane( vecWorld, vecPoint );
  360. }
  361. //-----------------------------------------------------------------------------
  362. // Purpose:
  363. //-----------------------------------------------------------------------------
  364. void CMapOverlay::Handles_Copy( Handles_t *pSrc, Handles_t *pDst )
  365. {
  366. pDst->m_iHit = pSrc->m_iHit;
  367. for ( int iHandle = 0; iHandle < OVERLAY_HANDLES_COUNT; ++iHandle )
  368. {
  369. pDst->m_vecBasisCoords[iHandle] = pSrc->m_vecBasisCoords[iHandle];
  370. pDst->m_vec3D[iHandle] = pSrc->m_vec3D[iHandle];
  371. }
  372. }
  373. //-----------------------------------------------------------------------------
  374. // Purpose:
  375. //-----------------------------------------------------------------------------
  376. void CMapOverlay::Handles_UpdateParentKey( void )
  377. {
  378. char szValue[80];
  379. CMapEntity *pEntity = ( CMapEntity* )GetParent();
  380. if ( pEntity )
  381. {
  382. sprintf( szValue, "%g %g %g", m_Handles.m_vecBasisCoords[0].x, m_Handles.m_vecBasisCoords[0].y, ( float )m_Basis.m_nAxesFlip[0] );
  383. pEntity->NotifyChildKeyChanged( this, "uv0", szValue );
  384. sprintf( szValue, "%g %g %g", m_Handles.m_vecBasisCoords[1].x, m_Handles.m_vecBasisCoords[1].y, ( float )m_Basis.m_nAxesFlip[1] );
  385. pEntity->NotifyChildKeyChanged( this, "uv1", szValue );
  386. sprintf( szValue, "%g %g %g", m_Handles.m_vecBasisCoords[2].x, m_Handles.m_vecBasisCoords[2].y, ( float )m_Basis.m_nAxesFlip[2] );
  387. pEntity->NotifyChildKeyChanged( this, "uv2", szValue );
  388. sprintf( szValue, "%g %g %g", m_Handles.m_vecBasisCoords[3].x, m_Handles.m_vecBasisCoords[3].y, 0.0f );
  389. pEntity->NotifyChildKeyChanged( this, "uv3", szValue );
  390. }
  391. }
  392. //=============================================================================
  393. //
  394. // ClipFace Functions
  395. //
  396. //-----------------------------------------------------------------------------
  397. // Purpose:
  398. //-----------------------------------------------------------------------------
  399. CMapOverlay::ClipFace_t *CMapOverlay::ClipFace_Create( int nSize )
  400. {
  401. ClipFace_t *pClipFace = new ClipFace_t;
  402. if ( pClipFace )
  403. {
  404. pClipFace->m_nPointCount = nSize;
  405. if ( nSize > 0 )
  406. {
  407. pClipFace->m_aPoints.SetSize( nSize );
  408. pClipFace->m_aDispPointUVs.SetSize( nSize );
  409. for ( int iCoord = 0; iCoord < NUM_CLIPFACE_TEXCOORDS; iCoord++ )
  410. {
  411. pClipFace->m_aTexCoords[iCoord].SetSize( nSize );
  412. }
  413. pClipFace->m_aBlends.SetSize( nSize );
  414. for ( int iPoint = 0; iPoint < nSize; iPoint++ )
  415. {
  416. pClipFace->m_aPoints[iPoint].Init();
  417. pClipFace->m_aDispPointUVs[iPoint].Init();
  418. pClipFace->m_aBlends[iPoint].Init();
  419. for ( int iCoord = 0; iCoord < NUM_CLIPFACE_TEXCOORDS; iCoord++ )
  420. {
  421. pClipFace->m_aTexCoords[iCoord][iPoint].Init();
  422. }
  423. }
  424. }
  425. }
  426. return pClipFace;
  427. }
  428. //-----------------------------------------------------------------------------
  429. // Purpose:
  430. //-----------------------------------------------------------------------------
  431. void CMapOverlay::ClipFace_Destroy( ClipFace_t **ppClipFace )
  432. {
  433. if( *ppClipFace )
  434. {
  435. delete *ppClipFace;
  436. *ppClipFace = NULL;
  437. }
  438. }
  439. //-----------------------------------------------------------------------------
  440. //-----------------------------------------------------------------------------
  441. CMapOverlay::ClipFace_t *CMapOverlay::ClipFace_Copy( ClipFace_t *pSrc )
  442. {
  443. ClipFace_t *pDst = ClipFace_Create( pSrc->m_nPointCount );
  444. if ( pDst )
  445. {
  446. for ( int iPoint = 0; iPoint < pSrc->m_nPointCount; iPoint++ )
  447. {
  448. pDst->m_aPoints[iPoint] = pSrc->m_aPoints[iPoint];
  449. pDst->m_aDispPointUVs[iPoint] = pSrc->m_aDispPointUVs[iPoint];
  450. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  451. {
  452. pDst->m_aTexCoords[iTexCoord][iPoint] = pSrc->m_aTexCoords[iTexCoord][iPoint];
  453. }
  454. pDst->m_aBlends[iPoint].m_nType = pSrc->m_aBlends[iPoint].m_nType;
  455. for ( int iBlend = 0; iBlend < 3; iBlend++ )
  456. {
  457. pDst->m_aBlends[iPoint].m_iPoints[iBlend] = pSrc->m_aBlends[iPoint].m_iPoints[iBlend];
  458. pDst->m_aBlends[iPoint].m_flBlends[iBlend] = pSrc->m_aBlends[iPoint].m_flBlends[iBlend];
  459. }
  460. }
  461. }
  462. return pDst;
  463. }
  464. //-----------------------------------------------------------------------------
  465. //-----------------------------------------------------------------------------
  466. void CMapOverlay::ClipFace_GetBounds( ClipFace_t *pClipFace, Vector &vecMin, Vector &vecMax )
  467. {
  468. if ( pClipFace )
  469. {
  470. vecMin = vecMax = pClipFace->m_aPoints.Element( 0 );
  471. for ( int iPoints = 1; iPoints < pClipFace->m_nPointCount; iPoints++ )
  472. {
  473. Vector vecPoint = pClipFace->m_aPoints.Element( iPoints );
  474. // Min
  475. if ( vecMin.x > vecPoint.x ) { vecMin.x = vecPoint.x; }
  476. if ( vecMin.y > vecPoint.y ) { vecMin.y = vecPoint.y; }
  477. if ( vecMin.z > vecPoint.z ) { vecMin.z = vecPoint.z; }
  478. // Max
  479. if ( vecMax.x < vecPoint.x ) { vecMax.x = vecPoint.x; }
  480. if ( vecMax.y < vecPoint.y ) { vecMax.y = vecPoint.y; }
  481. if ( vecMax.z < vecPoint.z ) { vecMax.z = vecPoint.z; }
  482. }
  483. }
  484. }
  485. //-----------------------------------------------------------------------------
  486. //-----------------------------------------------------------------------------
  487. void CMapOverlay::ClipFace_Clip( ClipFace_t *pClipFace, cplane_t *pClipPlane, float flEpsilon,
  488. ClipFace_t **ppFront, ClipFace_t **ppBack )
  489. {
  490. if ( !pClipFace )
  491. return;
  492. float flDists[128];
  493. int nSides[128];
  494. int nSideCounts[3];
  495. // Initialize
  496. *ppFront = *ppBack = NULL;
  497. // Determine "sidedness" of all the polygon points.
  498. nSideCounts[0] = nSideCounts[1] = nSideCounts[2] = 0;
  499. int iPoint;
  500. for ( iPoint = 0; iPoint < pClipFace->m_nPointCount; iPoint++ )
  501. {
  502. flDists[iPoint] = pClipPlane->normal.Dot( pClipFace->m_aPoints.Element( iPoint ) ) - pClipPlane->dist;
  503. if ( flDists[iPoint] > flEpsilon )
  504. {
  505. nSides[iPoint] = SIDE_FRONT;
  506. }
  507. else if ( flDists[iPoint] < -flEpsilon )
  508. {
  509. nSides[iPoint] = SIDE_BACK;
  510. }
  511. else
  512. {
  513. nSides[iPoint] = SIDE_ON;
  514. }
  515. nSideCounts[nSides[iPoint]]++;
  516. }
  517. // Wrap around (close the polygon).
  518. nSides[iPoint] = nSides[0];
  519. flDists[iPoint] = flDists[0];
  520. // All points in back - no split (copy face to back).
  521. if( !nSideCounts[SIDE_FRONT] )
  522. {
  523. *ppBack = ClipFace_Copy( pClipFace );
  524. return;
  525. }
  526. // All points in front - no split (copy face to front).
  527. if( !nSideCounts[SIDE_BACK] )
  528. {
  529. *ppFront = ClipFace_Copy( pClipFace );
  530. return;
  531. }
  532. // Build new front and back faces. Leave room for two extra points on each side because any
  533. // point might be on the plane, which would put it into both the front and back sides, and then
  534. // we need to allow for an additional vertex created by clipping.
  535. ClipFace_t *pFront = ClipFace_Create( pClipFace->m_nPointCount + 2 );
  536. ClipFace_t *pBack = ClipFace_Create( pClipFace->m_nPointCount + 2 );
  537. if ( !pFront || !pBack )
  538. {
  539. ClipFace_Destroy( &pFront );
  540. ClipFace_Destroy( &pBack );
  541. return;
  542. }
  543. // Reset the counts as they are used to build the surface.
  544. pFront->m_nPointCount = 0;
  545. pBack->m_nPointCount = 0;
  546. // For every point on the face being clipped, determine which side of the clipping plane it is on
  547. // and add it to a either a front list or a back list. Points that are on the plane are added to
  548. // both lists.
  549. for ( iPoint = 0; iPoint < pClipFace->m_nPointCount; iPoint++ )
  550. {
  551. // "On" clip plane.
  552. if ( nSides[iPoint] == SIDE_ON )
  553. {
  554. pFront->m_aPoints[pFront->m_nPointCount] = pClipFace->m_aPoints[iPoint];
  555. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  556. pFront->m_aTexCoords[iTexCoord][pFront->m_nPointCount] = pClipFace->m_aTexCoords[iTexCoord][iPoint];
  557. pFront->m_nPointCount++;
  558. pBack->m_aPoints[pBack->m_nPointCount] = pClipFace->m_aPoints[iPoint];
  559. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  560. pBack->m_aTexCoords[iTexCoord][pBack->m_nPointCount] = pClipFace->m_aTexCoords[iTexCoord][iPoint];
  561. pBack->m_nPointCount++;
  562. continue;
  563. }
  564. // "In back" of clip plane.
  565. if ( nSides[iPoint] == SIDE_BACK )
  566. {
  567. pBack->m_aPoints[pBack->m_nPointCount] = pClipFace->m_aPoints[iPoint];
  568. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  569. pBack->m_aTexCoords[iTexCoord][pBack->m_nPointCount] = pClipFace->m_aTexCoords[iTexCoord][iPoint];
  570. pBack->m_nPointCount++;
  571. }
  572. // "In front" of clip plane.
  573. if ( nSides[iPoint] == SIDE_FRONT )
  574. {
  575. pFront->m_aPoints[pFront->m_nPointCount] = pClipFace->m_aPoints[iPoint];
  576. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  577. pFront->m_aTexCoords[iTexCoord][pFront->m_nPointCount] = pClipFace->m_aTexCoords[iTexCoord][iPoint];
  578. pFront->m_nPointCount++;
  579. }
  580. if ( nSides[iPoint+1] == SIDE_ON || nSides[iPoint+1] == nSides[iPoint] )
  581. continue;
  582. // Split!
  583. float fraction = flDists[iPoint] / ( flDists[iPoint] - flDists[iPoint+1] );
  584. Vector vecPoint = pClipFace->m_aPoints[iPoint] + ( pClipFace->m_aPoints[(iPoint+1)%pClipFace->m_nPointCount] - pClipFace->m_aPoints[iPoint] ) * fraction;
  585. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  586. {
  587. Vector2D vecTexCoord = pClipFace->m_aTexCoords[iTexCoord][iPoint] + ( pClipFace->m_aTexCoords[iTexCoord][(iPoint+1)%pClipFace->m_nPointCount] - pClipFace->m_aTexCoords[iTexCoord][iPoint] ) * fraction;
  588. pFront->m_aTexCoords[iTexCoord][pFront->m_nPointCount] = vecTexCoord;
  589. pBack->m_aTexCoords[iTexCoord][pBack->m_nPointCount] = vecTexCoord;
  590. }
  591. pFront->m_aPoints[pFront->m_nPointCount] = vecPoint;
  592. pFront->m_nPointCount++;
  593. pBack->m_aPoints[pBack->m_nPointCount] = vecPoint;
  594. pBack->m_nPointCount++;
  595. }
  596. *ppFront = pFront;
  597. *ppBack = pBack;
  598. }
  599. //-----------------------------------------------------------------------------
  600. //-----------------------------------------------------------------------------
  601. void CMapOverlay::ClipFace_ClipBarycentric( ClipFace_t *pClipFace, cplane_t *pClipPlane, float flEpsilon,
  602. int iClip, CMapDisp *pDisp,
  603. ClipFace_t **ppFront, ClipFace_t **ppBack )
  604. {
  605. if ( !pClipFace )
  606. return;
  607. float flDists[128];
  608. int nSides[128];
  609. int nSideCounts[3];
  610. // Determine "sidedness" of all the polygon points.
  611. nSideCounts[0] = nSideCounts[1] = nSideCounts[2] = 0;
  612. int iPoint;
  613. for ( iPoint = 0; iPoint < pClipFace->m_nPointCount; iPoint++ )
  614. {
  615. flDists[iPoint] = pClipPlane->normal.Dot( pClipFace->m_aDispPointUVs.Element( iPoint ) ) - pClipPlane->dist;
  616. if ( flDists[iPoint] > flEpsilon )
  617. {
  618. nSides[iPoint] = SIDE_FRONT;
  619. }
  620. else if ( flDists[iPoint] < -flEpsilon )
  621. {
  622. nSides[iPoint] = SIDE_BACK;
  623. }
  624. else
  625. {
  626. nSides[iPoint] = SIDE_ON;
  627. }
  628. nSideCounts[nSides[iPoint]]++;
  629. }
  630. // Wrap around (close the polygon).
  631. nSides[iPoint] = nSides[0];
  632. flDists[iPoint] = flDists[0];
  633. // All points in back - no split (copy face to back).
  634. if( !nSideCounts[SIDE_FRONT] )
  635. {
  636. *ppBack = ClipFace_Copy( pClipFace );
  637. return;
  638. }
  639. // All points in front - no split (copy face to front).
  640. if( !nSideCounts[SIDE_BACK] )
  641. {
  642. *ppFront = ClipFace_Copy( pClipFace );
  643. return;
  644. }
  645. // Build new front and back faces.
  646. // NOTE: We are allowing to go over by 2 and then destroy the surface later. The old system
  647. // allowed for some bad data and we need to be able to load the map and destroy the surface!
  648. int nMaxPointCount = pClipFace->m_nPointCount + 1;
  649. ClipFace_t *pFront = ClipFace_Create( nMaxPointCount + 2 );
  650. ClipFace_t *pBack = ClipFace_Create( nMaxPointCount + 2 );
  651. if ( !pFront || !pBack )
  652. {
  653. ClipFace_Destroy( &pFront );
  654. ClipFace_Destroy( &pBack );
  655. return;
  656. }
  657. // Reset the counts as they are used to build the surface.
  658. pFront->m_nPointCount = 0;
  659. pBack->m_nPointCount = 0;
  660. for ( iPoint = 0; iPoint < pClipFace->m_nPointCount; iPoint++ )
  661. {
  662. // "On" clip plane.
  663. if ( nSides[iPoint] == SIDE_ON )
  664. {
  665. pFront->m_aPoints[pFront->m_nPointCount] = pClipFace->m_aPoints[iPoint];
  666. pFront->m_aDispPointUVs[pFront->m_nPointCount] = pClipFace->m_aDispPointUVs[iPoint];
  667. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  668. pFront->m_aTexCoords[iTexCoord][pFront->m_nPointCount] = pClipFace->m_aTexCoords[iTexCoord][iPoint];
  669. ClipFace_CopyBlendFrom( pFront, &pClipFace->m_aBlends[iPoint] );
  670. pFront->m_nPointCount++;
  671. pBack->m_aPoints[pBack->m_nPointCount] = pClipFace->m_aPoints[iPoint];
  672. pBack->m_aDispPointUVs[pBack->m_nPointCount] = pClipFace->m_aDispPointUVs[iPoint];
  673. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  674. pBack->m_aTexCoords[iTexCoord][pBack->m_nPointCount] = pClipFace->m_aTexCoords[iTexCoord][iPoint];
  675. ClipFace_CopyBlendFrom( pBack, &pClipFace->m_aBlends[iPoint] );
  676. pBack->m_nPointCount++;
  677. continue;
  678. }
  679. // "In back" of clip plane.
  680. if ( nSides[iPoint] == SIDE_BACK )
  681. {
  682. pBack->m_aPoints[pBack->m_nPointCount] = pClipFace->m_aPoints[iPoint];
  683. pBack->m_aDispPointUVs[pBack->m_nPointCount] = pClipFace->m_aDispPointUVs[iPoint];
  684. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  685. pBack->m_aTexCoords[iTexCoord][pBack->m_nPointCount] = pClipFace->m_aTexCoords[iTexCoord][iPoint];
  686. ClipFace_CopyBlendFrom( pBack, &pClipFace->m_aBlends[iPoint] );
  687. pBack->m_nPointCount++;
  688. }
  689. // "In front" of clip plane.
  690. if ( nSides[iPoint] == SIDE_FRONT )
  691. {
  692. pFront->m_aPoints[pFront->m_nPointCount] = pClipFace->m_aPoints[iPoint];
  693. pFront->m_aDispPointUVs[pFront->m_nPointCount] = pClipFace->m_aDispPointUVs[iPoint];
  694. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  695. pFront->m_aTexCoords[iTexCoord][pFront->m_nPointCount] = pClipFace->m_aTexCoords[iTexCoord][iPoint];
  696. ClipFace_CopyBlendFrom( pFront, &pClipFace->m_aBlends[iPoint] );
  697. pFront->m_nPointCount++;
  698. }
  699. if ( nSides[iPoint+1] == SIDE_ON || nSides[iPoint+1] == nSides[iPoint] )
  700. continue;
  701. // Split!
  702. float fraction = flDists[iPoint] / ( flDists[iPoint] - flDists[iPoint+1] );
  703. Vector vecPoint = pClipFace->m_aPoints[iPoint] + ( pClipFace->m_aPoints[(iPoint+1)%pClipFace->m_nPointCount] - pClipFace->m_aPoints[iPoint] ) * fraction;
  704. Vector vecDispPointUV = pClipFace->m_aDispPointUVs[iPoint] + ( pClipFace->m_aDispPointUVs[(iPoint+1)%pClipFace->m_nPointCount] - pClipFace->m_aDispPointUVs[iPoint] ) * fraction;
  705. Vector2D vecUV, vecTexCoord;
  706. PointInQuadToBarycentric( m_pOverlayFace->m_aPoints[0], m_pOverlayFace->m_aPoints[3],
  707. m_pOverlayFace->m_aPoints[2], m_pOverlayFace->m_aPoints[1],
  708. vecPoint, vecUV );
  709. vecUV.x = clamp( vecUV.x, 0.0f, 1.0f );
  710. vecUV.y = clamp( vecUV.y, 0.0f, 1.0f );
  711. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  712. {
  713. TexCoordInQuadFromBarycentric( m_pOverlayFace->m_aTexCoords[iTexCoord][0], m_pOverlayFace->m_aTexCoords[iTexCoord][3],
  714. m_pOverlayFace->m_aTexCoords[iTexCoord][2], m_pOverlayFace->m_aTexCoords[iTexCoord][1],
  715. vecUV, vecTexCoord );
  716. pFront->m_aTexCoords[iTexCoord][pFront->m_nPointCount] = vecTexCoord;
  717. pBack->m_aTexCoords[iTexCoord][pBack->m_nPointCount] = vecTexCoord;
  718. }
  719. pFront->m_aPoints[pFront->m_nPointCount] = vecPoint;
  720. pFront->m_aDispPointUVs[pFront->m_nPointCount] = vecDispPointUV;
  721. ClipFace_BuildBlend( pFront, pDisp, pClipPlane, iClip, vecDispPointUV, vecPoint );
  722. pFront->m_nPointCount++;
  723. pBack->m_aPoints[pBack->m_nPointCount] = vecPoint;
  724. pBack->m_aDispPointUVs[pBack->m_nPointCount] = vecDispPointUV;
  725. ClipFace_BuildBlend( pBack, pDisp, pClipPlane, iClip, vecDispPointUV, vecPoint );
  726. pBack->m_nPointCount++;
  727. }
  728. // Check for a bad surface.
  729. if ( ( pFront->m_nPointCount > nMaxPointCount ) || ( pBack->m_nPointCount > nMaxPointCount ) )
  730. return;
  731. *ppFront = pFront;
  732. *ppBack = pBack;
  733. }
  734. //-----------------------------------------------------------------------------
  735. //-----------------------------------------------------------------------------
  736. void CMapOverlay::ClipFace_PreClipDisp( ClipFace_t *pClipFace, CMapDisp *pDisp )
  737. {
  738. // Valid clip face and/or displacement surface.
  739. if ( !pClipFace || !pDisp )
  740. return;
  741. // Transform all of the overlay points into disp uv space.
  742. for ( int iPoint = 0; iPoint < pClipFace->m_nPointCount; iPoint++ )
  743. {
  744. Vector2D vecTmp;
  745. pDisp->BaseFacePlaneToDispUV( pClipFace->m_aPoints[iPoint], vecTmp );
  746. pClipFace->m_aDispPointUVs[iPoint].x = clamp(vecTmp.x, 0.0f, 1.0f);
  747. pClipFace->m_aDispPointUVs[iPoint].y = clamp(vecTmp.y, 0.0f, 1.0f);
  748. pClipFace->m_aDispPointUVs[iPoint].z = 0.0f;
  749. }
  750. // Set initial point barycentric blend types.
  751. for ( int iPoint = 0; iPoint < pClipFace->m_nPointCount; ++iPoint )
  752. {
  753. Vector2D vecDispUV;
  754. vecDispUV.x = pClipFace->m_aDispPointUVs[iPoint].x;
  755. vecDispUV.y = pClipFace->m_aDispPointUVs[iPoint].y;
  756. int iTris[3];
  757. Vector2D vecVertsUV[3];
  758. GetTriVerts( pDisp, vecDispUV, iTris, vecVertsUV );
  759. float flCoefs[3];
  760. if ( ClipFace_CalcBarycentricCooefs( pDisp, vecVertsUV, vecDispUV, flCoefs ) )
  761. {
  762. ClipFace_ResolveBarycentricClip( pDisp, pClipFace, iPoint, vecDispUV, flCoefs, iTris, vecVertsUV );
  763. }
  764. }
  765. }
  766. //-----------------------------------------------------------------------------
  767. //-----------------------------------------------------------------------------
  768. void CMapOverlay::ClipFace_PostClipDisp( void )
  769. {
  770. }
  771. //-----------------------------------------------------------------------------
  772. //-----------------------------------------------------------------------------
  773. bool CMapOverlay::ClipFace_CalcBarycentricCooefs( CMapDisp *pDisp, Vector2D *pVertsUV,
  774. const Vector2D &vecPointUV, float *pCoefs )
  775. {
  776. // Area in disp UV space is always the same.
  777. float flTotalArea = 0.5f;
  778. float flOOTotalArea = 1.0f / flTotalArea;
  779. int nInterval = pDisp->GetWidth();
  780. Vector2D vecScaledPointUV = vecPointUV * ( nInterval - 1.000001f );
  781. Vector2D vecSegment0, vecSegment1;
  782. // Get the area for cooeficient 0 (pt, v1, v2).
  783. vecSegment0 = pVertsUV[1] - vecScaledPointUV;
  784. vecSegment1 = pVertsUV[2] - vecScaledPointUV;
  785. // Cross
  786. float flSubArea = ( ( vecSegment1.x * vecSegment0.y ) - ( vecSegment0.x * vecSegment1.y ) ) * 0.5f;
  787. pCoefs[0] = flSubArea * flOOTotalArea;
  788. // Get the area for cooeficient 1 (v0, pt, v2).
  789. vecSegment0 = vecScaledPointUV - pVertsUV[0];
  790. vecSegment1 = pVertsUV[2] - pVertsUV[0];
  791. // Cross
  792. flSubArea = ( ( vecSegment1.x * vecSegment0.y ) - ( vecSegment0.x * vecSegment1.y ) ) * 0.5f;
  793. pCoefs[1] = flSubArea * flOOTotalArea;
  794. // Get the area for cooeficient 2 (v0, v1, pt).
  795. vecSegment0 = pVertsUV[1] - pVertsUV[0];
  796. vecSegment1 = vecScaledPointUV - pVertsUV[0];
  797. // Cross
  798. flSubArea = ( ( vecSegment1.x * vecSegment0.y ) - ( vecSegment0.x * vecSegment1.y ) ) * 0.5f;
  799. pCoefs[2] = flSubArea * flOOTotalArea;
  800. float flCoefTotal = pCoefs[0] + pCoefs[1] + pCoefs[2];
  801. if ( FloatMakePositive( 1.0f - flCoefTotal ) < 0.00001f )
  802. return true;
  803. return false;
  804. }
  805. //-----------------------------------------------------------------------------
  806. //-----------------------------------------------------------------------------
  807. void CMapOverlay::ClipFace_ResolveBarycentricClip( CMapDisp *pDisp, ClipFace_t *pClipFace, int iClipFacePoint,
  808. const Vector2D &vecPointUV, float *pCoefs,
  809. int *pTris, Vector2D *pVertsUV )
  810. {
  811. int nInterval = pDisp->GetWidth();
  812. Vector2D vecScaledPointUV = vecPointUV * ( nInterval - 1.000001f );
  813. // Find the number of coefficients "equal" to zero.
  814. int nZeroCount = 0;
  815. bool bZeroPoint[3];
  816. for ( int iVert = 0; iVert < 3; ++iVert )
  817. {
  818. bZeroPoint[iVert] = false;
  819. if ( fabs( pCoefs[iVert] ) < OVERLAY_BARYCENTRIC_EPSILON )
  820. {
  821. nZeroCount++;
  822. bZeroPoint[iVert] = true;
  823. }
  824. }
  825. // Check for points - set to a point.
  826. if ( nZeroCount == 2 )
  827. {
  828. for ( int iVert = 0; iVert < 3; ++iVert )
  829. {
  830. if ( !bZeroPoint[iVert] )
  831. {
  832. pClipFace->m_aBlends[iClipFacePoint].m_nType = OVERLAY_BLENDTYPE_VERT;
  833. pClipFace->m_aBlends[iClipFacePoint].m_iPoints[0] = pTris[iVert];
  834. return;
  835. }
  836. }
  837. }
  838. // Check for edges - setup edge blend.
  839. if ( nZeroCount == 1 )
  840. {
  841. for ( int iVert = 0; iVert < 3; ++iVert )
  842. {
  843. if ( bZeroPoint[iVert] )
  844. {
  845. pClipFace->m_aBlends[iClipFacePoint].m_nType = OVERLAY_BLENDTYPE_EDGE;
  846. pClipFace->m_aBlends[iClipFacePoint].m_iPoints[0] = pTris[(iVert+1)%3];
  847. pClipFace->m_aBlends[iClipFacePoint].m_iPoints[1] = pTris[(iVert+2)%3];
  848. Vector2D vecLength1, vecLength2;
  849. vecLength1 = vecScaledPointUV - pVertsUV[(iVert+1)%3];
  850. vecLength2 = pVertsUV[(iVert+2)%3] - pVertsUV[(iVert+1)%3];
  851. float flBlend = vecLength1.Length() / vecLength2.Length();
  852. pClipFace->m_aBlends[iClipFacePoint].m_flBlends[0] = flBlend;
  853. return;
  854. }
  855. }
  856. }
  857. // Lies inside triangles - setup full barycentric blend.
  858. pClipFace->m_aBlends[iClipFacePoint].m_nType = OVERLAY_BLENDTYPE_BARY;
  859. pClipFace->m_aBlends[iClipFacePoint].m_iPoints[0] = pTris[0];
  860. pClipFace->m_aBlends[iClipFacePoint].m_iPoints[1] = pTris[1];
  861. pClipFace->m_aBlends[iClipFacePoint].m_iPoints[2] = pTris[2];
  862. pClipFace->m_aBlends[iClipFacePoint].m_flBlends[0] = pCoefs[0];
  863. pClipFace->m_aBlends[iClipFacePoint].m_flBlends[1] = pCoefs[1];
  864. pClipFace->m_aBlends[iClipFacePoint].m_flBlends[2] = pCoefs[2];
  865. }
  866. //-----------------------------------------------------------------------------
  867. //-----------------------------------------------------------------------------
  868. int CMapOverlay::ClipFace_GetAxisType( cplane_t *pClipPlane )
  869. {
  870. if ( pClipPlane->normal[0] == 1.0f ) { return OVERLAY_ANGLE90; }
  871. if ( pClipPlane->normal[1] == 1.0f ) { return OVERLAY_ANGLE0; }
  872. if ( ( pClipPlane->normal[0] == 0.707f ) && ( pClipPlane->normal[1] == 0.707f ) ) { return OVERLAY_ANGLE45; }
  873. if ( ( pClipPlane->normal[0] == -0.707f ) && ( pClipPlane->normal[1] == 0.707f ) ) { return OVERLAY_ANGLE135; }
  874. return OVERLAY_ANGLE0;
  875. }
  876. //-----------------------------------------------------------------------------
  877. //-----------------------------------------------------------------------------
  878. void CMapOverlay::ClipFace_BuildBlend( ClipFace_t *pClipFace, CMapDisp *pDisp,
  879. cplane_t *pClipPlane, int iClip,
  880. const Vector &vecUV, const Vector &vecPoint )
  881. {
  882. // Get the displacement space interval.
  883. int nWidth = pDisp->GetWidth();
  884. int nHeight = pDisp->GetHeight();
  885. float flU = vecUV.x * ( nWidth - 1.000001f );
  886. float flV = vecUV.y * ( nHeight - 1.000001f );
  887. // find the triangle the "uv spot" resides in
  888. int nSnapU = static_cast<int>( flU );
  889. int nSnapV = static_cast<int>( flV );
  890. if ( nSnapU == ( nWidth - 1 ) ) { --nSnapU; }
  891. if ( nSnapV == ( nHeight - 1 ) ) { --nSnapV; }
  892. int nNextU = nSnapU + 1;
  893. int nNextV = nSnapV + 1;
  894. float flFracU = flU - static_cast<float>( nSnapU );
  895. float flFracV = flV - static_cast<float>( nSnapV );
  896. int iAxisType = ClipFace_GetAxisType( pClipPlane );
  897. switch( iAxisType )
  898. {
  899. case OVERLAY_ANGLE0:
  900. {
  901. // Vert type
  902. if ( fabs( flFracU ) < OVERLAY_DISPSPACE_EPSILON )
  903. {
  904. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = OVERLAY_BLENDTYPE_VERT;
  905. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[0] = ( nWidth * iClip ) + nSnapU;
  906. }
  907. // Edge type
  908. else
  909. {
  910. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = OVERLAY_BLENDTYPE_EDGE;
  911. int iPoint0 = ( nWidth * iClip ) + nSnapU;
  912. int iPoint1 = ( nWidth * iClip ) + nNextU;
  913. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[0] = iPoint0;
  914. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[1] = iPoint1;
  915. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_flBlends[0] = flFracU;
  916. }
  917. return;
  918. }
  919. case OVERLAY_ANGLE45:
  920. {
  921. // Vert type
  922. if ( ( fabs( flFracU ) < OVERLAY_DISPSPACE_EPSILON ) &&
  923. ( fabs( flFracV ) < OVERLAY_DISPSPACE_EPSILON ) )
  924. {
  925. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = OVERLAY_BLENDTYPE_VERT;
  926. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[0] = ( nWidth * nSnapV ) + nSnapU;
  927. }
  928. // Edge type
  929. else
  930. {
  931. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = OVERLAY_BLENDTYPE_EDGE;
  932. int iPoint0 = ( nWidth * nNextV ) + nSnapU;
  933. int iPoint1 = ( nWidth * nSnapV ) + nNextU;
  934. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[0] = iPoint0;
  935. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[1] = iPoint1;
  936. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_flBlends[0] = flFracU;
  937. }
  938. return;
  939. }
  940. case OVERLAY_ANGLE90:
  941. {
  942. // Vert type
  943. if ( fabs( flFracV ) < OVERLAY_DISPSPACE_EPSILON )
  944. {
  945. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = OVERLAY_BLENDTYPE_VERT;
  946. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[0] = ( nWidth * nSnapV ) + iClip;
  947. }
  948. // Edge type
  949. else
  950. {
  951. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = OVERLAY_BLENDTYPE_EDGE;
  952. int iPoint0 = ( nWidth * nSnapV ) + iClip;
  953. int iPoint1 = ( nWidth * nNextV ) + iClip;
  954. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[0] = iPoint0;
  955. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[1] = iPoint1;
  956. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_flBlends[0] = flFracV;
  957. }
  958. return;
  959. }
  960. case OVERLAY_ANGLE135:
  961. {
  962. // Vert type
  963. if ( ( fabs( flFracU ) < OVERLAY_DISPSPACE_EPSILON ) &&
  964. ( fabs( flFracV ) < OVERLAY_DISPSPACE_EPSILON ) )
  965. {
  966. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = OVERLAY_BLENDTYPE_VERT;
  967. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[0] = ( nWidth * nSnapV ) + nSnapU;
  968. }
  969. // Edge type
  970. else
  971. {
  972. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = OVERLAY_BLENDTYPE_EDGE;
  973. int iPoint0 = ( nWidth * nSnapV ) + nSnapU;
  974. int iPoint1 = ( nWidth * nNextV ) + nNextU;
  975. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[0] = iPoint0;
  976. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[1] = iPoint1;
  977. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_flBlends[0] = flFracU;
  978. }
  979. return;
  980. }
  981. }
  982. }
  983. //-----------------------------------------------------------------------------
  984. //-----------------------------------------------------------------------------
  985. void CMapOverlay::ClipFace_CopyBlendFrom( ClipFace_t *pClipFace, BlendData_t *pBlendFrom )
  986. {
  987. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_nType = pBlendFrom->m_nType;
  988. for ( int iPoint = 0; iPoint < 3; iPoint++ )
  989. {
  990. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_iPoints[iPoint] = pBlendFrom->m_iPoints[iPoint];
  991. pClipFace->m_aBlends[pClipFace->m_nPointCount].m_flBlends[iPoint] = pBlendFrom->m_flBlends[iPoint];
  992. }
  993. }
  994. //-----------------------------------------------------------------------------
  995. //-----------------------------------------------------------------------------
  996. void CMapOverlay::ClipFace_BuildFacesFromBlendedData( ClipFace_t *pClipFace )
  997. {
  998. if( pClipFace->m_pBuildFace->HasDisp() )
  999. {
  1000. EditDispHandle_t handle = pClipFace->m_pBuildFace->GetDisp();
  1001. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  1002. Vector vecPos[3];
  1003. for ( int iPoint = 0; iPoint < pClipFace->m_nPointCount; iPoint++ )
  1004. {
  1005. if ( pClipFace->m_aBlends[iPoint].m_nType == OVERLAY_BLENDTYPE_VERT )
  1006. {
  1007. pDisp->GetVert( pClipFace->m_aBlends[iPoint].m_iPoints[0], vecPos[0] );
  1008. pClipFace->m_aPoints[iPoint] = vecPos[0];
  1009. }
  1010. else if ( pClipFace->m_aBlends[iPoint].m_nType == OVERLAY_BLENDTYPE_EDGE )
  1011. {
  1012. pDisp->GetVert( pClipFace->m_aBlends[iPoint].m_iPoints[0], vecPos[0] );
  1013. pDisp->GetVert( pClipFace->m_aBlends[iPoint].m_iPoints[1], vecPos[1] );
  1014. pClipFace->m_aPoints[iPoint] = vecPos[0] + ( vecPos[1] - vecPos[0] ) * pClipFace->m_aBlends[iPoint].m_flBlends[0];
  1015. }
  1016. else if ( pClipFace->m_aBlends[iPoint].m_nType == OVERLAY_BLENDTYPE_BARY )
  1017. {
  1018. pDisp->GetVert( pClipFace->m_aBlends[iPoint].m_iPoints[0], vecPos[0] );
  1019. pDisp->GetVert( pClipFace->m_aBlends[iPoint].m_iPoints[1], vecPos[1] );
  1020. pDisp->GetVert( pClipFace->m_aBlends[iPoint].m_iPoints[2], vecPos[2] );
  1021. pClipFace->m_aPoints[iPoint] = ( vecPos[0] * pClipFace->m_aBlends[iPoint].m_flBlends[0] ) +
  1022. ( vecPos[1] * pClipFace->m_aBlends[iPoint].m_flBlends[1] ) +
  1023. ( vecPos[2] * pClipFace->m_aBlends[iPoint].m_flBlends[2] );
  1024. }
  1025. }
  1026. }
  1027. }
  1028. //=============================================================================
  1029. //
  1030. // CMapOverlay Material Functions
  1031. //
  1032. int MaxComponent( const Vector &v0 )
  1033. {
  1034. int nMax = 0;
  1035. if ( FloatMakePositive( v0[1] ) > FloatMakePositive( v0[nMax] ) )
  1036. {
  1037. nMax = 1;
  1038. }
  1039. if ( FloatMakePositive( v0[2] ) > FloatMakePositive( v0[nMax] ) )
  1040. {
  1041. nMax = 2;
  1042. }
  1043. return nMax;
  1044. }
  1045. //-----------------------------------------------------------------------------
  1046. //-----------------------------------------------------------------------------
  1047. void CMapOverlay::Material_Clear( void )
  1048. {
  1049. m_Material.m_pTexture = NULL;
  1050. m_Material.m_vecTextureU.Init( 0.0f, 1.0f );
  1051. m_Material.m_vecTextureV.Init( 0.0f, 1.0f );
  1052. }
  1053. //-----------------------------------------------------------------------------
  1054. // Purpose:
  1055. //-----------------------------------------------------------------------------
  1056. void CMapOverlay::Material_TexCoordInit( void )
  1057. {
  1058. int nMaxU = MaxComponent( m_Basis.m_vecAxes[OVERLAY_BASIS_U] );
  1059. int nMaxV = MaxComponent( m_Basis.m_vecAxes[OVERLAY_BASIS_V] );
  1060. bool bUPos = m_Basis.m_vecAxes[OVERLAY_BASIS_U][nMaxU] >= 0.0f;
  1061. bool bVPos = m_Basis.m_vecAxes[OVERLAY_BASIS_V][nMaxV] >= 0.0f;
  1062. m_Material.m_vecTextureU.Init( 0.0f, 1.0f );
  1063. m_Material.m_vecTextureV.Init( 1.0f, 0.0f );
  1064. if ( ( bUPos && !bVPos ) || ( !bUPos && bVPos ) )
  1065. {
  1066. m_Material.m_vecTextureU.Init( 1.0f, 0.0f );
  1067. m_Material.m_vecTextureV.Init( 0.0f, 1.0f );
  1068. }
  1069. Material_UpdateParentKey();
  1070. }
  1071. //-----------------------------------------------------------------------------
  1072. //-----------------------------------------------------------------------------
  1073. void CMapOverlay::Material_Copy( Material_t *pSrc, Material_t *pDst )
  1074. {
  1075. pDst->m_pTexture = pSrc->m_pTexture;
  1076. pDst->m_vecTextureU = pSrc->m_vecTextureU;
  1077. pDst->m_vecTextureV = pSrc->m_vecTextureV;
  1078. }
  1079. //-----------------------------------------------------------------------------
  1080. // Purpose:
  1081. //-----------------------------------------------------------------------------
  1082. void CMapOverlay::Material_UpdateParentKey( void )
  1083. {
  1084. char szValue[80];
  1085. CMapEntity *pEntity = ( CMapEntity* )GetParent();
  1086. if ( pEntity )
  1087. {
  1088. sprintf( szValue, "%g", m_Material.m_vecTextureU.x );
  1089. pEntity->NotifyChildKeyChanged( this, "StartU", szValue );
  1090. sprintf( szValue, "%g", m_Material.m_vecTextureU.y );
  1091. pEntity->NotifyChildKeyChanged( this, "EndU", szValue );
  1092. sprintf( szValue, "%g", m_Material.m_vecTextureV.x );
  1093. pEntity->NotifyChildKeyChanged( this, "StartV", szValue );
  1094. sprintf( szValue, "%g", m_Material.m_vecTextureV.y );
  1095. pEntity->NotifyChildKeyChanged( this, "EndV", szValue );
  1096. }
  1097. }
  1098. //=============================================================================
  1099. //
  1100. // CMapOverlay Functions
  1101. //
  1102. //-----------------------------------------------------------------------------
  1103. // Purpose: Construct a CMapOverlay instance.
  1104. //-----------------------------------------------------------------------------
  1105. CMapOverlay::CMapOverlay() : CMapSideList( "sides" )
  1106. {
  1107. Basis_Clear();
  1108. Handles_Clear();
  1109. Material_Clear();
  1110. m_bLoaded = false;
  1111. m_pOverlayFace = NULL;
  1112. m_uiFlags = 0;
  1113. }
  1114. //-----------------------------------------------------------------------------
  1115. // Purpose: Destruct a CMapOverlay instance.
  1116. //-----------------------------------------------------------------------------
  1117. CMapOverlay::~CMapOverlay()
  1118. {
  1119. ClipFace_Destroy( &m_pOverlayFace );
  1120. m_aRenderFaces.PurgeAndDeleteElements();
  1121. }
  1122. //-----------------------------------------------------------------------------
  1123. //-----------------------------------------------------------------------------
  1124. CMapClass *CMapOverlay::CreateMapOverlay( CHelperInfo *pInfo, CMapEntity *pParent )
  1125. {
  1126. CMapOverlay *pOverlay = new CMapOverlay;
  1127. return pOverlay;
  1128. }
  1129. //-----------------------------------------------------------------------------
  1130. // Purpose: Called after the entire map has been loaded. This allows the object
  1131. // to perform any linking with other map objects or to do other operations
  1132. // that require all world objects to be present.
  1133. // Input : pWorld - The world that we are in.
  1134. //-----------------------------------------------------------------------------
  1135. void CMapOverlay::PostloadWorld( CMapWorld *pWorld )
  1136. {
  1137. CMapSideList::PostloadWorld( pWorld );
  1138. // Support older overlay versions which didn't have specific basis axes.
  1139. if ( !Basis_IsValid() )
  1140. {
  1141. Basis_BuildFromSideList();
  1142. }
  1143. Handles_Build3D();
  1144. DoClip();
  1145. CalcBounds();
  1146. m_bLoaded = true;
  1147. }
  1148. //-----------------------------------------------------------------------------
  1149. //-----------------------------------------------------------------------------
  1150. CMapClass *CMapOverlay::Copy( bool bUpdateDependencies )
  1151. {
  1152. CMapOverlay *pCopy = new CMapOverlay;
  1153. if ( pCopy )
  1154. {
  1155. pCopy->CopyFrom( this, bUpdateDependencies );
  1156. }
  1157. return pCopy;
  1158. }
  1159. void CMapOverlay::Handles_FixOrder()
  1160. {
  1161. static bool s_FixingHandles = false;
  1162. // make sure that handle order and plane normal are in sync so CCW culling works correctly
  1163. Vector vNormal = GetNormalFromPoints( m_Handles.m_vec3D[0], m_Handles.m_vec3D[1], m_Handles.m_vec3D[2] );
  1164. if ( DotProduct( vNormal, m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL]) < 0.5 )
  1165. {
  1166. // dont try to fix twice
  1167. if ( s_FixingHandles )
  1168. {
  1169. Assert( !s_FixingHandles );
  1170. return;
  1171. }
  1172. s_FixingHandles = true;
  1173. // Flip handles.
  1174. Vector2D vecCoords[OVERLAY_HANDLES_COUNT];
  1175. for ( int iHandle = 0; iHandle < OVERLAY_HANDLES_COUNT; iHandle++ )
  1176. {
  1177. vecCoords[4-iHandle-1] = m_Handles.m_vecBasisCoords[iHandle];
  1178. }
  1179. for ( int iHandle = 0; iHandle < OVERLAY_HANDLES_COUNT; iHandle++ )
  1180. {
  1181. m_Handles.m_vecBasisCoords[iHandle] = vecCoords[iHandle];
  1182. }
  1183. // rebuild handles
  1184. Handles_Build3D();
  1185. s_FixingHandles = false;
  1186. }
  1187. }
  1188. //-----------------------------------------------------------------------------
  1189. //-----------------------------------------------------------------------------
  1190. CMapClass *CMapOverlay::CopyFrom( CMapClass *pObject, bool bUpdateDependencies )
  1191. {
  1192. // Verify the object is of the correct type and cast.
  1193. Assert( pObject->IsMapClass( MAPCLASS_TYPE( CMapOverlay ) ) );
  1194. CMapOverlay *pFrom = ( CMapOverlay* )pObject;
  1195. if ( pFrom )
  1196. {
  1197. // Copy the parent class data.
  1198. CMapSideList::CopyFrom( pObject, bUpdateDependencies );
  1199. // Copy basis data.
  1200. Basis_Copy( &pFrom->m_Basis, &m_Basis );
  1201. // Copy handle data.
  1202. Handles_Copy( &pFrom->m_Handles, &m_Handles );
  1203. // Copy material data.
  1204. Material_Copy( &pFrom->m_Material, &m_Material );
  1205. }
  1206. return this;
  1207. }
  1208. //-----------------------------------------------------------------------------
  1209. // Purpose: Notify me when a key has had a data change, so the overlay can
  1210. // update itself appropriately.
  1211. // Input: szKey - the key that changed
  1212. // szValue - the new value (key/data pair)
  1213. //-----------------------------------------------------------------------------
  1214. void CMapOverlay::OnParentKeyChanged( const char* szKey, const char* szValue )
  1215. {
  1216. // Pass this to the sidelist first.
  1217. CMapSideList::OnParentKeyChanged( szKey, szValue );
  1218. // Read side data.
  1219. if ( !stricmp( szKey, "sides" ) )
  1220. {
  1221. if ( m_Faces.Count() > 0 )
  1222. {
  1223. Basis_SetFace( m_Faces.Element( 0 ) );
  1224. }
  1225. }
  1226. // Read geometry data.
  1227. float flDummy;
  1228. if ( !stricmp( szKey, "uv0" ) )
  1229. {
  1230. sscanf( szValue, "%f %f %f", &m_Handles.m_vecBasisCoords[0].x, &m_Handles.m_vecBasisCoords[0].y, &flDummy );
  1231. m_Basis.m_nAxesFlip[0] = ( int )flDummy;
  1232. }
  1233. if ( !stricmp( szKey, "uv1" ) )
  1234. {
  1235. sscanf( szValue, "%f %f %f", &m_Handles.m_vecBasisCoords[1].x, &m_Handles.m_vecBasisCoords[1].y, &flDummy );
  1236. m_Basis.m_nAxesFlip[1] = ( int )flDummy;
  1237. }
  1238. if ( !stricmp( szKey, "uv2" ) )
  1239. {
  1240. sscanf( szValue, "%f %f %f", &m_Handles.m_vecBasisCoords[2].x, &m_Handles.m_vecBasisCoords[2].y, &flDummy );
  1241. m_Basis.m_nAxesFlip[2] = ( int )flDummy;
  1242. }
  1243. if ( !stricmp( szKey, "uv3" ) )
  1244. {
  1245. sscanf( szValue, "%f %f %f", &m_Handles.m_vecBasisCoords[3].x, &m_Handles.m_vecBasisCoords[3].y, &flDummy );
  1246. }
  1247. // Read basis data.
  1248. if ( !stricmp( szKey, "BasisOrigin" ) )
  1249. {
  1250. sscanf( szValue, "%f %f %f", &m_Basis.m_vecOrigin.x, &m_Basis.m_vecOrigin.y, &m_Basis.m_vecOrigin.z );
  1251. }
  1252. if ( !stricmp( szKey, "BasisU" ) )
  1253. {
  1254. sscanf( szValue, "%f %f %f", &m_Basis.m_vecAxes[OVERLAY_BASIS_U].x, &m_Basis.m_vecAxes[OVERLAY_BASIS_U].y, &m_Basis.m_vecAxes[OVERLAY_BASIS_U].z );
  1255. }
  1256. if ( !stricmp( szKey, "BasisV" ) )
  1257. {
  1258. sscanf( szValue, "%f %f %f", &m_Basis.m_vecAxes[OVERLAY_BASIS_V].x, &m_Basis.m_vecAxes[OVERLAY_BASIS_V].y, &m_Basis.m_vecAxes[OVERLAY_BASIS_V].z );
  1259. }
  1260. if ( !stricmp( szKey, "BasisNormal" ) )
  1261. {
  1262. sscanf( szValue, "%f %f %f", &m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].x, &m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].y, &m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].z );
  1263. }
  1264. // Read material data.
  1265. if ( !stricmp( szKey, "material" ) )
  1266. {
  1267. // Get the new material.
  1268. IEditorTexture *pTex = g_Textures.FindActiveTexture( szValue );
  1269. if ( !pTex )
  1270. return;
  1271. // Save the new material.
  1272. m_Material.m_pTexture = pTex;
  1273. }
  1274. if ( !stricmp( szKey, "StartU" ) )
  1275. {
  1276. m_Material.m_vecTextureU.x = atof( szValue );
  1277. }
  1278. if ( !stricmp( szKey, "EndU" ) )
  1279. {
  1280. m_Material.m_vecTextureU.y = atof( szValue );
  1281. }
  1282. if ( !stricmp( szKey, "StartV" ) )
  1283. {
  1284. m_Material.m_vecTextureV.x = atof( szValue );
  1285. }
  1286. if ( !stricmp( szKey, "EndV" ) )
  1287. {
  1288. m_Material.m_vecTextureV.y = atof( szValue );
  1289. }
  1290. if ( m_bLoaded )
  1291. {
  1292. // Clip - this needs to be done for everything other than a material change, so go ahead.
  1293. DoClip();
  1294. // Post updated.
  1295. PostUpdate( Notify_Changed );
  1296. }
  1297. }
  1298. //-----------------------------------------------------------------------------
  1299. //-----------------------------------------------------------------------------
  1300. void CMapOverlay::OnUndoRedo( void )
  1301. {
  1302. PostModified();
  1303. }
  1304. //-----------------------------------------------------------------------------
  1305. //-----------------------------------------------------------------------------
  1306. void CMapOverlay::CalcBounds( BOOL bFullUpdate )
  1307. {
  1308. // Pass the info along.
  1309. CMapSideList::CalcBounds( bFullUpdate );
  1310. // Verify that we have valid data.
  1311. if ( !Basis_IsValid() )
  1312. return;
  1313. // Calculate the 2d bounds.
  1314. Vector vecMins, vecMaxs;
  1315. vecMins = m_Origin - Vector( 2.0f, 2.0f, 2.0f );
  1316. vecMaxs = m_Origin + Vector( 2.0f, 2.0f, 2.0f );
  1317. // Reset bounds
  1318. m_CullBox.ResetBounds();
  1319. m_Render2DBox.ResetBounds();
  1320. for ( int iHandle = 0; iHandle < 4; ++iHandle )
  1321. {
  1322. for ( int iAxis = 0; iAxis < 3; ++iAxis )
  1323. {
  1324. // Min
  1325. if ( m_Handles.m_vec3D[iHandle][iAxis] < vecMins[iAxis] )
  1326. {
  1327. vecMins[iAxis] = m_Handles.m_vec3D[iHandle][iAxis];
  1328. }
  1329. // Max
  1330. if ( m_Handles.m_vec3D[iHandle][iAxis] > vecMaxs[iAxis] )
  1331. {
  1332. vecMaxs[iAxis] = m_Handles.m_vec3D[iHandle][iAxis];
  1333. }
  1334. }
  1335. }
  1336. // Don't allow for NULL bounds.
  1337. for ( int iAxis = 0; iAxis < 3; ++iAxis )
  1338. {
  1339. if( ( vecMaxs[iAxis] - vecMins[iAxis] ) == 0.0f )
  1340. {
  1341. vecMins[iAxis] -= 0.5f;
  1342. vecMaxs[iAxis] += 0.5f;
  1343. }
  1344. }
  1345. // Update the bounds.
  1346. m_CullBox.UpdateBounds( vecMins, vecMaxs );
  1347. m_BoundingBox = m_CullBox;
  1348. m_Render2DBox.UpdateBounds( vecMins, vecMaxs );
  1349. }
  1350. //-----------------------------------------------------------------------------
  1351. // Purpose:
  1352. //-----------------------------------------------------------------------------
  1353. void CMapOverlay::PostModified( void )
  1354. {
  1355. // update face and origin
  1356. if ( m_Faces.Count() > 0 )
  1357. {
  1358. Basis_SetFace( m_Faces.Element( 0 ) );
  1359. Basis_UpdateOrigin();
  1360. }
  1361. else
  1362. {
  1363. m_Basis.m_pFace = NULL;
  1364. }
  1365. Handles_Build3D();
  1366. DoClip();
  1367. }
  1368. //-----------------------------------------------------------------------------
  1369. // Purpose:
  1370. // Input : pTransBox -
  1371. //-----------------------------------------------------------------------------
  1372. void CMapOverlay::DoTransform( const VMatrix &matrix )
  1373. {
  1374. BaseClass::DoTransform( matrix );
  1375. VMatrix tmpMatrix = matrix;
  1376. // erase move component
  1377. tmpMatrix.SetTranslation( vec3_origin );
  1378. // check if matrix would still change something
  1379. if ( !tmpMatrix.IsIdentity() )
  1380. {
  1381. // make sure axes are normalized (they should be anyways)
  1382. m_Basis.m_vecAxes[OVERLAY_BASIS_U].NormalizeInPlace();
  1383. m_Basis.m_vecAxes[OVERLAY_BASIS_V].NormalizeInPlace();
  1384. Vector vecU = m_Basis.m_vecAxes[OVERLAY_BASIS_U];
  1385. Vector vecV = m_Basis.m_vecAxes[OVERLAY_BASIS_V];
  1386. Vector vecNormal = m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL];
  1387. TransformPoint( tmpMatrix, vecU );
  1388. TransformPoint( tmpMatrix, vecV );
  1389. TransformPoint( tmpMatrix, vecNormal );
  1390. float fScaleU = vecU.Length();
  1391. float fScaleV = vecV.Length();
  1392. float flScaleNormal = vecNormal.Length();
  1393. bool bIsUnit = ( fequal( fScaleU, 1.0f, 0.0001 ) && fequal( fScaleV, 1.0f, 0.0001 ) && fequal( flScaleNormal, 1.0f, 0.0001 ) );
  1394. bool bIsPerp = ( fequal( DotProduct( vecU, vecV ), 0.0f, 0.0025 ) && fequal( DotProduct( vecU, vecNormal ), 0.0f, 0.0025 ) && fequal( DotProduct( vecV, vecNormal ), 0.0f, 0.0025 ) );
  1395. // if ( fequal(fScaleU,1,0.0001) && fequal(fScaleV,1,0.0001) && fequal(DotProduct( vecU, vecV ),0,0.0025) )
  1396. if ( bIsUnit && bIsPerp )
  1397. {
  1398. // transformation doesnt scale or shear anything, so just update base axes
  1399. m_Basis.m_vecAxes[OVERLAY_BASIS_U] = vecU;
  1400. m_Basis.m_vecAxes[OVERLAY_BASIS_V] = vecV;
  1401. m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL] = vecNormal;
  1402. }
  1403. else
  1404. {
  1405. // more complex transformation, move UV coordinates, but leave base axes
  1406. for ( int iHandle=0; iHandle<OVERLAY_HANDLES_COUNT;iHandle++)
  1407. {
  1408. Vector2D vecUV = m_Handles.m_vecBasisCoords[iHandle];
  1409. Vector vecPos = ( vecUV.x * m_Basis.m_vecAxes[OVERLAY_BASIS_U] + vecUV.y * m_Basis.m_vecAxes[OVERLAY_BASIS_V] );
  1410. // to transform in world space
  1411. TransformPoint( tmpMatrix, vecPos );
  1412. vecUV.x = m_Basis.m_vecAxes[OVERLAY_BASIS_U].Dot( vecPos );
  1413. vecUV.y = m_Basis.m_vecAxes[OVERLAY_BASIS_V].Dot( vecPos );
  1414. m_Handles.m_vecBasisCoords[iHandle] = vecUV;
  1415. }
  1416. if ( !Options.IsLockingTextures() )
  1417. {
  1418. // scale textures if locking is off
  1419. m_Material.m_vecTextureU *= fScaleU;
  1420. m_Material.m_vecTextureV *= fScaleV;
  1421. Material_UpdateParentKey();
  1422. }
  1423. }
  1424. }
  1425. // Send modified notice.
  1426. PostModified();
  1427. Handles_UpdateParentKey();
  1428. }
  1429. //-----------------------------------------------------------------------------
  1430. // Purpose: Notifies us that a copy of ourselves was pasted.
  1431. //-----------------------------------------------------------------------------
  1432. void CMapOverlay::OnPaste( CMapClass *pCopy, CMapWorld *pSourceWorld, CMapWorld *pDestWorld,
  1433. const CMapObjectList &OriginalList, CMapObjectList &NewList)
  1434. {
  1435. //
  1436. // NOTE: currently pCopy is the Overlay being pasted into the world, "this" is
  1437. // what is being copied from
  1438. //
  1439. CMapSideList::OnPaste( pCopy, pSourceWorld, pDestWorld, OriginalList, NewList );
  1440. CMapOverlay *pOverlay = dynamic_cast<CMapOverlay*>( pCopy );
  1441. if ( pOverlay )
  1442. {
  1443. pOverlay->Basis_BuildFromSideList();
  1444. pOverlay->PostModified();
  1445. }
  1446. }
  1447. //-----------------------------------------------------------------------------
  1448. // Purpose: Notifies us that we created a copy of ourselves (a clone).
  1449. //-----------------------------------------------------------------------------
  1450. void CMapOverlay::OnClone( CMapClass *pClone, CMapWorld *pWorld,
  1451. const CMapObjectList &OriginalList, CMapObjectList &NewList )
  1452. {
  1453. CMapSideList::OnClone( pClone, pWorld, OriginalList, NewList );
  1454. CMapOverlay *pOverlay = dynamic_cast<CMapOverlay*>( pClone );
  1455. if ( pOverlay )
  1456. {
  1457. if ( ( GetOverlayType() && OVERLAY_TYPE_SHORE ) == 0 )
  1458. {
  1459. // Update the clone's solid dependencies (this doesn't happen on clone generally).
  1460. int nFaceCount = pOverlay->GetFaceCount();
  1461. for ( int iFace = 0; iFace < nFaceCount; ++iFace )
  1462. {
  1463. CMapFace *pFace = pOverlay->GetFace( iFace );
  1464. CMapSolid *pSolid = ( CMapSolid* )pFace->GetParent();
  1465. pOverlay->UpdateDependency( NULL, pSolid );
  1466. }
  1467. }
  1468. pOverlay->PostModified();
  1469. }
  1470. }
  1471. //-----------------------------------------------------------------------------
  1472. // Purpose: Notifys this decal of a change to a solid that it is attached to.
  1473. //-----------------------------------------------------------------------------
  1474. void CMapOverlay::OnNotifyDependent( CMapClass *pObject, Notify_Dependent_t eNotifyType )
  1475. {
  1476. // Chain to base class FIRST so it can rebuild the face list if necessary.
  1477. CMapSideList::OnNotifyDependent( pObject, eNotifyType );
  1478. //
  1479. // NOTE: the solid moving (changing) can update the overlay/solid(face) dependency
  1480. // so "rebuild" the overlay
  1481. //
  1482. switch ( eNotifyType )
  1483. {
  1484. case Notify_Changed:
  1485. case Notify_Undo:
  1486. case Notify_Transform:
  1487. {
  1488. PostModified();
  1489. break;
  1490. }
  1491. case Notify_Removed:
  1492. case Notify_Clipped:
  1493. {
  1494. m_aRenderFaces.Purge();
  1495. PostModified();
  1496. break;
  1497. }
  1498. case Notify_Rebuild:
  1499. {
  1500. UpdateDispBarycentric();
  1501. break;
  1502. }
  1503. case Notify_Rebuild_Full:
  1504. {
  1505. DoClip();
  1506. CenterEntity();
  1507. Handles_Build3D();
  1508. break;
  1509. }
  1510. }
  1511. }
  1512. //-----------------------------------------------------------------------------
  1513. //-----------------------------------------------------------------------------
  1514. void CMapOverlay::Render3D( CRender3D *pRender )
  1515. {
  1516. int nFaceCount = m_aRenderFaces.Count();
  1517. if ( nFaceCount != 0 )
  1518. {
  1519. // dont draw textured during manipulating
  1520. if ( GetSelectionState() != SELECT_MODIFY )
  1521. {
  1522. // Bind the matrial -- if there is one!!
  1523. bool bTextured = false;
  1524. if ( m_Material.m_pTexture )
  1525. {
  1526. pRender->BindTexture( m_Material.m_pTexture );
  1527. pRender->PushRenderMode( RENDER_MODE_TEXTURED );
  1528. bTextured = true;
  1529. }
  1530. else
  1531. {
  1532. // Default state.
  1533. pRender->PushRenderMode( RENDER_MODE_FLAT );
  1534. }
  1535. for ( int iFace = 0; iFace < nFaceCount; iFace++ )
  1536. {
  1537. ClipFace_t *pRenderFace = m_aRenderFaces.Element( iFace );
  1538. if( !pRenderFace )
  1539. continue;
  1540. MaterialPrimitiveType_t type = MATERIAL_POLYGON;
  1541. // Get a dynamic mesh.
  1542. CMeshBuilder meshBuilder;
  1543. CMatRenderContextPtr pRenderContext( MaterialSystemInterface() );
  1544. IMesh* pMesh = pRenderContext->GetDynamicMesh();
  1545. meshBuilder.Begin( pMesh, type, pRenderFace->m_nPointCount );
  1546. for ( int iPoint = 0; iPoint < pRenderFace->m_nPointCount; iPoint++ )
  1547. {
  1548. if ( !bTextured )
  1549. {
  1550. meshBuilder.Color3ub( 0, 128, 0 );
  1551. }
  1552. else
  1553. {
  1554. meshBuilder.TexCoord2f( 0, pRenderFace->m_aTexCoords[0][iPoint].x, pRenderFace->m_aTexCoords[0][iPoint].y );
  1555. meshBuilder.TexCoord2f( 2, pRenderFace->m_aTexCoords[1][iPoint].x, pRenderFace->m_aTexCoords[1][iPoint].y );
  1556. meshBuilder.Color4ub( 255, 255, 255, 255 );
  1557. }
  1558. meshBuilder.Position3f( pRenderFace->m_aPoints[iPoint].x, pRenderFace->m_aPoints[iPoint].y, pRenderFace->m_aPoints[iPoint].z );
  1559. meshBuilder.AdvanceVertex();
  1560. }
  1561. meshBuilder.End();
  1562. pMesh->Draw();
  1563. }
  1564. pRender->PopRenderMode();
  1565. }
  1566. // Render wireframe on top when seleted.
  1567. if ( GetSelectionState() != SELECT_NONE )
  1568. {
  1569. pRender->PushRenderMode( RENDER_MODE_WIREFRAME );
  1570. for ( int iFace = 0; iFace < nFaceCount; iFace++ )
  1571. {
  1572. ClipFace_t *pRenderFace = m_aRenderFaces.Element( iFace );
  1573. if( !pRenderFace )
  1574. continue;
  1575. MaterialPrimitiveType_t type = MATERIAL_LINE_LOOP;
  1576. // get a dynamic mesh
  1577. CMeshBuilder meshBuilder;
  1578. CMatRenderContextPtr pRenderContext( MaterialSystemInterface() );
  1579. IMesh* pMesh = pRenderContext->GetDynamicMesh();
  1580. meshBuilder.Begin( pMesh, type, pRenderFace->m_nPointCount );
  1581. for( int iPoint = 0; iPoint < pRenderFace->m_nPointCount; iPoint++ )
  1582. {
  1583. meshBuilder.Color3ub( 0, 255, 0 );
  1584. meshBuilder.Position3f( pRenderFace->m_aPoints[iPoint].x, pRenderFace->m_aPoints[iPoint].y, pRenderFace->m_aPoints[iPoint].z );
  1585. meshBuilder.AdvanceVertex();
  1586. }
  1587. meshBuilder.End();
  1588. pMesh->Draw();
  1589. }
  1590. pRender->PopRenderMode();
  1591. }
  1592. }
  1593. // Render the handles - if selected or in overlay tool mode.
  1594. if ( ( ToolManager()->GetActiveToolID() == TOOL_OVERLAY ) && Basis_IsValid() && IsSelected() )
  1595. {
  1596. Handles_Render3D( pRender );
  1597. }
  1598. }
  1599. //-----------------------------------------------------------------------------
  1600. // Purpose: Clip the overlay "face" to all of the faces in the overlay sidelist.
  1601. // The sidelist defines all faces affected by the "overlay."
  1602. //-----------------------------------------------------------------------------
  1603. void CMapOverlay::DoClip( void )
  1604. {
  1605. // Check to see if we have any faces to clip against.
  1606. int nFaceCount = m_Faces.Count();
  1607. if( nFaceCount == 0 )
  1608. return;
  1609. // Destroy the render face cache.
  1610. m_aRenderFaces.Purge();
  1611. // clip the overlay against all faces in the sidelist
  1612. for ( int iFace = 0; iFace < nFaceCount; iFace++ )
  1613. {
  1614. CMapFace *pFace = m_Faces.Element( iFace );
  1615. if ( pFace )
  1616. {
  1617. PreClip();
  1618. DoClipFace( pFace );
  1619. PostClip();
  1620. }
  1621. }
  1622. }
  1623. //-----------------------------------------------------------------------------
  1624. //-----------------------------------------------------------------------------
  1625. void CMapOverlay::PreClip( void )
  1626. {
  1627. //
  1628. // Create the initial face to be clipped - the overlay.
  1629. //
  1630. m_pOverlayFace = ClipFace_Create( OVERLAY_HANDLES_COUNT );
  1631. if ( m_pOverlayFace )
  1632. {
  1633. for ( int iPoint = 0; iPoint < OVERLAY_HANDLES_COUNT; iPoint++ )
  1634. {
  1635. OverlayUVToOverlayPlane( m_Handles.m_vecBasisCoords[iPoint], m_pOverlayFace->m_aPoints[iPoint] );
  1636. // translate texture UV to texture coords:
  1637. Vector2D vTexCoord;
  1638. switch( iPoint )
  1639. {
  1640. case 0 : vTexCoord = Vector2D(m_Material.m_vecTextureU.x, m_Material.m_vecTextureV.x); break;
  1641. case 1 : vTexCoord = Vector2D(m_Material.m_vecTextureU.x, m_Material.m_vecTextureV.y); break;
  1642. case 2 : vTexCoord = Vector2D(m_Material.m_vecTextureU.y, m_Material.m_vecTextureV.y); break;
  1643. case 3 : vTexCoord = Vector2D(m_Material.m_vecTextureU.y, m_Material.m_vecTextureV.x); break;
  1644. default : Assert( iPoint <= OVERLAY_HANDLES_COUNT);
  1645. }
  1646. m_pOverlayFace->m_aTexCoords[0][iPoint] = vTexCoord;
  1647. if ( m_Basis.m_pFace->HasDisp() )
  1648. {
  1649. EditDispHandle_t handle = m_Basis.m_pFace->GetDisp();
  1650. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  1651. if ( pDisp )
  1652. {
  1653. Vector2D vecTmp;
  1654. pDisp->BaseFacePlaneToDispUV( m_pOverlayFace->m_aPoints[iPoint], vecTmp );
  1655. m_pOverlayFace->m_aDispPointUVs[iPoint].x = vecTmp.x;
  1656. m_pOverlayFace->m_aDispPointUVs[iPoint].y = vecTmp.y;
  1657. m_pOverlayFace->m_aDispPointUVs[iPoint].z = 0.0f;
  1658. }
  1659. }
  1660. }
  1661. // The second set of texcoords on the overlay is used for alpha by certain shaders,
  1662. // and they want to stretch the texture across the whole overlay.
  1663. m_pOverlayFace->m_aTexCoords[1][0].Init( 0, 0 );
  1664. m_pOverlayFace->m_aTexCoords[1][1].Init( 0, 1 );
  1665. m_pOverlayFace->m_aTexCoords[1][2].Init( 1, 1 );
  1666. m_pOverlayFace->m_aTexCoords[1][3].Init( 1, 0 );
  1667. }
  1668. }
  1669. //-----------------------------------------------------------------------------
  1670. //-----------------------------------------------------------------------------
  1671. void CMapOverlay::PostClip( void )
  1672. {
  1673. ClipFace_Destroy( &m_pOverlayFace );
  1674. }
  1675. //-----------------------------------------------------------------------------
  1676. //-----------------------------------------------------------------------------
  1677. void CMapOverlay::DoClipFace( CMapFace *pFace )
  1678. {
  1679. // Valid face?
  1680. Assert( pFace != NULL );
  1681. if( !pFace )
  1682. return;
  1683. // Copy the original overlay to the "clipped" overlay.
  1684. ClipFace_t *pClippedFace = ClipFace_Copy( m_pOverlayFace );
  1685. if ( !pClippedFace )
  1686. return;
  1687. //
  1688. // Project all face points into the overlay plane.
  1689. //
  1690. int nPointCount = pFace->nPoints;
  1691. Vector *pPoints = new Vector[nPointCount];
  1692. int nEdgePlaneCount = nPointCount;
  1693. cplane_t *pEdgePlanes = new cplane_t[nEdgePlaneCount];
  1694. if ( !pPoints || !pEdgePlanes )
  1695. {
  1696. delete [] pPoints;
  1697. delete [] pEdgePlanes;
  1698. return;
  1699. }
  1700. for ( int iPoint = 0; iPoint < nPointCount; iPoint++ )
  1701. {
  1702. WorldToOverlayPlane( pFace->Points[iPoint], pPoints[iPoint] );
  1703. }
  1704. // Create the face clipping planes (edges cross overlay plane normal).
  1705. BuildEdgePlanes( pPoints, nPointCount, pEdgePlanes, nEdgePlaneCount );
  1706. //
  1707. // Clip overlay against all the edge planes.
  1708. //
  1709. for ( int iClipPlane = 0; iClipPlane < nEdgePlaneCount; iClipPlane++ )
  1710. {
  1711. ClipFace_t *pFront = NULL;
  1712. ClipFace_t *pBack = NULL;
  1713. if ( pClippedFace )
  1714. {
  1715. // Clip the overlay and delete the data (we are done with it - we are only interested in what is left).
  1716. ClipFace_Clip( pClippedFace, &pEdgePlanes[iClipPlane], OVERLAY_WORLDSPACE_EPSILON, &pFront, &pBack );
  1717. ClipFace_Destroy( &pClippedFace );
  1718. // Keep the backside -- if it exists and continue clipping.
  1719. if ( pBack )
  1720. {
  1721. pClippedFace = pBack;
  1722. }
  1723. // Destroy the front side -- if it exists.
  1724. if ( pFront )
  1725. {
  1726. ClipFace_Destroy( &pFront );
  1727. }
  1728. }
  1729. }
  1730. //
  1731. // Free temporary memory (clip planes and point).
  1732. //
  1733. delete [] pPoints;
  1734. delete [] pEdgePlanes;
  1735. //
  1736. // If it exists, move points from the overlay plane back into
  1737. // the base face plane.
  1738. //
  1739. if ( !pClippedFace )
  1740. return;
  1741. for ( int iPoint = 0; iPoint < pClippedFace->m_nPointCount; iPoint++ )
  1742. {
  1743. Vector2D vecUV;
  1744. PointInQuadToBarycentric( m_pOverlayFace->m_aPoints[0], m_pOverlayFace->m_aPoints[3],
  1745. m_pOverlayFace->m_aPoints[2], m_pOverlayFace->m_aPoints[1],
  1746. pClippedFace->m_aPoints[iPoint], vecUV );
  1747. Vector vecTmp;
  1748. OverlayPlaneToWorld( pFace, pClippedFace->m_aPoints[iPoint], vecTmp );
  1749. pClippedFace->m_aPoints[iPoint] = vecTmp;
  1750. Vector2D vecTexCoord;
  1751. for ( int iTexCoord=0; iTexCoord < NUM_CLIPFACE_TEXCOORDS; iTexCoord++ )
  1752. {
  1753. TexCoordInQuadFromBarycentric( m_pOverlayFace->m_aTexCoords[iTexCoord][0], m_pOverlayFace->m_aTexCoords[iTexCoord][3],
  1754. m_pOverlayFace->m_aTexCoords[iTexCoord][2], m_pOverlayFace->m_aTexCoords[iTexCoord][1],
  1755. vecUV, vecTexCoord );
  1756. pClippedFace->m_aTexCoords[iTexCoord][iPoint] = vecTexCoord;
  1757. }
  1758. }
  1759. //
  1760. // If the face has a displacement map -- continue clipping.
  1761. //
  1762. if( pFace->HasDisp() )
  1763. {
  1764. DoClipDisp( pFace, pClippedFace );
  1765. }
  1766. // Done - save it!
  1767. else
  1768. {
  1769. pClippedFace->m_pBuildFace = pFace;
  1770. m_aRenderFaces.AddToTail( pClippedFace );
  1771. }
  1772. }
  1773. //-----------------------------------------------------------------------------
  1774. //-----------------------------------------------------------------------------
  1775. bool CMapOverlay::BuildEdgePlanes( Vector const *pPoints, int nPointCount,
  1776. cplane_t *pEdgePlanes, int nEdgePlaneCount )
  1777. {
  1778. for ( int iPoint = 0; iPoint < nPointCount; iPoint++ )
  1779. {
  1780. Vector vecEdge;
  1781. vecEdge = pPoints[(iPoint+1)%nPointCount] - pPoints[iPoint];
  1782. VectorNormalize( vecEdge );
  1783. pEdgePlanes[iPoint].normal = m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].Cross( vecEdge );
  1784. pEdgePlanes[iPoint].dist = pEdgePlanes[iPoint].normal.Dot( pPoints[iPoint] );
  1785. // Check normal facing.
  1786. float flDist = pEdgePlanes[iPoint].normal.Dot( pPoints[(iPoint+2)%nPointCount] ) - pEdgePlanes[iPoint].dist;
  1787. if( flDist > 0.0f )
  1788. {
  1789. // flip
  1790. pEdgePlanes[iPoint].normal.Negate();
  1791. pEdgePlanes[iPoint].dist = -pEdgePlanes[iPoint].dist;
  1792. }
  1793. }
  1794. return true;
  1795. }
  1796. //-----------------------------------------------------------------------------
  1797. // Purpose:
  1798. //-----------------------------------------------------------------------------
  1799. void CMapOverlay::Disp_ClipFragments( CMapDisp *pDisp, ClipFaces_t &aDispFragments )
  1800. {
  1801. cplane_t clipPlane;
  1802. // Cache the displacement interval.
  1803. int nInterval = pDisp->GetWidth() - 1;
  1804. // Displacement-space clipping in V.
  1805. clipPlane.normal.Init( 1.0f, 0.0f, 0.0f );
  1806. Disp_DoClip( pDisp, aDispFragments, clipPlane, 1.0f, nInterval, 1, nInterval, 1 );
  1807. // Displacement-space clipping in U.
  1808. clipPlane.normal.Init( 0.0f, 1.0f, 0.0f );
  1809. Disp_DoClip( pDisp, aDispFragments, clipPlane, 1.0f, nInterval, 1, nInterval, 1 );
  1810. // Displacement-space clipping UV from top-left to bottom-right.
  1811. clipPlane.normal.Init( 0.707f, 0.707f, 0.0f ); // 45 degrees
  1812. Disp_DoClip( pDisp, aDispFragments, clipPlane, 0.707f, nInterval, 2, ( nInterval * 2 - 1 ), 2 );
  1813. // Displacement-space clipping UV from bottom-left to top-right.
  1814. clipPlane.normal.Init( -0.707f, 0.707f, 0.0f ); // 135 degrees
  1815. Disp_DoClip( pDisp, aDispFragments, clipPlane, 0.707f, nInterval, -( nInterval - 2 ), ( nInterval - 1 ), 2 );
  1816. }
  1817. //-----------------------------------------------------------------------------
  1818. // Purpose:
  1819. //-----------------------------------------------------------------------------
  1820. void CMapOverlay::Disp_DoClip( CMapDisp *pDisp, ClipFaces_t &aDispFragments,
  1821. cplane_t &clipPlane, float clipDistStart, int nInterval,
  1822. int nLoopStart, int nLoopEnd, int nLoopInc )
  1823. {
  1824. // Setup interval information.
  1825. float flInterval = static_cast<float>( nInterval );
  1826. float flOOInterval = 1.0f / flInterval;
  1827. // Holds the current set of clipped faces.
  1828. ClipFaces_t aClippedFragments;
  1829. for ( int iInterval = nLoopStart; iInterval < nLoopEnd; iInterval += nLoopInc )
  1830. {
  1831. // Copy the current list to clipped face list.
  1832. aClippedFragments.CopyArray( aDispFragments.Base(), aDispFragments.Count() );
  1833. aDispFragments.Purge();
  1834. // Clip in V.
  1835. int nFragCount = aClippedFragments.Count();
  1836. for ( int iFrag = 0; iFrag < nFragCount; iFrag++ )
  1837. {
  1838. ClipFace_t *pClipFrag = aClippedFragments[iFrag];
  1839. if ( pClipFrag )
  1840. {
  1841. ClipFace_t *pFront = NULL, *pBack = NULL;
  1842. clipPlane.dist = clipDistStart * ( ( float )iInterval * flOOInterval );
  1843. ClipFace_ClipBarycentric( pClipFrag, &clipPlane, OVERLAY_DISPSPACE_EPSILON, iInterval, pDisp, &pFront, &pBack );
  1844. ClipFace_Destroy( &pClipFrag );
  1845. if ( pFront )
  1846. {
  1847. aDispFragments.AddToTail( pFront );
  1848. }
  1849. if ( pBack )
  1850. {
  1851. aDispFragments.AddToTail( pBack );
  1852. }
  1853. }
  1854. }
  1855. }
  1856. // Clean up!
  1857. aClippedFragments.Purge();
  1858. }
  1859. //-----------------------------------------------------------------------------
  1860. //-----------------------------------------------------------------------------
  1861. void CMapOverlay::DoClipDisp( CMapFace *pFace, ClipFace_t *pClippedFace )
  1862. {
  1863. // Get the displacement data.
  1864. EditDispHandle_t handle = pFace->GetDisp();
  1865. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  1866. // Initialize local clip data.
  1867. ClipFace_PreClipDisp( pClippedFace, pDisp );
  1868. // Setup clipped face lists.
  1869. ClipFaces_t aCurrentFaces;
  1870. aCurrentFaces.AddToTail( pClippedFace );
  1871. Disp_ClipFragments( pDisp, aCurrentFaces );
  1872. //
  1873. // Project points back onto the displacement surface.
  1874. //
  1875. int nFaceCount = aCurrentFaces.Count();
  1876. for( int iFace = 0; iFace < nFaceCount; iFace++ )
  1877. {
  1878. ClipFace_t *pClipFace = aCurrentFaces[iFace];
  1879. if ( pClipFace )
  1880. {
  1881. // Save for re-building later!
  1882. pClipFace->m_pBuildFace = pFace;
  1883. m_aRenderFaces.AddToTail( aCurrentFaces[iFace] );
  1884. ClipFace_BuildFacesFromBlendedData( pClipFace );
  1885. }
  1886. }
  1887. // Clean up!
  1888. aCurrentFaces.Purge();
  1889. }
  1890. //-----------------------------------------------------------------------------
  1891. //-----------------------------------------------------------------------------
  1892. void CMapOverlay::HandlesReset( void )
  1893. {
  1894. m_Handles.m_iHit = -1;
  1895. }
  1896. //-----------------------------------------------------------------------------
  1897. //-----------------------------------------------------------------------------
  1898. bool CMapOverlay::HandlesHitTest( CMapView *pView, const Vector2D &vPoint )
  1899. {
  1900. int handleRadius = 8;
  1901. for ( int iPoint = 0; iPoint < 4; iPoint++ )
  1902. {
  1903. Vector2D vHandle;
  1904. pView->WorldToClient( vHandle, m_Handles.m_vec3D[iPoint] );
  1905. if ( vPoint.x < (vHandle.x-handleRadius) || vPoint.x > ( vHandle.x+handleRadius) )
  1906. continue;
  1907. if ( vPoint.y < (vHandle.y-handleRadius) || vPoint.y > ( vHandle.y+handleRadius) )
  1908. continue;
  1909. m_Handles.m_iHit = iPoint;
  1910. return true;
  1911. }
  1912. return false;
  1913. }
  1914. //-----------------------------------------------------------------------------
  1915. //-----------------------------------------------------------------------------
  1916. void CMapOverlay::HandlesDragTo( Vector &vecImpact, CMapFace *pFace )
  1917. {
  1918. // Check handle index range.
  1919. if ( ( m_Handles.m_iHit < 0 ) || ( m_Handles.m_iHit > 3 ) )
  1920. return;
  1921. // Save
  1922. m_Handles.m_vec3D[m_Handles.m_iHit] = vecImpact;
  1923. // Project the point into the overlay plane (from face/disp).
  1924. Vector vecOverlay;
  1925. Vector2D vecUVOverlay;
  1926. Handles_SurfToOverlayPlane( pFace, vecImpact, vecOverlay );
  1927. OverlayPlaneToOverlayUV( vecOverlay, vecUVOverlay );
  1928. m_Handles.m_vecBasisCoords[m_Handles.m_iHit] = vecUVOverlay;
  1929. }
  1930. //-----------------------------------------------------------------------------
  1931. //-----------------------------------------------------------------------------
  1932. void CMapOverlay::HandleMoveTo( int iHandle, Vector &vecPoint, CMapFace *pFace )
  1933. {
  1934. if ( ( iHandle < 0 ) || ( iHandle > 3 ) )
  1935. return;
  1936. m_Handles.m_vec3D[iHandle] = vecPoint;
  1937. // Project the point into the overlay plane (from face/disp).
  1938. Vector vecOverlay;
  1939. Vector2D vecUVOverlay;
  1940. Handles_SurfToOverlayPlane( pFace, vecPoint, vecOverlay );
  1941. OverlayPlaneToOverlayUV( vecOverlay, vecUVOverlay );
  1942. m_Handles.m_vecBasisCoords[iHandle] = vecUVOverlay;
  1943. }
  1944. //-----------------------------------------------------------------------------
  1945. // Purpose:
  1946. //-----------------------------------------------------------------------------
  1947. void CMapOverlay::SetTexCoords( Vector2D vecTexCoords[4] )
  1948. {
  1949. m_Material.m_vecTextureU.x = vecTexCoords[0][0];
  1950. m_Material.m_vecTextureV.x = vecTexCoords[0][1];
  1951. // m_Material.m_vecTextureU.x = vecTexCoord[1][0];
  1952. m_Material.m_vecTextureV.y = vecTexCoords[1][1];
  1953. m_Material.m_vecTextureU.y = vecTexCoords[2][0];
  1954. // m_Material.m_vecTextureV.y = vecTexCoord[2][1];
  1955. // m_Material.m_vecTextureU.y = vecTexCoord[3][0];
  1956. // m_Material.m_vecTextureV.x = vecTexCoord[3][1];
  1957. }
  1958. //-----------------------------------------------------------------------------
  1959. //-----------------------------------------------------------------------------
  1960. void CMapOverlay::UpdateDispBarycentric( void )
  1961. {
  1962. //
  1963. // Project points back onto the displacement surface.
  1964. //
  1965. int nFaceCount = m_aRenderFaces.Count();
  1966. for ( int iFace = 0; iFace < nFaceCount; iFace++ )
  1967. {
  1968. // Get the current face and remove it from the list.
  1969. ClipFace_t *pClipFace = m_aRenderFaces[iFace];
  1970. if ( pClipFace )
  1971. {
  1972. if ( pClipFace->m_pBuildFace->HasDisp() )
  1973. {
  1974. ClipFace_BuildFacesFromBlendedData( pClipFace );
  1975. }
  1976. }
  1977. }
  1978. // Update the entity position.
  1979. CenterEntity();
  1980. // Update the handles.
  1981. Handles_Build3D();
  1982. }
  1983. //-----------------------------------------------------------------------------
  1984. // Purpose:
  1985. //-----------------------------------------------------------------------------
  1986. void CMapOverlay::CenterEntity( void )
  1987. {
  1988. // Center in overlay plane.
  1989. Vector vecTotal;
  1990. Vector vecHandle;
  1991. vecTotal.Init();
  1992. for( int iHandle = 0; iHandle < OVERLAY_HANDLES_COUNT; ++iHandle )
  1993. {
  1994. OverlayUVToOverlayPlane( m_Handles.m_vecBasisCoords[iHandle], vecHandle );
  1995. vecTotal += vecHandle;
  1996. }
  1997. vecTotal *= 0.25f;
  1998. // Center in overlay uv-space.
  1999. Vector2D vecNewCenter;
  2000. OverlayPlaneToOverlayUV( vecTotal, vecNewCenter );
  2001. for( int iHandle = 0; iHandle < OVERLAY_HANDLES_COUNT; ++iHandle )
  2002. {
  2003. m_Handles.m_vecBasisCoords[iHandle] -= vecNewCenter;
  2004. }
  2005. // Update the entity's origin.
  2006. m_Basis.m_vecOrigin = vecTotal;
  2007. CMapEntity *pEntity = ( CMapEntity* )GetParent();
  2008. if ( pEntity )
  2009. {
  2010. Vector vecSurfPoint;
  2011. OverlayPlaneToSurfFromList( vecTotal, vecSurfPoint );
  2012. pEntity->SetOrigin( vecSurfPoint );
  2013. }
  2014. // Update the property box.
  2015. Basis_UpdateParentKey();
  2016. Handles_UpdateParentKey();
  2017. }
  2018. //-----------------------------------------------------------------------------
  2019. //-----------------------------------------------------------------------------
  2020. void CMapOverlay::GetPlane( cplane_t &plane )
  2021. {
  2022. plane.normal = m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL];
  2023. plane.dist = plane.normal.Dot( m_Basis.m_vecOrigin );
  2024. }
  2025. //-----------------------------------------------------------------------------
  2026. //-----------------------------------------------------------------------------
  2027. void CMapOverlay::GetHandlePos( int iHandle, Vector &vecPos )
  2028. {
  2029. Assert( iHandle >= 0 );
  2030. Assert( iHandle < 4 );
  2031. vecPos = m_Handles.m_vec3D[iHandle];
  2032. }
  2033. //-----------------------------------------------------------------------------
  2034. //-----------------------------------------------------------------------------
  2035. void CMapOverlay::SideList_Init( CMapFace *pFace )
  2036. {
  2037. // Valid face?
  2038. if ( !pFace )
  2039. return;
  2040. // Purge side list as this should be the initial face!
  2041. m_Faces.Purge();
  2042. m_Faces.AddToTail( pFace );
  2043. if ( ( GetOverlayType() && OVERLAY_TYPE_SHORE ) == 0 )
  2044. {
  2045. // Update dependencies.
  2046. UpdateDependency( NULL, ( CMapSolid* )pFace->GetParent() );
  2047. UpdateParentKey();
  2048. }
  2049. // Initialize the overlay.
  2050. Basis_Init( pFace );
  2051. PostModified();
  2052. }
  2053. //-----------------------------------------------------------------------------
  2054. // Purpose:
  2055. //-----------------------------------------------------------------------------
  2056. void CMapOverlay::SideList_AddFace( CMapFace *pFace )
  2057. {
  2058. // Valid face?
  2059. if ( !pFace )
  2060. return;
  2061. // Purge side list as this should be the initial face!
  2062. m_Faces.AddToTail( pFace );
  2063. if ( ( GetOverlayType() && OVERLAY_TYPE_SHORE ) == 0 )
  2064. {
  2065. // Update dependencies.
  2066. UpdateDependency( NULL, ( CMapSolid* )pFace->GetParent() );
  2067. UpdateParentKey();
  2068. }
  2069. PostModified();
  2070. }
  2071. //=============================================================================
  2072. //
  2073. // Overlay Utility Functions
  2074. //
  2075. //-----------------------------------------------------------------------------
  2076. //-----------------------------------------------------------------------------
  2077. void CMapOverlay::OverlayUVToOverlayPlane( const Vector2D &vecUV, Vector &vecOverlayPoint )
  2078. {
  2079. vecOverlayPoint = ( vecUV.x * m_Basis.m_vecAxes[OVERLAY_BASIS_U] +
  2080. vecUV.y * m_Basis.m_vecAxes[OVERLAY_BASIS_V] );
  2081. vecOverlayPoint += m_Basis.m_vecOrigin;
  2082. }
  2083. //-----------------------------------------------------------------------------
  2084. //-----------------------------------------------------------------------------
  2085. void CMapOverlay::OverlayPlaneToOverlayUV( const Vector &vecOverlayPoint, Vector2D &vecUV )
  2086. {
  2087. Vector vecDelta;
  2088. vecDelta = vecOverlayPoint - m_Basis.m_vecOrigin;
  2089. vecUV.x = m_Basis.m_vecAxes[OVERLAY_BASIS_U].Dot( vecDelta );
  2090. vecUV.y = m_Basis.m_vecAxes[OVERLAY_BASIS_V].Dot( vecDelta );
  2091. }
  2092. //-----------------------------------------------------------------------------
  2093. //-----------------------------------------------------------------------------
  2094. void CMapOverlay::WorldToOverlayPlane( const Vector &vecWorldPoint, Vector &vecOverlayPoint )
  2095. {
  2096. Vector vecDelta = vecWorldPoint - m_Basis.m_vecOrigin;
  2097. float flDist = m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL].Dot( vecDelta );
  2098. vecOverlayPoint = vecWorldPoint - ( m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL] * flDist );
  2099. }
  2100. //-----------------------------------------------------------------------------
  2101. //-----------------------------------------------------------------------------
  2102. void CMapOverlay::OverlayPlaneToWorld( CMapFace *pFace, const Vector &vecOverlayPoint,
  2103. Vector &vecWorldPoint )
  2104. {
  2105. // Create the overlay plane - the base face plane.
  2106. cplane_t surfacePlane;
  2107. pFace->GetFaceNormal( surfacePlane.normal );
  2108. VectorNormalize( surfacePlane.normal );
  2109. Vector vecPoint;
  2110. pFace->GetPoint( vecPoint, 0 );
  2111. surfacePlane.dist = surfacePlane.normal.Dot( vecPoint );
  2112. float flDistToSurface = surfacePlane.normal.Dot( vecOverlayPoint ) - surfacePlane.dist;
  2113. float flDist = flDistToSurface;
  2114. float flDot = surfacePlane.normal.Dot( m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL] );
  2115. if ( flDot != 0.0f )
  2116. {
  2117. flDist = ( 1.0f / flDot ) * flDistToSurface;
  2118. }
  2119. vecWorldPoint = vecOverlayPoint - ( m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL] * flDist );
  2120. }
  2121. //-----------------------------------------------------------------------------
  2122. //-----------------------------------------------------------------------------
  2123. void CMapOverlay::OverlayPlaneToSurfFromList( const Vector &vecOverlayPoint, Vector &vecSurfPoint )
  2124. {
  2125. // Initialize the point with the overlay point.
  2126. vecSurfPoint = vecOverlayPoint;
  2127. int nFaceCount = GetFaceCount();
  2128. CUtlVector<Vector> aPoints;
  2129. CUtlVector<cplane_t> aPlanes;
  2130. for ( int iFace = 0; iFace < nFaceCount; ++iFace )
  2131. {
  2132. CMapFace *pFace = GetFace( iFace );
  2133. if ( !pFace )
  2134. continue;
  2135. // Set points.
  2136. aPoints.Purge();
  2137. aPoints.SetSize( pFace->nPoints );
  2138. aPlanes.Purge();
  2139. aPlanes.SetSize( pFace->nPoints );
  2140. // Project all the face points into the overlay plane.
  2141. for ( int iPoint = 0; iPoint < pFace->nPoints; ++iPoint )
  2142. {
  2143. WorldToOverlayPlane( pFace->Points[iPoint], aPoints[iPoint] );
  2144. }
  2145. // Create edge planes for clipping.
  2146. BuildEdgePlanes( aPoints.Base(), aPoints.Count(), aPlanes.Base(), aPlanes.Count() );
  2147. // Check to see if a point lies behind all of the edge planes - this is our face.
  2148. int iPlane;
  2149. for ( iPlane = 0; iPlane < aPlanes.Count(); ++iPlane )
  2150. {
  2151. float flDist = aPlanes[iPlane].normal.Dot( vecOverlayPoint ) - aPlanes[iPlane].dist;
  2152. if( flDist >= 0.0f )
  2153. break;
  2154. }
  2155. // Point lies outside off at least one plane.
  2156. if( iPlane != aPlanes.Count() )
  2157. {
  2158. continue;
  2159. }
  2160. // Project the point up to the base face plane (displacement if necessary).
  2161. OverlayPlaneToWorld( pFace, vecOverlayPoint, vecSurfPoint );
  2162. if( pFace->HasDisp() )
  2163. {
  2164. Vector2D vecTmp;
  2165. EditDispHandle_t handle = pFace->GetDisp();
  2166. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  2167. pDisp->BaseFacePlaneToDispUV( vecSurfPoint, vecTmp );
  2168. pDisp->DispUVToSurf( vecTmp, vecSurfPoint, NULL, NULL );
  2169. }
  2170. // Clean-up.
  2171. aPoints.Purge();
  2172. aPlanes.Purge();
  2173. return;
  2174. }
  2175. // Clean-up.
  2176. aPoints.Purge();
  2177. aPlanes.Purge();
  2178. }
  2179. //-----------------------------------------------------------------------------
  2180. // Purpose:
  2181. //-----------------------------------------------------------------------------
  2182. bool CMapOverlay::EntityOnSurfFromListToBaseFacePlane( const Vector &vecWorldPoint, Vector &vecBasePoint )
  2183. {
  2184. int nFaceCount = GetFaceCount();
  2185. for ( int iFace = 0; iFace < nFaceCount; ++iFace )
  2186. {
  2187. CMapFace *pFace = GetFace( iFace );
  2188. if ( !pFace )
  2189. continue;
  2190. if ( !pFace->HasDisp() )
  2191. continue;
  2192. EditDispHandle_t handle = pFace->GetDisp();
  2193. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  2194. if ( pDisp->SurfToBaseFacePlane( vecWorldPoint, vecBasePoint ) )
  2195. return true;
  2196. }
  2197. return false;
  2198. }
  2199. //-----------------------------------------------------------------------------
  2200. // Purpose:
  2201. //-----------------------------------------------------------------------------
  2202. void CMapOverlay::GetTriVerts( CMapDisp *pDisp, const Vector2D &vecSurfUV, int *pTris, Vector2D *pVertsUV )
  2203. {
  2204. // Get the displacement width.
  2205. int nWidth = pDisp->GetWidth();
  2206. int nHeight = pDisp->GetHeight();
  2207. // scale the u, v coordinates the displacement grid size
  2208. float flU = vecSurfUV.x * ( nWidth - 1.000001f );
  2209. float flV = vecSurfUV.y * ( nHeight - 1.000001f );
  2210. // find the triangle the "uv spot" resides in
  2211. int nSnapU = static_cast<int>( flU );
  2212. int nSnapV = static_cast<int>( flV );
  2213. if ( nSnapU == ( nWidth - 1 ) ) { --nSnapU; }
  2214. if ( nSnapV == ( nHeight - 1 ) ) { --nSnapV; }
  2215. int nNextU = nSnapU + 1;
  2216. int nNextV = nSnapV + 1;
  2217. // Fractional portion
  2218. float flFracU = flU - static_cast<float>( nSnapU );
  2219. float flFracV = flV - static_cast<float>( nSnapV );
  2220. bool bOdd = ( ( ( nSnapV * nWidth ) + nSnapU ) % 2 ) == 1;
  2221. if ( bOdd )
  2222. {
  2223. if( ( flFracU + flFracV ) >= ( 1.0f + OVERLAY_DISPSPACE_EPSILON ) )
  2224. {
  2225. pVertsUV[0].x = nSnapU; pVertsUV[0].y = nNextV;
  2226. pVertsUV[1].x = nNextU; pVertsUV[1].y = nNextV;
  2227. pVertsUV[2].x = nNextU; pVertsUV[2].y = nSnapV;
  2228. }
  2229. else
  2230. {
  2231. pVertsUV[0].x = nSnapU; pVertsUV[0].y = nSnapV;
  2232. pVertsUV[1].x = nSnapU; pVertsUV[1].y = nNextV;
  2233. pVertsUV[2].x = nNextU; pVertsUV[2].y = nSnapV;
  2234. }
  2235. }
  2236. else
  2237. {
  2238. if ( flFracU < flFracV )
  2239. {
  2240. pVertsUV[0].x = nSnapU; pVertsUV[0].y = nSnapV;
  2241. pVertsUV[1].x = nSnapU; pVertsUV[1].y = nNextV;
  2242. pVertsUV[2].x = nNextU; pVertsUV[2].y = nNextV;
  2243. }
  2244. else
  2245. {
  2246. pVertsUV[0].x = nSnapU; pVertsUV[0].y = nSnapV;
  2247. pVertsUV[1].x = nNextU; pVertsUV[1].y = nNextV;
  2248. pVertsUV[2].x = nNextU; pVertsUV[2].y = nSnapV;
  2249. }
  2250. }
  2251. // Calculate the triangle indices.
  2252. for( int iVert = 0; iVert < 3; ++iVert )
  2253. {
  2254. pTris[iVert] = pVertsUV[iVert].y * nWidth + pVertsUV[iVert].x;
  2255. }
  2256. }
  2257. //-----------------------------------------------------------------------------
  2258. // Purpose:
  2259. //-----------------------------------------------------------------------------
  2260. void CMapOverlay::SetMaterial( const char *szMaterialName )
  2261. {
  2262. // Get the new material.
  2263. IEditorTexture *pTex = g_Textures.FindActiveTexture( szMaterialName );
  2264. if ( !pTex )
  2265. return;
  2266. // Save the new material.
  2267. m_Material.m_pTexture = pTex;
  2268. }
  2269. //-----------------------------------------------------------------------------
  2270. // Purpose:
  2271. //-----------------------------------------------------------------------------
  2272. ChunkFileResult_t CMapOverlay::SaveDataToVMF( CChunkFile *pFile, CSaveInfo *pSaveInfo )
  2273. {
  2274. ChunkFileResult_t eResult = pFile->BeginChunk("overlaydata");
  2275. // Save the material name.
  2276. if ( eResult == ChunkFile_Ok )
  2277. {
  2278. eResult = pFile->WriteKeyValue( "material", m_Material.m_pTexture->GetName() );
  2279. }
  2280. // Save the u,v data.
  2281. if ( eResult == ChunkFile_Ok )
  2282. {
  2283. eResult = pFile->WriteKeyValueFloat( "StartU", m_Material.m_vecTextureU.x );
  2284. }
  2285. if ( eResult == ChunkFile_Ok )
  2286. {
  2287. eResult = pFile->WriteKeyValueFloat( "EndU", m_Material.m_vecTextureU.y );
  2288. }
  2289. if ( eResult == ChunkFile_Ok )
  2290. {
  2291. eResult = pFile->WriteKeyValueFloat( "StartV", m_Material.m_vecTextureV.x );
  2292. }
  2293. if ( eResult == ChunkFile_Ok )
  2294. {
  2295. eResult = pFile->WriteKeyValueFloat( "EndV", m_Material.m_vecTextureV.y );
  2296. }
  2297. // Basis data.
  2298. if ( eResult == ChunkFile_Ok )
  2299. {
  2300. eResult = pFile->WriteKeyValueVector3( "BasisOrigin", m_Basis.m_vecOrigin );
  2301. }
  2302. if ( eResult == ChunkFile_Ok )
  2303. {
  2304. eResult = pFile->WriteKeyValueVector3( "BasisU", m_Basis.m_vecAxes[OVERLAY_BASIS_U] );
  2305. }
  2306. if ( eResult == ChunkFile_Ok )
  2307. {
  2308. eResult = pFile->WriteKeyValueVector3( "BasisV", m_Basis.m_vecAxes[OVERLAY_BASIS_V] );
  2309. }
  2310. if ( eResult == ChunkFile_Ok )
  2311. {
  2312. eResult = pFile->WriteKeyValueVector3( "BasisNormal", m_Basis.m_vecAxes[OVERLAY_BASIS_NORMAL] );
  2313. }
  2314. if ( eResult == ChunkFile_Ok )
  2315. {
  2316. Vector vecTmp( m_Handles.m_vecBasisCoords[0].x, m_Handles.m_vecBasisCoords[0].y, ( float )m_Basis.m_nAxesFlip[0] );
  2317. eResult = pFile->WriteKeyValueVector3( "uv0", vecTmp );
  2318. }
  2319. if ( eResult == ChunkFile_Ok )
  2320. {
  2321. Vector vecTmp( m_Handles.m_vecBasisCoords[1].x, m_Handles.m_vecBasisCoords[1].y, ( float )m_Basis.m_nAxesFlip[1] );
  2322. eResult = pFile->WriteKeyValueVector3( "uv1", vecTmp );
  2323. }
  2324. if ( eResult == ChunkFile_Ok )
  2325. {
  2326. Vector vecTmp( m_Handles.m_vecBasisCoords[2].x, m_Handles.m_vecBasisCoords[2].y, ( float )m_Basis.m_nAxesFlip[2] );
  2327. eResult = pFile->WriteKeyValueVector3( "uv2", vecTmp );
  2328. }
  2329. if ( eResult == ChunkFile_Ok )
  2330. {
  2331. Vector vecTmp( m_Handles.m_vecBasisCoords[3].x, m_Handles.m_vecBasisCoords[3].y, 0.0f );
  2332. eResult = pFile->WriteKeyValueVector3( "uv3", vecTmp );
  2333. }
  2334. // Sidelist.
  2335. if ( eResult == ChunkFile_Ok )
  2336. {
  2337. char szSetValue[KEYVALUE_MAX_VALUE_LENGTH];
  2338. CMapWorld::FaceID_FaceListsToString( szSetValue, sizeof( szSetValue ), &m_Faces, NULL );
  2339. eResult = pFile->WriteKeyValue( "sides", szSetValue );
  2340. }
  2341. if ( eResult == ChunkFile_Ok )
  2342. {
  2343. eResult = pFile->EndChunk();
  2344. }
  2345. return eResult;
  2346. }