Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1543 lines
39 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdafx.h>
  8. #include "hammer.h"
  9. #include "ToolDisplace.h"
  10. #include "MainFrm.h"
  11. #include "FaceEditSheet.h"
  12. #include "GlobalFunctions.h"
  13. #include "MapAtom.h"
  14. #include "MapSolid.h"
  15. #include "MapView3D.h"
  16. #include "History.h"
  17. #include "Camera.h"
  18. #include "MapDoc.h"
  19. #include "ChunkFile.h"
  20. #include "ToolManager.h"
  21. #include "SculptOptions.h"
  22. // memdbgon must be the last include file in a .cpp file!!!
  23. #include <tier0/memdbgon.h>
  24. //=============================================================================
  25. //-----------------------------------------------------------------------------
  26. //-----------------------------------------------------------------------------
  27. CToolDisplace::CToolDisplace()
  28. {
  29. m_uiTool = DISPTOOL_SELECT;
  30. m_uiEffect = DISPPAINT_EFFECT_RAISELOWER;
  31. m_uiBrushType = DISPPAINT_BRUSHTYPE_SOFT;
  32. m_iPaintChannel = DISPPAINT_CHANNEL_POSITION;
  33. m_flPaintValueGeo = 5.0f;
  34. m_flPaintValueData = 25.0f;
  35. m_iPaintAxis = DISPPAINT_AXIS_FACE;
  36. m_vecPaintAxis.Init( 0.0f, 0.0f, 1.0f );
  37. m_bAutoSew = false;
  38. m_bSpatial = false;
  39. m_flSpatialRadius = 15.0f;
  40. m_bSpatialRadius = false;
  41. m_bSelectMaskTool = true;
  42. m_bGridMaskTool = false;
  43. m_bLMBDown = false;
  44. m_bRMBDown = false;
  45. m_bNudge = false;
  46. m_bNudgeInit = false;
  47. m_EditDispHandle = EDITDISPHANDLE_INVALID;
  48. // load filters from file
  49. static char szProgramDir[MAX_PATH];
  50. APP()->GetDirectory( DIR_PROGRAM, szProgramDir );
  51. strcat( szProgramDir, "filters\\dispfilters.txt" );
  52. LoadFilters( szProgramDir );
  53. AddFiltersToManagers();
  54. m_SculptTool = NULL;
  55. m_MousePoint.Init( 0.0f, 0.0f );
  56. }
  57. //-----------------------------------------------------------------------------
  58. //-----------------------------------------------------------------------------
  59. CToolDisplace::~CToolDisplace()
  60. {
  61. // destroy filters
  62. m_FilterRaiseLowerMgr.Destroy();
  63. m_FilterRaiseToMgr.Destroy();
  64. m_FilterSmoothMgr.Destroy();
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Called when the tool is activated.
  68. // Input : eOldTool - The ID of the previously active tool.
  69. //-----------------------------------------------------------------------------
  70. void CToolDisplace::OnActivate()
  71. {
  72. //
  73. // initialize masks
  74. //
  75. CMapDisp::SetSelectMask( m_bSelectMaskTool );
  76. CMapDisp::SetGridMask( m_bGridMaskTool );
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose: Called when the tool is deactivated.
  80. // Input : eNewTool - The ID of the tool that is being activated.
  81. //-----------------------------------------------------------------------------
  82. void CToolDisplace::OnDeactivate()
  83. {
  84. //
  85. // reset masks
  86. //
  87. CMapDisp::SetSelectMask( false );
  88. CMapDisp::SetGridMask( false );
  89. if ( m_pDocument->GetTools()->GetActiveToolID() != TOOL_FACEEDIT_MATERIAL )
  90. {
  91. // Clear the selected faces when we are deactivated.
  92. m_pDocument->SelectFace(NULL, 0, scClear );
  93. }
  94. }
  95. //-----------------------------------------------------------------------------
  96. //-----------------------------------------------------------------------------
  97. inline void CToolDisplace::UpdateMapViews( CMapView3D *pView )
  98. {
  99. CMapDoc *pDoc = pView->GetMapDoc();
  100. if( pDoc )
  101. {
  102. pDoc->SetModifiedFlag();
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. //-----------------------------------------------------------------------------
  107. inline void CToolDisplace::CalcViewCenter( CMapView3D *pView )
  108. {
  109. CRect windowRect;
  110. pView->GetWindowRect( windowRect );
  111. m_viewCenter = windowRect.CenterPoint();
  112. }
  113. //-----------------------------------------------------------------------------
  114. //-----------------------------------------------------------------------------
  115. bool CToolDisplace::OnLMouseDown3D( CMapView3D *pView, UINT nFlags, const Vector2D &vPoint )
  116. {
  117. // Set down flags
  118. m_bLMBDown = true;
  119. if( m_uiTool == DISPTOOL_PAINT_SCULPT )
  120. {
  121. m_SculptTool->OnLMouseDown3D( pView, nFlags, vPoint );
  122. m_SculptTool->BeginPaint( pView, vPoint );
  123. ApplySculptSpatialPaintTool( pView, nFlags, vPoint );
  124. // update
  125. UpdateMapViews( pView );
  126. return true;
  127. }
  128. // Selection.
  129. if( m_uiTool == DISPTOOL_SELECT || ( GetAsyncKeyState( VK_CONTROL ) & 0x8000 ) )
  130. {
  131. // handle selection at point
  132. HandleSelection( pView, vPoint );
  133. // update
  134. UpdateMapViews( pView );
  135. return true;
  136. }
  137. // Tagging.
  138. if ( m_uiTool == DISPTOOL_TAG_WALKABLE || m_uiTool == DISPTOOL_TAG_BUILDABLE )
  139. {
  140. // Do tagging.
  141. HandleTagging( pView, vPoint );
  142. return true;
  143. }
  144. // Resize the spatial painting sphere.
  145. if( ( m_uiTool == DISPTOOL_PAINT ) && ( IsSpatialPainting() ) &&
  146. ( GetAsyncKeyState( VK_MENU ) & 0x8000 ) )
  147. {
  148. ResizeSpatialRadius_Activate( pView );
  149. return true;
  150. }
  151. // Nudging.
  152. if( ( m_uiTool == DISPTOOL_PAINT ) && ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) )
  153. {
  154. // is the current effect raise/lower (nudge only works in raise/lower mode)
  155. if( m_uiEffect == DISPPAINT_EFFECT_RAISELOWER )
  156. {
  157. EditDispHandle_t handle = GetHitPos( pView, vPoint );
  158. if( handle != EDITDISPHANDLE_INVALID )
  159. {
  160. m_EditDispHandle = handle;
  161. Nudge_Activate( pView, handle );
  162. UpdateMapViews( pView );
  163. return true;
  164. }
  165. }
  166. }
  167. // Painting.
  168. if( m_uiTool == DISPTOOL_PAINT )
  169. {
  170. // get hit info
  171. EditDispHandle_t handle = GetHitPos( pView, vPoint );
  172. if( handle == EDITDISPHANDLE_INVALID )
  173. return false;
  174. m_EditDispHandle = handle;
  175. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  176. IWorldEditDispMgr *pDispMgr = GetActiveWorldEditDispManager();
  177. if( !pDispMgr )
  178. return false;
  179. pDispMgr->PreUndo( "Displacement Modifier" );
  180. // Paint using the correct mode.
  181. if ( m_bSpatial )
  182. {
  183. int nDispCount = pDispMgr->SelectCount();
  184. for ( int iDisp = 0; iDisp < nDispCount; iDisp++ )
  185. {
  186. CMapDisp *pDisp = pDispMgr->GetFromSelect( iDisp );
  187. if ( pDisp )
  188. {
  189. pDisp->Paint_Init( DISPPAINT_CHANNEL_POSITION );
  190. }
  191. }
  192. // setup for undo - modifying the displacement (painting)
  193. ApplySpatialPaintTool( nFlags, vPoint, pDisp );
  194. }
  195. else
  196. {
  197. // setup for undo - modifying the displacement (painting)
  198. pDispMgr->Undo( handle, true );
  199. pDisp = EditDispMgr()->GetDisp( handle );
  200. ApplyPaintTool( nFlags, vPoint, pDisp );
  201. }
  202. // update
  203. UpdateMapViews( pView );
  204. }
  205. return true;
  206. }
  207. //-----------------------------------------------------------------------------
  208. //-----------------------------------------------------------------------------
  209. bool CToolDisplace::OnLMouseUp3D( CMapView3D *pView, UINT nFlags, const Vector2D &vPoint )
  210. {
  211. // left button up
  212. m_bLMBDown = false;
  213. if( m_uiTool == DISPTOOL_PAINT_SCULPT )
  214. {
  215. m_SculptTool->OnLMouseUp3D( pView, nFlags, vPoint );
  216. return true;
  217. }
  218. if ( m_bNudge )
  219. {
  220. Nudge_Deactivate();
  221. }
  222. if ( m_bSpatialRadius )
  223. {
  224. ResizeSpatialRadius_Deactivate();
  225. }
  226. IWorldEditDispMgr *pDispMgr = GetActiveWorldEditDispManager();
  227. if( pDispMgr )
  228. {
  229. pDispMgr->PostUndo();
  230. }
  231. return true;
  232. }
  233. //-----------------------------------------------------------------------------
  234. //-----------------------------------------------------------------------------
  235. bool CToolDisplace::OnRMouseDown3D( CMapView3D *pView, UINT nFlags, const Vector2D &vPoint )
  236. {
  237. // left button down
  238. m_bRMBDown = true;
  239. if( m_uiTool == DISPTOOL_PAINT_SCULPT )
  240. {
  241. m_SculptTool->OnRMouseDown3D( pView, nFlags, vPoint );
  242. m_SculptTool->BeginPaint( pView, vPoint );
  243. ApplySculptSpatialPaintTool( pView, nFlags, vPoint );
  244. // update
  245. UpdateMapViews( pView );
  246. return true;
  247. }
  248. //
  249. // lifting the face normal - painting with the axis set to "Face Normal"
  250. //
  251. if( ( m_uiTool == DISPTOOL_PAINT ) && ( m_iPaintAxis == DISPPAINT_AXIS_FACE ) &&
  252. ( GetAsyncKeyState( VK_MENU ) & 0x8000 ) )
  253. {
  254. LiftFaceNormal( pView, vPoint );
  255. return true;
  256. }
  257. // Tagging.
  258. if ( m_uiTool == DISPTOOL_TAG_WALKABLE || m_uiTool == DISPTOOL_TAG_BUILDABLE )
  259. {
  260. // Do tagging.
  261. HandleTaggingReset( pView, vPoint );
  262. return true;
  263. }
  264. //
  265. // handle the normal paint procedure
  266. //
  267. if( m_uiTool == DISPTOOL_PAINT )
  268. {
  269. // get hit info
  270. EditDispHandle_t handle = GetHitPos( pView, vPoint );
  271. if( handle == EDITDISPHANDLE_INVALID )
  272. return false;
  273. m_EditDispHandle = handle;
  274. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  275. IWorldEditDispMgr *pDispMgr = GetActiveWorldEditDispManager();
  276. if( !pDispMgr )
  277. return false;
  278. pDispMgr->PreUndo( "Displacement Modifier" );
  279. // apply the current displacement tool
  280. if ( m_bSpatial )
  281. {
  282. int nDispCount = pDispMgr->SelectCount();
  283. for ( int iDisp = 0; iDisp < nDispCount; iDisp++ )
  284. {
  285. CMapDisp *pDisp = pDispMgr->GetFromSelect( iDisp );
  286. if ( pDisp )
  287. {
  288. pDisp->Paint_Init( DISPPAINT_CHANNEL_POSITION );
  289. }
  290. }
  291. ApplySpatialPaintTool( nFlags, vPoint, pDisp );
  292. }
  293. else
  294. {
  295. // setup for undo
  296. pDispMgr->Undo( handle, true );
  297. pDisp = EditDispMgr()->GetDisp( handle );
  298. ApplyPaintTool( nFlags, vPoint, pDisp );
  299. }
  300. // update
  301. UpdateMapViews( pView );
  302. }
  303. return true;
  304. }
  305. //-----------------------------------------------------------------------------
  306. //-----------------------------------------------------------------------------
  307. bool CToolDisplace::OnRMouseUp3D( CMapView3D *pView, UINT nFlags, const Vector2D &vPoint )
  308. {
  309. // left button up
  310. m_bRMBDown = false;
  311. if( m_uiTool == DISPTOOL_PAINT_SCULPT )
  312. {
  313. m_SculptTool->OnRMouseUp3D( pView, nFlags, vPoint );
  314. return true;
  315. }
  316. IWorldEditDispMgr *pDispMgr = GetActiveWorldEditDispManager();
  317. if( pDispMgr )
  318. {
  319. pDispMgr->PostUndo();
  320. }
  321. return true;
  322. }
  323. //-----------------------------------------------------------------------------
  324. //-----------------------------------------------------------------------------
  325. bool CToolDisplace::OnMouseMove3D( CMapView3D *pView, UINT nFlags, const Vector2D &vPoint )
  326. {
  327. m_MousePoint = vPoint;
  328. if( m_uiTool == DISPTOOL_PAINT_SCULPT )
  329. {
  330. m_SculptTool->OnMouseMove3D( pView, nFlags, vPoint );
  331. if( ( m_bLMBDown || m_bRMBDown ) )
  332. {
  333. ApplySculptSpatialPaintTool( pView, nFlags, vPoint );
  334. }
  335. // update
  336. UpdateMapViews( pView );
  337. return true;
  338. }
  339. // nudging
  340. if ( ( m_uiTool == DISPTOOL_PAINT ) && ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) &&
  341. m_bLMBDown && m_bNudge )
  342. {
  343. Nudge_Do();
  344. }
  345. // Resizing the spatial sphere.
  346. else if ( ( m_uiTool == DISPTOOL_PAINT ) && ( GetAsyncKeyState( VK_MENU ) & 0x8000 ) &&
  347. m_bLMBDown && m_bSpatialRadius )
  348. {
  349. ResizeSpatialRadius_Do();
  350. }
  351. // painting
  352. else
  353. {
  354. // get hit info
  355. EditDispHandle_t handle = GetHitPos( pView, vPoint );
  356. if( handle == EDITDISPHANDLE_INVALID )
  357. return false;
  358. m_EditDispHandle = handle;
  359. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  360. //
  361. // continue with tool operation?!
  362. //
  363. if( ( m_bLMBDown || m_bRMBDown ) && !( GetAsyncKeyState( VK_CONTROL ) & 0x8000 ) &&
  364. ( m_uiTool == DISPTOOL_PAINT ) )
  365. {
  366. if ( m_bSpatial )
  367. {
  368. ApplySpatialPaintTool( nFlags, vPoint, pDisp );
  369. }
  370. else
  371. {
  372. ApplyPaintTool( nFlags, vPoint, pDisp );
  373. }
  374. }
  375. // not nudging anymore -- if we were
  376. if( m_bNudge )
  377. {
  378. Nudge_Deactivate();
  379. }
  380. if ( m_bSpatialRadius )
  381. {
  382. ResizeSpatialRadius_Deactivate();
  383. }
  384. }
  385. // update
  386. UpdateMapViews( pView );
  387. return true;
  388. }
  389. //-----------------------------------------------------------------------------
  390. //-----------------------------------------------------------------------------
  391. void CToolDisplace::LiftFaceNormal( CMapView3D *pView, const Vector2D &vPoint )
  392. {
  393. //
  394. // check for closest solid object
  395. //
  396. ULONG ulFace;
  397. CMapClass *pObject;
  398. if( ( ( pObject = pView->NearestObjectAt( vPoint, ulFace ) ) != NULL ) )
  399. {
  400. if( pObject->IsMapClass( MAPCLASS_TYPE( CMapSolid ) ) )
  401. {
  402. // get the solid
  403. CMapSolid *pSolid = ( CMapSolid* )pObject;
  404. if( !pSolid )
  405. return;
  406. // trace a line and get the normal -- will get a displacement normal
  407. // if one exists
  408. CMapFace *pFace = pSolid->GetFace( ulFace );
  409. if( !pFace )
  410. return;
  411. Vector vRayStart, vRayEnd;
  412. pView->GetCamera()->BuildRay( vPoint, vRayStart, vRayEnd );
  413. Vector vHitPos, vHitNormal;
  414. if( pFace->TraceLine( vHitPos, vHitNormal, vRayStart, vRayEnd ) )
  415. {
  416. // set the paint direction
  417. m_vecPaintAxis = vHitNormal;
  418. }
  419. else
  420. {
  421. // will default to z if no normal found
  422. m_vecPaintAxis.Init( 0.0f, 0.0f, 1.0f );
  423. }
  424. }
  425. }
  426. }
  427. //-----------------------------------------------------------------------------
  428. //-----------------------------------------------------------------------------
  429. void CToolDisplace::Nudge_Activate( CMapView3D *pView, EditDispHandle_t dispHandle )
  430. {
  431. IWorldEditDispMgr *pDispMgr = GetActiveWorldEditDispManager();
  432. if( !pDispMgr )
  433. return;
  434. pDispMgr->PreUndo( "Displacement Nudge" );
  435. // Setup paint (nudge) using the correct mode.
  436. if ( m_bSpatial )
  437. {
  438. int nDispCount = pDispMgr->SelectCount();
  439. for ( int iDisp = 0; iDisp < nDispCount; iDisp++ )
  440. {
  441. CMapDisp *pDisp = pDispMgr->GetFromSelect( iDisp );
  442. if ( pDisp )
  443. {
  444. pDisp->Paint_Init( DISPPAINT_CHANNEL_POSITION );
  445. }
  446. }
  447. }
  448. else
  449. {
  450. // setup for undo
  451. pDispMgr->Undo( dispHandle, true );
  452. }
  453. // setup the cursor for "nudging"
  454. CalcViewCenter( pView );
  455. SetCursorPos( m_viewCenter.x, m_viewCenter.y );
  456. pView->SetCapture();
  457. // set nudging
  458. m_bNudge = true;
  459. m_bNudgeInit = true;
  460. }
  461. //-----------------------------------------------------------------------------
  462. //-----------------------------------------------------------------------------
  463. void CToolDisplace::Nudge_Deactivate( void )
  464. {
  465. ReleaseCapture();
  466. m_bNudge = false;
  467. }
  468. //-----------------------------------------------------------------------------
  469. //-----------------------------------------------------------------------------
  470. void CToolDisplace::Nudge_Do( void )
  471. {
  472. CMapDisp *pNudgeDisp = GetEditDisp();
  473. if (pNudgeDisp == NULL)
  474. {
  475. return;
  476. }
  477. //
  478. // find the greatest delta and "nudge"
  479. //
  480. // NOTE: using get cursor position, because it is different than the
  481. // "point" incoming into mouse move????
  482. //
  483. CPoint nudgePos;
  484. GetCursorPos( &nudgePos );
  485. CPoint nudgeDelta;
  486. nudgeDelta.x = nudgePos.x - m_viewCenter.x;
  487. nudgeDelta.y = nudgePos.y - m_viewCenter.y;
  488. float delta;
  489. if( abs( nudgeDelta.x ) < abs( nudgeDelta.y ) )
  490. {
  491. delta = nudgeDelta.y;
  492. }
  493. else
  494. {
  495. delta = nudgeDelta.x;
  496. }
  497. delta = -delta;
  498. if ( !IsSpatialPainting() )
  499. {
  500. CDispMapImageFilter *pFilter = m_FilterRaiseLowerMgr.GetActiveFilter();
  501. if( !pFilter )
  502. return;
  503. // set the dynamic filter data
  504. pFilter->m_DataType = DISPPAINT_CHANNEL_POSITION;
  505. pFilter->m_Scale = ( delta * 0.25 ) * ( float )( ( int )( m_flPaintValueGeo / 10.0f ) + 1 ) ;
  506. // apply the filter to the displacement surface(s)
  507. m_FilterRaiseLowerMgr.Apply( pFilter, pNudgeDisp, m_iPaintAxis, m_vecPaintAxis, m_bAutoSew );
  508. }
  509. else
  510. {
  511. // Get the hit index and check for validity.
  512. int iHit = pNudgeDisp->GetTexelHitIndex();
  513. if ( iHit != -1 )
  514. {
  515. // Initialize the spatial paint data.
  516. SpatialPaintData_t spatialData;
  517. spatialData.m_nEffect = DISPPAINT_EFFECT_RAISELOWER;
  518. spatialData.m_uiBrushType = m_uiBrushType;
  519. spatialData.m_flRadius = m_flSpatialRadius;
  520. spatialData.m_flScalar = delta;
  521. spatialData.m_bNudge = true;
  522. spatialData.m_bNudgeInit = m_bNudgeInit;
  523. pNudgeDisp->GetVert( iHit, spatialData.m_vCenter );
  524. VectorCopy( m_vecPaintAxis, spatialData.m_vPaintAxis );
  525. m_DispPaintMgr.Paint( spatialData, m_bAutoSew );
  526. // Done with the init.
  527. m_bNudgeInit = false;
  528. }
  529. }
  530. // reset the cursor pos
  531. SetCursorPos( m_viewCenter.x, m_viewCenter.y );
  532. }
  533. //-----------------------------------------------------------------------------
  534. //-----------------------------------------------------------------------------
  535. void CToolDisplace::ApplyPaintTool( UINT nFlags, const Vector2D &vPoint, CMapDisp *pDisp )
  536. {
  537. switch( m_uiEffect )
  538. {
  539. case DISPPAINT_EFFECT_RAISELOWER:
  540. {
  541. CDispMapImageFilter *pFilter = m_FilterRaiseLowerMgr.GetActiveFilter();
  542. if( pFilter )
  543. {
  544. pFilter->m_DataType = m_iPaintChannel;
  545. if ( m_iPaintChannel == DISPPAINT_CHANNEL_POSITION )
  546. {
  547. pFilter->m_Scale = m_flPaintValueGeo;
  548. }
  549. else if ( m_iPaintChannel == DISPPAINT_CHANNEL_ALPHA )
  550. {
  551. pFilter->m_Scale = m_flPaintValueData;
  552. }
  553. if( m_bRMBDown )
  554. {
  555. pFilter->m_Scale = -pFilter->m_Scale;
  556. }
  557. // apply the filter to the displacement surface(s)
  558. m_FilterRaiseLowerMgr.Apply( pFilter, pDisp, m_iPaintAxis, m_vecPaintAxis, m_bAutoSew );
  559. }
  560. return;
  561. }
  562. case DISPPAINT_EFFECT_MODULATE:
  563. {
  564. // no modulate filters or filter manager currently!
  565. return;
  566. }
  567. case DISPPAINT_EFFECT_SMOOTH:
  568. {
  569. CDispMapImageFilter *pFilter = m_FilterSmoothMgr.GetActiveFilter();
  570. if( pFilter )
  571. {
  572. pFilter->m_DataType = m_iPaintChannel;
  573. pFilter->m_Scale = 1.0f;
  574. int areaValue = 3;
  575. if ( m_iPaintChannel == DISPPAINT_CHANNEL_POSITION )
  576. {
  577. areaValue = ( m_flPaintValueGeo * 2 ) + 1;
  578. }
  579. else if ( m_iPaintChannel == DISPPAINT_CHANNEL_ALPHA )
  580. {
  581. areaValue = ( m_flPaintValueData * 2 ) + 1;
  582. }
  583. if( areaValue < 3 ) { areaValue = 3; }
  584. if( areaValue > 7 ) { areaValue = 7; }
  585. pFilter->m_AreaHeight = areaValue;
  586. pFilter->m_AreaWidth = areaValue;
  587. // apply the filter to the displacement surface(s)
  588. m_FilterSmoothMgr.Apply( pFilter, pDisp, m_iPaintAxis, m_vecPaintAxis, m_bAutoSew );
  589. }
  590. return;
  591. }
  592. case DISPPAINT_EFFECT_RAISETO:
  593. {
  594. CDispMapImageFilter *pFilter = m_FilterRaiseToMgr.GetActiveFilter();
  595. if( pFilter )
  596. {
  597. pFilter->m_DataType = m_iPaintChannel;
  598. if ( m_iPaintChannel == DISPPAINT_CHANNEL_POSITION )
  599. {
  600. pFilter->m_Scale = m_flPaintValueGeo;
  601. }
  602. else if ( m_iPaintChannel == DISPPAINT_CHANNEL_ALPHA )
  603. {
  604. pFilter->m_Scale = m_flPaintValueData;
  605. }
  606. // apply the filter to the displacement surface(s)
  607. m_FilterRaiseToMgr.Apply( pFilter, pDisp, m_iPaintAxis, m_vecPaintAxis, m_bAutoSew );
  608. }
  609. return;
  610. }
  611. }
  612. }
  613. //-----------------------------------------------------------------------------
  614. //-----------------------------------------------------------------------------
  615. void CToolDisplace::ApplySpatialPaintTool( UINT nFlags, const Vector2D &vPoint, CMapDisp *pDisp )
  616. {
  617. // Right mouse button only used to paint in a Raise/Lower situation.
  618. if ( ( m_uiEffect != DISPPAINT_EFFECT_RAISELOWER ) && m_bRMBDown )
  619. return;
  620. // Get the hit index and check for validity.
  621. int iHit = pDisp->GetTexelHitIndex();
  622. if ( iHit == -1 )
  623. return;
  624. // Initialize the spatial paint data.
  625. SpatialPaintData_t spatialData;
  626. spatialData.m_nEffect = m_uiEffect;
  627. spatialData.m_uiBrushType = m_uiBrushType;
  628. spatialData.m_flRadius = m_flSpatialRadius;
  629. spatialData.m_flScalar = m_flPaintValueGeo;
  630. spatialData.m_bNudge = false;
  631. if ( m_bRMBDown )
  632. {
  633. spatialData.m_flScalar = -spatialData.m_flScalar;
  634. }
  635. pDisp->GetVert( iHit, spatialData.m_vCenter );
  636. VectorCopy( m_vecPaintAxis, spatialData.m_vPaintAxis );
  637. m_DispPaintMgr.Paint( spatialData, m_bAutoSew );
  638. }
  639. //-----------------------------------------------------------------------------
  640. //-----------------------------------------------------------------------------
  641. void CToolDisplace::ApplySculptSpatialPaintTool( CMapView3D *pView, UINT nFlags, const Vector2D &vPoint )
  642. {
  643. // Initialize the spatial paint data.
  644. SpatialPaintData_t spatialData;
  645. spatialData.m_vCenter.Init();
  646. // get hit info
  647. EditDispHandle_t handle = GetHitPos( pView, vPoint );
  648. if( handle != EDITDISPHANDLE_INVALID )
  649. {
  650. m_EditDispHandle = handle;
  651. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  652. // Get the hit index and check for validity.
  653. int iHit = pDisp->GetTexelHitIndex();
  654. if ( iHit != -1 )
  655. {
  656. pDisp->GetVert( iHit, spatialData.m_vCenter );
  657. }
  658. }
  659. spatialData.m_nEffect = m_uiEffect;
  660. spatialData.m_uiBrushType = m_uiBrushType;
  661. spatialData.m_flRadius = m_flSpatialRadius;
  662. spatialData.m_flScalar = m_flPaintValueGeo;
  663. spatialData.m_bNudge = false;
  664. if ( m_bRMBDown )
  665. {
  666. spatialData.m_flScalar = -spatialData.m_flScalar;
  667. }
  668. VectorCopy( m_vecPaintAxis, spatialData.m_vPaintAxis );
  669. m_SculptTool->Paint( pView, vPoint, spatialData );
  670. }
  671. //-----------------------------------------------------------------------------
  672. //-----------------------------------------------------------------------------
  673. void CToolDisplace::ResizeSpatialRadius_Activate( CMapView3D *pView )
  674. {
  675. // Calculate the center of the view and capture the mouse cursor.
  676. CalcViewCenter( pView );
  677. SetCursorPos( m_viewCenter.x, m_viewCenter.y );
  678. pView->SetCapture();
  679. m_bSpatialRadius = true;
  680. }
  681. //-----------------------------------------------------------------------------
  682. //-----------------------------------------------------------------------------
  683. void CToolDisplace::ResizeSpatialRadius_Deactivate( void )
  684. {
  685. ReleaseCapture();
  686. m_bSpatialRadius = false;
  687. }
  688. //-----------------------------------------------------------------------------
  689. //-----------------------------------------------------------------------------
  690. void CToolDisplace::ResizeSpatialRadius_Do( void )
  691. {
  692. CPoint cursorPos;
  693. GetCursorPos( &cursorPos );
  694. // Calculate the delta between the cursor from last frame and this one.
  695. CPoint cursorDelta;
  696. cursorDelta.x = cursorPos.x - m_viewCenter.x;
  697. cursorDelta.y = cursorPos.y - m_viewCenter.y;
  698. float flDelta;
  699. if( abs( cursorDelta.x ) < abs( cursorDelta.y ) )
  700. {
  701. flDelta = cursorDelta.y;
  702. }
  703. else
  704. {
  705. flDelta = cursorDelta.x;
  706. }
  707. flDelta = -flDelta;
  708. // Adjust the sphere radius.
  709. m_flSpatialRadius += flDelta;
  710. // reset the cursor pos
  711. SetCursorPos( m_viewCenter.x, m_viewCenter.y );
  712. //
  713. // Update the paint dialog.
  714. //
  715. CFaceEditSheet *pSheet = GetMainWnd()->GetFaceEditSheet();
  716. if( pSheet )
  717. {
  718. pSheet->m_DispPage.UpdatePaintDialogs();
  719. }
  720. }
  721. //-----------------------------------------------------------------------------
  722. //-----------------------------------------------------------------------------
  723. void CToolDisplace::HandleSelection( CMapView3D *pView, const Vector2D &vPoint )
  724. {
  725. //
  726. // check for closest solid object
  727. //
  728. ULONG ulFace;
  729. CMapClass *pObject;
  730. bool bShift = ( ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) != 0 );
  731. if( ( ( pObject = pView->NearestObjectAt( vPoint, ulFace ) ) != NULL ) )
  732. {
  733. if( pObject->IsMapClass( MAPCLASS_TYPE( CMapSolid ) ) )
  734. {
  735. // get the solid
  736. CMapSolid *pSolid = ( CMapSolid* )pObject;
  737. // setup initial command state
  738. int cmd = scToggle | scClear;
  739. //
  740. // don't "clear" if CTRL is pressed
  741. //
  742. if( GetAsyncKeyState( VK_CONTROL ) & 0x8000 )
  743. {
  744. cmd &= ~scClear;
  745. }
  746. CMapDoc *pDoc = pView->GetMapDoc();
  747. if( !pDoc )
  748. return;
  749. // If they are holding down SHIFT, select the entire solid.
  750. if ( bShift )
  751. {
  752. pDoc->SelectFace( pSolid, -1, cmd );
  753. }
  754. // Otherwise, select a single face.
  755. else
  756. {
  757. pDoc->SelectFace( pSolid, ulFace, cmd );
  758. }
  759. }
  760. }
  761. }
  762. //-----------------------------------------------------------------------------
  763. // Purpose: Handle the overriding of displacement triangle tag.
  764. //-----------------------------------------------------------------------------
  765. void CToolDisplace::HandleTagging( CMapView3D *pView, const Vector2D &vPoint )
  766. {
  767. // Get the displacement face (if any) at the 2d point.
  768. ULONG ulFace;
  769. CMapClass *pObject = NULL;
  770. if( ( ( pObject = pView->NearestObjectAt( vPoint, ulFace ) ) != NULL ) )
  771. {
  772. if( pObject->IsMapClass( MAPCLASS_TYPE( CMapSolid ) ) )
  773. {
  774. // Get the face and check for a displacement.
  775. CMapSolid *pSolid = ( CMapSolid* )pObject;
  776. CMapFace *pFace = pSolid->GetFace( ( int )ulFace );
  777. if ( pFace && pFace->HasDisp() )
  778. {
  779. EditDispHandle_t hDisp = pFace->GetDisp();
  780. CMapDisp *pDisp = EditDispMgr()->GetDisp( hDisp );
  781. Vector vecStart, vecEnd;
  782. pView->GetCamera()->BuildRay( vPoint, vecStart, vecEnd );
  783. float flFraction;
  784. int iTri = pDisp->CollideWithDispTri( vecStart, vecEnd, flFraction );
  785. if ( iTri != -1 )
  786. {
  787. if ( m_uiTool == DISPTOOL_TAG_WALKABLE )
  788. {
  789. if ( pDisp->IsTriTag( iTri, COREDISPTRI_TAG_FORCE_WALKABLE_BIT ) )
  790. {
  791. pDisp->ToggleTriTag( iTri, COREDISPTRI_TAG_FORCE_WALKABLE_VAL );
  792. }
  793. else
  794. {
  795. pDisp->SetTriTag( iTri, COREDISPTRI_TAG_FORCE_WALKABLE_BIT );
  796. if ( !pDisp->IsTriTag( iTri, COREDISPTRI_TAG_WALKABLE ) )
  797. {
  798. pDisp->SetTriTag( iTri, COREDISPTRI_TAG_FORCE_WALKABLE_VAL );
  799. }
  800. else
  801. {
  802. pDisp->ResetTriTag( iTri, COREDISPTRI_TAG_FORCE_WALKABLE_VAL );
  803. }
  804. }
  805. pDisp->UpdateWalkable();
  806. }
  807. else if ( m_uiTool == DISPTOOL_TAG_BUILDABLE )
  808. {
  809. if ( pDisp->IsTriTag( iTri, COREDISPTRI_TAG_FORCE_BUILDABLE_BIT ) )
  810. {
  811. pDisp->ToggleTriTag( iTri, COREDISPTRI_TAG_FORCE_BUILDABLE_VAL );
  812. }
  813. else
  814. {
  815. pDisp->SetTriTag( iTri, COREDISPTRI_TAG_FORCE_BUILDABLE_BIT );
  816. if ( !pDisp->IsTriTag( iTri, COREDISPTRI_TAG_BUILDABLE ) )
  817. {
  818. pDisp->SetTriTag( iTri, COREDISPTRI_TAG_FORCE_BUILDABLE_VAL );
  819. }
  820. else
  821. {
  822. pDisp->ResetTriTag( iTri, COREDISPTRI_TAG_FORCE_BUILDABLE_VAL );
  823. }
  824. }
  825. pDisp->UpdateBuildable();
  826. }
  827. }
  828. }
  829. }
  830. }
  831. }
  832. //-----------------------------------------------------------------------------
  833. // Purpose: Handle the overriding of displacement triangle tag.
  834. //-----------------------------------------------------------------------------
  835. void CToolDisplace::HandleTaggingReset( CMapView3D *pView, const Vector2D &vPoint )
  836. {
  837. // Get the displacement face (if any) at the 2d point.
  838. ULONG ulFace;
  839. CMapClass *pObject = NULL;
  840. if( ( ( pObject = pView->NearestObjectAt( vPoint, ulFace ) ) != NULL ) )
  841. {
  842. if( pObject->IsMapClass( MAPCLASS_TYPE( CMapSolid ) ) )
  843. {
  844. // Get the face and check for a displacement.
  845. CMapSolid *pSolid = ( CMapSolid* )pObject;
  846. CMapFace *pFace = pSolid->GetFace( ( int )ulFace );
  847. if ( pFace && pFace->HasDisp() )
  848. {
  849. EditDispHandle_t hDisp = pFace->GetDisp();
  850. CMapDisp *pDisp = EditDispMgr()->GetDisp( hDisp );
  851. Vector vecStart, vecEnd;
  852. pView->GetCamera()->BuildRay( vPoint, vecStart, vecEnd );
  853. float flFraction;
  854. int iTri = pDisp->CollideWithDispTri( vecStart, vecEnd, flFraction );
  855. if ( iTri != -1 )
  856. {
  857. if ( m_uiTool == DISPTOOL_TAG_WALKABLE )
  858. {
  859. pDisp->ResetTriTag( iTri, COREDISPTRI_TAG_FORCE_WALKABLE_BIT );
  860. pDisp->UpdateWalkable();
  861. }
  862. else if ( m_uiTool == DISPTOOL_TAG_BUILDABLE )
  863. {
  864. pDisp->ResetTriTag( iTri, COREDISPTRI_TAG_FORCE_BUILDABLE_BIT );
  865. pDisp->UpdateBuildable();
  866. }
  867. }
  868. }
  869. }
  870. }
  871. }
  872. //-----------------------------------------------------------------------------
  873. //-----------------------------------------------------------------------------
  874. EditDispHandle_t CToolDisplace::GetHitPos( CMapView3D *pView, const Vector2D &vPoint )
  875. {
  876. //
  877. // get ray info
  878. //
  879. Vector rayStart, rayEnd;
  880. pView->GetCamera()->BuildRay( vPoint, rayStart, rayEnd );
  881. // generate selected displacement list
  882. int dispCount = GetSelectedDisps();
  883. if( dispCount == 0 )
  884. return NULL;
  885. // collide against all "active" displacements and set texel hit data
  886. return CollideWithSelectedDisps( rayStart, rayEnd );
  887. }
  888. //-----------------------------------------------------------------------------
  889. //-----------------------------------------------------------------------------
  890. int CToolDisplace::GetSelectedDisps( void )
  891. {
  892. //
  893. // get a valid displacement manager
  894. //
  895. IWorldEditDispMgr *pDispMgr = GetActiveWorldEditDispManager();
  896. if( !pDispMgr )
  897. return 0;
  898. // clear the selection list
  899. pDispMgr->SelectClear();
  900. //
  901. // add all selected displacements to "displacement manager"'s selection list
  902. //
  903. CFaceEditSheet *pSheet = GetMainWnd()->GetFaceEditSheet();
  904. if( !pSheet )
  905. return 0;
  906. int faceCount = pSheet->GetFaceListCount();
  907. for( int i = 0; i < faceCount; i++ )
  908. {
  909. CMapFace *pFace = pSheet->GetFaceListDataFace( i );
  910. if( !pFace )
  911. continue;
  912. if( pFace->HasDisp() )
  913. {
  914. EditDispHandle_t handle = pFace->GetDisp();
  915. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  916. pDisp->ResetTexelHitIndex();
  917. pDispMgr->AddToSelect( handle );
  918. }
  919. }
  920. // return the number of displacements in list
  921. return pDispMgr->SelectCount();
  922. }
  923. //-----------------------------------------------------------------------------
  924. //-----------------------------------------------------------------------------
  925. EditDispHandle_t CToolDisplace::CollideWithSelectedDisps( const Vector &rayStart, const Vector &rayEnd )
  926. {
  927. EditDispHandle_t handle = EDITDISPHANDLE_INVALID;
  928. float minDist = 99999.9f;
  929. int minIndex = -1;
  930. //
  931. // get a valid displacement manager
  932. //
  933. IWorldEditDispMgr *pDispMgr = GetActiveWorldEditDispManager();
  934. if( !pDispMgr )
  935. return NULL;
  936. int dispCount = pDispMgr->SelectCount();
  937. for( int i = 0; i < dispCount; i++ )
  938. {
  939. // get the current displacement
  940. CMapDisp *pDisp = pDispMgr->GetFromSelect( i );
  941. if( !pDisp )
  942. continue;
  943. bool bCollide = RayAABBTest( pDisp, rayStart, rayEnd );
  944. if( bCollide )
  945. {
  946. Vector point;
  947. int size = pDisp->GetSize();
  948. for( int j = 0; j < size; j++ )
  949. {
  950. // get current point
  951. pDisp->GetVert( j, point );
  952. // find point closest to ray
  953. float dist = DistFromPointToRay( rayStart, rayEnd, point );
  954. if( dist < minDist )
  955. {
  956. CMapFace *pFace = ( CMapFace* )pDisp->GetParent();
  957. handle = pFace->GetDisp();
  958. minDist = dist;
  959. minIndex = j;
  960. }
  961. }
  962. }
  963. }
  964. //
  965. // set the texel hit index
  966. //
  967. if( handle != EDITDISPHANDLE_INVALID )
  968. {
  969. CMapDisp *pDisp = EditDispMgr()->GetDisp( handle );
  970. pDisp->SetTexelHitIndex( minIndex );
  971. }
  972. return handle;
  973. }
  974. //-----------------------------------------------------------------------------
  975. //-----------------------------------------------------------------------------
  976. bool CToolDisplace::RayAABBTest( CMapDisp *pDisp, const Vector &rayStart, const Vector &rayEnd )
  977. {
  978. //
  979. // make planes
  980. //
  981. PLANE planes[6];
  982. Vector boxMin, boxMax;
  983. pDisp->GetBoundingBox( boxMin, boxMax );
  984. BuildParallelepiped( boxMin, boxMax, planes );
  985. bool bCollide = false;
  986. for( int planeIndex = 0; planeIndex < 6; planeIndex++ )
  987. {
  988. bCollide = RayPlaneTest( &planes[planeIndex], rayStart, rayEnd );
  989. if( !bCollide )
  990. return false;
  991. }
  992. return true;
  993. }
  994. //-----------------------------------------------------------------------------
  995. //-----------------------------------------------------------------------------
  996. void CToolDisplace::BuildParallelepiped( const Vector &boxMin, const Vector &boxMax,
  997. PLANE planes[6] )
  998. {
  999. int planeIndex = 0;
  1000. for( int axis = 0; axis < 3; axis++ )
  1001. {
  1002. for( int direction = -1; direction < 2; direction += 2 )
  1003. {
  1004. // clear the current plane info
  1005. VectorClear( planes[planeIndex].normal );
  1006. planes[planeIndex].normal[axis] = direction;
  1007. if( direction == 1 )
  1008. {
  1009. planes[planeIndex].dist = boxMax[axis];
  1010. }
  1011. else
  1012. {
  1013. planes[planeIndex].dist = -boxMin[axis];
  1014. }
  1015. planeIndex++;
  1016. }
  1017. }
  1018. }
  1019. //-----------------------------------------------------------------------------
  1020. //-----------------------------------------------------------------------------
  1021. bool CToolDisplace::RayPlaneTest( PLANE *pPlane, const Vector& rayStart, const Vector& rayEnd /*, float *fraction*/ )
  1022. {
  1023. //
  1024. // get the distances both trace start and end from the bloated plane
  1025. //
  1026. float distStart = DotProduct( rayStart, pPlane->normal ) - pPlane->dist;
  1027. float distEnd = DotProduct( rayEnd, pPlane->normal ) - pPlane->dist;
  1028. //
  1029. // no collision - both points are in front or behind of the given plane
  1030. //
  1031. if( ( distStart > 0.0f ) && ( distEnd > 0.0f ) )
  1032. return false;
  1033. if( ( distStart > 0.0f ) && ( distEnd > 0.0f ) )
  1034. return false;
  1035. // calculate the parameterized "t" component along the ray
  1036. //*fraction = distStart / ( distStart - distEnd );
  1037. // collision
  1038. return true;
  1039. }
  1040. //-----------------------------------------------------------------------------
  1041. //-----------------------------------------------------------------------------
  1042. float CToolDisplace::DistFromPointToRay( const Vector& rayStart, const Vector& rayEnd,
  1043. const Vector& point )
  1044. {
  1045. //
  1046. // calculate the ray
  1047. //
  1048. Vector ray;
  1049. VectorSubtract( rayEnd, rayStart, ray );
  1050. VectorNormalize( ray );
  1051. //
  1052. // get a ray to point
  1053. //
  1054. Vector seg;
  1055. VectorSubtract( point, rayStart, seg );
  1056. //
  1057. // project point segment onto ray - get perpendicular point
  1058. //
  1059. float value = DotProduct( ray, seg );
  1060. VectorScale( ray, value, ray );
  1061. VectorAdd( rayStart, ray, ray );
  1062. //
  1063. // find the distance between the perpendicular point and point
  1064. //
  1065. VectorSubtract( ray, point, seg );
  1066. float dist = VectorLength( seg );
  1067. return dist;
  1068. }
  1069. //-----------------------------------------------------------------------------
  1070. //-----------------------------------------------------------------------------
  1071. void CToolDisplace::AddFiltersToManagers( void )
  1072. {
  1073. int count = m_FilterLoaderMgr.GetFilterCount();
  1074. for( int ndxFilter = 0; ndxFilter < count; ndxFilter++ )
  1075. {
  1076. CDispMapImageFilter *pFilter = m_FilterLoaderMgr.GetFilter( ndxFilter );
  1077. if( pFilter )
  1078. {
  1079. switch( pFilter->m_Type )
  1080. {
  1081. case DISPPAINT_EFFECT_RAISELOWER:
  1082. {
  1083. m_FilterRaiseLowerMgr.Add( pFilter );
  1084. break;
  1085. }
  1086. case DISPPAINT_EFFECT_RAISETO:
  1087. {
  1088. m_FilterRaiseToMgr.Add( pFilter );
  1089. break;
  1090. }
  1091. case DISPPAINT_EFFECT_SMOOTH:
  1092. {
  1093. m_FilterSmoothMgr.Add( pFilter );
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. }
  1099. }
  1100. //-----------------------------------------------------------------------------
  1101. //-----------------------------------------------------------------------------
  1102. bool CToolDisplace::LoadFilters( const char *filename )
  1103. {
  1104. //
  1105. // Open the file.
  1106. //
  1107. CChunkFile File;
  1108. ChunkFileResult_t eResult = File.Open( filename, ChunkFile_Read );
  1109. if( eResult != ChunkFile_Ok )
  1110. {
  1111. Msg( mwError, "Couldn't load filter file %s!\n", filename );
  1112. }
  1113. //
  1114. // Read the file.
  1115. //
  1116. if (eResult == ChunkFile_Ok)
  1117. {
  1118. //
  1119. // Set up handlers for the subchunks that we are interested in.
  1120. //
  1121. CChunkHandlerMap Handlers;
  1122. Handlers.AddHandler( "Filter", ( ChunkHandler_t )CToolDisplace::LoadFiltersCallback, this );
  1123. File.PushHandlers( &Handlers );
  1124. //
  1125. // Read the sub-chunks. We ignore keys in the root of the file, so we don't pass a
  1126. // key value callback to ReadChunk.
  1127. //
  1128. while (eResult == ChunkFile_Ok)
  1129. {
  1130. eResult = File.ReadChunk();
  1131. }
  1132. if (eResult == ChunkFile_EOF)
  1133. {
  1134. eResult = ChunkFile_Ok;
  1135. }
  1136. File.PopHandlers();
  1137. }
  1138. return( eResult == ChunkFile_Ok );
  1139. }
  1140. //-----------------------------------------------------------------------------
  1141. //-----------------------------------------------------------------------------
  1142. ChunkFileResult_t CToolDisplace::LoadFiltersCallback( CChunkFile *pFile, CToolDisplace *pDisplaceTool )
  1143. {
  1144. //
  1145. // allocate a new filter
  1146. //
  1147. CDispMapImageFilter *pFilter = pDisplaceTool->m_FilterLoaderMgr.Create();
  1148. if( !pFilter )
  1149. return ChunkFile_Fail;
  1150. // load the filter data
  1151. ChunkFileResult_t eResult = pFilter->LoadFilter( pFile );
  1152. return( eResult );
  1153. }
  1154. //-----------------------------------------------------------------------------
  1155. // Purpose:
  1156. // Input : *pRender -
  1157. // *pTool -
  1158. //-----------------------------------------------------------------------------
  1159. void CToolDisplace::RenderPaintSphere( CRender3D *pRender )
  1160. {
  1161. CMapDisp *pDisp = GetEditDisp();
  1162. if (pDisp == NULL)
  1163. return;
  1164. // Get the sphere center.
  1165. int iHit = pDisp->GetTexelHitIndex();
  1166. if ( iHit == -1 )
  1167. return;
  1168. // Get the sphere center and radius.
  1169. Vector vCenter;
  1170. pDisp->GetVert( iHit, vCenter );
  1171. float flRadius = GetSpatialRadius();
  1172. int size = ( int )( flRadius * 0.05f );
  1173. if ( size < 6 ) { size = 6; }
  1174. if ( size > 12 ) { size = 12; }
  1175. // Render the sphere.
  1176. if ( !IsNudging() )
  1177. {
  1178. pRender->RenderWireframeSphere( vCenter, flRadius, size, size, 0, 255, 0 );
  1179. }
  1180. else
  1181. {
  1182. pRender->RenderWireframeSphere( vCenter, flRadius, size, size, 255, 255, 0 );
  1183. }
  1184. // Render the displacement axis (as an arrow).
  1185. int nPaintAxis;
  1186. Vector vPaintAxis;
  1187. GetPaintAxis( nPaintAxis, vPaintAxis );
  1188. if( nPaintAxis == DISPPAINT_AXIS_SUBDIV )
  1189. {
  1190. pDisp->GetSubdivNormal( iHit, vPaintAxis );
  1191. }
  1192. float flBloat = flRadius * 0.15f;
  1193. pRender->RenderArrow( vCenter, vCenter + ( vPaintAxis * ( flRadius + flBloat ) ), 255, 255, 0 );
  1194. // Render cube at center point.
  1195. Vector vBoxMin, vBoxMax;
  1196. for ( int iAxis = 0; iAxis < 3; iAxis++ )
  1197. {
  1198. vBoxMin[iAxis] = vCenter[iAxis] - ( flBloat * 0.25f );
  1199. vBoxMax[iAxis] = vCenter[iAxis] + ( flBloat * 0.25f );
  1200. }
  1201. pRender->RenderBox( vBoxMin, vBoxMax, 255, 255, 0, SELECT_NONE );
  1202. }
  1203. //-----------------------------------------------------------------------------
  1204. // Purpose:
  1205. // Input : pRender -
  1206. // bNudge -
  1207. //-----------------------------------------------------------------------------
  1208. void CToolDisplace::RenderHitBox( CRender3D *pRender )
  1209. {
  1210. CMapDisp *pDisp = GetEditDisp();
  1211. if (pDisp == NULL)
  1212. return;
  1213. //
  1214. // get selection
  1215. //
  1216. int index = pDisp->GetTexelHitIndex();
  1217. if( index == -1 )
  1218. return;
  1219. //
  1220. // get the displacement map width and height
  1221. //
  1222. int width = pDisp->GetWidth();
  1223. int height = pDisp->GetHeight();
  1224. Vector seg[2];
  1225. Vector points[2];
  1226. pDisp->GetVert( 0, points[0] );
  1227. pDisp->GetVert( ( width - 1 ), points[1] );
  1228. VectorSubtract( points[1], points[0], seg[0] );
  1229. pDisp->GetVert( ( ( width - 1 ) * height ), points[1] );
  1230. VectorSubtract( points[1], points[0], seg[1] );
  1231. VectorAdd( seg[0], seg[1], seg[0] );
  1232. VectorScale( seg[0], 0.5f, seg[0] );
  1233. //
  1234. // determine a good size to make the "box" surrounding the selected point
  1235. //
  1236. float length = VectorLength( seg[0] );
  1237. length *= 0.025f;
  1238. //
  1239. // render the box
  1240. //
  1241. pDisp->GetVert( index, points[0] );
  1242. Vector minb, maxb;
  1243. minb[0] = points[0][0] - length;
  1244. minb[1] = points[0][1] - length;
  1245. minb[2] = points[0][2] - length;
  1246. maxb[0] = points[0][0] + length;
  1247. maxb[1] = points[0][1] + length;
  1248. maxb[2] = points[0][2] + length;
  1249. if( !IsNudging() )
  1250. {
  1251. pRender->RenderWireframeBox( minb, maxb, 0, 255, 0 );
  1252. }
  1253. else
  1254. {
  1255. pRender->RenderWireframeBox( minb, maxb, 255, 255, 0 );
  1256. }
  1257. //
  1258. // render the normal
  1259. //
  1260. // get hit box origin
  1261. Vector hbOrigin;
  1262. pDisp->GetVert( index, hbOrigin );
  1263. // get 4x length
  1264. float length4 = length * 4.0f;
  1265. int paintAxis;
  1266. Vector vecPaint;
  1267. GetPaintAxis( paintAxis, vecPaint );
  1268. if( paintAxis == DISPPAINT_AXIS_SUBDIV )
  1269. {
  1270. pDisp->GetSubdivNormal( index, vecPaint );
  1271. }
  1272. //
  1273. // render the normal -- just a yellow line at this point
  1274. //
  1275. pRender->RenderArrow( hbOrigin, hbOrigin + ( vecPaint * length4 ), 255, 255, 0 );
  1276. #if 0
  1277. CMeshBuilder meshBuilder;
  1278. IMesh *pMesh = MaterialSystemInterface()->GetDynamicMesh();
  1279. meshBuilder.Begin( pMesh, MATERIAL_LINES, 1 );
  1280. meshBuilder.Position3f( hbOrigin.x, hbOrigin.y, hbOrigin.z );
  1281. meshBuilder.Color3ub( 255, 255, 0 );
  1282. meshBuilder.AdvanceVertex();
  1283. meshBuilder.Position3f( hbOrigin.x + ( normal.x * length4 ),
  1284. hbOrigin.y + ( normal.y * length4 ),
  1285. hbOrigin.z + ( normal.z * length4 ) );
  1286. meshBuilder.Color3ub( 255, 255, 0 );
  1287. meshBuilder.AdvanceVertex();
  1288. meshBuilder.End();
  1289. pMesh->Draw();
  1290. #endif
  1291. }
  1292. //-----------------------------------------------------------------------------
  1293. // Purpose:
  1294. // Input : *pRender -
  1295. //-----------------------------------------------------------------------------
  1296. void CToolDisplace::RenderTool3D(CRender3D *pRender)
  1297. {
  1298. unsigned int uiTool = GetTool();
  1299. if ( uiTool == DISPTOOL_PAINT )
  1300. {
  1301. if ( IsSpatialPainting() )
  1302. {
  1303. RenderPaintSphere( pRender );
  1304. }
  1305. else
  1306. {
  1307. RenderHitBox( pRender );
  1308. }
  1309. }
  1310. if ( uiTool == DISPTOOL_PAINT_SCULPT )
  1311. {
  1312. m_SculptTool->RenderTool3D( pRender );
  1313. }
  1314. }