Game Development Reference
In-Depth Information
The value of the second nonparallel vector is as follows:
Vector 2 = V2 - V3
V1 , V2 , and V3 are the three vertices of a triangle primitive.
The calculation of the normal at a vertex shared by multiple triangles/primitives
is a little complicated as both primitives contribute to its calculation. The following
diagram shows a vertex, V4 , shared between triangles 1 and 2 :
N
N 1
N 2
V 4
1
2
V 1
V 3
V 2
N = N 1
+
N 2
... N n
Hence, if two primitives, ( V1 , V2 , V4 ) and ( V2 , V4 , V3 ), share a common vertex,
V4 , then we will calculate N1 from the primitive 1 ( V1 , V2 , V3 ) and N2 from the
primitive 2 ( V2 , V3 , V4 ), and the final normal N on vertex V4 would be the vector
sum of N1 and N2 .
Calculating normals from vertices and indices
Open the 02-NormalCalculation.html file in your favorite text editor. A normal is
the cross-product of the vectors with common vertices as discussed in the preceding
section. The following code calculates the normals array from the vertices and
indices arrays:
function calculateVertexNormals(vertices,indices){
var vertexVectors=[];
var normalVectors=[];
var normals=[];
for(var i=0;i<vertices.length;i=i+3){
var vector=
vec3.fromValues(vertices[i],vertices[i+1],vertices[i+2]);
var normal=vec3.create();//Initialized normal array
normalVectors.push(normal);
vertexVectors.push(vector);
}
 
Search WWH ::




Custom Search