c - OpenGL lighting vector normalization -


i attempting find vector normalization arbitrary object loaded opengl program. trying calculate normals vertices. have gathered need first calculate normals faces, find average of normals vertices.

when run program of object lit correctly, part not. triangles on each independent face seems lit correctly, not adjacent face. calculate normal of each vertex assume have find normalized average of 6 connecting triangles, attempting did not seem work.

here code using trying calculate normals of each face.

vec3 one, two; for(int = 0; < vertices.vertexnumber; += 3) {     one.x = point3[i+1].x - point3[i].x;     one.y = point3[i+1].y - point3[i].y;     one.z = point3[i+1].z - point3[i].z;      two.x = point3[i+2].x - point3[i].x;     two.y = point3[i+2].y - point3[i].y;     two.z = point3[i+2].z - point3[i].z;      vec3 normal = normalizevec3(crossvec3(one, two));      normalized[i] = normal; } 

and function using normalize vectors

vec3 normalizevec3(vec3 v) { float veclength = lengthvec3(v); vec3 dividebyzero = {0.0, 0.0, 0.0}; if (veclength == 0)     return dividebyzero; float x, y, z; x = v.x / veclength; y = v.y / veclength; z = v.z / veclength; vec3 u = {x, y, z}; return u; } 

after refactoring here cube looks like.

enter image description here

enter image description here

the normal values getting

0.000000, 0.000000, 1.000000 0.000000, 0.000000, 1.000000 0.000000, 0.000000, 1.000000 0.000000, 0.000000, 1.000000 -1.000000, 0.000000, -0.000000 -1.000000, 0.000000, 0.000000 -1.000000, 0.000000, -0.000000 -1.000000, 0.000000, 0.000000 0.000000, -1.000000, 0.000000 -0.000000, -1.000000, 0.000000 0.000000, -1.000000, 0.000000 -0.000000, -1.000000, 0.000000 

the normal triangle given order of vertices.

what doing there wrong, because substracting directions , using cross product obtain perpendicular direction, nonsense (you can substract 2 points , vector out of that, , normalize direction, 2 directions make no sense substract them).

if want find average normal of triangle (considering triangle has different normals each vector) use raw average normalize. work fine.

you seem have normal information (normals vector). that's sufficient lightning !

the time need compute vertex "normal" when compute it's tangent , binormal in order apply normal maps. first implement correctly phong shader can go normal maps .


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -