7 #include "ShaderMacros.h"
9 #include "VectorMath.h"
19 namespace PackedDiffuseShaderCode
21 const std::string PositionName =
"Position";
22 const std::string UVName =
"UV";
23 const std::string UnpackModelViewProjectionMatrixName =
"UnpackModelViewProjectionMatrix";
24 const std::string LightColorMatrixName =
"LightColorMatrix";
25 const std::string MinUVRangeName =
"MinUVRange";
26 const std::string MaxUVRangeName =
"MaxUVRange";
27 const std::string DiffuseName =
"Diffuse";
28 const std::string LightDotUnpackName =
"LightDotUnpack";
30 const std::string _vertexDecls =
31 "attribute highp vec4 "+PositionName+
";\n"
32 "attribute highp vec2 "+UVName+
";\n"
33 "varying lowp vec4 ColorVarying;\n"
34 "varying mediump vec2 DestinationUV;\n"
35 "uniform highp mat4 "+UnpackModelViewProjectionMatrixName+
";\n"
36 "uniform lowp mat4 "+LightColorMatrixName+
";\n"
37 "uniform highp vec2 "+MinUVRangeName+
";\n"
38 "uniform highp vec2 "+MaxUVRangeName+
";\n"
39 "uniform highp vec3 "+LightDotUnpackName+
";\n";
41 const std::string _fragmentDecls =
42 "varying lowp vec4 ColorVarying;\n"
43 "varying mediump vec2 DestinationUV;\n"
44 "uniform sampler2D "+DiffuseName+
";\n";
46 const std::string _vertexCode =
47 "void main(void) { \n"
48 "DestinationUV = mix(MinUVRange, MaxUVRange, UV);\n"
49 "ColorVarying = LightColorMatrix * vec4(fract(Position.w * LightDotUnpack), 1.0);\n"
50 "gl_Position = UnpackModelViewProjectionMatrix * vec4(Position.xyz, 1.0);\n"
53 const std::string _fragmentPunchThroughCode =
54 "void main(void) { \n"
55 "highp vec4 col = " TEXTURE2D(Diffuse, DestinationUV)
"; \n"
56 "if(col.w<" EEGEO_ALPHA_TEST_VALUE
") discard; \n"
57 "gl_FragColor.rgb = col.rgb * ColorVarying.rgb; \n"
58 "gl_FragColor.a = 1.0;\n"
61 const std::string _fragmentCode =
62 "void main(void) { \n"
63 "gl_FragColor.rgb = " TEXTURE2D(Diffuse, DestinationUV)
".rgb * ColorVarying.rgb; \n"
64 "gl_FragColor.a = 1.0;\n"
75 PackedDiffuseShaderCode::_vertexDecls + PackedDiffuseShaderCode::_vertexCode,
76 PackedDiffuseShaderCode::_fragmentDecls + PackedDiffuseShaderCode::_fragmentCode
84 PackedDiffuseShaderCode::_vertexDecls + PackedDiffuseShaderCode::_vertexCode,
85 PackedDiffuseShaderCode::_fragmentDecls + PackedDiffuseShaderCode::_fragmentPunchThroughCode
89 const GLuint GetDiffuseSamplerId()
const {
return 0; }
91 void SetMVP(
const m44& mvp)
const
93 bool transpose =
false;
94 SetUniformM44(mvp, m_mvpUniformLocation, transpose);
97 void SetLightColors(
const m44& colors)
const
99 bool transpose =
false;
100 SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
105 SetUniformV2(min, m_minUVRangeUniformLocation);
106 SetUniformV2(max, m_maxUVRangeUniformLocation);
112 SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
114 Eegeo::v3 lightDotUnpack(1.f, 32.f, 1024.f);
115 SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
119 PackedDiffuseShader(
const TShaderId shaderId,
const std::string& vertexShaderCode,
const std::string& fragmentShaderCode) :
Shader(shaderId)
121 CompileProgram(vertexShaderCode, fragmentShaderCode);
123 m_mvpUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::UnpackModelViewProjectionMatrixName);
124 m_lightColorsUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::LightColorMatrixName);
125 m_minUVRangeUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::MinUVRangeName);
126 m_maxUVRangeUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::MaxUVRangeName);
127 m_diffuseTextureSamplerUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::DiffuseName);
128 m_lightDotUnpackUniformLocation = GetUniformLocation(PackedDiffuseShaderCode::LightDotUnpackName);
132 GLuint m_mvpUniformLocation ;
133 GLuint m_lightColorsUniformLocation;
134 GLuint m_minUVRangeUniformLocation;
135 GLuint m_maxUVRangeUniformLocation;
136 GLuint m_diffuseTextureSamplerUniformLocation;
137 GLuint m_lightDotUnpackUniformLocation;