Diffuse Lighting For A Moving Object
Solution 1:
I have to move the luminous source along with the object itself
Why does the light source move with the object?
If the light is a point light source in the world, and the object moves, then the illumination of the object changes (in the "real" world).
In your case, the lighting is computed in view space. If the light source is a point in the world, then you have to transform the position by the view matrix (the view matrix transforms from world space to view space). e.g:
uniform mat4 u_viewMatrix;
voidmain()
{
// [...]
vec3 lightPosView = vec3(u_viewMatrix * vec4(u_lightPosition.xyz, 1.0));
vec3 lightVector = normalize(u_lightPosition - modelViewVertex);
// [...]
}
Anyway, if the object moves and the light source is somehow anchored to the object, the you have to apply the transformations, which are applied to the vertices of the object, to the light source, too.
In that case u_lightPosition
has to be a position in the model space of the object, that means it is relative to the object (u_lightModelPosition
). Then you can do:
uniform vec3 u_lightModelPosition;
voidmain()
{
mat3 normalMat = inverse(transpose(mat3(u_mvMatrix)));
vec3 modelViewNormal = normalMat * a_normal;
vec3 modelViewVertex = vec3(u_mvMatrix * a_position);
vec3 modelViewLight = vec3(u_mvMatrix * vec4(u_lightModelPosition, 1.0));
vec3 lightVector = normalize(modelViewLight - modelViewVertex);
// [...]
}
If you want a light, that doesn't depend on the position, the you have to use a directional light. In that case the light source is not a point in the world, it is a direction only. e.g.:
vec3 lightVector = -u_lightRayDirection;
u_lightRayDirection
has to be in the space of the light calculations. Since the lighting is computed in view space, u_lightRayDirection
has to be a direction in view space, too. If u_lightRayDirection
is a vector in world space, then it has to be transformed by mat3(u_viewMatrix)
.
A directional light has no distance (or a constant distance).
If the light source is anchored to the camera, no transformations are required at all (because you the light calculations in view space).
Post a Comment for "Diffuse Lighting For A Moving Object"