8 #include "VectorMath.h"
9 #include "ShaderMacros.h"
18 namespace DiffuseShaderCode
20 const std::string PositionName =
"Position";
21 const std::string UVName =
"UV";
22 const std::string ModelViewProjectionMatrixName =
"ModelViewProjectionMatrix";
23 const std::string LightColorMatrixName =
"LightColorMatrix";
24 const std::string DiffuseName =
"Diffuse";
25 const std::string LightDotUnpackName =
"LightDotUnpack";
27 const std::string _vertexDecls =
28 "attribute highp vec4 "+PositionName+
";\n"
29 "attribute highp vec2 "+UVName+
";\n"
30 "varying lowp vec4 ColorVarying;\n"
31 "varying mediump vec2 DestinationUV;\n"
32 "uniform highp mat4 "+ModelViewProjectionMatrixName+
";\n"
33 "uniform lowp mat4 "+LightColorMatrixName+
";\n"
34 "uniform highp vec3 "+LightDotUnpackName+
";\n";
36 const std::string _fragmentDecls =
37 "varying lowp vec4 ColorVarying;\n"
38 "varying mediump vec2 DestinationUV;\n"
39 "uniform sampler2D "+DiffuseName+
";\n";
41 const std::string _vertexCode =
42 "void main(void) { \n"
43 "DestinationUV = UV;\n"
44 "ColorVarying = LightColorMatrix * vec4(fract(Position.w * LightDotUnpack), 1.0);\n"
45 "gl_Position = ModelViewProjectionMatrix * vec4(Position.xyz, 1.0);\n"
48 const std::string _fragmentCode =
49 "void main(void) { \n"
50 "gl_FragColor.rgb = " TEXTURE2D(Diffuse, DestinationUV)
".rgb * ColorVarying.rgb; \n"
51 "gl_FragColor.a = 1.0;\n"
62 DiffuseShaderCode::_vertexDecls + DiffuseShaderCode::_vertexCode,
63 DiffuseShaderCode::_fragmentDecls + DiffuseShaderCode::_fragmentCode
67 const GLuint GetDiffuseSamplerId()
const {
return 0; }
69 void SetMVP(
const m44& mvp)
const
71 bool transpose =
false;
72 SetUniformM44(mvp, m_mvpUniformLocation, transpose);
75 void SetLightColors(
const m44& colors)
const
77 bool transpose =
false;
78 SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
84 SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
86 const GLfloat lightDotUnpack[] = {1.f, 32.f, 1024.f};
87 SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
91 DiffuseShader(
const TShaderId shaderId,
const std::string& vertexShaderCode,
const std::string& fragmentShaderCode) :
Shader(shaderId)
93 CompileProgram(vertexShaderCode, fragmentShaderCode);
95 m_mvpUniformLocation = GetUniformLocation(DiffuseShaderCode::ModelViewProjectionMatrixName);
96 m_lightColorsUniformLocation = GetUniformLocation(DiffuseShaderCode::LightColorMatrixName);
97 m_diffuseTextureSamplerUniformLocation = GetUniformLocation(DiffuseShaderCode::DiffuseName);
98 m_lightDotUnpackUniformLocation = GetUniformLocation(DiffuseShaderCode::LightDotUnpackName);
102 GLuint m_mvpUniformLocation ;
103 GLuint m_lightColorsUniformLocation;
104 GLuint m_diffuseTextureSamplerUniformLocation;
105 GLuint m_lightDotUnpackUniformLocation;