Game Development Reference
In-Depth Information
3.12.2 Cube
To generate a cube we will build upon the previous method of generating quads. A cube has
six faces, each in the direction of a basis vector. We will use this fact to build one quad per
direction or face normal.
Figure 44 - For each face we use the face normal to create the basis, we then calculate the position of each vertex.
As before, we will store four vertices per quad in the vertex buffer, indexed by six indices
representing two triangles.
const math::vector3 normals[] = {
math::vector3(0, 0, 1),
math::vector3(0, 0, -1),
math::vector3(1, 0, 0),
math::vector3(-1, 0, 0),
math::vector3(0, 1, 0),
math::vector3(0, -1, 0),
};
const math::vector2 texcoords[] = {
math::vector2(1, 0),
math::vector2(1, 1),
math::vector2(0, 1),
math::vector2(0, 0)
};
vertex_default vertices[6 * 4];
unsigned short indices[6 * 6];
for (int i = 0; i < 6; ++i )
{
math::vector3 normal = normals[i];
math::vector3 basis = (i >= 4) ? math::vector3::UnitZ : math::vector3::UnitY;
math::vector3 side1 = normal.Cross( basis );
math::vector3 side2 = normal.Cross( side1);
const int vertexIndex0 = i * 4 + 0;
const int vertexIndex1 = i * 4 + 1;
Search WWH ::




Custom Search