Light1Position is the location of the red dot and I’m translating the spheres using the modelview matrix. How can I make the lighting look correct? Also, I’d like the lighting intensity to decrease with distance.
That doesn’t change the fact that the projection matrix should not be used in lighting calculations.
Using the projection matrix is just one of many possible mistakes. Just because you eliminate that particular mistake, it doesn’t meant that there won’t be others.
Other common mistakes include: mixing coordinate systems (e.g. using a vertex position specified in one with a light or eye position specified in another), calculating the normals incorrectly (e.g. having them point inward rather than outward), transforming normals incorrectly (if you have non-uniform scaling or shear, the normal matrix needs to be the inverse of the transpose of the model-view matrix), failing to normalise vectors, performing dot products on 4D vectors (i.e. failing to divide by w first).
Calculating in eye space (with the model-view transformation applied) is fine (and fairly typical), so long as everything is eye space: vertex position, light position, eye position, normals.
Calculating in screen space (with both the model-view and projection transformations applied) won’t work with a perspective projection.
try it like that.
also, you can premultiply light position with view matrix before passing it to shaders.
and storing position in world-space(or object-space?) is not very convenient.
try it like that.
also, you can premultiply light position with view matrix before passing it to shaders.
and storing position in world-space(or object-space?) is not very convenient.[/QUOTE]
I’m googling how to calculate the view matrix, but it seems nontrivial.
note, that rotation in this case goes before translation.
but you better off starting with Tutorial 3 : Matrices
and reading some other literature on matrix transformations and spaces. because you clearly don’t know what you’re doing.
also, you can use this image as reference for transformation spaces:
Rotation applies to more vertex-attributes(meaning normals for this). And in practice, when approximating a sphere with triangles, one may see quite a difference in the shape depending on the viewpoint.
But: believe it or not, what I write above is wrong: You just apply the Rotation to the object - not to the lights relative Position.
That is what you might be doing if multiplying the normal with gl_NormalMatrix - depending on how you use the ModelViewMatrix.