OpenGL: How to make light independent of rotation?

I have a diffuse lighting shader that seems to work when the object is not spinning. However, when I apply the rotation transformation, the light also rotates with the object. It looks like an object and the light remains stationary, but the camera is the one that moves around the object.

Here is my vertex shader code:

#version 110

uniform mat4 projectionMatrix;
uniform mat4 modelviewMatrix;
uniform vec3 lightSource;

attribute vec3 vertex;
attribute vec3 normal;

varying vec2 texcoord;

void main() {
    gl_Position = projectionMatrix * modelviewMatrix * vec4( vertex, 1.0 );

    vec3 N = gl_NormalMatrix * normalize( normal );
    vec4 V = modelviewMatrix * vec4( vertex, 1.0 );
    vec3 L = normalize( lightSource - V.xyz );

    float NdotL = max( 0.0, dot( N, L ) );

    gl_FrontColor = vec4( gl_Color.xyz * NdotL, 1.0 );

    gl_TexCoord[0] = gl_MultiTexCoord0;
}

and here is the code that performs the rotation:

scene.LoadIdentity();
scene.Translate( 0.0f, -5.0f, -20.0f );
scene.Rotate( angle, 0.0f, 1.0f, 0.0f );
object->Draw();

I sent the light position of the eye through glUniform3f, inside the object -> Draw (). The light position is static and is defined as:

glm::vec4 lightPos( light.x, light.y, light.z, 1.0 );
glm::vec4 lightEyePos = modelviewMatrix * lightPos;
glUniform3f( uniforms.lightSource, lightEyePos.x, lightEyePos.y, lightEyePos.z );

What is wrong with this approach?

Edit: glm :: lookAt code

Scene scene;
scene.LoadMatrix( projection );
scene.SetMatrixMode( Scene::Modelview );
scene.LoadIdentity();
scene.SetViewMatrix( glm::lookAt( glm::vec3( 0.0f, 0.0f, 0.0f ), glm::vec3( 0.0f, -5.0f, -20.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ) ) );

SetViewMatrix Code:

void Scene::SetViewMatrix( const glm::mat4 &matrix ) {
    viewMatrix = matrix;
    TransformMatrix( matrix );
}

Then I just changed the modelviewMatrix that I used for viewMatrix:

glm::vec4 lightPos( light.x, light.y, light.z, 1.0 );
glm::vec4 lightEyePos = viewMatrix * lightPos;
glUniform3f( uniforms.lightSource, lightEyePos.x, lightEyePos.y, lightEyePos.z );
+5
source share
1 answer

1: .

2: lightEyePos = modelviewMatrix * lightPos;

. , .

lightPos , viewMatrix modelviewMatrix. modelviewMatrix , ( ).

+6

All Articles