All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ThemeStateController.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "ICityThemeStateObservable.h"
7 #include "Rendering.h"
8 #include "Lighting.h"
9 #include "Weather.h"
10 
11 // TODO: Move implementation into cpp and get rid of all these includes.
12 #include "WeatherOverlayMaterial2.h"
13 #include "WeatherOverlayController.h"
14 #include "PackedRenderable.h"
15 #include "EnvironmentMaterialRepository.h"
16 #include "VectorMath.h"
17 #include "IInterestPointProvider.h"
18 #include "GlobalShadowing.h"
19 #include "HttpAsyncTextureLoader.h"
20 #include "ICityThemeStateObserver.h"
21 #include "SceneElementRepository.h"
22 #include "EnvironmentStateTextures.h"
23 #include "GlobalLighting.h"
24 #include "GlobalFogging.h"
25 #include "EarthConstants.h"
26 #include "CityThemeState.h"
27 #include "SpaceHelpers.h"
28 
29 namespace Eegeo
30 {
31  namespace Resources
32  {
33  namespace CityThemes
34  {
36  : protected Eegeo::NonCopyable
38  {
39  public:
41  Rendering::EnvironmentMaterialRepository& materialRepository,
42  Rendering::Scene::SceneElementRepository<Rendering::Renderables::PackedRenderable>& buildingsRenderables, // this concern can go to another type..?
45  Lighting::GlobalLighting& globalLighting,
46  Lighting::GlobalFogging& globalFogging,
47  Lighting::GlobalShadowing& globalShadowing,
50  Eegeo::Weather::WeatherOverlayController & weatherOverlayController,
51  Rendering::Materials::WeatherOverlayMaterial2& weatherOverlayMaterial)
52  : m_materialRepository(materialRepository)
53  , m_fromTextures(NULL)
54  , m_toTextures(NULL)
55  , m_transportRenderables(transportRenderables)
56  , m_terrainRenderables(terrainRenderables)
57  , m_buildingRenderables(buildingsRenderables)
58  , m_globalLighting(globalLighting)
59  , m_globalFogging(globalFogging)
60  , m_globalShadowing(globalShadowing)
61  , m_vertexBindingPool(vertexBindingPool)
62  , m_asyncTextureLoader(asyncTextureLoader)
63  , m_weatherOverlayController(weatherOverlayController)
64  , m_weatherOverlayMaterial(weatherOverlayMaterial)
65  {
66  }
67 
68  void ResetFromState()
69  {
70  m_fromTextures = NULL;
71  }
72 
73  void SetState(const Resources::CityThemes::CityThemeState& state, const Rendering::EnvironmentStateTextures& stateTextures)
74  {
75  m_fromState = state;
76  m_fromTextures = &stateTextures;
77  m_toState = state;
78  m_toTextures = &stateTextures;
79 
80  SetMaterials();
81  SetWeatherOverlay();
82 
83  NotifyObservers(state);
84  }
85 
86  void StartTransitionToState(const Resources::CityThemes::CityThemeState& targetState, const Rendering::EnvironmentStateTextures& targetStateTextures)
87  {
88  m_toState = targetState;
89  m_toTextures = &targetStateTextures;
90 
91  SetMaterials();
92  SetWeatherOverlay();
93  }
94 
95  void UpdateMaterialsOnRenderables(const Rendering::Scene::ISceneElementSource<Rendering::Renderables::PackedRenderable>& sceneElements)
96  {
97  typedef std::vector<Rendering::Scene::SceneElement<Rendering::Renderables::PackedRenderable>*> TSceneElementPtrVec;
98  TSceneElementPtrVec allSceneElements;
99  sceneElements.GetSceneElements(allSceneElements);
100 
101  for (TSceneElementPtrVec::iterator iter = allSceneElements.begin(); iter != allSceneElements.end(); ++iter)
102  {
103  Rendering::Renderables::PackedRenderable& renderable = (*iter)->GetResource();
104  const std::string& materialName = renderable.GetMaterial()->GetName();
105  Rendering::Materials::IMaterial* material = m_materialRepository.GetMaterial(materialName);
106  renderable.SetMaterial(material, m_vertexBindingPool);
107  }
108  }
109 
110  void SetTransition(float t, const dv3& ecefInterestPoint)
111  {
112  m44 interpolatedLightColours;
113  m44::Lerp(interpolatedLightColours, m_fromState.Lighting, m_toState.Lighting, t);
114  m_globalLighting.SetLightColourMatrix(interpolatedLightColours);
115  m_materialRepository.SetTransitionParam(t);
116  m_globalLighting.SetNightLightmapColour(v3::Lerp(m_fromState.LightMapColor, m_toState.LightMapColor, t));
117  m_globalLighting.SetNightGlowLightmapIntensity(Math::Lerp(m_fromState.LightMapIntensity, m_toState.LightMapIntensity, t));
118  m_globalLighting.SetAlternativeAmbientColour(v3::Lerp(m_fromState.AlternativeAmbientLight, m_toState.AlternativeAmbientLight, t));
119 
120  float fogDensity = Math::Lerp(m_fromState.FogIntensity, m_toState.FogIntensity, t);
121  m_globalFogging.SetFogDensity(fogDensity);
122 
123  v3 fogColor = v3::Lerp(m_fromState.FogColor, m_toState.FogColor, t);
124  m_globalFogging.SetFogColour(v4(fogColor.GetX(), fogColor.GetY(), fogColor.GetZ(), 1.f));
125 
126  m_globalFogging.SetBaseAltitude(static_cast<float>(Space::SpaceHelpers::GetAltitude(ecefInterestPoint)));
127 
128  v3 shadowColor = v3::Lerp(m_fromState.ShadowColor, m_toState.ShadowColor, t);
129  m_globalShadowing.SetShadowColor(shadowColor);
130 
131 
132  float fromIntensity = m_fromState.OverlayIntensity;
133  float toIntensity = m_toState.OverlayIntensity;
134 
135  m_weatherOverlayController.SetIntensity(Math::Lerp(fromIntensity, toIntensity,t));
136  }
137 
138  Resources::CityThemes::CityThemeState& GetFromState() { return m_fromState; }
139  const Resources::CityThemes::CityThemeState& GetToState() const { return m_toState; }
140 
141  void RegisterObserver(ICityThemeStateObserver& themeStateObserver)
142  {
143  m_observers.push_back(&themeStateObserver);
144  }
145 
146  void UnregisterObserver(ICityThemeStateObserver& themeStateObserver)
147  {
148  TObservers::iterator toRemove = std::find(m_observers.begin(), m_observers.end(), &themeStateObserver);
149  if(toRemove != m_observers.end())
150  {
151  m_observers.erase(toRemove);
152  }
153  }
154 
155  private:
156  void SetMaterials()
157  {
158  m_materialRepository.SetCurrentRepository(m_fromState, m_fromTextures, m_toState, m_toTextures);
159  m_asyncTextureLoader.UpdatePlaceholderEnvironmentTexture(m_toTextures->GetAsyncPlaceholderEnvironmentTexture());
160  UpdateMaterialsOnRenderables(m_buildingRenderables);
161  UpdateMaterialsOnRenderables(m_transportRenderables);
162  UpdateMaterialsOnRenderables(m_terrainRenderables);
163  }
164 
165  void SetWeatherOverlay()
166  {
167  m_weatherOverlayMaterial.SetTextureId(m_toTextures->GetOverlayEffect());
168  m_weatherOverlayController.SetWeatherLayersParameters(m_toTextures->GetOverlayEffect() != 0,
169  m_toState.OverlayLayerOneOffset,
170  m_toState.OverlayLayerOneVelocity,
171  m_toState.OverlayLayerOneAlpha,
172  m_toState.OverlayLayerOneScale,
173  m_toState.OverlayLayerTwoOffset,
174  m_toState.OverlayLayerTwoVelocity,
175  m_toState.OverlayLayerTwoAlpha,
176  m_toState.OverlayLayerTwoScale);
177  }
178 
179  void NotifyObservers(const Resources::CityThemes::CityThemeState& state)
180  {
181  for(TObservers::const_iterator it = m_observers.begin(); it != m_observers.end(); ++it)
182  {
183  (*it)->OnThemeStateChanged(state);
184  }
185  }
186 
187  Rendering::EnvironmentMaterialRepository& m_materialRepository;
188 
190  const Rendering::EnvironmentStateTextures* m_fromTextures;
191 
193  const Rendering::EnvironmentStateTextures* m_toTextures;
194 
198 
199  Lighting::GlobalLighting& m_globalLighting;
200  Lighting::GlobalFogging& m_globalFogging;
201  Lighting::GlobalShadowing& m_globalShadowing;
202 
203  Rendering::VertexLayouts::VertexBindingPool& m_vertexBindingPool;
204 
206 
207  Eegeo::Weather::WeatherOverlayController& m_weatherOverlayController;
208  Rendering::Materials::WeatherOverlayMaterial2& m_weatherOverlayMaterial;
209 
210  typedef std::vector<ICityThemeStateObserver*> TObservers;
211  TObservers m_observers;
212  };
213  }
214  }
215 }