Finally, I made the shader with ambient, diffuse, specular, and texture colors working on GLES 2. It is not very flexible, because does not take material/light/environment color properties into account, but will work for my purpose. The solution was in constructing position vector as vec4(position, 1.0) and normal vector as vec4(normal, 0.0). Then transformation by modelViewMatrix works as expected. Thank you all for help.
Code: Select all
ZZDC<?xml version="1.0" encoding="iso-8859-1" ?>
<Shader Name="LightShaderES">
<VertexShaderSource>
<![CDATA[precision mediump float;
varying vec3 N;
varying vec3 v;
varying vec2 tcoord;
//The variables below are predefined in ZGE
uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelViewMatrix;
attribute vec3 position; //vertex position
attribute vec3 normal; //vertex normal
void main()
{
// position and normal
vec4 p = vec4(position, 1.0);
v = vec3(modelViewMatrix * p);
N = normalize(vec3(modelViewMatrix * vec4(normal, 0.0)));
// texture coords
if(abs(normal.x) > 0.001) tcoord = position.zy;
else if(abs(normal.y) > 0.001) tcoord = position.xz;
else tcoord = position.xy;
//tcoord = texCoord;
gl_Position = modelViewProjectionMatrix * p;
}]]>
</VertexShaderSource>
<FragmentShaderSource>
<![CDATA[precision mediump float;
varying vec3 N;
varying vec3 v;
varying vec2 tcoord;
uniform float shininess;
uniform sampler2D tex1;
void main()
{
vec3 L = normalize(-v); // light position = player position
float dist = length(v); // distance factor
dist = min(1.0, shininess / (dist*dist));
vec3 R = normalize(-reflect(L,N)); // reflection
// ambient color
vec4 col = vec4(0.05);
// diffuse color
col += vec4(max(dot(N,L), 0.0));
// specular color
col += vec4(clamp(pow(max(dot(R,L), 0.0), 70.0)/5.0, 0.0, 1.0));
// texture color
col *= texture2D(tex1,tcoord);
// result
gl_FragColor = clamp(col * dist, 0.0, 1.0);
}]]>
</FragmentShaderSource>
<UniformVariables>
<ShaderVariable Name="A" VariableName="shininess" Value="70"/>
</UniformVariables>
</Shader>
BTW cannot be position of type vec4? Ville if you decide to create normal matrix, it should be of type mat3.