Game Development Reference
In-Depth Information
const int vertexIndex2 = i * 4 + 2;
const int vertexIndex3 = i * 4 + 3;
vertices[vertexIndex0] = vertex_default( normal - side1 - side2, color::WHITE, texcoords[0]);
vertices[vertexIndex1] = vertex_default( normal - side1 + side2, color::WHITE, texcoords[1]);
vertices[vertexIndex2] = vertex_default( normal + side1 + side2, color::WHITE, texcoords[2]);
vertices[vertexIndex3] = vertex_default( normal + side1 - side2, color::WHITE, texcoords[3]);
indices[i* 6 + 0] = vertexIndex0;
indices[i* 6 + 1] = vertexIndex1;
indices[i* 6 + 2] = vertexIndex2;
indices[i* 6 + 3] = vertexIndex0;
indices[i* 6 + 4] = vertexIndex2;
indices[i* 6 + 5] = vertexIndex3;
}
First we will define an array of face normals, these are unit vectors in each of the possible
directions each of the cube's faces can be in. Next, we keep an array with the possible tex-
turecoordinatesforeachquad,thisallowustoindexthevertexcoordinatesaswebuildthe
cube.
We will need 24 vertices, this is 6 faces times 4 vertices per face and we will need 36 in-
dices, this is 6 faces times 6 indices per face, we create two arrays that will hold this data.
Weiterateoverthenumberoffaces,takingeachofthefacenormalsasourdirectionvector
to calculate the quad, the next line may seem a little confusing:
math::vector3 basis = (i >= 4) ? math::vector3::UnitZ : math::vector3::UnitY;
We cannot use UnitY as the basis when using the face normals: normals[4] and normals[5]
becausethecrossproductofavectoragainstitselfwouldbeazerovector,whichwouldfail
to create the basis we are looking for, so for these two face normals we replace the basis
vectorby UnitZ ,thiswillensurewewillgetaperpendicularvectorastheresultofthecross
product against the face normal.
Having determined the basis, we can calculate the sides for the face, this is done by com-
puting the cross product between the face normal and the basis, and then the cross product
of the face normal against the recently computed side.
math::vector3 side1 = normal.Cross( basis );
math::vector3 side2 = normal.Cross( side1 );
Having the sides we can now calculate the vertices in the same way as we calculate the
geometry for a quad, with one extra step, we need to add in the normal vertex to be certain
that the vertex is offset in the direction of the face we are computing.
const int vertexIndex0 = i * 4 + 0;
vertices[vertexIndex0] = vertex_default( normal - side1 - side2, color::WHITE, texcoords[0]);
Search WWH ::




Custom Search