reaktor24

09-17-2015, 10:02 AM

Not sure whether to put this in the beginners section or advanced but anyway. The problem is that I am having issues with my lighting position. First in my program loop I create a standard viewing matrix. Then after that I create the lights position vector in eye space using this method:

M_Translation.Translate(light_world_space.x, light_world_space.y, light_world_space.z);

M_ModelMatrix = M_Translation * M_Rotation;

M_LightMatrix = M_Viewing * M_ModelMatrix;

light_eye_space = M_LightMatrix * light_world_space;

Does this seem correct? I do this once per rendering frame. After this I draw a box object in the scene and from the initial point of view from the camera I can see that only one side of the box (the correct side) is lit and as the box rotates so does the lit side. In other words the light is rotating with the object.

Inside the object transformation function is this code to position and rotate the object. Into it I pass the current light position in eye space as well as the current view matrix.

void Billboard::TransformTest(mat4 viewMatrix, vec3 light_position)

{

mModelMatrix = mTranslationMatrix * mRotationMatrix;

mModelViewMatrix = viewMatrix * mModelMatrix;

mModelViewMatrix.GetMatrix(mv_Matrix);

// Now we calculate the modelview-projection matrix

mMVPMatrix = mProjectionMatrix * mModelViewMatrix;

mMVPMatrix.GetMatrix(mvp_Matrix);

// Finally we calculate the normal matrix

mNormalMatrix = mModelViewMatrix;

mNormalMatrix.InvertM();

mNormalMatrix.TransposeM();

mNormalMatrix.GetNormalMatrix(normal_Matrix);

glUniform3f(0, 1.0f, 1.0f, 1.0f); // Diffuse reflectivity

glUniform3f(1, 1.0f, 1.0f, 1.0f); // Light intensity

glUniform4f(2, light_position.x, light_position.y, light_position.z, 1.0f);

glUniformMatrix4fv(3, 1, GL_FALSE, mv_Matrix);

glUniformMatrix4fv(4, 1, GL_FALSE, mvp_Matrix);

glUniformMatrix3fv(5, 1, GL_FALSE, normal_Matrix);

glUniform1i(6, 0);

}

Any idea what I'm doing wrong? By the way as it is currently experimental code, all the Uniform locations have been numbered manually and they've all been checked and are correct.

EDIT: Ok changing 'mNormalMatrix = viewMatrix;' to 'mNormalMatrix = mModelViewMatrix;' fixed the light rotating with box problem. Now when the camera is static at the initial viewing location it does actually look correct. Moving and rotating the camera position still causes the light to move however. I wonder what I'm doing wrong. :(

M_Translation.Translate(light_world_space.x, light_world_space.y, light_world_space.z);

M_ModelMatrix = M_Translation * M_Rotation;

M_LightMatrix = M_Viewing * M_ModelMatrix;

light_eye_space = M_LightMatrix * light_world_space;

Does this seem correct? I do this once per rendering frame. After this I draw a box object in the scene and from the initial point of view from the camera I can see that only one side of the box (the correct side) is lit and as the box rotates so does the lit side. In other words the light is rotating with the object.

Inside the object transformation function is this code to position and rotate the object. Into it I pass the current light position in eye space as well as the current view matrix.

void Billboard::TransformTest(mat4 viewMatrix, vec3 light_position)

{

mModelMatrix = mTranslationMatrix * mRotationMatrix;

mModelViewMatrix = viewMatrix * mModelMatrix;

mModelViewMatrix.GetMatrix(mv_Matrix);

// Now we calculate the modelview-projection matrix

mMVPMatrix = mProjectionMatrix * mModelViewMatrix;

mMVPMatrix.GetMatrix(mvp_Matrix);

// Finally we calculate the normal matrix

mNormalMatrix = mModelViewMatrix;

mNormalMatrix.InvertM();

mNormalMatrix.TransposeM();

mNormalMatrix.GetNormalMatrix(normal_Matrix);

glUniform3f(0, 1.0f, 1.0f, 1.0f); // Diffuse reflectivity

glUniform3f(1, 1.0f, 1.0f, 1.0f); // Light intensity

glUniform4f(2, light_position.x, light_position.y, light_position.z, 1.0f);

glUniformMatrix4fv(3, 1, GL_FALSE, mv_Matrix);

glUniformMatrix4fv(4, 1, GL_FALSE, mvp_Matrix);

glUniformMatrix3fv(5, 1, GL_FALSE, normal_Matrix);

glUniform1i(6, 0);

}

Any idea what I'm doing wrong? By the way as it is currently experimental code, all the Uniform locations have been numbered manually and they've all been checked and are correct.

EDIT: Ok changing 'mNormalMatrix = viewMatrix;' to 'mNormalMatrix = mModelViewMatrix;' fixed the light rotating with box problem. Now when the camera is static at the initial viewing location it does actually look correct. Moving and rotating the camera position still causes the light to move however. I wonder what I'm doing wrong. :(