java - Normals transforming, but lighting still weird? -
i have basic model viewer displays parsed obj model. have light positioned camera is, model being viewed lit on 1 side. reason, transformations aren't doing expected in renderer. when rotate model 90 degrees x- or y-axis, mesh dark. rotate 90 degrees, , lit again.
am doing transformations wrong? or normals wrong begin with?
i calculated , applied proper transform normals in app (transpose of inverse of modelview matrix)
matrix.invertm(mnormalmatrix, 0, mmvmatrix, 0); matrix.transposem(mnormalmatrix, 0, mnormalmatrix, 0);
before passing shaders:
/*vertex shader*/ attribute vec3 vposition; attribute vec3 vnormal; uniform mat4 modelviewmatrix; uniform mat4 mmvpmatrix; uniform mat4 mviewmatrix; uniform mat4 normalmatrix; uniform float lightingenabled; varying float lightsenabled; varying vec3 lightposeye; varying vec3 normaleye; varying vec3 verteye; void main() { /*calculate normal matrix*/ vec4 normal = vec4(vnormal, 0.0); normaleye = normalize(vec3(normalmatrix * normal)); lightsenabled = lightingenabled; lightposeye = vec3(mviewmatrix * vec4(0.0, 0.0, 3.0, 1.0)); verteye = vec3(modelviewmatrix * vec4(vposition, 1.0)); gl_position = mmvpmatrix * vec4(vposition, 1.0); }
/*fragment shader*/ precision mediump float; /*uniform vec4 vcolor; */ varying float lightsenabled; varying vec3 lightposeye; varying vec3 normaleye; varying vec3 verteye; void main() { /*light output components*/ vec3 ia; vec3 id; /*light source components*/ vec3 la = vec3(0.5); vec3 ld = vec3(1.0); /*vec3 ls = vec3(1.0);*/ vec3 ka = vec3(0.3); /*ambient reflectance term*/ vec3 kd = vec3(1.0); /*diffuse term*/ /*ambient light term*/ ia = la * ka; float dotprod; vec3 lighttosurface; if(lightsenabled > 0.5){ /*diffuse light term*/ lighttosurface = normalize(lightposeye - verteye); dotprod = dot(lighttosurface, normaleye); dotprod = max(dotprod, 0.0); } else { dotprod = 1.0; } id = ld * kd * dotprod; gl_fragcolor = vec4(ia + id, 1.0); }
i know been while, i've found solution. problem line in vertex shader:
normaleye = normalize(vec3(normalmatrix * normal));
i changed to:
normaleye = normalize(vec3(modelviewmatrix * normal));
and works fine. although have no idea why second line works when first 1 should.
Comments
Post a Comment