c++ - OpenGL: How to make light to be independent of rotation? -
i have diffuse lighting shader seems work when object not rotating. however, when apply rotation transform, light seems rotate along object. it's object , light stay still camera 1 moves around object.
here's 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's code rotation:
scene.loadidentity(); scene.translate( 0.0f, -5.0f, -20.0f ); scene.rotate( angle, 0.0f, 1.0f, 0.0f ); object->draw();
i sent eye-space light position through gluniform3f, inside object->draw() function. light position static, , 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's wrong 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 ) ) );
the setviewmatrix code:
void scene::setviewmatrix( const glm::mat4 &matrix ) { viewmatrix = matrix; transformmatrix( matrix ); }
then changed modelviewmatrix used 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 );
statement 1: light position static.
statement 2:
lighteyepos = modelviewmatrix * lightpos;
these 2 claims inconsistent. if light position supposed static, shouldn't applying rotated model matrix it.
if lightpos defined in world coordinates, should multiply viewmatrix
not modelviewmatrix
. modelviewmatrix contains model matrix, contains model's rotation (you don't want apply fixed light source).
Comments
Post a Comment