Game Development Reference
In-Depth Information
strip, or we could use six vertices, duplicating two of them to draw two separate triangles
again using a triangle list.
Figure 41 - A quad consists of four vertices and two triangles.
const vertex_default vertices_indexed[] =
{
vertex_default(math::vector3(-1, -1, 0), color::WHITE, 1.f, 1.f),
vertex_default(math::vector3(-1, 1, 0), color::WHITE, 1.f, 0.f),
vertex_default(math::vector3( 1, 1, 0), color::WHITE, 0.f, 0.f),
vertex_default(math::vector3( 1, -1, 0), color::WHITE, 0.f, 1.f),
};
const unsigned short indices[] = { 0, 1, 2, 3, 0, 2 };
If we use this data to create a vertex and index buffer, we would draw a proper texture
mapped quad on screen, this works well, but it is very verbose, and we start running into
some difficulties later when we want to generate other primitives that have more faces. We
can produce a more general way of creating a quad.
math::vector3 normal(0, 0, 1);
math::vector3 right = normal.Cross( math::vector3::UnitY ) / 2.f;
math::vector3 up = right.Cross( normal );
const vertex_default vertices_gen[] =
{
vertex_default( right + up, color::WHITE, 1.f, 0.f),
vertex_default( right - up, color::WHITE, 1.f, 1.f),
vertex_default(-right - up, color::WHITE, 0.f, 1.f),
vertex_default(-right + up, color::WHITE, 0.f, 0.f),
};
We will start with the z unit vector, which represents the quad's face normal. We will build
the quad around this direction. First we calculate the basis vector right by taking the cross
product of the normal and the world's y axis. Because we are interested in creating a unit
Search WWH ::




Custom Search