Game Development Reference
In-Depth Information
}
this.normals=[];
for(var j=0;j<normalVectors.length;j=j+1){
vec3.normalize(normalVectors[j],normalVectors[j]);
this.normals.push(normalVectors[j][0]);
this.normals.push(normalVectors[j][1]);
this.normals.push(normalVectors[j][2]);
}
},
In the preceding code, we iterated over the
faces
array. If a face normal is defined
and vertex normals are not defined for that face, we copy the face normal on each
vertex normal (
a
,
b
,
c
). In the following diagram, the vertex normals will be the same
as the face normal:
C
XA
YA
FA
ZA
FA=Face Normal A=XA=YA=ZA
~~~
Elements of the normal array have one-to-one correspondence to the
vertices
array.
Hence, if a normal is already defined for the vertex index
this.faces[i].a
, then
add the new normal to the existing normal, or simply copy the normal at that vertex
index. This can be done using the following code:
if(normalVectors[this.faces[i].a]===undefined)
{
normalVectors[this.faces[i].a]=vec3.clone(this.faces[i].
vertexNormals["a"]);
}
else{
vec3.add(normalVectors[this.faces[i].a],normalVectors
[this.faces[i].a],this.faces[i].vertexNormals["a"])
}
In the last loop, we iterate over the newly created
normalVectors
array. First,
we normalize the vector and then copy each element to the
normals
array of the
Geometry
class.