libdonut  2.3.2
Application framework for cross-platform game development in C++20
obj.hpp
Go to the documentation of this file.
1 #ifndef DONUT_OBJ_HPP
2 #define DONUT_OBJ_HPP
3 
4 #include <donut/math.hpp>
5 
6 #include <cstddef> // std::size_t
7 #include <cstdint> // std::uint8_t, std::uint32_t
8 #include <stdexcept> // std::runtime_error
9 #include <string> // std::string
10 #include <string_view> // std::string_view
11 #include <vector> // std::vector
12 
13 namespace donut::obj {
14 
18 struct Error : std::runtime_error {
22  std::string_view::iterator position;
23 
27  std::size_t lineNumber;
28 
29  Error(const std::string& message, std::string_view::iterator position, std::size_t lineNumber)
30  : std::runtime_error(message)
33 
34  Error(const char* message, std::string_view::iterator position, std::size_t lineNumber)
35  : std::runtime_error(message)
38 };
39 
43 struct FaceVertex {
44  std::uint32_t vertexIndex = 0;
45  std::uint32_t textureCoordinateIndex = 0;
46  std::uint32_t normalIndex = 0;
47 };
48 
52 struct Face {
53  std::vector<FaceVertex> vertices{};
54 };
55 
59 struct Group {
60  std::string name{};
61  std::vector<Face> faces{};
62  std::string materialName{};
63 };
64 
68 struct Object {
69  std::string name{};
70  std::vector<Group> groups{};
71 };
72 
76 struct Scene {
87  [[nodiscard]] static Scene parse(std::string_view objString);
88 
89  std::vector<std::string> materialLibraryFilenames{};
90  std::vector<vec3> vertices{};
91  std::vector<vec2> textureCoordinates{};
92  std::vector<vec3> normals{};
93  std::vector<Object> objects{};
94 };
95 
96 namespace mtl {
97 
101 enum class IlluminationModel : std::uint8_t {
102  FLAT = 0,
103  LAMBERT = 1,
104  BLINN_PHONG = 2,
110  BLINN_PHONG_REFLECT = 8,
112  SHADOW = 10,
113 };
114 
118 struct Material {
119  std::string name{};
120  std::string ambientMapName{};
121  std::string diffuseMapName{};
122  std::string specularMapName{};
123  std::string emissiveMapName{};
124  std::string specularExponentMapName{};
125  std::string dissolveFactorMapName{};
126  std::string bumpMapName{};
127  vec3 ambientColor{1.0f, 1.0f, 1.0f};
128  vec3 diffuseColor{1.0f, 1.0f, 1.0f};
129  vec3 specularColor{1.0f, 1.0f, 1.0f};
130  vec3 emissiveColor{0.0f, 0.0f, 0.0f};
131  float specularExponent = 1.0f;
132  float dissolveFactor = 0.0f;
134 };
135 
140 struct Library {
151  [[nodiscard]] static Library parse(std::string_view mtlString);
152 
153  std::vector<Material> materials{};
154 };
155 
156 } // namespace mtl
157 } // namespace donut::obj
158 
159 #endif
IlluminationModel
Illumination model to use when rendering a specific Material.
Definition: obj.hpp:101
@ BLINN_PHONG_RAYTRACE_FRESNEL
Implementation-defined.
@ LAMBERT
Implementation-defined.
@ BLINN_PHONG_RAYTRACE_GLASS
Implementation-defined.
@ BLINN_PHONG_RAYTRACE_REFRACT_FRESNEL
Implementation-defined.
@ BLINN_PHONG_REFLECT
Implementation-defined.
@ BLINN_PHONG_REFLECT_GLASS
Implementation-defined.
@ SHADOW
Implementation-defined.
@ BLINN_PHONG_RAYTRACE
Implementation-defined.
@ BLINN_PHONG_RAYTRACE_REFRACT
Implementation-defined.
@ BLINN_PHONG
Implementation-defined.
@ FLAT
Implementation-defined.
Definition: utilities.hpp:114
Exception type for errors originating from the OBJ API.
Definition: obj.hpp:18
std::size_t lineNumber
Line number, starting at 1 for the first line, where the error occured.
Definition: obj.hpp:27
Error(const std::string &message, std::string_view::iterator position, std::size_t lineNumber)
Definition: obj.hpp:29
std::string_view::iterator position
Iterator into the source OBJ string where the error originated from.
Definition: obj.hpp:22
Error(const char *message, std::string_view::iterator position, std::size_t lineNumber)
Definition: obj.hpp:34
Single vertex of a polygonal Face element.
Definition: obj.hpp:43
std::uint32_t textureCoordinateIndex
Index of the texture coordinates in the Scene that define the texture coordinates of the vertex.
Definition: obj.hpp:45
std::uint32_t normalIndex
Index of the normal vector in the Scene that define the vertex normal.
Definition: obj.hpp:46
std::uint32_t vertexIndex
Index of the vertex coordinates in the Scene that define the vertex position.
Definition: obj.hpp:44
Face element forming a single polygon of FaceVertex vertices.
Definition: obj.hpp:52
std::vector< FaceVertex > vertices
List of vertices that make up the polygon.
Definition: obj.hpp:53
Group containing polygonal Face elements within an Object.
Definition: obj.hpp:59
std::string materialName
Name of the material of this group, which should be found in one of the associated material libraries...
Definition: obj.hpp:62
std::string name
Name of the group, or empty if no name was specified.
Definition: obj.hpp:60
std::vector< Face > faces
List of faces belonging to this group.
Definition: obj.hpp:61
Object mesh containing Group elements within a Scene.
Definition: obj.hpp:68
std::string name
Name of the object, or empty if no name was specified.
Definition: obj.hpp:69
std::vector< Group > groups
List of groups belonging to this object.
Definition: obj.hpp:70
Scene of Object elements defined by an OBJ file.
Definition: obj.hpp:76
std::vector< vec2 > textureCoordinates
List of texture coordinates referenced by the face vertices defined in this scene.
Definition: obj.hpp:91
std::vector< vec3 > normals
List of normal vectors referenced by the face vertices defined in this scene.
Definition: obj.hpp:92
static Scene parse(std::string_view objString)
Parse a scene from an OBJ string.
std::vector< Object > objects
List of objects belonging to this scene.
Definition: obj.hpp:93
std::vector< std::string > materialLibraryFilenames
List of relative filepaths of the material libraries associated with this scene.
Definition: obj.hpp:89
std::vector< vec3 > vertices
List of vertex positions referenced by the face vertices defined in this scene.
Definition: obj.hpp:90
Material library that stores the material properties for objects defined in a Scene.
Definition: obj.hpp:140
std::vector< Material > materials
List of materials belonging to this library.
Definition: obj.hpp:153
static Library parse(std::string_view mtlString)
Parse a material library from an MTL string.
Material properties of an Object.
Definition: obj.hpp:118
vec3 diffuseColor
Diffuse color factor to multiply the sampled diffuse map value by.
Definition: obj.hpp:128
std::string specularExponentMapName
Relative filepath of the specular exponent map image, or empty for no specular exponent map.
Definition: obj.hpp:124
vec3 ambientColor
Ambient color factor to multiply the sampled ambient map value by.
Definition: obj.hpp:127
float dissolveFactor
Dissolve factor to multiply the sampled dissolve factor map value by.
Definition: obj.hpp:132
float specularExponent
Specular exponent factor to multiply the sampled specular exponent map value by.
Definition: obj.hpp:131
vec3 specularColor
Specular color factor to multiply the sampled specular map value by.
Definition: obj.hpp:129
IlluminationModel illuminationModel
Illumination model to use for rendering this material.
Definition: obj.hpp:133
std::string ambientMapName
Relative filepath of the ambient map image, or empty for no ambient map.
Definition: obj.hpp:120
std::string specularMapName
Relative filepath of the specular map image, or empty for no specular map.
Definition: obj.hpp:122
std::string diffuseMapName
Relative filepath of the diffuse map image, or empty for no diffuse map.
Definition: obj.hpp:121
std::string bumpMapName
Relative filepath of the bump/normal map image, or empty for no bump/normal map.
Definition: obj.hpp:126
std::string emissiveMapName
Relative filepath of the emissive map image, or empty for no emissive map.
Definition: obj.hpp:123
std::string dissolveFactorMapName
Relative filepath of the dissolve factor map image, or empty for no dissolve factor map.
Definition: obj.hpp:125
std::string name
Name of the material.
Definition: obj.hpp:119
vec3 emissiveColor
Emissive color factor to multiply the sampled emissive map value by.
Definition: obj.hpp:130