All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ModelShader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "IDiffuseMaterial.h"
6 #include "Types.h"
7 #include "GlobalLighting.h"
8 #include "GlobalFogging.h"
9 #include "Rendering.h"
10 #include "Shader.h"
11 #include "ShaderMacros.h"
12 
13 #include "FogShaderIncludes.h"
14 #include "FogShaderUniforms.h"
15 #include "FogShaderOld.h"
16 
17 namespace Eegeo
18 {
19  namespace Rendering
20  {
21  namespace Shaders
22  {
23  namespace ModelShaderCode
24  {
25  const std::string PositionName = "Position";
26  const std::string NormalName = "Normal";
27  const std::string UVName = "UV";
28  const std::string DiffuseName = "Diffuse";
29  const std::string ModelViewProjectionMatrixName = "ModelViewProjectionMatrix";
30  const std::string WorldInverseTransposeMatrixName = "WorldInverseTransposeMatrix";
31  const std::string LightDirectionMatrixName = "LightDirections";
32  const std::string LightColorMatrixName = "LightColors";
33  const std::string AlphaName = "Alpha";
34 
35  const std::string _vertexDecls =
36  "attribute highp vec3 "+PositionName+";\n"
37  "attribute mediump vec3 "+NormalName+";\n"
38  "attribute highp vec2 "+UVName+";\n"
39  "varying lowp vec4 ColorVarying;\n"
40  "varying lowp float FogDensity;\n"
41  "varying mediump vec2 DestinationUV;\n"
42  "uniform highp mat4 "+ModelViewProjectionMatrixName+";\n"
43  "uniform highp mat4 "+WorldInverseTransposeMatrixName+";\n"
44  "uniform highp mat4 "+LightDirectionMatrixName+";\n"
45  "uniform highp mat4 "+LightColorMatrixName+";\n"
46  "uniform lowp float "+AlphaName+";\n"
47  FOG_VERTEX_SHADER_UNIFORMS;
48 
49  const std::string _fragmentDecls =
50  "varying lowp vec4 ColorVarying;\n"
51  "varying lowp float FogDensity;\n"
52  "varying mediump vec2 DestinationUV;\n"
53  "uniform sampler2D "+DiffuseName+";\n"
54  "uniform lowp vec4 "+Rendering::FogShaderIncludes::FogColourName+";\n";
55 
56  const std::string _vertexCode =
57  FOG_VERTEX_SHADER_FUNCTIONS
58  "void main(void) { \n"
59  "DestinationUV = UV;\n"
60  "vec4 myNormal = vec4 ("+NormalName+", 1.0) * "+WorldInverseTransposeMatrixName+";\n"
61  "myNormal.w = 0.0;\n"
62  "vec4 dots = -normalize(myNormal) * "+LightDirectionMatrixName+";\n"
63  "dots = clamp (dots, 0.0, 1.0);\n"
64  "dots.w = 1.0;\n"
65  "ColorVarying.rgb = ("+LightColorMatrixName+" * dots).rgb;\n"
66  "ColorVarying.a = "+AlphaName+";\n"
67  "FogDensity = CalcHeightFogDensity(Position);\n"
68  "gl_Position = "+ModelViewProjectionMatrixName+" * vec4(Position.xyz, 1.0);\n"
69  "}";
70 
71  const std::string _fragmentCode =
72  "void main(void) { \n"
73  "gl_FragColor.rgba = mix(" TEXTURE2D(Diffuse, DestinationUV) ".rgba * ColorVarying.rgba, vec4(FogColour.rgb, ColorVarying.a), FogDensity); \n"
74  "}";
75  }
76 
77 
78  class ModelShader : public Shader
79  {
80  public:
81  static ModelShader* Create(const TShaderId shaderId)
82  {
83  return Eegeo_NEW(ModelShader)(
84  shaderId,
85  ModelShaderCode::_vertexDecls + ModelShaderCode::_vertexCode,
86  ModelShaderCode::_fragmentDecls + ModelShaderCode::_fragmentCode
87  );
88  }
89 
90  const GLuint GetDiffuseSamplerId() const { return 0; }
91 
92  void SetMVP(const m44& mvp) const
93  {
94  bool transpose = false;
95  SetUniformM44(mvp, m_mvpUniformLocation, transpose);
96  }
97 
98  void SetInverseWorldMatrix(const m44& inverseWorld)
99  {
100  bool transpose = false;
101  SetUniformM44(inverseWorld, m_inverseWorldUniformLocation, transpose);
102  }
103 
104  void SetLightColors(const m44& colors) const
105  {
106  bool transpose = false;
107  SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
108  }
109 
110  void SetLightDirections(const m44& dirs) const
111  {
112  bool transpose = false;
113  SetUniformM44(dirs, m_lightDirectionsUniformLocation, transpose);
114  }
115 
116  void SetAlpha(const float alpha) const
117  {
118  SetUniformFloat(alpha, m_alphaUniformLocation);
119  }
120 
121  void SetFogUniforms(Lighting::GlobalFoggingUniformValues& fogUniformValues) const
122  {
123  Eegeo::Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
124  }
125 
126  void SetCameraRelativeOrigin(const Eegeo::v3& cameraRelativeOrigin) const
127  {
128  SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
129  }
130 
131  void Use(Rendering::GLState& glState) const
132  {
133  UseProgram(glState);
134  }
135 
136  protected:
137  ModelShader(const TShaderId shaderId, const std::string& vertexShaderCode, const std::string& fragmentShaderCode) : Shader(shaderId)
138  {
139  CompileProgram(vertexShaderCode, fragmentShaderCode);
140 
141  m_mvpUniformLocation = GetUniformLocation(ModelShaderCode::ModelViewProjectionMatrixName);
142  m_inverseWorldUniformLocation = GetUniformLocation(ModelShaderCode::WorldInverseTransposeMatrixName);
143  m_lightDirectionsUniformLocation = GetUniformLocation(ModelShaderCode::LightDirectionMatrixName);
144  m_lightColorsUniformLocation = GetUniformLocation(ModelShaderCode::LightColorMatrixName);
145  m_diffuseTextureSamplerUniformLocation = GetUniformLocation(ModelShaderCode::DiffuseName);
146  m_alphaUniformLocation = GetUniformLocation(ModelShaderCode::AlphaName);
147  m_cameraRelativeModelOriginUniformLocation = GetUniformLocation(Eegeo::Rendering::FogShaderIncludes::CameraRelativeModelOriginName);
148 
149  m_fogShaderUniforms.DetermineUniformsFromShader(this);
150  }
151 
152  private:
153  GLuint m_mvpUniformLocation;
154  GLuint m_inverseWorldUniformLocation;
155  GLuint m_lightDirectionsUniformLocation;
156  GLuint m_lightColorsUniformLocation;
157  GLuint m_diffuseTextureSamplerUniformLocation;
158  GLuint m_alphaUniformLocation;
159  GLuint m_cameraRelativeModelOriginUniformLocation;
160 
161  Rendering::Shaders::FogShaderUniforms m_fogShaderUniforms;
162  };
163 
164  }
165  }
166 }