opengl - GLSL Normal Mapping (Areas With 0.0 Lambert Gets Lit) -
when use model's normal , result fine ( there dark areas , lit areas , expect simple lambert diffuse shader )
but when use normal map , dark areas gets lit!
i want use normal map , still correct diffuse lighting these examples
here code , without normal mapping
and here code uses normal map
vertex shader
varying vec3 normal,lightdir; attribute vec3 vertex,normalvec,tangent; attribute vec2 uv; void main(){ gl_texcoord[0] = gl_texturematrix[0] * vec4(uv,0.0,0.0); normal = normalize (gl_normalmatrix * normalvec); vec3 t = normalize (gl_normalmatrix * tangent); vec3 b = cross (normal, t); vec3 vertexposition = normalize(vec3(gl_modelviewmatrix * vec4(vertex,1.0))); vec3 v; v.x = dot (lightdir, t); v.y = dot (lightdir, b); v.z = dot (lightdir, normal); lightdir = normalize (v); lightdir = normalize(vec3(1.0,0.5,1.0) - vertexposition); gl_position = gl_modelviewprojectionmatrix*vec4(vertex,1.0); }
fragment shader
vec4 computediffuselight (const in vec3 direction, const in vec4 lightcolor, const in vec3 normal, const in vec4 mydiffuse){ float ndotl = dot(normal, direction); vec4 lambert = mydiffuse * lightcolor * max (ndotl, 0.0); return lambert; } varying vec3 normal,lightdir; uniform sampler2d textures[8]; void main(){ vec3 normalvector = normalize( 2 * texture2d(textures[0],gl_texcoord[0].st).rgb - 1 ); vec4 diffuse = computediffuselight (lightdir, vec4(1,1,1,1) , normalvector, vec4(0.7,0.7,0.7,0)); gl_fragcolor = diffuse ; }
note: actual normal mapping works correctly seen in specular highlights
i used assimp load model ( md5mesh ) , calculated tangents using assimp , sent shaders attribute
here link code , screenshots of problem
https://dl.dropboxusercontent.com/u/32670019/code%20and%20screenshots.zip
is problem in code or having misconception ?
updated code , screenshots
https://dl.dropboxusercontent.com/u/32670019/updated%20code%20and%20screenshots.zip
now normal map works diffuse , diffuse alone not correct
for answer, see below.
quick (possibly wrong) observation: line
vec3 normalvector = normalize( 2 * texture2d(textures[0],gl_texcoord[0].st).rgb - 1 );
in fragment shader correctly rescales normal allow negative values. if normal map incorrect, negative values might occur not want them (your y axis presume). negative values in normal can result in reversed lighting.
my question you: normal map correct?
answer: after bit of discussion found problem, i've edited post keep thread clean, solution darko's problems in comments here. came down uninitialized varying called lightdir.
original comment:
lightdir = normalize (v); lightdir = normalize(vec3(1.0,0.5,1.0) - vertexposition); strange, overwrite instantly, wrong? dont seem keep correctly translated lightdir... or crazy... lightdir varying, don't set @ all. calculate v vector nothing?
Comments
Post a Comment