Skip to content

Import a mesh

Romain Milbert edited this page Dec 31, 2023 · 17 revisions

Importing a mesh can either be done using a specific format loading function, or Raz::MeshFormat::load(), available in RaZ/Data/MeshFormat.hpp. The latter automatically chooses the format from the file extension.

#include <RaZ/Data/Mesh.hpp>
#include <RaZ/Data/MeshFormat.hpp>
#include <RaZ/Render/MeshRenderer.hpp>

// An Application & a World must have been created
// If not, refer to https://github.com/Razakhel/RaZ/wiki/Setting-up-a-project

Raz::Entity& mesh = world.addEntity();

// To import a mesh from a file, Raz::MeshFormat can be used. This returns
//  two components which each have their own use
// Note that adding both is not strictly required. Add the Mesh only if you want
//  to manipulate its data, and/or the MeshRenderer only if you want to render it
auto [mesh, meshRenderer] = Raz::MeshFormat::load("cube.obj");
mesh.addComponent<Raz::Mesh>(std::move(mesh));
mesh.addComponent<Raz::MeshRenderer>(std::move(meshRenderer));

// If you want it to be rendered, don't forget to add a Transform component
mesh.addComponent<Raz::Transform>(); // Default position is [0, 0, 0]

NB: As of now, RaZ can only import meshes from glTF, GLB, OBJ, FBX & OFF files.

Clone this wiki locally