5 #include "IDiffuseMaterial.h"
7 #include "GlobalLighting.h"
8 #include "GlobalFogging.h"
11 #include "ShaderMacros.h"
13 #include "FogShaderIncludes.h"
14 #include "FogShaderUniforms.h"
15 #include "FogShaderOld.h"
23 namespace ModelShaderCode
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";
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;
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";
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"
62 "vec4 dots = -normalize(myNormal) * "+LightDirectionMatrixName+
";\n"
63 "dots = clamp (dots, 0.0, 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"
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"
81 static ModelShader* Create(
const TShaderId shaderId)
85 ModelShaderCode::_vertexDecls + ModelShaderCode::_vertexCode,
86 ModelShaderCode::_fragmentDecls + ModelShaderCode::_fragmentCode
90 const GLuint GetDiffuseSamplerId()
const {
return 0; }
92 void SetMVP(
const m44& mvp)
const
94 bool transpose =
false;
95 SetUniformM44(mvp, m_mvpUniformLocation, transpose);
98 void SetInverseWorldMatrix(
const m44& inverseWorld)
100 bool transpose =
false;
101 SetUniformM44(inverseWorld, m_inverseWorldUniformLocation, transpose);
104 void SetLightColors(
const m44& colors)
const
106 bool transpose =
false;
107 SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
110 void SetLightDirections(
const m44& dirs)
const
112 bool transpose =
false;
113 SetUniformM44(dirs, m_lightDirectionsUniformLocation, transpose);
116 void SetAlpha(
const float alpha)
const
118 SetUniformFloat(alpha, m_alphaUniformLocation);
123 Eegeo::Base::FogShaderOld::SetPerMaterialRenderState(m_fogShaderUniforms, fogUniformValues);
126 void SetCameraRelativeOrigin(
const Eegeo::v3& cameraRelativeOrigin)
const
128 SetUniformV3(cameraRelativeOrigin, m_cameraRelativeModelOriginUniformLocation);
137 ModelShader(
const TShaderId shaderId,
const std::string& vertexShaderCode,
const std::string& fragmentShaderCode) :
Shader(shaderId)
139 CompileProgram(vertexShaderCode, fragmentShaderCode);
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);
149 m_fogShaderUniforms.DetermineUniformsFromShader(
this);
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;