Model Loader
Draw Model
Now that we have a way to create models and load the model 3d file, we want to be able to render the model.
First we will need to create a new function to draw the model:
void Model_Draw(GLuint ShaderID, model *Model) {
for (unsigned int i = 0; i < Model->Meshes.size(); i++) {
Mesh_Draw(ShaderID, &Model->Meshes[i]);
}
}
This function uses the Mesh_Draw function that we have already created for the mesh type. So here
We just need to go over all the meshes in the model and draw them. As easy as that.
Now we need a way to add a model to an entity to be used for rendering.
enum class entity_type {
...
Model,
};
struct entity {
...
model Model;
};
We have added the model to the entity and also a new entity_type Model which we will use for drawing the correct
thing.
Now we can add a new entity to the scene using a model for that entity.
model BackpackModel;
Model_Create(&BackpackModel, "./resources/models/backpack/backpack.obj",
false);
entity Backpack = {
.Type = entity_type::Model,
.Position = glm::vec3(4.0f, 0.0f, 0.0f),
.Scale = glm::vec3(0.4f),
.Rotation = glm::vec4(0.0f, 1.0f, 0.3f, 0.5f),
.Color = glm::vec4(1.0f, 0.5f, 0.31f, 1.0f),
.IsSelected = false,
.Model = BackpackModel,
};
Scene_AddEntity(Scene, Backpack);
Finally, in the draw call we check if the entity_type is Model to draw the model.
void Renderer_DrawScene(const renderer &Renderer, const scene &Scene,
const context &Context) {
for (entity Entity : Scene.Entities) {
switch (Entity.Type) {
...
case entity_type::Model:
Renderer_DrawModel(Renderer, Entity.Position, Entity.Scale,
Entity.Rotation, Entity.Color, Entity.Model,
Entity.IsSelected);
}
}
}