// Note: every 'Object' has the following matrices:
// - Local matrix, changed rarely
// - Current matrix, regularly changed
// - Operation matrix, applied to Current matrix whenever default operation is called,
// this can be used to create objects that constantly rotate around it's axis or something
// ... The operation matrix is really there to illustrate this example
// Create a 3d box that can fit inside of a unit length circle
Object* Create3dBox()
{
// Vertice units so that square fits inside a unit length circle
Fixed32 a(Fixed32(2).SquareRoot());
int vertexCount = 8;
Vertex *vertices = new Vertex[vertexCount];
vertices[0] = Vertex(a, a, a);
vertices[1] = Vertex(a, a, -a);
vertices[2] = Vertex(a, -a, -a);
// ... and so on
int faceCount = 6;
Face *faces = new Face[faceCount];
int *indexArray;
int faceIndexCount = 4;
// Face 0 (east)
indexArray = new int[faceIndexCount];
indexArray[0] = 0;
indexArray[1] = 1;
indexArray[2] = 2;
indexArray[3] = 3;
faces[0] = Face(vertices, indexArray, faceIndexCount);
// Face 1 (front)
indexArray = new int[faceIndexCount];
indexArray[0] = 4;
indexArray[1] = 0;
indexArray[2] = 3;
indexArray[3] = 7;
// ... and so on
return new Object(vertices, vertexCount, faces, faceCount);
} // Create3dBox
// Creates a model of our solar system, fitting inside of a unit
// length circle (with planets being represented by cubes)
// The moon will circle the Earth
Model* CreateSolarSystem()
{
int objectCount = 9; // Sun, 8 (real planets that does not include pluto)
// filled with objectCount+1(with the moon's data being the last element).
Fixed32 solarSystemObjectSizes[] = { /* Array filled with valid data */};
Fixed32 distanceFromSun[] = { /*Array filled with valid data */};
Fixed32 orbitRotation[] = { /*Array filled with valid data */};
Model *models = new Model[objectCount];
// Moon will orbit the Earth
Model *moon = new Model[1];
for(int i = 0; i < objectCount+1; ++i)
{
// Note that the moon is object 9 and will orbit the Earth, which will be later reflected in the model created
Matrix local;
local.setToIdentity();
// Is moon object ?
if( i == 9 )
{
// Descale size of Earth for the moon, since the moon's operations will occur after the Earth
local.scale(Fixed32(1) / Fixed32(solarSystemObjectSizes[3]));
}
local.translate(distanceFromSun[i], 0, 0);
local.scale(solarSystemObjectSizes[i]);
// Operation matrix that will make the object orbit
Matrix operation;
operation.setToIdentity();
operation.rotateY(orbitRotation)
Matrix current;
current.setToIdentity();
Object *object = Create3dBox();
Model *childModel = null;
int childModelCount = 0;
// Is earth object ?
if( i == 3 )
{
childModel = moon;
childModelCount = 1;
}
Model *model = new Model(object, local, current, operation,
childModel, childModelCount);
}
Matrix identity;
matrix.setToIdentity();
Object *nullObject = null;
Model *solarSystem = new Model(nullObject, identity, identity, identity,
models, objectCount+1);
return solarSystem;
} |