Object Mesh
I want to define a structure that holds the information of the geometry of the 3D object. Something like a blueprint to draw as many objects of that geometry. Normally, this type of structure is called a Mesh. It contains all the information that defines the shape of the 3D object.
Quad Mesh
A Quad is a rectangular object that is flat in one of the 3D axes. Think of it as a plane. The Quad is nothing more that 2 triangles together and can be expressed with 6 vertices.
// 6 vertices with size 3 each
float vertices[6*3] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
};
And the mesh object would look something like this:
struct QuadMesh {
float vertices[18];
uint32_t numVertices;
uint32_t vertexSize;
};
// Function that constructs a QuadMesh
QuadMesh CreateQuadMesh() {
QuadMesh mesh = {
.vertices{
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
},
.numVertices = 6,
.vertexSize = 3,
};
return mesh;
}
Here I have also created a constructor function that creates a QuadMesh.
I also want to add a function that draws a QuadMesh. This function will call the Renderer_DrawTriangles method that we have been using.
void Renderer_DrawQuad(Renderer *r, Vec3 position, Vec3 rotation, Vec3 scale,
ColorRGBA color) {
if (r == nullptr) {
return;
}
QuadMesh mesh = CreateQuadMesh();
Renderer_DrawTriangles(r, mesh.vertices, mesh.numVertices, mesh.vertexSize,
position, rotation, scale, color);
}
Cube Mesh
I can also do the same for a 3D Cube object. A cube is composed of 6 faces (Quads).
CubeMesh CreateCubeMesh() {
CubeMesh mesh = {
.vertices{
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
},
.numVertices = 36,
.vertexSize = 3,
};
return mesh;
}
And the function that draws a cube:
void Renderer_DrawCube(Renderer *r, Vec3 position, Vec3 rotation, Vec3 scale,
ColorRGBA color) {
if (r == nullptr) {
return;
}
CubeMesh mesh = CreateCubeMesh();
Renderer_DrawTriangles(r, mesh.vertices, mesh.numVertices, mesh.vertexSize,
position, rotation, scale, color);
}