## glsl es 2.0 lighting madness

I am setting my light position in "world space" - 0,1000,0. (straight overhead).

I have a uniform modelviewmatrix matrix.( only translations, rotations, and uniform scaling)

My normals are part of an obj model, and hence are in "model space".

I transform my normals by the modelviewmatrix.

The model renders correctly, except for the lighting. The problem is that model position is affecting the lighting. Models closer to the light are getting more light. Models farther away quickly become all black.

I thought that transforming the normals by the modelviewmatrix will take them to world space...but I think that is where my problem is.

The shader is:

gl_Position = u_mvp_matrix * position; //this works

vec3 tNorm = normalize(vec3(u_mv_matrix * normal));

vec3 lightPosition = normalize(vec3(0,1000,0));//world space

vec3 lightVec = normalize(lightPosition-ecPosition);

colorVarying = mix(vec4(0,0,0,0), vec4(1,1,1,1), max( dot(tNorm, lightVec), 0.0 ));

In old school openGL, I believe the light is automatically multiplied by the viewmatrix?

Also, glNormalMatrix is used instead of the modelview matrix, but it shouldn't matter if it is orthogonal, correct?

Most of the examples I find on the net are for old school open gl.

BTW - If I don't transform my normals, lighting works perfectly, but then rotations of the model are ignored and not correctly lit.

Are there any good examples of es 2 lighting?

I have the orange book and es 2 programming book, but neither is helping enough...

Thanks for any advice or feedback

Anyway, I think I figured out my error.

Ultimately, the lighting relies on computing the dot product between the light direction and the normal.

Both of these values have to be in the same "space".

I was trying to move the normal into the world space of the light and having issues.

I resolved it by moving the light direction into the model space of the normal by using the inverse of the modelview matrix.

Ultimately I want to implement normal mapping, so this approach makes more sense anyway.

Let me know if anyone is interested in seeing the source for this and I will post...

When you come to doing normal mapping, you need to be in tangent space, which is close to model space but not quite - it's the orientation of each textured triangle. You'll need a matrix mul there too so you can factor into that any further transforms you like at the CPU end.

I do all my lighting in worldspace because it feels more natural, especially when you start making those silly shapes with your fingers and end up looking like a failed rap artist!

Hope this helps.