All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
LightmappedPackedDiffuseTransitionMaterial.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "PackedDiffuseShader.h"
7 #include "GLState.h"
8 #include "GlobalLighting.h"
9 #include "GlobalFogging.h"
10 #include "IDiffuseMaterial.h"
11 #include "LightmappedPackedDiffuseTransitionShader.h"
12 #include "LightmappedRenderable.h"
13 #include "TextureMinifyType.h"
14 #include "VectorMath.h"
15 #include "GLHelpers.h"
16 #include "IAsyncTexture.h"
17 #include <string>
18 
19 namespace Eegeo
20 {
21  namespace Rendering
22  {
23  namespace Materials
24  {
26  {
27  public:
29  const TMaterialId materialId,
30  const std::string& name,
32  const Lighting::GlobalLighting& lighting,
33  const Lighting::GlobalFogging& fogging,
34  AsyncTexturing::IAsyncTexture& fromTextureId,
35  AsyncTexturing::IAsyncTexture& toTextureId,
36  Rendering::TextureMinifyType textureMinifyType,
37  bool useAlternativeAmbient,
38  bool textureRepeat
39  )
40  : m_id(materialId)
41  , m_name(name)
42  , m_shader(shader)
43  , m_lighting(lighting)
44  , m_fogging(fogging)
45  , m_fromTextureId(&fromTextureId)
46  , m_toTextureId(&toTextureId)
47  , m_textureMinifyType(textureMinifyType)
48  , m_useAlternativeAmbient(useAlternativeAmbient)
49  , m_textureRepeat(textureRepeat)
50  , m_lerpParam(0.f)
51  {
52  }
53 
55  {
56  }
57 
58  bool IsUsingAlternativeAmbient() const { return m_useAlternativeAmbient; }
59  void UseAlternativeAmbient(bool useAlternativeAmbient) { m_useAlternativeAmbient = useAlternativeAmbient; }
60 
61  const TMaterialId GetId() const { return m_id; }
62  const Shader& GetShader() const { return m_shader; }
63  const std::string& GetName() const { return m_name; }
64 
65  void SetFromDiffuseTexture(AsyncTexturing::IAsyncTexture& textureId) { m_fromTextureId = &textureId; }
66  AsyncTexturing::IAsyncTexture& GetFromTextureId() const { return *m_fromTextureId; }
67 
68  void SetToDiffuseTexture(AsyncTexturing::IAsyncTexture& textureId) { m_toTextureId = &textureId; }
69  AsyncTexturing::IAsyncTexture& GetToTextureId() const { return *m_toTextureId; }
70 
71  void SetLerpParam(float t) { m_lerpParam = t; }
72  float GetLerpParam() const { return m_lerpParam; }
73 
74  void SetState(Rendering::GLState& glState) const
75  {
76  m_shader.Use(glState);
77 
78  glState.Blend.Disable();
79  glState.DepthTest.Enable();
80 
81  const Eegeo::m44 &lightColors = m_lighting.GetColors(m_useAlternativeAmbient);
82  m_shader.SetLightColors(lightColors);
83  m_shader.SetLightmapColour(m_lighting.GetNightLightmapColour());
84  m_shader.SetLightmapIntensity(m_lighting.GetNightGlowLightmapIntensity());
85  v4 ambientColorRow = lightColors.GetRow(3);
86 
88  m_fogging.GetUniformValues(fogValues);
89  m_shader.SetFogUniforms(fogValues);
90 
91  m_shader.SetAmbientColor(v3(ambientColorRow.GetX(), ambientColorRow.GetY(), ambientColorRow.GetZ()));
92  m_shader.SetLerpParam(m_lerpParam);
93 
94  Helpers::GLHelpers::BindTexture2D(glState, m_shader.GetFromDiffuseSamplerId(), m_fromTextureId->GetTextureInfo().textureId, m_textureMinifyType, m_textureRepeat);
95  Helpers::GLHelpers::BindTexture2D(glState, m_shader.GetToDiffuseSamplerId(), m_toTextureId->GetTextureInfo().textureId, m_textureMinifyType, m_textureRepeat);
96  }
97 
98  void SetStatePerRenderable(const Rendering::RenderableBase* renderableBase, Rendering::GLState& glState) const
99  {
100  const Rendering::Renderables::LightmappedRenderable* lightmappedRenderable = Eegeo::CheckedCast<const Rendering::Renderables::LightmappedRenderable*>(renderableBase);
101  BindLightmapTexture(glState, lightmappedRenderable->GetLightmapTextureId());
102  m_shader.SetMVP(lightmappedRenderable->GetModelViewProjection());
103  m_shader.SetUVBounds(lightmappedRenderable->GetUVBoundsMin(), lightmappedRenderable->GetUVBoundsMax());
104  m_shader.SetPositionBounds(lightmappedRenderable->GetPositionBoundsMin(), lightmappedRenderable->GetPositionBoundsMax());
105  m_shader.SetCameraRelativeOrigin(lightmappedRenderable->GetCameraRelativeModelOrigin());
106  }
107 
108  protected:
109  void BindLightmapTexture(Rendering::GLState& glState, TTextureId lightmapId) const
110  {
111  Helpers::GLHelpers::BindTexture2D(glState, m_shader.GetLightmapSamplerId(), lightmapId, Rendering::TextureMinify_Nearest, false);
112  }
113 
114  const TMaterialId m_id;
115  const std::string m_name;
117  const Lighting::GlobalLighting& m_lighting;
118  const Lighting::GlobalFogging& m_fogging;
119  AsyncTexturing::IAsyncTexture* m_fromTextureId;
120  AsyncTexturing::IAsyncTexture* m_toTextureId;
121  Rendering::TextureMinifyType m_textureMinifyType;
122  bool m_useAlternativeAmbient;
123  bool m_textureRepeat;
124  float m_lerpParam;
125  };
126  }
127  }
128 }