blackberry/GamePlay — Issue #8: Added function for Mesh Class
Author: Carandiru
Created: 2011-12-13 01:57:52+00:00
State: open

I wanted to share this function for you too add to the mesh class:

Mesh* Mesh::createBox(const BoundingBox& box)
{
// box
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
//

float vertices[] =
{
// v0-v1-v2-v3 front
box.max.x, box.max.y, box.max.z, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,	// 0
box.min.x, box.max.y, box.max.z, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,	// 1
box.min.x, box.min.y, box.max.z, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,	// 2
box.max.x, box.min.y, box.max.z, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,	// 3

// v0-v3-v4-v5 right
box.max.x, box.max.y, box.max.z, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,	// 4
box.max.x, box.min.y, box.max.z, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,	// 5
box.max.x, box.min.y, box.min.z, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,	// 6
box.max.x, box.max.y, box.min.z, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,	// 7

// v0-v5-v6-v1 top
box.max.x, box.max.y, box.max.z, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,	// 8
box.max.x, box.max.y, box.min.z, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,	// 9
box.min.x, box.max.y, box.min.z, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,	// 10
box.min.x, box.max.y, box.max.z, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,	// 11

// v1-v6-v7-v2 left
box.min.x, box.max.y, box.max.z, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,	// 12
box.min.x, box.max.y, box.min.z, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,	// 13
box.min.x, box.min.y, box.min.z, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,	// 14
box.min.x, box.min.y, box.max.z, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,	// 15

// v7-v4-v3-v2 bottom
box.min.x, box.min.y, box.min.z, 0.0f,-1.0f, 0.0f, 0.0f, 0.0f,	// 16
box.max.x, box.min.y, box.min.z, 0.0f,-1.0f, 0.0f, 1.0f, 0.0f,	// 17
box.max.x, box.min.y, box.max.z, 0.0f,-1.0f, 0.0f, 1.0f, 1.0f,	// 18
box.min.x, box.min.y, box.max.z, 0.0f,-1.0f, 0.0f, 0.0f, 1.0f,	// 19

// v4-v7-v6-v5 back
box.max.x, box.min.y, box.min.z, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,	// 20
box.min.x, box.min.y, box.min.z, 0.0f, 0.0f,-1.0f, 1.0f, 0.0f,	// 21
box.min.x, box.max.y, box.min.z, 0.0f, 0.0f,-1.0f, 1.0f, 1.0f,	// 22
box.max.x, box.max.y, box.min.z, 0.0f, 0.0f,-1.0f, 0.0f, 1.0f	// 23
};

VertexFormat::Element elements[] =
{
VertexFormat::Element(VertexFormat::POSITION, 3),
VertexFormat::Element(VertexFormat::NORMAL, 3),
VertexFormat::Element(VertexFormat::TEXCOORD0, 2)
};
VertexFormat* format = VertexFormat::create(elements,3);
Mesh* mesh = Mesh::createMesh(format, 24);
SAFE_RELEASE(format);
if (mesh == NULL)
{
return NULL;
}

mesh->_primitiveType = TRIANGLES;
mesh->setVertexData(vertices, 0, 24);

// index array
unsigned char indices[] =
{
0, 1, 2, 0, 2, 3, // front
4, 5, 6, 4, 6, 7, // right
8, 9,10, 8,10,11, // top
12,13,14, 12,14,15, // left
16,17,18, 16,18,19, // bottom
20,21,22, 20,22,23 // back
};

MeshPart* pPart = mesh->addPart( TRIANGLES, INDEX8, 36 );
pPart->setIndexData( indices, 0, 36 );

return mesh;
}