-
-#define MAKE_SHADER(A)#A
-
-// Diffuse illumination shader
-
-const char* SIMPLE_VERTEX_SHADER = MAKE_SHADER(
- attribute highp vec3 aPosition;\n
- attribute highp vec2 aTexCoord;\n
- attribute highp vec3 aNormal;\n
- varying mediump vec3 vIllumination;\n
- uniform mediump vec3 uSize;\n
- uniform mediump mat4 uMvpMatrix;\n
- uniform mediump mat4 uModelView;\n
- uniform mediump mat3 uNormalMatrix;
- uniform mediump mat4 uObjectMatrix;\n
- uniform mediump vec3 uLightPosition;\n
- \n
- void main()\n
- {\n
- vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
- vertexPosition = uObjectMatrix * vertexPosition;\n
- vertexPosition = uMvpMatrix * vertexPosition;\n
- \n
- //Illumination in Model-View space - Transform attributes and uniforms\n
- vec4 vertPos4 = uModelView * vec4(aPosition.xyz, 1.0);\n
- vec3 vertPos = vec3(vertPos4) / vertPos4.w;\n
- \n
- vec3 normalInterp = uNormalMatrix * aNormal;\n
- \n
- vec4 lightPos4 = uModelView * vec4(uLightPosition, 1.0);\n
- vec3 lightPos = vec3(lightPos4) / lightPos4.w;\n
- \n
- vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
- \n
- float lightDiffuse = dot( vecToLight, normalInterp );\n
- lightDiffuse = max(0.0,lightDiffuse);\n
- vIllumination = vec3(lightDiffuse * 0.5 + 0.5);\n
- \n
- gl_Position = vertexPosition;\n
- }\n
-);
-
-const char* SIMPLE_FRAGMENT_SHADER = MAKE_SHADER(
- precision mediump float;\n
- varying mediump vec3 vIllumination;\n
- uniform lowp vec4 uColor;\n
- \n
- void main()\n
- {\n
- gl_FragColor.rgb = vIllumination.rgb * uColor.rgb;\n
- gl_FragColor.a = uColor.a;\n
- }\n
-);
-
-// Diffuse and specular illumination shader with albedo texture
-
-const char* VERTEX_SHADER = MAKE_SHADER(
- attribute highp vec3 aPosition;\n
- attribute highp vec2 aTexCoord;\n
- attribute highp vec3 aNormal;\n
- varying mediump vec2 vTexCoord;\n
- varying mediump vec3 vIllumination;\n
- varying mediump float vSpecular;\n
- uniform mediump vec3 uSize;\n
- uniform mediump mat4 uMvpMatrix;\n
- uniform mediump mat4 uModelView;
- uniform mediump mat3 uNormalMatrix;
- uniform mediump mat4 uObjectMatrix;\n
- uniform mediump vec3 uLightPosition;\n
- \n
- void main()
- {\n
- vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
- vertexPosition = uObjectMatrix * vertexPosition;\n
- vertexPosition = uMvpMatrix * vertexPosition;\n
- \n
- //Illumination in Model-View space - Transform attributes and uniforms\n
- vec4 vertPos4 = uModelView * vec4(aPosition.xyz, 1.0);\n
- vec3 vertPos = vec3(vertPos4) / vertPos4.w;\n
- \n
- vec4 lightPos4 = uModelView * vec4(uLightPosition, 1.0);\n
- vec3 lightPos = vec3(lightPos4) / lightPos4.w;\n
- \n
- vec3 normalInterp = normalize(uNormalMatrix * aNormal);\n
- \n
- vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
- vec3 viewDir = normalize(-vertPos);
- \n
- vec3 halfVector = normalize(viewDir + vecToLight);
- \n
- float lightDiffuse = dot( vecToLight, normalInterp );\n
- lightDiffuse = max(0.0,lightDiffuse);\n
- vIllumination = vec3(lightDiffuse * 0.5 + 0.5);\n
- \n
- // this is blinn phong
- //float specAngle = max(dot(halfVector, normalInterp), 0.0);\n
- //vSpecular = pow(specAngle, 16.0);\n
- \n
- // this is phong (for comparison)
- vec3 reflectDir = reflect(-vecToLight, normalInterp);
- float specAngle = max(dot(reflectDir, viewDir), 0.0);
- // note that the exponent is different here
- vSpecular = pow(specAngle, 16.0/4.0);
- \n
- vTexCoord = aTexCoord;\n
- gl_Position = vertexPosition;\n
- }\n
-);
-
-const char* FRAGMENT_SHADER = MAKE_SHADER(
- precision mediump float;\n
- varying mediump vec2 vTexCoord;\n
- varying mediump vec3 vIllumination;\n
- varying mediump float vSpecular;\n
- uniform sampler2D sDiffuse;\n
- uniform lowp vec4 uColor;\n
- \n
- void main()\n
- {\n
- vec4 texture = texture2D( sDiffuse, vTexCoord );\n
- gl_FragColor.rgb = vIllumination.rgb * texture.rgb * uColor.rgb + vSpecular * 0.3;\n
- gl_FragColor.a = texture.a * uColor.a;\n
- }\n
-);
-
-// Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader
-
-const char* NRMMAP_VERTEX_SHADER = MAKE_SHADER(
- attribute highp vec3 aPosition;\n
- attribute highp vec2 aTexCoord;\n
- attribute highp vec3 aNormal;\n
- attribute highp vec3 aTangent;\n
- attribute highp vec3 aBiNormal;\n
- varying mediump vec2 vTexCoord;\n
- varying mediump vec3 vLightDirection;\n
- varying mediump vec3 vHalfVector;\n
- uniform mediump vec3 uSize;\n
- uniform mediump mat4 uMvpMatrix;\n
- uniform mediump mat4 uModelView;
- uniform mediump mat3 uNormalMatrix;
- uniform mediump mat4 uObjectMatrix;\n
- uniform mediump vec3 uLightPosition;\n
- \n
- void main()
- {\n
- vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
- vertexPosition = uObjectMatrix * vertexPosition;\n
- vertexPosition = uMvpMatrix * vertexPosition;\n
- \n
- vTexCoord = aTexCoord;\n
- \n
- vec3 vNormal = normalize(uNormalMatrix * aNormal);\n
- vec3 vTangent = normalize(uNormalMatrix * aTangent);\n
- vec3 vBiNormal = normalize(uNormalMatrix * aBiNormal);\n
- \n
- vec4 vertPos4 = uModelView * vec4(aPosition.xyz, 1.0);\n
- vec3 vertPos = vec3(vertPos4) / vertPos4.w;\n
- \n
- vec4 lightPos4 = uModelView * vec4(uLightPosition, 1.0);\n
- vec3 lightPos = vec3(lightPos4) / lightPos4.w;\n
- \n
- vec3 vecToLight = lightPos - vertPos;
- vLightDirection.x = dot(vecToLight, vTangent);
- vLightDirection.y = dot(vecToLight, vBiNormal);
- vLightDirection.z = dot(vecToLight, vNormal);
- vLightDirection = normalize(vLightDirection);
- \n
- vec3 viewDir = normalize(vertPos);
- \n
- vec3 halfVector = normalize(viewDir + vecToLight);
- vHalfVector.x = dot (halfVector, vTangent);
- vHalfVector.y = dot (halfVector, vBiNormal);
- vHalfVector.z = dot (halfVector, vNormal);
- \n
- gl_Position = vertexPosition;\n
-
- //vHalfVector = aTangent;
- }\n
-);
-
-const char* NRMMAP_FRAGMENT_SHADER = MAKE_SHADER(
- precision mediump float;\n
- varying mediump vec2 vTexCoord;\n
- varying mediump vec3 vLightDirection;\n
- varying mediump vec3 vHalfVector;\n
- uniform sampler2D sDiffuse;\n
- uniform sampler2D sNormal;\n
- uniform sampler2D sGloss;\n
- uniform lowp vec4 uColor;\n
- \n
- void main()\n
- {\n
- vec4 texture = texture2D( sDiffuse, vTexCoord );\n
- vec4 nrmMap = texture2D( sNormal, vTexCoord ) * 2.0 - 1.0;\n
- vec4 glossMap = texture2D( sGloss, vTexCoord );\n
- \n
- vec3 normalizedLightDirection = normalize(vLightDirection);\n
- float lightDiffuse = max( 0.0, dot( nrmMap.xyz, normalizedLightDirection ) );\n
- lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
- \n
- float shininess = pow (max (dot (vHalfVector, nrmMap.xyz), 0.0), 16.0) ;
- \n
- gl_FragColor.rgb = texture.rgb * uColor.rgb * lightDiffuse + shininess * glossMap.rgb;\n
- gl_FragColor.a = texture.a * uColor.a;\n
-
- //gl_FragColor.rgb = vHalfVector.rgb;
- }\n
-);
-
-