Jump to content
  • Advertisement
Sign in to follow this  

OpenGL OpenGL lighting problem

This topic is 1132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am suffering from the 'classic' problem of lighting moving with camera rotation. I have seen so many topics on it across the web and so many different solutions but none of them seem to fix my problem. My vertex shader is very simple.

#version 450 core

uniform vec4    light_pos;            // Light Position in eye-coordinates
uniform vec3    Kd;                    // Diffuse reflectivity
uniform vec3    Ld;                    // Light source intensity

uniform mat4    mv_Matrix;            // ModelView Matrix
uniform mat4    mvp_Matrix;            // ModelViewProjection Matrix
uniform mat3    normal_Matrix;        // Normal Matrix

in vec4            in_position;
in vec3            in_colour;
in vec3            in_normal;
in vec2            in_texcoord;

out vec2        out_texcoord;
out vec3        out_colour;
out vec3        out_lightIntensity;

void main()
    // Convert normal and position to eye coordinates
    vec3 tnorm = normalize(normal_Matrix * in_normal);
    vec4 eyeCoords = mv_Matrix * in_position;

//    vec4 test = mv_Matrix * light_pos;        // TESTING

    vec3 s = normalize(vec3(light_pos - eyeCoords));

    // The diffuse shading equation
    out_lightIntensity = Ld * Kd * max(dot(s, tnorm), 0.0);

    // Send the texture coordinates to the fragment shader
    out_texcoord = in_texcoord;
    // Send the input colour to the fragment shader
    out_colour = in_colour;

    // Convert position to clip coordinates and pass to fragment shader
    gl_Position = mvp_Matrix * in_position;

My transform code is a little more complex looking but is basically just setting up the relevent matrices. The light should stay fixed in the one location but no matter what I try nothing stops it from moving with the camera rotation.

void Mesh3D::TransformTest(mat4 viewMatrix)
    GLfloat invert[16];
    vec3 light_eye_space;
    vec3 light_world_space;
    light_world_space.SetVector(36.0f, 10.0f, 60.0f);

    // First we calculate the model-view matrix
    mModelMatrix = mTranslationMatrix * mRotationMatrix;
    mModelviewMatrix = viewMatrix * mModelMatrix;

    light_eye_space = mModelviewMatrix * light_world_space;
    // Next we calculate the model-view-projection matrix
    mModelviewProjectionMatrix = mProjectionMatrix * mModelviewMatrix;
    // Finally we calculate the normal matrix
//    mNormalMatrix.InverseTransposeM(mModelviewMatrix);

    glUniform3f(0, 0.5f, 0.5f, 0.5f);                    // Diffuse reflectivity
    glUniform3f(1, 1.0f, 1.0f, 1.0f);                    // Light intensity
    glUniform4f(2, light_eye_space.x, light_eye_space.y, light_eye_space.z, 1.0f);    // Light position (TEST!!!)
    glUniformMatrix4fv(3, 1, GL_FALSE, mv_Matrix);
    glUniformMatrix4fv(4, 1, GL_FALSE, mvp_Matrix);
    glUniformMatrix3fv(5, 1, GL_FALSE, normal_Matrix);
    glUniform1i(6, 0);

I've tried messing with the normal matrix. All it does at the moment is get the 3x3 portion of the modelview matrix. That's all the GetNormalMatrix function does. I'm assuming the problem lies in the conversion to eye space coordinates for the light position but nothing I do solves the problem. Anyone got any ideas??

Edited by reaktor

Share this post

Link to post
Share on other sites

I'm guessing that since mModelviewMatrix takes a point from object space to view space, and the light is defined in world space, the light_eye_space should be calculated this way:

light_eye_space = viewMatrix * light_world_space;

Share this post

Link to post
Share on other sites

Also, are you sure that the uniform locations you pass are correct? I'd try to use glGetUniformLocation on each of them to be sure.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!