FlattenBrush.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*************************************************************************************************\
  2. FlattenBrush.cpp : Implementation of the FlattenBrush component.
  3. //---------------------------------------------------------------------------//
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. //===========================================================================//
  6. \*************************************************************************************************/
  7. #include "FlattenBrush.h"
  8. //-------------------------------------------------------------------------------------------------
  9. FlattenBrush::FlattenBrush()
  10. {
  11. pCurAction = NULL;
  12. }
  13. //-------------------------------------------------------------------------------------------------
  14. FlattenBrush::~FlattenBrush()
  15. {
  16. }
  17. //-------------------------------------------------------------------------------------------------
  18. bool FlattenBrush::beginPaint()
  19. {
  20. pCurAction = new ActionPaintTile();
  21. return true;
  22. }
  23. //-------------------------------------------------------------------------------------------------
  24. Action* FlattenBrush::endPaint()
  25. {
  26. ActionPaintTile* pRetAction = pCurAction;
  27. if (pCurAction)
  28. {
  29. if ( !pCurAction->vertexInfoList.Count() )
  30. {
  31. delete pCurAction;
  32. pRetAction = NULL;
  33. }
  34. }
  35. // land->reCalcLight();
  36. land->recalcWater();
  37. pCurAction = NULL;
  38. return pRetAction;
  39. }
  40. //-------------------------------------------------------------------------------------------------
  41. bool FlattenBrush::paint( Stuff::Vector3D& worldPos, int screenX, int screenY )
  42. {
  43. int closestX = floor( (worldPos.x - land->mapTopLeft3d.x)/land->worldUnitsPerVertex + .5 );
  44. int closestY = floor( (land->mapTopLeft3d.y - worldPos.y)/land->worldUnitsPerVertex + .5 );
  45. float height = 0.f;
  46. float viable = 0.f;
  47. for ( int i = closestX - 1; i < closestX + 2; ++i )
  48. {
  49. for ( int j = closestY - 1; j < closestY + 2; ++j )
  50. {
  51. if ( j < land->realVerticesMapSide && j > -1
  52. && i < land->realVerticesMapSide && i > -1 )
  53. {
  54. height += land->getTerrainElevation( j, i );
  55. viable += 1.0f;
  56. }
  57. }
  58. }
  59. height/=viable;
  60. if ( closestY > -1 && closestY < land->realVerticesMapSide
  61. && closestX < land->realVerticesMapSide && closestX > -1 )
  62. flattenVertex( closestY, closestX, height );
  63. return true;
  64. }
  65. //-------------------------------------------------------------------------------------------------
  66. Action* FlattenBrush::applyToSelection()
  67. {
  68. float height = getAverageHeightOfSelection();
  69. return applyHeightToSelection( height );
  70. }
  71. //-------------------------------------------------------------------------------------------------
  72. float FlattenBrush::getAverageHeightOfSelection( )
  73. {
  74. float height = 0.f;
  75. float count = 0.0f;
  76. for ( int j = 0; j < land->realVerticesMapSide; ++j )
  77. {
  78. for ( int i = 0; i < land->realVerticesMapSide; ++i )
  79. {
  80. if ( land->isVertexSelected( j, i ) )
  81. {
  82. count += 1.f;
  83. height += land->getTerrainElevation( j, i );
  84. }
  85. }
  86. }
  87. height /= count;
  88. return height;
  89. }
  90. //-------------------------------------------------------------------------------------------------
  91. Action* FlattenBrush::applyHeightToSelection( float height )
  92. {
  93. beginPaint();
  94. for ( int j = 0; j < land->realVerticesMapSide; ++j )
  95. {
  96. for ( int i = 0; i < land->realVerticesMapSide; ++i )
  97. {
  98. if ( land->isVertexSelected( j, i ) )
  99. {
  100. flattenVertex( j, i, height );
  101. }
  102. }
  103. }
  104. return endPaint();
  105. }
  106. //-------------------------------------------------------------------------------------------------
  107. void FlattenBrush::flattenVertex( int row, int col, float val )
  108. {
  109. #if 1 /*flattening without "area effect"*/
  110. {
  111. int i = col;
  112. int j = row;
  113. if ( i > -1 && i < land->realVerticesMapSide
  114. && j > -1 && j < land->realVerticesMapSide )
  115. {
  116. pCurAction->addChangedVertexInfo( j, i );
  117. land->setVertexHeight( j * land->realVerticesMapSide + i, val );
  118. }
  119. }
  120. #else /*flattening without "area effect"*/
  121. for ( int i = col - 1; i < col + 2; ++i )
  122. {
  123. for ( int j = row - 1; j < row + 2; ++j )
  124. {
  125. if ( i > -1 && i < land->realVerticesMapSide
  126. && j > -1 && j < land->realVerticesMapSide )
  127. {
  128. pCurAction->addChangedVertexInfo( j, i );
  129. land->setVertexHeight( j * land->realVerticesMapSide + i, val );
  130. }
  131. }
  132. }
  133. #endif /*flattening without "area effect"*/
  134. }
  135. //*************************************************************************************************
  136. // end of file ( FlattenBrush.cpp )