Scene & Objects
Before starting to draw things, I want to define a couple more structures that will make the code a bit more organized. I want to define a container for any type of object that will be drawn into the screen. Additionally, I want to also define a “Scene” where all the objects will be positioned.
The Entity
The entity will be any type of object that I would like to draw. Here I define 3 different variants
of entity, a Triangle, a Rectangle (a.k.a Quad), and a Cube. They all have a 3d position, scale, rotation,
and color.
#ifndef ENTITY_H_
#define ENTITY_H_
#include <glm/glm.hpp>
enum class entity_type {
Triangle,
Quad,
Cube,
};
struct entity {
entity_type Type;
glm::vec3 Position;
glm::vec3 Scale;
glm::vec4 Rotation;
glm::vec4 Color;
};
#endif
The Scene
All the entities will be contained in this scene struct. What I want to achieve with this
at the end is to perform 1 draw call with the scene and the renderer will know how to draw
the different objects that belong to the Scene.
#ifndef SCENE_H_
#define SCENE_H_
#include <vector>
#include "entity.h"
struct scene {
std::vector<entity> Entities;
};
scene Scene_Create();
void Scene_Destroy(scene &Scene);
void Scene_AddEntity(scene &Scene, entity &Entity);
#endif
Drawing the Scene
I will add a new method to the renderer to draw the scene. For now it will be empty but I will
implement it as I progress in this series.
In renderer.h:
void Renderer_DrawScene(const renderer &Renderer, const scene &Scene);
And in renderer.cpp:
void Renderer_DrawScene(const renderer &Renderer, const scene &Scene) {
// TODO: Implement
}
Creating the Scene
Now, in main.cpp I can create a scene and add entities to it.
int main() {
...
scene Scene = Scene_Create();
entity Triangle = {
.Type = entity_type::Triangle,
.Position = glm::vec3(0.0f, 0.0f, 0.0f),
.Scale = glm::vec3(0.0f),
.Rotation = glm::vec4(0.0f, 1.0f, 0.3f, 0.5f),
.Color = glm::vec4(1.0f, 0.5f, 0.31f, 1.0f),
};
Scene_AddEntity(Scene, Triangle);
...
while (!glfwWindowShouldClose(Context.Window)) {
...
Renderer_ClearBackground(0.1f, 0.1f, 0.1f, 1.0f);
Renderer_DrawScene(Renderer, Scene);
}
}
I’m ready to start drawing things on the screen!