Models - simple-entertainment/simplicity GitHub Wiki

Models are handy for lots of things. They can be rendered, used as bounding volumes or provide geometry to physical bodies.

Models are Components!

If you want a model to be rendered, just add it to an entity!

std::unique_ptr<Entity> entity(new Entity);
std::unique_ptr<Model> model(new Cube(5.0f));
entity->addUniqueComponent(move(model));
Simplicity::getScene()->addEntity(move(entity));

Shapes

Shapes are objects that can be used instead basic of meshes to create basic geometry. They are very easy to use. A wide variety of shapes are include in simplicity:

  • Box
  • Capsule
  • Cube
  • Cylinder
  • Sphere
  • Square
  • Torus
  • Triangle

Shapes will eventually be converted to meshes by renderers etc. before they can be rendered. To assist with this shapes have a level of detail property that acts as a hint to the renderer as to how detailed a mesh it should create.

std::unique_ptr<Shape> sphere(new Sphere(5.0f));
sphere->setLevelOfDetail(10);

For some shapes like cubes which are very basic meshes this hint will be ignored since there is no need for it.

Meshes and The Model Factory

Meshes need to be constructed using the model factory since the implementation of a mesh is dependent on the rendering plugin being used e.g. OpenGL or Direct3D. You can create a mesh using your own vertex and index data:

std::vector<Vertex> vertices;
std::vector<unsiigned int> indices;
// Fill vertex and index vectors...
std::unique_ptr<Mesh> mesh = ModelFactory::getInstance()->createMesh(vertices, indices);

Or if you can't be bothered with indices...

std::vector<Vertex> vertices;
// Fill vertex vector...
std::unique_ptr<Mesh> mesh = ModelFactory::getInstance()->createMesh(vertices);

You can also create a whole range of predefined meshes e.g. a cube:

float halfExtent = 5.0f;
Vector4 color(1.0f, 1.0f, 1.0f, 1.0f);
bool doubleSided = false;
std::unique_ptr<Mesh> cubeMesh = ModelFactory::getInstance()->createCubeMesh(halfExtent, color, doubleSided);

The model factory can also be used to insert vertices and indices of a particular basic geometry into existing vectors of vertices and indices.

ModelFactory::insertCubeVertices(...);
ModelFactory::insertCubeIndices(...);

Model Functions

There are a few model functions supplied to modify vertices in a mesh:

std::unique_ptr<Mesh> mesh = // Some mesh...

ModelFunctions::colorizeVertices(mesh->getVertices(), Vector4(1.0f, 0.0f, 0.0f, 1.0f));

TODO finish this!

Other Models

Other models are also provided to describe basic geometrical concepts:

  • Line
  • Plane
  • Point

Casting

Since there are lots of different model types and it is common to only get a pointer to a Model (e.g. in a Renderer) the ID member + static_cast pattern has been provided. Instead of this:

Model* model = // some model...
Square* square = dynamic_cast<Square*>(model);
if (square != NULL)
{
    // do stuff...
}

You can do this to avoid the expensive dynamic_cast operation:

Model* model = // some model...
if (model->getTypeID() == Square::TYPE_ID)
{
    Square* square = static_cast<Square*>(model);
    // do stuff...
}
⚠️ **GitHub.com Fallback** ⚠️