Game Development Reference
In-Depth Information
//Since the normal calculated from three vertices is same for
all the three vertices(same face/surface), the contribution
from each normal to the corresponding vertex is the same
vec3.add(normalVectors[this.indices[j]],
normalVectors[this.indices[j]],normal);
vec3.add(normalVectors[this.indices[j+1]],
normalVectors[this.indices[j+1]],normal);
vec3.add(normalVectors[this.indices[j+2]],
normalVectors[this.indices[j+2]],normal);
}
for(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]);
}
},
We have added two new functions to the
Geometry
class:
clone
and
morphedVertexNormalsFromObj
. The
clone
function simply copies variables of the
Geometry
class to create a new
geometry
object.
The other function
morphedVertexNormalsFromObj
is important. The exported
obj
file from Blender contains information on normals of every vertex of every face of the
model. In such cases, we do not need to calculate the normal. This information is also
encoded in our JSON file. While we parse our JSON file, we populate each face with
its vertex normals. The following code snippet is taken from the
parseJSON.js
file to
confirm the preceding statement:
if ( hasFaceVertexNormal ) {
var aVertices=["a","b","c","d"]
for ( i = 0; i < nVertices; i++ ) {
var aVertex=aVertices[i];
normalIndex = faces[ offset ++ ] * 3;
normal = vec3.fromValues(normals[ normalIndex ++ ],
normals[ normalIndex ++ ],normals[ normalIndex ]);
face.vertexNormals[aVertex]= normal;
}
}