BVB Source Codes

CRYENGINE Show TerrainModifications.cpp Source code

Return Download CRYENGINE: download TerrainModifications.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "TerrainModifications.h"         // CTerrainModifications
  5. #include "terrain.h"                      // CTerrain
  6. #include "terrain_sector.h"               // CTerrainNode
  7.  
  8. CTerrainModifications::CTerrainModifications() : m_pModifiedTerrain(0)
  9. {
  10. }
  11.  
  12. void CTerrainModifications::FreeData()
  13. {
  14.         m_TerrainMods.clear();
  15. }
  16.  
  17. bool CTerrainModifications::PushModification(const Vec3& vPos, const float fRadius)
  18. {
  19.         assert(fRadius > 0);
  20.         assert(m_pModifiedTerrain);       // you need to call SetTerrain()
  21.  
  22.         m_TerrainMods.push_back(STerrainMod(vPos, fRadius, m_pModifiedTerrain->GetZ((int)vPos.x, (int)vPos.y, GetDefSID())));
  23.  
  24.         MakeCrater(m_TerrainMods.back(), m_TerrainMods.size() - 1);
  25.  
  26.         return true;
  27. }
  28.  
  29. // Arguments:
  30. //   fDamage - 0=outside..1=midpoint
  31. static float CalcDepth(const float fDamage)
  32. {
  33.         return (fDamage);
  34. }
  35.  
  36. float CTerrainModifications::ComputeMaxDepthAt(const float fX, const float fY, const uint32 dwCheckExistingMods) const
  37. {
  38.         float fRet = 0;
  39.  
  40.         std::vector<STerrainMod>::const_iterator it, end = m_TerrainMods.end();
  41.  
  42.         float fGroundPos = m_pModifiedTerrain->GetZ((int)fX, (int)fY, GetDefSID());
  43.  
  44.         uint32 dwI = 0;
  45.         for (it = m_TerrainMods.begin(); it != end && dwI < dwCheckExistingMods; ++it, ++dwI)
  46.         {
  47.                 const STerrainMod& ref = *it;
  48.  
  49.                 float fDist2 = sqr(ref.m_vPos.x - fX) + sqr(ref.m_vPos.y - fY) + sqr(ref.m_vPos.z - fGroundPos);
  50.  
  51.                 if (fDist2 < ref.m_fRadius * ref.m_fRadius)
  52.                 {
  53.                         float fDamage = 1.0f - sqrtf(fDist2) / ref.m_fRadius;
  54.  
  55.                         fRet += CalcDepth(fDamage);
  56.                 }
  57.         }
  58.  
  59.         return fRet;
  60. }
  61.  
  62. void CTerrainModifications::MakeCrater(const STerrainMod& ref, const uint32 dwCheckExistingMod)
  63. {
  64.         if (!GetCVars()->e_TerrainDeformations)
  65.                 return;
  66.  
  67.         assert(m_pModifiedTerrain);
  68.  
  69.         // calculate 2d area
  70.         int nUnitSize = m_pModifiedTerrain->GetHeightMapUnitSize();
  71.         int x1 = int(ref.m_vPos.x - ref.m_fRadius - nUnitSize);
  72.         int y1 = int(ref.m_vPos.y - ref.m_fRadius - nUnitSize);
  73.         int x2 = int(ref.m_vPos.x + ref.m_fRadius + nUnitSize);
  74.         int y2 = int(ref.m_vPos.y + ref.m_fRadius + nUnitSize);
  75.         x1 = x1 / nUnitSize * nUnitSize;
  76.         x2 = x2 / nUnitSize * nUnitSize;
  77.         y1 = y1 / nUnitSize * nUnitSize;
  78.         y2 = y2 / nUnitSize * nUnitSize;
  79.         if (x1 < 0) x1 = 0;
  80.         if (y1 < 0) y1 = 0;
  81.         if (x2 >= CTerrain::GetTerrainSize()) x2 = CTerrain::GetTerrainSize() - 1;
  82.         if (y2 >= CTerrain::GetTerrainSize()) y2 = CTerrain::GetTerrainSize() - 1;
  83.  
  84.         float fOceanLevel = m_pModifiedTerrain->GetWaterLevel() + 0.25f;
  85.  
  86.         float fInvExplRadius = 1.0f / ref.m_fRadius;
  87.  
  88.         // to limit the height modifications to a reasonable value
  89.         float fMaxDepth = ComputeMaxDepthAt(ref.m_vPos.x, ref.m_vPos.y, dwCheckExistingMod);
  90.  
  91.         // modify height map
  92.         for (int x = x1; x <= x2; x += nUnitSize)
  93.                 for (int y = y1; y <= y2; y += nUnitSize)
  94.                 {
  95.                         float fHeight = m_pModifiedTerrain->GetZ(x, y, GetDefSID());
  96.  
  97.                         float fDamage = 1.0f - ref.m_vPos.GetDistance(Vec3((float)x, (float)y, fHeight)) * fInvExplRadius;
  98.                         if (fDamage < 0)
  99.                                 continue;
  100.  
  101.                         float fDepthMod = CLAMP(CalcDepth(fDamage) - fMaxDepth, 0, TERRAIN_DEFORMATION_MAX_DEPTH);
  102.  
  103.                         // modify elevation if above the ocean level
  104.                         if (fHeight > fOceanLevel && fHeight > fDepthMod)
  105.                         {
  106.                                 fHeight -= fDepthMod;
  107.                                 if (fHeight < fOceanLevel)
  108.                                         fHeight = fOceanLevel;
  109.                         }
  110.  
  111.                         m_pModifiedTerrain->m_bHeightMapModified = 1;
  112.  
  113.                         m_pModifiedTerrain->SetZ(x, y, fHeight, GetDefSID());
  114.                 }
  115.  
  116.         // update mesh or terrain near sectors
  117.         PodArray<CTerrainNode*> lstNearSecInfos;
  118.         Vec3 vRadius(ref.m_fRadius, ref.m_fRadius, ref.m_fRadius);
  119.         m_pModifiedTerrain->IntersectWithBox(AABB(ref.m_vPos - vRadius, ref.m_vPos + vRadius), &lstNearSecInfos, GetDefSID());
  120.         for (int s = 0; s < lstNearSecInfos.Count(); s++)
  121.                 if (CTerrainNode* pSecInfo = lstNearSecInfos[s])
  122.                         pSecInfo->ReleaseHeightMapGeometry();
  123.  
  124.         m_pModifiedTerrain->ResetHeightMapCache();
  125. }
  126.  
  127. void CTerrainModifications::SetTerrain(CTerrain& rTerrain)
  128. {
  129.         m_pModifiedTerrain = &rTerrain;
  130. }
  131.  
  132. void CTerrainModifications::SerializeTerrainState(TSerialize ser)
  133. {
  134.         assert(m_pModifiedTerrain);       // you need to call SetTerrain()
  135.  
  136.         if (ser.IsReading())
  137.         {
  138.                 FreeData();
  139.         }
  140.  
  141.         //      if(ser.IsReading())
  142.         //              m_TerrainMods.clear();
  143.  
  144.         ser.Value("TerrainMods", m_TerrainMods);
  145.  
  146.         if (ser.IsReading())
  147.         {
  148.                 uint32 dwI = 0;
  149.                 std::vector<STerrainMod>::const_iterator it, end = m_TerrainMods.end();
  150.  
  151.                 for (it = m_TerrainMods.begin(); it != end; ++it, ++dwI)
  152.                 {
  153.                         const STerrainMod& ref = *it;
  154.  
  155.                         MakeCrater(ref, dwI);
  156.                 }
  157.         }
  158. }
  159.  
downloadTerrainModifications.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top