BVB Source Codes

CRYENGINE Show terrain_sector_tex.cpp Source code

Return Download CRYENGINE: download terrain_sector_tex.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   terrain_sector_tex.cpp
  5. //  Version:     v1.00
  6. //  Created:     28/5/2001 by Vladimir Kajalin
  7. //  Compilers:   Visual Studio.NET
  8. //  Description: terrain texture management
  9. // -------------------------------------------------------------------------
  10. //  History:
  11. //
  12. ////////////////////////////////////////////////////////////////////////////
  13.  
  14. #include "StdAfx.h"
  15. #include "terrain_sector.h"
  16. #include "terrain.h"
  17. #include "ObjMan.h"
  18.  
  19. // load textures from buffer
  20. int CTerrainNode::CreateSectorTexturesFromBuffer(float * pSectorHeightMap)
  21. {
  22.         FUNCTION_PROFILER_3DENGINE;
  23.  
  24.         InvalidatePermanentRenderObject();
  25.  
  26.         STerrainTextureLayerFileHeader* pLayers = GetTerrain()->m_arrBaseTexInfos[m_nSID].m_TerrainTextureLayer;
  27.  
  28.         //      uint32 nSectorDataSize = pLayers[0].nSectorSizeBytes + pLayers[1].nSectorSizeBytes;
  29.  
  30.         // make RGB texture
  31.         m_nNodeTexSet.nTex0 = m_pTerrain->m_texCache[0].GetTexture(GetTerrain()->m_arrBaseTexInfos[m_nSID].m_ucpDiffTexTmpBuffer, m_nNodeTexSet.nSlot0);
  32.  
  33.         // make normal map
  34.         m_nNodeTexSet.nTex1 = m_pTerrain->m_texCache[1].GetTexture(GetTerrain()->m_arrBaseTexInfos[m_nSID].m_ucpDiffTexTmpBuffer + pLayers[0].nSectorSizeBytes, m_nNodeTexSet.nSlot1);
  35.  
  36.         // make height map
  37.         m_nNodeTexSet.nTex2 = m_pTerrain->m_texCache[2].GetTexture((byte*)pSectorHeightMap, m_nNodeTexSet.nSlot2);
  38.  
  39.         if (GetCVars()->e_TerrainTextureStreamingDebug == 2)
  40.                 PrintMessage("CTerrainNode::CreateSectorTexturesFromBuffer: sector %d, level=%d", GetSecIndex(), m_nTreeLevel);
  41.  
  42.         assert(m_nNodeTexSet.nTex0);
  43.  
  44. #if defined(FEATURE_SVO_GI)
  45.         // keep low res terrain colors in system memory for CPU-side voxelization
  46.         if (!m_pParent)
  47.         {
  48.                 int nDim = GetTerrain()->m_arrBaseTexInfos[0].m_TerrainTextureLayer[0].nSectorSizePixels;
  49.                 ETEX_Format texFormat = GetTerrain()->m_arrBaseTexInfos[0].m_TerrainTextureLayer[0].eTexFormat;
  50.                 int nSizeDxtMip0 = GetRenderer()->GetTextureFormatDataSize(nDim, nDim, 1, 1, texFormat);
  51.                 int nSizeMip0 = GetRenderer()->GetTextureFormatDataSize(nDim, nDim, 1, 1, eTF_R8G8B8A8);
  52.  
  53.                 SAFE_DELETE(GetTerrain()->m_pTerrainRgbLowResSystemCopy);
  54.                 GetTerrain()->m_pTerrainRgbLowResSystemCopy = new PodArray<ColorB>;
  55.                 GetTerrain()->m_pTerrainRgbLowResSystemCopy->CheckAllocated(nSizeMip0 / sizeof(ColorB));
  56.  
  57.                 GetRenderer()->DXTDecompress(GetTerrain()->m_arrBaseTexInfos[m_nSID].m_ucpDiffTexTmpBuffer, nSizeDxtMip0,
  58.                                              (byte*)GetTerrain()->m_pTerrainRgbLowResSystemCopy->GetElements(), nDim, nDim, 1, texFormat, false, 4);
  59.         }
  60. #endif
  61.  
  62.         return (m_nNodeTexSet.nTex0 > 0);
  63. }
  64.  
  65. void CTerrainNode::StreamAsyncOnComplete(IReadStream* pStream, unsigned nError)
  66. {
  67.         FUNCTION_PROFILER_3DENGINE;
  68.  
  69.         if (pStream->IsError())
  70.         {
  71.                 // file was not loaded successfully
  72.                 if (pStream->GetError() == ERROR_USER_ABORT)
  73.                         Warning("CTerrainNode::StreamAsyncOnComplete: node streaming aborted, sector %d, level=%d", GetSecIndex(), m_nTreeLevel);
  74.                 else
  75.                 {
  76.                         Warning("CTerrainNode::StreamAsyncOnComplete: error streaming node, sector %d, level=%d, error=%s", GetSecIndex(), m_nTreeLevel, pStream->GetErrorName());
  77.                         assert(!"Error streaming node");
  78.                 }
  79.                 m_eTexStreamingStatus = ecss_NotLoaded;
  80.                 m_pReadStream = NULL;
  81.                 return;
  82.         }
  83.  
  84.         Array2d<float> arrHmData;
  85.         FillSectorHeightMapTextureData(arrHmData);
  86.  
  87.         memcpy((float*)pStream->GetUserData(), arrHmData.GetData(), arrHmData.GetDataSize());
  88. }
  89.  
  90. void CTerrainNode::StreamOnComplete(IReadStream* pStream, unsigned nError)
  91. {
  92.         FUNCTION_PROFILER_3DENGINE;
  93.  
  94.         if (pStream->IsError())
  95.         {
  96.                 // file was not loaded successfully
  97.                 if (pStream->GetError() == ERROR_USER_ABORT)
  98.                         Warning("CTerrainNode::StreamOnComplete: node streaming aborted, sector %d, level=%d", GetSecIndex(), m_nTreeLevel);
  99.                 else
  100.                 {
  101.                         Warning("CTerrainNode::StreamOnComplete: error streaming node, sector %d, level=%d, error=%s", GetSecIndex(), m_nTreeLevel, pStream->GetErrorName());
  102.                         assert(!"Error streaming node");
  103.                 }
  104.                 m_eTexStreamingStatus = ecss_NotLoaded;
  105.                 m_pReadStream = NULL;
  106.                 return;
  107.         }
  108.  
  109.         memcpy(GetTerrain()->m_arrBaseTexInfos[m_nSID].m_ucpDiffTexTmpBuffer, pStream->GetBuffer(), pStream->GetBytesRead());
  110.         CreateSectorTexturesFromBuffer((float*)pStream->GetUserData());
  111.         delete [] (float*)pStream->GetUserData();
  112.  
  113.         CalculateTexGen(this, m_nNodeTexSet.fTexOffsetX, m_nNodeTexSet.fTexOffsetY, m_nNodeTexSet.fTexScale);
  114.  
  115.         m_eTexStreamingStatus = ecss_Ready;
  116.  
  117.         m_pReadStream = NULL;
  118.  
  119. #ifndef _RELEASE
  120.         // check and request texture update from editor side data
  121.         if (m_eTextureEditingState == eTES_SectorIsModified_AtlasIsUpToDate)
  122.                 m_eTextureEditingState = eTES_SectorIsModified_AtlasIsDirty;
  123. #endif
  124. }
  125.  
  126. void CTerrainNode::StartSectorTexturesStreaming(bool bFinishNow)
  127. {
  128.         assert(m_eTexStreamingStatus == ecss_NotLoaded);
  129.  
  130.         CTerrain* pT = GetTerrain();
  131.  
  132.         if (!pT->m_arrBaseTexInfos[m_nSID].m_nDiffTexIndexTableSize)
  133.                 return; // file was not opened
  134.  
  135. #if defined(FEATURE_SVO_GI)
  136.         // make sure top most node is ready immediately
  137.         if (!m_pParent && GetCVars()->e_svoTI_Apply)
  138.                 bFinishNow = true;
  139. #endif
  140.  
  141.         STerrainTextureLayerFileHeader* pLayers = GetTerrain()->m_arrBaseTexInfos[m_nSID].m_TerrainTextureLayer;
  142.  
  143.         uint32 nSectorDataSize = pLayers[0].nSectorSizeBytes + pLayers[1].nSectorSizeBytes;
  144.  
  145.         // calculate sector data offset in file
  146.         int nFileOffset = sizeof(SCommonFileHeader) + sizeof(STerrainTextureFileHeader)
  147.                           + pT->m_arrBaseTexInfos[m_nSID].m_hdrDiffTexInfo.nLayerCount * sizeof(STerrainTextureLayerFileHeader)
  148.                           + pT->m_arrBaseTexInfos[m_nSID].m_nDiffTexIndexTableSize
  149.                           + m_nNodeTextureOffset * nSectorDataSize;
  150.  
  151.         // start streaming
  152.         StreamReadParams params;       
  153.         params.dwUserData = (DWORD_PTR) new float[m_pTerrain->m_texCache[2].m_nDim * m_pTerrain->m_texCache[2].m_nDim];
  154.         params.nSize = nSectorDataSize;
  155.         params.nLoadTime = 1000;
  156.         params.nMaxLoadTime = 0;
  157.         params.nOffset = nFileOffset;
  158.         if (bFinishNow)
  159.                 params.ePriority = estpUrgent;
  160.  
  161.         m_pReadStream = GetSystem()->GetStreamEngine()->StartRead(eStreamTaskTypeTerrain,
  162.                                                                   Get3DEngine()->GetLevelFilePath(GetTerrain()->m_arrSegmentPaths[m_nSID] + COMPILED_TERRAIN_TEXTURE_FILE_NAME), this, &params);
  163.  
  164.         m_eTexStreamingStatus = ecss_InProgress;
  165.  
  166.         if (GetCVars()->e_TerrainTextureStreamingDebug == 2)
  167.                 PrintMessage("CTerrainNode::StartSectorTexturesStreaming: sector %d, level=%d", GetSecIndex(), m_nTreeLevel);
  168.  
  169.         if (bFinishNow)
  170.                 m_pReadStream->Wait();
  171. }
  172.  
  173. void CTerrainNode::CalculateTexGen(const CTerrainNode* pTextureSourceNode, float& fTexOffsetX, float& fTexOffsetY, float& fTexScale)
  174. {
  175.         float fSectorSizeScale = 1.0f;
  176.         uint16 nSectorSizePixels = GetTerrain()->m_arrBaseTexInfos[m_nSID].m_TerrainTextureLayer[0].nSectorSizePixels;
  177.         if (nSectorSizePixels)
  178.                 fSectorSizeScale -= 1.0f / (float)(nSectorSizePixels); // we don't use half texel border so we have to compensate
  179.  
  180.         float dCSS = fSectorSizeScale / (CTerrain::GetSectorSize() << pTextureSourceNode->m_nTreeLevel);
  181.         fTexOffsetX = -dCSS * pTextureSourceNode->m_nOriginY + GetTerrain()->m_arrSegmentOrigns[m_nSID].y;
  182.         fTexOffsetY = -dCSS * pTextureSourceNode->m_nOriginX + GetTerrain()->m_arrSegmentOrigns[m_nSID].x;
  183.         fTexScale = dCSS;
  184.  
  185.         // shift texture by 0.5 pixel
  186.         if (float fTexRes = nSectorSizePixels)
  187.         {
  188.                 fTexOffsetX += 0.5f / fTexRes;
  189.                 fTexOffsetY += 0.5f / fTexRes;
  190.         }
  191.  
  192.         if (pTextureSourceNode->m_nNodeTexSet.nSlot0 != 0xffff)
  193.         {
  194.                 RectI region;
  195.                 m_pTerrain->m_texCache[0].GetSlotRegion(region, pTextureSourceNode->m_nNodeTexSet.nSlot0);
  196.  
  197.                 float fPoolScale = (float)region.w / (float)m_pTerrain->m_texCache[0].GetPoolTexDim();
  198.                 float fPoolTexDim = (float)m_pTerrain->m_texCache[0].GetPoolTexDim();
  199.  
  200.                 fTexOffsetX = fTexOffsetX * fPoolScale + (float)(region.x) / fPoolTexDim;
  201.                 fTexOffsetY = fTexOffsetY * fPoolScale + (float)(region.y) / fPoolTexDim;
  202.                 fTexScale *= fPoolScale;
  203.  
  204.                 if (GetCVars()->e_TerrainTextureStreamingDebug && GetConsole()->GetCVar("r_ShowTexture"))
  205.                         GetConsole()->GetCVar("r_ShowTexture")->Set(m_pTerrain->m_texCache[0].m_nPoolTexId);
  206.         }
  207. }
  208.  
downloadterrain_sector_tex.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