libdonut  2.3.2
Application framework for cross-platform game development in C++20
Model.hpp
Go to the documentation of this file.
1 #ifndef DONUT_GRAPHICS_MODEL_HPP
2 #define DONUT_GRAPHICS_MODEL_HPP
3 
4 #include <donut/Filesystem.hpp>
7 #include <donut/math.hpp>
8 
9 #include <cstddef> // std::size_t
10 #include <vector> // std::vector
11 
12 namespace donut::graphics {
13 
14 class Renderer; // Forward declaration, to avoid a circular include of Renderer.hpp.
15 
20 struct Model {
27  struct Object {
34  struct Vertex {
35  vec3 position;
36  vec3 normal;
37  vec3 tangent;
38  vec3 bitangent;
40  };
41 
48  using Index = u32;
49 
56  struct Instance {
58  mat3 normalMatrix;
60  vec4 tintColor;
63  };
64 
68  struct Material {
73  vec3 diffuseColor;
75  vec3 normalScale;
80  };
81 
84 
87 
90 
93 
96 
97  static constexpr std::int32_t TEXTURE_UNIT_DIFFUSE = 0;
98  static constexpr std::int32_t TEXTURE_UNIT_SPECULAR = 1;
99  static constexpr std::int32_t TEXTURE_UNIT_NORMAL = 2;
100  static constexpr std::int32_t TEXTURE_UNIT_EMISSIVE = 3;
101  static constexpr std::int32_t TEXTURE_UNIT_COUNT = 4;
102 
107 
112 
116  std::size_t indexCount;
117  };
118 
126  static const Model* const QUAD;
127 
135  static const Model* const CUBE;
136 
142  explicit Model(std::vector<Object> objects) noexcept
143  : objects(std::move(objects)) {}
144 
164  Model(const Filesystem& filesystem, const char* filepath);
165 
169  std::vector<Object> objects;
170 
171 private:
172  friend Renderer;
173 
174  static void createSharedModels();
175  static void destroySharedModels() noexcept;
176 };
177 
178 } // namespace donut::graphics
179 
180 #endif
Persistent system for managing the virtual filesystem.
Definition: Filesystem.hpp:185
Generic abstraction of a GPU vertex array object and its associated buffers.
Definition: Mesh.hpp:226
Persistent system for rendering the batched draw commands of a RenderPass onto a Framebuffer,...
Definition: Renderer.hpp:38
Storage for multidimensional data, such as 2D images, on the GPU, combined with a sampler configurati...
Definition: Texture.hpp:75
Definition: Buffer.hpp:7
MeshBufferUsage
Hint to the graphics driver implementation regarding the intended access pattern of a particular GPU ...
Definition: Mesh.hpp:44
MeshPrimitiveType
Specification of which kind of graphical primitive is defined by an associated sequence of vertices i...
Definition: Mesh.hpp:60
@ TRIANGLES
Each consecutive triple of points forms an individual filled triangle.
MeshIndexType
Specification of which type of indices is used in the index buffer of a particular Mesh.
Definition: Mesh.hpp:73
@ U32
Unsigned 32-bit integer.
Data layout for the attributes of a single instance of the mesh.
Definition: Model.hpp:56
vec3 specularFactor
Specular factor to use when rendering.
Definition: Model.hpp:61
vec4 tintColor
Tint color to use when rendering.
Definition: Model.hpp:60
vec3 emissiveFactor
Emissive factor to use when rendering.
Definition: Model.hpp:62
mat4 transformation
Model transformation matrix.
Definition: Model.hpp:57
mat3 normalMatrix
Transposed 3x3 basis of the model transformation matrix.
Definition: Model.hpp:58
vec4 textureOffsetAndScale
Texture offset (xy) and texture scale (zw) to apply to the texture coordinates before sampling the te...
Definition: Model.hpp:59
Material attributes of the mesh.
Definition: Model.hpp:68
float dissolveFactor
Dissolve factor for transparency.
Definition: Model.hpp:78
Texture specularMap
Texture used for specular highlights.
Definition: Model.hpp:70
vec3 specularColor
Specular color.
Definition: Model.hpp:74
float specularExponent
Specular exponent for specular highlights.
Definition: Model.hpp:77
Texture normalMap
Texture used for normal mapping.
Definition: Model.hpp:71
vec3 normalScale
Normal map scale.
Definition: Model.hpp:75
Texture diffuseMap
Texture used for the base color.
Definition: Model.hpp:69
vec3 emissiveColor
Emissive color.
Definition: Model.hpp:76
vec3 diffuseColor
Base color.
Definition: Model.hpp:73
float occlusionFactor
Occlusion factor.
Definition: Model.hpp:79
Texture emissiveMap
Texture used for emissive mapping.
Definition: Model.hpp:72
Data layout for the attributes of a single vertex of the mesh.
Definition: Model.hpp:34
vec3 tangent
Unit vector pointing in some direction along the vertex surface.
Definition: Model.hpp:37
vec3 position
Position relative to the model origin.
Definition: Model.hpp:35
vec3 bitangent
Unit vector that is the cross product of the normal and the tangent.
Definition: Model.hpp:38
vec2 textureCoordinates
Texture UV coordinates that map to this vertex.
Definition: Model.hpp:39
vec3 normal
Unit vector pointing away from the vertex surface.
Definition: Model.hpp:36
A single 3D mesh with an associated material.
Definition: Model.hpp:27
static constexpr MeshBufferUsage INDICES_USAGE
Hint regarding the intended memory access pattern of the index buffer.
Definition: Model.hpp:86
static constexpr std::int32_t TEXTURE_UNIT_SPECULAR
Texture unit index to use for the Material::specularMap.
Definition: Model.hpp:98
std::size_t indexCount
Number of indices stored in the index buffer of the mesh.
Definition: Model.hpp:116
static constexpr std::int32_t TEXTURE_UNIT_EMISSIVE
Texture unit index to use for the Material::emissiveMap.
Definition: Model.hpp:100
static constexpr std::int32_t TEXTURE_UNIT_COUNT
Total number of texture units required to render an object.
Definition: Model.hpp:101
static constexpr MeshBufferUsage VERTICES_USAGE
Hint regarding the intended memory access pattern of the vertex buffer.
Definition: Model.hpp:83
static constexpr std::int32_t TEXTURE_UNIT_NORMAL
Texture unit index to use for the Material::normalMap.
Definition: Model.hpp:99
Mesh< Vertex, Index, Instance > mesh
Mesh data stored on the GPU.
Definition: Model.hpp:106
static constexpr MeshIndexType INDEX_TYPE
Index type of the mesh indices.
Definition: Model.hpp:95
Material material
Material attributes.
Definition: Model.hpp:111
static constexpr MeshPrimitiveType PRIMITIVE_TYPE
The type of 3D primitives represented by the mesh vertices.
Definition: Model.hpp:92
u32 Index
Data type used in the index buffer of the mesh.
Definition: Model.hpp:48
static constexpr MeshBufferUsage INSTANCES_USAGE
Hint regarding the intended memory access pattern of the instance buffer.
Definition: Model.hpp:89
static constexpr std::int32_t TEXTURE_UNIT_DIFFUSE
Texture unit index to use for the Material::diffuseMap.
Definition: Model.hpp:97
Container for a set of 3D triangle meshes stored on the GPU, combined with associated materials.
Definition: Model.hpp:20
Model(const Filesystem &filesystem, const char *filepath)
Load a model from a virtual file.
static const Model *const QUAD
Pointer to the statically allocated storage for the built-in quad model.
Definition: Model.hpp:126
std::vector< Object > objects
List of objects defined by the loaded model.
Definition: Model.hpp:169
static const Model *const CUBE
Pointer to the statically allocated storage for the built-in cube model.
Definition: Model.hpp:135
Model(std::vector< Object > objects) noexcept
Construct a model from a list of meshes.
Definition: Model.hpp:142