Meshes
Meshes are fundamental components in Raftel Engine that represent 3D geometry in your scenes. The mesh system handles loading, rendering, and manipulation of 3D models, supporting various file formats and providing tools for creating simple geometric shapes programmatically.
Key Features
- Support for loading models from OBJ, FBX, DAE, STL, GLB and GLTF formats
- Built-in primitive shape generation (cube, sphere, plane, cone, terrain)
- Multi-threaded mesh loading for performance
- Material assignment and texture mapping
- Submesh handling for complex models
- Automatic normal calculation
Architecture Overview
The mesh system in Raftel Engine consists of three main components:
Vertex Structure
The Vertex
structure represents a single vertex in 3D space, containing position, normal,
and texture coordinate attributes. Vertices are the building blocks of a mesh and are used to define its
shape and surface properties.
Mesh Class
The Mesh
class manages a collection of vertices and indices (defining triangles) along with
associated materials. It handles loading mesh data from files, setting up OpenGL buffers (VAO, VBO, EBO),
and rendering the mesh with specified shaders.
MeshFactory Class
The MeshFactory
class provides factory methods for creating common geometric shapes
(cube, sphere, plane, cone, terrain) without requiring external model files. This is useful for
prototyping and creating simple objects.
+----------------+ +----------------+
| Vertex | | Material |
+----------------+ +----------------+
| - position | | - textures |
| - normal |<---------+ | - colors |
| - texCoord | | | - properties |
+----------------+ | +----------------+
|
| +----------------+
+----------------+ | | Engine |
| MeshFactory | | +----------------+
+----------------+ | | - meshes |
| - createCube() | | +----------------+
| - createSphere()| |
| - createPlane()| |
| - createCone() | |
| - createTerrain()| |
+----------------+ |
| |
v |
+----------------+ |
| Mesh |----------+
+----------------+
| - submeshVertex |
| - submeshIndices|
| - materials |
| - VAOs, VBOs, EBOs |
+----------------+
| + loadMesh() |
| + draw() |
| + setupMesh() |
+----------------+
Basic Usage
Here are some examples of how to use the Mesh system in Raftel Engine:
Loading a 3D Model
#include "raftel/mesh.hpp"
#include "raftel/shader.hpp"
int main() {
// Create a window and initialize OpenGL context (see Window System documentation)
// Load a mesh from a file
auto modelMesh = Raftel::Mesh::Create("assets/models/character.obj", true);
// Create and set up a shader program
auto shader = Raftel::ShaderProgram::Create("shaders/default.vert", "shaders/default.frag");
// Create a material and assign it to the mesh
auto material = std::make_shared();
material->setAlbedoColor(glm::vec3(0.8f, 0.2f, 0.2f)); // Red color
modelMesh->addMaterial(material);
// Main rendering loop
while (!window->ShouldClose()) {
window->clear();
shader->use();
// Set up camera and transformation matrices...
// Draw the mesh
modelMesh->draw(*shader);
window->swapBuffers();
}
return 0;
}
Creating a Primitive Shape
#include "raftel/mesh.hpp"
#include "raftel/shader.hpp"
int main() {
// Create a window and initialize OpenGL context
// Create a sphere with radius 2.0 and 32 segments
auto sphereMesh = Raftel::MeshFactory::createSphere(2.0f, 32);
// Create and set up a shader program
auto shader = Raftel::ShaderProgram::Create("shaders/default.vert", "shaders/default.frag");
// Create a material with a texture
auto material = std::make_shared();
auto texture = Raftel::Texture::loadTexture("assets/textures/earth.jpg");
material->setAlbedo(texture);
// Assign the material to the sphere mesh
sphereMesh->setMaterial(material);
// Main rendering loop
while (!window->ShouldClose()) {
window->clear();
shader->use();
// Set up camera and transformation matrices...
// Draw the sphere
sphereMesh->draw(*shader);
window->swapBuffers();
}
return 0;
}
Vertex Structure Reference
Represents a 3D vertex with attributes like position, normal, and texture coordinates.
Attributes
glm::vec3 position
- The position of the vertex in 3D space.glm::vec3 normal
- The normal vector of the vertex for lighting calculations.glm::vec2 texCoord
- The texture coordinates of the vertex.
Mesh Class Reference
Constructors
Default constructor for the Mesh class.
Constructs a mesh by loading from a file.
Parameters:
filePath
- Path to the mesh file (OBJ, FBX, DAE, STL, GLB, GLTF)
Constructs a mesh with provided vertices, indices, and materials.
Parameters:
verticesPerSubmesh
- Vector of vertex arrays for each submeshindicesPerSubmesh
- Vector of index arrays for each submeshmats
- Vector of materials
Static Factory Methods
Static factory method to create a mesh from a file.
Parameters:
filePath
- Path to the mesh filemultithread
- Whether to load the mesh in a separate thread
Returns: A shared pointer to the created mesh
Mesh Management Methods
Loads a mesh from a file.
Parameters:
filePath
- Path to the mesh file
Returns: True if the mesh was loaded successfully, false otherwise
Sets up OpenGL buffers (VAO, VBO, EBO) for the mesh. This method should be called after loading the mesh or creating it from scratch.
Computes the axis-aligned bounding box (AABB) of the mesh.
Parameters:
outMin
- Output parameter for the minimum corner of the bounding boxoutMax
- Output parameter for the maximum corner of the bounding box
Material Management Methods
Sets the materials for the mesh.
Parameters:
newMaterials
- Vector of materials to apply to the mesh
Sets a single material for the entire mesh.
Parameters:
mat
- Material to apply to the mesh
Adds a material to the mesh's material list.
Parameters:
mat
- Material to add
Retrieves a material by its index.
Parameters:
index
- The index of the material to retrieve
Returns: A shared pointer to the material at the given index
Retrieves all materials associated with the mesh.
Returns: A reference to the vector of materials
Rendering Methods
Renders the mesh using the specified shader program.
Parameters:
shader
- The shader program to use for rendering
Sets the model, view, and projection uniform variables for the shader program.
Parameters:
shaderProgramID
- The ID of the shader program
Renders the material properties using the specified shader.
Parameters:
shader
- The shader program to use for rendering the material
MeshFactory Class Reference
Creates a plane mesh with the specified dimensions.
Parameters:
width
- The width of the planeheight
- The height of the plane
Returns: A shared pointer to the created plane mesh
Creates a cube mesh with the specified size.
Parameters:
size
- The size of the cube (length of each edge)
Returns: A shared pointer to the created cube mesh
Creates a sphere mesh with the specified radius and number of segments.
Parameters:
radius
- The radius of the spheresegments
- The number of segments used to approximate the sphere's surface
Returns: A shared pointer to the created sphere mesh
Creates a cone mesh with the specified radius, height, and number of segments.
Parameters:
radius
- The radius of the base of the coneheight
- The height of the conesegments
- The number of segments used to approximate the circular base
Returns: A shared pointer to the created cone mesh
Creates a terrain mesh from a heightmap image.
Parameters:
heightMapFile
- Path to the heightmap imagesize
- The size of each terrain grid cellheightMultiplier
- Multiplier for height valuesisCentered
- Whether the terrain should be centered at (0,0,0)
Returns: A shared pointer to the created terrain mesh
Supported File Formats
Raftel Engine's mesh system supports the following file formats:
OBJ (.obj)
Wavefront OBJ files are handled by Raftel Engine's native parser, which supports vertices, normals, texture coordinates, and materials (MTL).
FBX (.fbx)
Autodesk FBX files are supported through the Assimp library, preserving mesh data, materials, and texture coordinates.
COLLADA (.dae)
COLLADA files are supported through the Assimp library, maintaining mesh structure and material assignments.
STL (.stl)
STL files (both ASCII and binary) are supported through the Assimp library, though they only contain geometry without material information.
GLTF (.gltf)
GLTF format is supported for efficient transmission and loading of 3D models in applications, maintaining a rich scene representation including animations and node hierarchies.
GLB (.glb)
GLB format is the binary version of GLTF, supported for efficient streaming and loading of 3D models with embedded resources such as textures and shaders, commonly used in AR and VR platforms.
Best Practices
- Use multi-threaded mesh loading (
Mesh::Create(path, true)
) for large models to avoid freezing the main thread. - Reuse meshes when possible to reduce memory usage and drawing overhead.
- Use the appropriate primitive from MeshFactory for simple shapes rather than loading model files.
- Consider using submeshes for complex models that need different materials applied to different parts.
- Assign materials before drawing a mesh to ensure proper rendering appearance.
- When loading terrain from heightmaps, normalize the height values and use an appropriate heightMultiplier to achieve the desired scale.
- Check for loading errors with
getLastError()
if a mesh fails to load correctly.
Advanced Features
Multi-Threaded Loading
For large models, Raftel Engine supports loading meshes in background threads to prevent freezing
the main thread. This is enabled by passing true
as the second parameter to
Mesh::Create()
. When using multi-threaded loading, the mesh will be loaded in the
background, but you'll need to call setupMesh()
on the main thread when loading is
complete.
Submeshes
Complex models often consist of multiple parts that need different materials. Raftel Engine handles this with a submesh system, where each submesh can have its own vertices, indices, and material. When loading models from files, submeshes are automatically created based on the model's structure and material assignments.
MTL Material Loading
When loading OBJ files, Raftel Engine automatically loads associated MTL material files and applies the materials to the appropriate submeshes. This preserves the material assignments from the original model.
Related Topics
Loading 3D Meshes Tutorial
Step-by-step guide to loading and rendering 3D meshes in Raftel Engine.
Materials
Learn about the material system used to control the appearance of meshes.
Shaders
Understanding shaders that define how meshes are rendered.
Textures
Learn about textures that can be applied to mesh materials.