Game Development Reference
In-Depth Information
face.vertexNormals["b"]=vec3.clone(normal);
face.vertexNormals["c"]=vec3.clone(normal);
this.faces.push( face );
this.faceVertexUvs[0][faceIndex]=[];
this.faceVertexUvs[0][faceIndex]["a"]= faceUVIndex+1;
this.faceVertexUvs[0][faceIndex]["b"]= faceUVIndex+2;
this.faceVertexUvs[0][faceIndex]["c"]= faceUVIndex+3;
faceIndex=faceIndex+1;
faceUVIndex=faceUVIndex+4;
In the following code, once our
vertices
and
faces
(
a
,
b
,
c
,
vertexNormals
),
uvs
, and
faceVertexUvs
arrays of our
Geometry
class are computed, we invoke
the
verticesFromFaceUvs()
function to make the number of vertices equal to the
number of
uvs
(for vertex shaders). We also invoked the
indicesFromFaces()
function to copy the face attributes,
a
,
b
, and
c
, to the
indices
array of the
Geometry
class and invoke the
morphedVertexNormalsFromObj()
function to prepare our
normals
array of the
Geometry
class:
this.verticesFromFaceUvs(this.vertices,uvs,0);
this.indicesFromFaces();
this.morphedVertexNormalsFromObj();
Rendering our plane geometry
The preceding code computes our
vertices
,
indices
,
normals
, and
uvs
arrays. We
generally read these values from the JSON file and store them in their corresponding
arrays. So, the rendering code of the geometry is similar to any other geometry.
Open the
Plane.js
file from
primitive/game
in your favorite text editor. The
objective of the code is to initialize the
PlaneGeometry
object with the required
parameters (
width
,
height
,
widthOfSegments
,
heightOfSegments
). The following
is the code snippet from the
Plane.js
file:
Plane= inherit(StageObject, function (width, height,
widthOfSegments, heightOfSegments,textureName){
superc(this);
this.geometry=null;
this.width=width;
this.height=height;
this.ws=widthOfSegments;
this.wh=heightOfSegments;
this.geometry=new PlaneGeometry(width, height, widthOfSegments,
heightOfSegments;
this.materialFile=textureName;//"terrain_1024.jpg";
});