# My lighting is way off

imgur.com/hWE1vxS.png

``````dot(
normalize( Light1Position - vec3(modelViewProjectionMatrix * position) ),
normalize( normalMatrix * normal )
)

``````

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.

Are there any solid tutorials online for this?

The projection matrix shouldn’t be involved in lighting calculations, i.e. you should just use the model-view matrix.

I tried that too and it didn’t work. It looks as if the light is coming from the camera, not from the red dot.

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).

It appears to be calculating in eye space and I’m not sure how to avoid doing that.

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.

My lighting shader looks something like this one, but the comments say the light position needs to be in eye coords.

I have all of my positions in absolute space.

``````
dot( normalize( viewMatrix * Light1Position - vec3(modelViewMatrix * position)), normalize( normalMatrix * normal ) )
``````

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.

I subtracted the light position from the camera and now it looks better from some angles but not others.

[QUOTE=Nowhere-01;1253175]

``````
dot( normalize( viewMatrix * Light1Position - vec3(modelViewMatrix * position)), normalize( normalMatrix * normal ) )
``````

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.

LightRelPos = LightAbsolutePos - SpectatorAbsolutePosition;
LightPosInSpectationSpace = SpectatorRotationMatrix * LighRelPos

The SpectatorRotationMatrix should just be the NormalMatrix if you don’t rotate the objet to be lit also.

LightRelPos = LightAbsolutePos - ObjectAbsolutePos
LightPosInObjectSpace = ObjectRotation * LightRelPos

dot(LightPosInObjectSpace, normal)

[QUOTE=hlewin;1253203]LightRelPos = LightAbsolutePos - ObjectAbsolutePos
LightPosInObjectSpace = ObjectRotation * LightRelPos

dot(LightPosInObjectSpace, normal)[/QUOTE]

They’re spheres. Can they really be rotated?

there’s GLM library for vector\matrix math, that has syntax based GLSL: http://glm.g-truc.net/

generating viewMatrix in GLM can be done with glm::LookAt function, or using a set of manual transformations:

``````glm::mat4 viewMatrix = glm::rotate(glm::mat4(1.0), -cameraRotation.x, glm::vec3(1, 0, 0));
viewMatrix = glm::rotate(viewMatrix,        -cameraRotation.y, glm::vec3(0, 1, 0));
viewMatrix = glm::rotate(viewMatrix,        -cameraRotation.z, glm::vec3(0, 0, 1));

viewMatrix = glm::translate(viewMatrix, -cameraPosition);
``````

note, that rotation in this case goes before translation.

but you better off starting with http://www.opengl-tutorial.org/beginners-tutorials/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.

[QUOTE=Nowhere-01;1253208]you clearly don’t know what you’re doing.
[/QUOTE]

Well, duh. I did ask for tutorials in the first post, so I’ll check that one out. Thanks.