Game Development Reference
In-Depth Information
The rest of the vertices, (1, 2), (2, 2), (3, 2), (0, 3), (1, 3), (2, 3), and (3, 3), are calculated
in a similar manner.
Let's look at our code to generate vertices. Open the PlaneGeometry.js file from
the primitive folder of the code files of this chapter in your favorite editor. The
following code snippet is present in this file:
PlaneGeometry = inherit(Geometry, function (width, height,
widthOfSegments, heightOfSegments)
{
superc(this);
this.width = width;
this.height = height;
this.widthOfSegments = widthOfSegments || 1;
this.heightOfSegments = heightOfSegments || 1;
var i, j;
var widthHalf = width / 2;
var heightHalf = height / 2;
var gridX = this.widthOfSegments;
var gridZ = this.heightOfSegments;
var gridXN = gridX + 1;
var gridZN = gridZ + 1;
var segmentWidth = this.width / gridX;
var segmentHeight = this.height / gridZ;
var normal = vec3.fromValues( 0, 0, 1 );
for ( i = 0; i<gridZN; i ++ ) {
for ( j = 0; j <gridXN; j ++ ) {
var x = j * segmentWidth - widthHalf;
var y = i * segmentHeight - heightHalf;
this.vertices.push(x);
this.vertices.push(- y);
this.vertices.push(0);
}
}
In the preceding code, first we calculate the number of vertices on each side ( gridXN
= gridX + 1; and gridZN = gridZ + 1; ). The segmentWidth = this.width /
gridX statement calculates the length of each segment, which in our case is 64/3.
Hence, if we look at the formula in the preceding code, var x = j * segmentWidth
- widthHalf , we get the following values:
Values of i and j
x coordinate
y coordinate
For i = 0 and j = 0
x = 0 * 64/3 - 32
y = 0 * 64/3 - 32
For i = 0 and j = 1
x = 1 * 64/3 - 32
y = 0 * 64/3 - 32
Search WWH ::




Custom Search