When calculating diffuse lighting for a moving object, I have to move the luminous source along with the object itself:
@Override
public void draw() { // draw frame
...
// Move object
GLES20.glVertexAttribPointer(aPositionLink, 3, GLES30.GL_FLOAT,
false, 0, object3D.getVertices());
// The luminous source moves nearby the object, so the
// object is always illuminated from one side
GLES20.glUniform3f(lightPositionLink, object3D.getX(),
object3D.getY(), object3D.getZ() + 2.0f);
...
}
Snippet of vertex shader:
#version 300 es
uniform mat4 u_mvMatrix; // model-view matrix of object
uniform vec3 u_lightPosition; // position of the luminous source
in vec4 a_position; // vertex data is loaded here
in vec3 a_normal; // normal data is loaded here
struct DiffuseLight {
vec3 color;
float intensity;
};
uniform DiffuseLight u_diffuseLight;
...
void main() {
...
vec3 modelViewNormal = vec3(u_mvMatrix * vec4(a_normal, 0.0));
vec3 modelViewVertex = vec3(u_mvMatrix * a_position);
// calculate the light vector by subtracting the
// position of the object from the light position
vec3 lightVector = normalize(u_lightPosition - modelViewVertex);
float diffuse = max(dot(modelViewNormal, lightVector), 0.1);
float distance = length(u_lightPosition - modelViewVertex);
diffuse = diffuse * (1.0 / (1.0 + pow(distance, 2.0)));
// calculate the final color for diffuse lighting
lowp vec3 diffuseColor = diffuse * u_diffuseLight.color * u_diffuseLight.intensity;
v_commonLight = vec4((ambientColor + diffuseColor), 1.0);
...
}
Is this the right approach? Or is there another rational option with stationary luminous source so as not to expend resources on calculating the position of the luminous source each frame? Note: Increasing the distance does not help. Thanks in advance.
SOLUTION:
On the advice of Rabbid76 I applied directional light as described here.