7 #include "ShaderMacros.h"
9 #include "VectorMath.h"
19 namespace PackedDiffuseAlphaShaderCode
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";
29 const std::string ColorName =
"Color";
31 const std::string _vertexDecls =
32 "attribute highp vec4 "+PositionName+
";\n"
33 "attribute highp vec2 "+UVName+
";\n"
34 "varying lowp vec4 ColorVarying;\n"
35 "varying mediump vec2 DestinationUV;\n"
36 "uniform highp mat4 "+UnpackModelViewProjectionMatrixName+
";\n"
37 "uniform lowp mat4 "+LightColorMatrixName+
";\n"
38 "uniform highp vec2 "+MinUVRangeName+
";\n"
39 "uniform highp vec2 "+MaxUVRangeName+
";\n"
40 "uniform highp vec3 "+LightDotUnpackName+
";\n";
42 const std::string _fragmentDecls =
43 "varying lowp vec4 ColorVarying;\n"
44 "varying mediump vec2 DestinationUV;\n"
45 "uniform sampler2D "+DiffuseName+
";\n"
46 "uniform lowp vec4 "+ColorName+
";\n";
48 const std::string _vertexCode =
49 "void main(void) { \n"
50 "DestinationUV = mix(MinUVRange, MaxUVRange, UV);\n"
51 "ColorVarying = LightColorMatrix * vec4(fract(Position.w * LightDotUnpack), 1.0);\n"
52 "gl_Position = UnpackModelViewProjectionMatrix * vec4(Position.xyz, 1.0);\n"
55 const std::string _fragmentPunchThroughCode =
56 "void main(void) { \n"
57 "highp vec4 col = " TEXTURE2D(Diffuse, DestinationUV)
"; \n"
58 "if(col.w<" EEGEO_ALPHA_TEST_VALUE
") discard; \n"
59 "gl_FragColor.rgb = col.rgb * ColorVarying.rgb; \n"
60 "gl_FragColor.a = "+ColorName+
".a;\n"
63 const std::string _fragmentCode =
64 "void main(void) { \n"
65 "gl_FragColor.rgb = " TEXTURE2D(Diffuse, DestinationUV)
".rgb * ColorVarying.rgb * "+ColorName+
".rgb;\n"
66 "gl_FragColor.a = "+ColorName+
".a;\n"
76 PackedDiffuseAlphaShaderCode::_vertexDecls + PackedDiffuseAlphaShaderCode::_vertexCode,
77 PackedDiffuseAlphaShaderCode::_fragmentDecls + PackedDiffuseAlphaShaderCode::_fragmentCode
84 PackedDiffuseAlphaShaderCode::_vertexDecls + PackedDiffuseAlphaShaderCode::_vertexCode,
85 PackedDiffuseAlphaShaderCode::_fragmentDecls + PackedDiffuseAlphaShaderCode::_fragmentPunchThroughCode
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 SetLightColors(
const m44& colors)
const
100 bool transpose =
false;
101 SetUniformM44(colors, m_lightColorsUniformLocation, transpose);
106 SetUniformV2(min, m_minUVRangeUniformLocation);
107 SetUniformV2(max, m_maxUVRangeUniformLocation);
110 void SetColor(
const v4& color)
112 SetUniformV4(color, m_colorUniformLocation);
118 SetUniformTextureSampler(glState, GetDiffuseSamplerId(), m_diffuseTextureSamplerUniformLocation);
120 Eegeo::v3 lightDotUnpack(1.f, 32.f, 1024.f);
121 SetUniformV3(lightDotUnpack, m_lightDotUnpackUniformLocation);
127 CompileProgram(vertexShaderCode, fragmentShaderCode);
129 m_mvpUniformLocation = GetUniformLocation(PackedDiffuseAlphaShaderCode::UnpackModelViewProjectionMatrixName);
130 m_lightColorsUniformLocation = GetUniformLocation(PackedDiffuseAlphaShaderCode::LightColorMatrixName);
131 m_minUVRangeUniformLocation = GetUniformLocation(PackedDiffuseAlphaShaderCode::MinUVRangeName);
132 m_maxUVRangeUniformLocation = GetUniformLocation(PackedDiffuseAlphaShaderCode::MaxUVRangeName);
133 m_diffuseTextureSamplerUniformLocation = GetUniformLocation(PackedDiffuseAlphaShaderCode::DiffuseName);
134 m_lightDotUnpackUniformLocation = GetUniformLocation(PackedDiffuseAlphaShaderCode::LightDotUnpackName);
135 m_colorUniformLocation = GetUniformLocation(PackedDiffuseAlphaShaderCode::ColorName);
139 GLuint m_mvpUniformLocation ;
140 GLuint m_lightColorsUniformLocation;
141 GLuint m_minUVRangeUniformLocation;
142 GLuint m_maxUVRangeUniformLocation;
143 GLuint m_diffuseTextureSamplerUniformLocation;
144 GLuint m_lightDotUnpackUniformLocation;
145 GLuint m_colorUniformLocation;