View Full Version : My lighting is way off

Rakehell

07-25-2013, 11:21 AM

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?

GClements

07-25-2013, 01:17 PM

dot(

normalize( Light1Position - vec3(modelViewProjectionMatrix * position) ),

normalize( normalMatrix * normal )

)

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

Rakehell

07-25-2013, 03:25 PM

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.

GClements

07-25-2013, 04:37 PM

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

Rakehell

07-26-2013, 07:28 AM

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

GClements

07-26-2013, 07:40 AM

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.

Rakehell

07-26-2013, 08:04 AM

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

http://www.gamedev.net/page/resources/_/technical/opengl/the-basics-of-glsl-40-shaders-r2861

I have all of my positions in absolute space.

Nowhere-01

07-26-2013, 08:22 AM

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.

Rakehell

07-26-2013, 08:33 AM

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

Rakehell

07-26-2013, 04:42 PM

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'm googling how to calculate the view matrix, but it seems nontrivial.

hlewin

07-26-2013, 05:59 PM

LightRelPos = LightAbsolutePos - ObjectAbsolutePos

LightPosInObjectSpace = ObjectRotation * LightRelPos

dot(LightPosInObjectSpace, normal)

Rakehell

07-26-2013, 07:25 PM

LightRelPos = LightAbsolutePos - ObjectAbsolutePos

LightPosInObjectSpace = ObjectRotation * LightRelPos

dot(LightPosInObjectSpace, normal)

They're spheres. Can they really be rotated?

Nowhere-01

07-26-2013, 10:17 PM

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:

http://www.paulsprojects.net/tutorials/smt/spaces.jpg

hlewin

07-27-2013, 01:36 AM

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.

Rakehell

07-30-2013, 04:14 PM

you clearly don't know what you're doing.

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

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.