BVB Source Codes

CRYENGINE Show ColorGradientManager.cpp Source code

Return Download CRYENGINE: download ColorGradientManager.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.  
  5. -------------------------------------------------------------------------
  6. History:
  7. - 15:05:2009   Created by Federico Rebora
  8.  
  9. *************************************************************************/
  10.  
  11. #include "StdAfx.h"
  12.  
  13. #include "ColorGradientManager.h"
  14.  
  15. CColorGradientManager::CColorGradientManager()
  16. {
  17. }
  18.  
  19. void CColorGradientManager::Reset()
  20. {
  21.         stl::free_container(m_colorGradientsToLoad);
  22.  
  23.         for (std::vector<LoadedColorGradient>::iterator it = m_currentGradients.begin(), itEnd = m_currentGradients.end(); it != itEnd; ++ it)
  24.         {
  25.                 LoadedColorGradient& cg = *it;
  26.                 if (cg.m_layer.m_texID >= 0)
  27.                 {
  28.                         GetColorGradingController().UnloadColorChart(cg.m_layer.m_texID);
  29.                 }
  30.         }
  31.  
  32.         stl::free_container(m_currentGradients);
  33. }
  34.  
  35. void CColorGradientManager::Serialize(TSerialize serializer)
  36. {
  37.         if(serializer.IsReading())
  38.                 Reset();
  39.  
  40.         serializer.BeginGroup("ColorGradientManager");
  41.         {
  42.                 int numToLoad = (int)m_colorGradientsToLoad.size();
  43.                 int numLoaded = (int)m_currentGradients.size();
  44.                 int numGradients = numToLoad + numLoaded;
  45.                 serializer.Value("ColorGradientCount", numGradients);
  46.                 if(serializer.IsWriting())
  47.                 {
  48.                         for(int i=0; i<numToLoad; ++i)
  49.                         {
  50.                                 LoadingColorGradient& gradient = m_colorGradientsToLoad[i];
  51.                                 serializer.BeginGroup("ColorGradient");
  52.                                 serializer.Value("FilePath", gradient.m_filePath);
  53.                                 serializer.Value("FadeInTime", gradient.m_fadeInTimeInSeconds);
  54.                                 serializer.EndGroup();
  55.                         }
  56.                         for(int i=0; i<numLoaded; ++i)
  57.                         {
  58.                                 LoadedColorGradient& gradient = m_currentGradients[i];
  59.                                 serializer.BeginGroup("ColorGradient");
  60.                                 serializer.Value("FilePath", gradient.m_filePath);
  61.                                 serializer.Value("BlendAmount", gradient.m_layer.m_blendAmount);
  62.                                 serializer.Value("FadeInTime", gradient.m_fadeInTimeInSeconds);
  63.                                 serializer.Value("ElapsedTime", gradient.m_elapsedTime);
  64.                                 serializer.Value("MaximumBlendAmount", gradient.m_maximumBlendAmount);
  65.                                 serializer.EndGroup();
  66.                         }
  67.                 }
  68.                 else
  69.                 {
  70.                         m_currentGradients.reserve(numGradients);
  71.                         for(int i=0; i<numGradients; ++i)
  72.                         {
  73.                                 serializer.BeginGroup("ColorGradient");
  74.                                 string filePath;
  75.                                 float blendAmount = 1.0f;
  76.                                 float fadeInTimeInSeconds = 0.0f;
  77.                                 serializer.Value("FilePath", filePath);
  78.                                 serializer.Value("BlendAmount", blendAmount);
  79.                                 serializer.Value("FadeInTime", fadeInTimeInSeconds);
  80.                                 const int textureID = GetColorGradingController().LoadColorChart(filePath);
  81.                                 LoadedColorGradient gradient(filePath, SColorChartLayer(textureID, blendAmount), fadeInTimeInSeconds);
  82.  
  83.                                 // Optional
  84.                                 serializer.ValueWithDefault("ElapsedTime", gradient.m_elapsedTime, 0.0f);
  85.                                 serializer.ValueWithDefault("MaximumBlendAmount", gradient.m_maximumBlendAmount, 1.0f);
  86.  
  87.                                 m_currentGradients.push_back(gradient);
  88.                                 serializer.EndGroup();
  89.                         }
  90.                 }
  91.                 serializer.EndGroup();
  92.         }
  93. }
  94.  
  95. void CColorGradientManager::TriggerFadingColorGradient(const string& filePath, const float fadeInTimeInSeconds)
  96. {
  97.         const unsigned int numGradients = (int) m_currentGradients.size();
  98.         for (unsigned int currentGradientIndex = 0; currentGradientIndex < numGradients; ++currentGradientIndex)
  99.         {
  100.                 m_currentGradients[currentGradientIndex].FreezeMaximumBlendAmount();
  101.         }
  102.  
  103.         m_colorGradientsToLoad.push_back(LoadingColorGradient(filePath, fadeInTimeInSeconds));
  104. }
  105.  
  106. void CColorGradientManager::UpdateForThisFrame(const float frameTimeInSeconds)
  107. {
  108.         RemoveZeroWeightedLayers();
  109.        
  110.         LoadGradients();
  111.  
  112.         FadeInLastLayer(frameTimeInSeconds);
  113.         FadeOutCurrentLayers();
  114.  
  115.         SetLayersForThisFrame();
  116. }
  117.  
  118. void CColorGradientManager::FadeInLastLayer(const float frameTimeInSeconds)
  119. {
  120.         if (m_currentGradients.empty())
  121.         {
  122.                 return;
  123.         }
  124.  
  125.         m_currentGradients.back().FadeIn(frameTimeInSeconds);
  126. }
  127.  
  128. void CColorGradientManager::FadeOutCurrentLayers()
  129. {
  130.         if (m_currentGradients.size() <= 1u)
  131.         {
  132.                 return;
  133.         }
  134.  
  135.         const unsigned int numberofFadingOutGradients = (int) m_currentGradients.size() - 1;
  136.         const float fadingInLayerBlendAmount = m_currentGradients[numberofFadingOutGradients].m_layer.m_blendAmount;
  137.         for (unsigned int index = 0; index < numberofFadingOutGradients; ++index)
  138.         {
  139.                 m_currentGradients[index].FadeOut(fadingInLayerBlendAmount);
  140.         }
  141. }
  142.  
  143. void CColorGradientManager::RemoveZeroWeightedLayers()
  144. {
  145.         std::vector<LoadedColorGradient>::iterator currentGradient = m_currentGradients.begin();
  146.  
  147.         while (currentGradient != m_currentGradients.end())
  148.         {
  149.                 if (currentGradient->m_layer.m_blendAmount == 0.0f)
  150.                 {
  151.                         GetColorGradingController().UnloadColorChart(currentGradient->m_layer.m_texID);
  152.  
  153.                         currentGradient = m_currentGradients.erase(currentGradient);
  154.                 }
  155.      else
  156.      {
  157.        ++currentGradient;
  158.      }
  159.         }
  160. }
  161.  
  162. void CColorGradientManager::SetLayersForThisFrame()
  163. {
  164.         std::vector<SColorChartLayer> thisFrameLayers;
  165.  
  166.         const unsigned int numberOfFadingInGradients = (int) m_currentGradients.size();
  167.         thisFrameLayers.reserve(numberOfFadingInGradients + thisFrameLayers.size());
  168.         for (unsigned int index = 0; index < numberOfFadingInGradients; ++index)
  169.         {
  170.                 thisFrameLayers.push_back(m_currentGradients[index].m_layer);
  171.         }
  172.  
  173.         const uint32 numLayers = (uint32) thisFrameLayers.size();
  174.         const SColorChartLayer* pLayers = numLayers ? &thisFrameLayers.front() : 0;
  175.         GetColorGradingController().SetLayers(pLayers, numLayers);
  176. }
  177.  
  178. void CColorGradientManager::LoadGradients()
  179. {
  180.         const unsigned int numGradientsToLoad = (int) m_colorGradientsToLoad.size();
  181.         m_currentGradients.reserve(numGradientsToLoad +  m_currentGradients.size());
  182.         for (unsigned int index = 0; index < numGradientsToLoad; ++index)
  183.         {
  184.                 LoadedColorGradient loadedGradient = m_colorGradientsToLoad[index].Load(GetColorGradingController());
  185.                
  186.                 m_currentGradients.push_back(loadedGradient);
  187.         }
  188.  
  189.         m_colorGradientsToLoad.clear();
  190. }
  191.  
  192. IColorGradingController& CColorGradientManager::GetColorGradingController()
  193. {
  194.         return *gEnv->pRenderer->GetIColorGradingController();
  195. }
  196.  
  197. CColorGradientManager::LoadedColorGradient::LoadedColorGradient(const string& filePath, const SColorChartLayer& layer, const float fadeInTimeInSeconds)
  198. : m_filePath(filePath)
  199. , m_layer(layer)
  200. , m_fadeInTimeInSeconds(fadeInTimeInSeconds)
  201. , m_elapsedTime(0.0f)
  202. , m_maximumBlendAmount(1.0f)
  203. {
  204.  
  205. }
  206.  
  207. void CColorGradientManager::LoadedColorGradient::FadeIn(const float frameTimeInSeconds)
  208. {
  209.         if (m_fadeInTimeInSeconds == 0.0f)
  210.         {
  211.                 m_layer.m_blendAmount = 1.0f;
  212.  
  213.                 return;
  214.         }
  215.  
  216.         m_elapsedTime += frameTimeInSeconds;
  217.  
  218.         const float blendAmount = m_elapsedTime / m_fadeInTimeInSeconds;
  219.  
  220.         m_layer.m_blendAmount = min(blendAmount, 1.0f);
  221. }
  222.  
  223. void CColorGradientManager::LoadedColorGradient::FadeOut( const float blendAmountOfFadingInGradient )
  224. {
  225.         m_layer.m_blendAmount = m_maximumBlendAmount * (1.0f - blendAmountOfFadingInGradient);
  226. }
  227.  
  228. void CColorGradientManager::LoadedColorGradient::FreezeMaximumBlendAmount()
  229. {
  230.         m_maximumBlendAmount = m_layer.m_blendAmount;
  231. }
  232.  
  233. CColorGradientManager::LoadingColorGradient::LoadingColorGradient(const string& filePath, const float fadeInTimeInSeconds)
  234. : m_filePath(filePath)
  235. , m_fadeInTimeInSeconds(fadeInTimeInSeconds)
  236. {
  237.  
  238. }
  239.  
  240. CColorGradientManager::LoadedColorGradient CColorGradientManager::LoadingColorGradient::Load(IColorGradingController& colorGradingController) const
  241. {
  242.         const int textureID = colorGradingController.LoadColorChart(m_filePath);
  243.  
  244.         return LoadedColorGradient(m_filePath, SColorChartLayer(textureID, 1.0f), m_fadeInTimeInSeconds);
  245. }
  246.  
downloadColorGradientManager.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