Skip to content

OBJ file format

Romain Milbert edited this page Nov 10, 2024 · 6 revisions

Meshes from all available formats can be loaded with Raz::MeshFormat::load() and saved with Raz::MeshFormat::save(). OBJ meshes specifically can be imported using Raz::ObjFormat::load(), which returns both a Mesh and a MeshRenderer, and exported with Raz::ObjFormat::save(), both from RaZ/Data/ObjFormat.hpp.

#include <RaZ/Data/ObjFormat.hpp>

auto [mesh, meshRenderer] = Raz::ObjFormat::load("path/to/mesh.obj"); // Or Raz::MeshFormat::load(...)

// Adding the Mesh component is purely optional, unless you want to manipulate its data
// If you want to render the mesh, the MeshRenderer is however needed
entity.addComponent<Raz::Mesh>(std::move(mesh));
entity.addComponent<Raz::MeshRenderer>(std::move(meshRenderer));

Regarding the MTL & textures import, they will be found relatively to the file. As such, writing mtllib test.mtl to reference the MTL file available in the same directory as the OBJ you're importing will work just fine. In the same way, if you want to reference in your MTL file the textures available in a textures/ directory, writing map_Kd textures/diffuse.png is the way to go.


RaZ handles some unofficial PBR tags for MTL files, as stated on Wikipedia:

Pm/map_Pm     # metallic factor/metallic texture
Pr/map_Pr     # roughness factor/roughness texture
Ke/map_Ke     # emissive factor/emissive texture
norm          # normal map, same format as bump map

A material will be considered PBR (and therefore should use the Cook-Torrance shader) if it contains at least one of the tags Pm, map_Pm, Pr, map_Pr. If none of those is stated, submeshes associated with the material will be rendered with the Blinn-Phong shader.

Clone this wiki locally