samedi 25 avril 2015

OpenGL lighting vector normalization


I am attempting to find the vector normalization for an arbitrary object loaded into an opengl program. I am trying to calculate the normals for all the vertices (assuming this is what I need to do) for the lights. When I run the program some of the object is lit correctly, but for the most part it is not. The majority of the faces are lit separately with some of them not lit at all.

Here is the code I am using for trying to calculate the normals of each vertex.

vec3 one, two;
for(int i = 0; i < object.vertexNumber; i++)
{
    one.x = normals[i+1].x - normals[i].x;
    one.y = normals[i+1].y - normals[i].y;
    one.z = normals[i+1].z - normals[i].z;

    two.x = normals[i+2].x - normals[i].x;
    two.y = normals[i+2].y - normals[i].y;
    two.z = normals[i+2].z - normals[i].z;

    vec3 normal = normalizevec3(crossvec3(one, two));

    normalized[i] = normal;
}

and the function I am using to normalize the 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;
}


Aucun commentaire:

Enregistrer un commentaire