iDevGames Forums
glsl es 2.0 lighting madness - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: glsl es 2.0 lighting madness (/thread-8095.html)



glsl es 2.0 lighting madness - OptimisticMonkey - Sep 10, 2010 03:07 PM

I am trying to implement lighting in glsl es 2.0, using a simple "hemisphere lighting" shader out of the Orange book.

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


RE: glsl es 2.0 lighting madness - OptimisticMonkey - Sep 11, 2010 10:39 AM

Sorry if the previous post was confusing - I posted in a moment of frustration....

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


RE: glsl es 2.0 lighting madness - Applewood - Oct 8, 2010 03:33 PM

I think it more likely that you're not using a 3x3 matrix multiply on your normals transform, so this effectively moving them as well as rotating them.

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! Smile

Hope this helps.