# Lighting algorithm question

Hi

Looking here

http://www.lighthouse3d.com/tutorials/glsl-tutorial/point-lights/

I can see that to get the light direction you need to use the formula

“light direction = light position – vertex position”

The text following this says

“Before we can compute this vector we must apply the View Model matrix (m_viewModel in the shader) to the vertex position.”

The vertex position would not be calculated from the viewModel matrix it would be calculated from the model matrix only.

This tutorial uses the model matrix as I would expect but then has other logic to work out the spec light

https://learnopengl.com/#!Lighting/Basic-Lighting

Can someone say if there is a typo in the first formula or if there is something basic that I haven;t understood which would explain it and also what the difference is between the two tutorials?

there are many way to lit your scene. i’m not sure what the “lighthouse” tutorial actually does, i think it’s calculating light effect in “view space”, not in “world space” (?)

imho, the easiest way to understand this topic is to do lighting in world space:

there are different ways to “shade” your scene, that is different lighting models:
— etc…

most of them consider light as a mix of 3 different parts:
– ambient part
– diffuse part
– specular part

only diffuse and specular come from light sources, ambient has no source, it comes from “everywhere” (a kind of “cheat” to avoid completely dark (black) parts in your scene). diffuse and specular have to accumulated for each light source

You can perform lighting calculations in any space, so long as

[ol]
[li] you are consistent, i.e. all positions and directions are in the same space, and
[/li][li] that space is affine to world space, i.e. none of the transformations have a projective component (this is why conventionally the projection matrix is separate from the model-view matrix). And it’s easier if that space is orthogonal to world space (i.e. only rotations, translations and uniform scaling; no non-uniform scaling or shear); otherwise you need to take additional steps to preserve orthogonality of e.g. normal vectors.
[/li][/ol]
The fixed-function pipeline uses eye space. World space works if you have such a thing. Object space can be made to work but if objects have distinct transformations, then you need to transform the eye and light vectors into each object’s space, which is inconvenient.

Re lighting calculations and which space you choose…

One other point to consider is whether your WORLD-space is so large that you need double-precision to represent it to sufficient accuracy.

If so, and if you are doing lighting calculations with point light sources (which have you subtracting points in your “lighting space”), you may very well want to choose another space besides WORLD-space for your lighting calculations as single-precision floats likely won’t provide enough precision (recall: in normal shaders, your positions are going to be represented with single-precision floats).

On the other hand, single-precision floats provide plenty of precision for EYE-space lighting math. This is one reason why choosing EYE-space for your lighting calculations is a reasonable choice.