# Mesh of Box

Warning! Some information on this page is older than 3 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

21:16
Mon
21
Jan 2013

Mesh of Box

Too many times I had to come up with triangle mesh of a box to hardcode it in my program, written just from memory or with help of a sheet of paper. It's easy to make a mistake and have a box with one face missing or something like that. So in case me or somebody in the future will need it, here it is. Parameters:

Box spanning from (-1, -1, -1) to (+1, +1, +1). Contains 3D positions and normals. Topology is triangle strip, using strip-cut index. Backface culling can be used, front faces are clockwise (using Direct3D coordinate system).

`// H filestruct SVertex {    vec3 Position;    vec3 Normal;};const size_t BOX_VERTEX_COUNT = 6 * 4;const size_t BOX_INDEX_COUNT  = 6 * 5;extern const SVertex BOX_VERTICES[];extern const uint16_t BOX_INDICES[];// CPP fileconst SVertex BOX_VERTICES[] = {    // -X    { vec3(-1.f, -1.f,  1.f), vec3(-1.f,  0.f,  0.f) },    { vec3(-1.f,  1.f,  1.f), vec3(-1.f,  0.f,  0.f) },    { vec3(-1.f, -1.f, -1.f), vec3(-1.f,  0.f,  0.f) },    { vec3(-1.f,  1.f, -1.f), vec3(-1.f,  0.f,  0.f) },    // -Z    { vec3(-1.f, -1.f, -1.f), vec3( 0.f,  0.f, -1.f) },    { vec3(-1.f,  1.f, -1.f), vec3( 0.f,  0.f, -1.f) },    { vec3( 1.f, -1.f, -1.f), vec3( 0.f,  0.f, -1.f) },    { vec3( 1.f,  1.f, -1.f), vec3( 0.f,  0.f, -1.f) },    // +X    { vec3( 1.f, -1.f, -1.f), vec3( 1.f,  0.f,  0.f) },    { vec3( 1.f,  1.f, -1.f), vec3( 1.f,  0.f,  0.f) },    { vec3( 1.f, -1.f,  1.f), vec3( 1.f,  0.f,  0.f) },    { vec3( 1.f,  1.f,  1.f), vec3( 1.f,  0.f,  0.f) },    // +Z    { vec3( 1.f, -1.f,  1.f), vec3( 0.f,  0.f,  1.f) },    { vec3( 1.f,  1.f,  1.f), vec3( 0.f,  0.f,  1.f) },    { vec3(-1.f, -1.f,  1.f), vec3( 0.f,  0.f,  1.f) },    { vec3(-1.f,  1.f,  1.f), vec3( 0.f,  0.f,  1.f) },    // -Y    { vec3(-1.f, -1.f,  1.f), vec3( 0.f, -1.f,  0.f) },    { vec3(-1.f, -1.f, -1.f), vec3( 0.f, -1.f,  0.f) },    { vec3( 1.f, -1.f,  1.f), vec3( 0.f, -1.f,  0.f) },    { vec3( 1.f, -1.f, -1.f), vec3( 0.f, -1.f,  0.f) },    // +Y    { vec3(-1.f,  1.f, -1.f), vec3( 0.f,  1.f,  0.f) },    { vec3(-1.f,  1.f,  1.f), vec3( 0.f,  1.f,  0.f) },    { vec3( 1.f,  1.f, -1.f), vec3( 0.f,  1.f,  0.f) },    { vec3( 1.f,  1.f,  1.f), vec3( 0.f,  1.f,  0.f) },};const uint16_t BOX_INDICES[] = {     0,  1,  2,  3, 0xFFFF, // -X     4,  5,  6,  7, 0xFFFF, // -Z     8,  9, 10, 11, 0xFFFF, // +X    12, 13, 14, 15, 0xFFFF, // +Z    16, 17, 18, 19, 0xFFFF, // -Y    20, 21, 22, 23, 0xFFFF, // +Y};`