Mandrill 2025.6.0
Loading...
Searching...
No Matches
Scene.h
1#pragma once
2
3#include "Common.h"
4
5#include "AccelerationStructure.h"
6#include "Camera.h"
7#include "Descriptor.h"
8#include "Device.h"
9#include "Layout.h"
10#include "Sampler.h"
11#include "Swapchain.h"
12#include "Texture.h"
13
14namespace Mandrill
15{
16 struct Vertex {
17 alignas(16) glm::vec3 position; // Position of vertex in 3D space
18 alignas(16) glm::vec3 normal; // Normal vector of vertex
19 alignas(16) glm::vec2 texcoord; // Texture coordinates
20 alignas(16) glm::vec3 tangent; // Tangent vector for normal mapping
21 alignas(16) glm::vec3 binormal; // Binormal vector for normal mapping
22
28 bool operator==(const Vertex& other) const
29 {
30 return position == other.position && normal == other.normal && texcoord == other.texcoord &&
31 tangent == other.tangent && binormal == other.binormal;
32 }
33 };
34
35 struct Mesh {
36 std::vector<Vertex> vertices;
37 std::vector<uint32_t> indices;
38 uint32_t materialIndex{};
39
40 // Device offset are set when uploading to device
41 VkDeviceSize deviceVerticesOffset{};
42 VkDeviceSize deviceIndicesOffset{};
43 };
44
45 struct alignas(16) MaterialParams {
46 glm::vec3 diffuse;
47 float shininess;
48 glm::vec3 specular;
49 float indexOfRefraction;
50 glm::vec3 ambient;
51 float opacity;
52 glm::vec3 emission;
53 uint32_t hasTexture;
54 };
55
56 struct alignas(16) MaterialDevice {
57 MaterialParams params;
58 uint32_t diffuseTextureIndex;
59 uint32_t specularTextureIndex;
60 uint32_t ambientTextureIndex;
61 uint32_t emissionTextureIndex;
62 uint32_t normalTextureIndex;
63 };
64
65 struct Material {
66 MaterialParams params{};
67 MaterialParams* paramsDevice{}; // This is set during Scene::compile()
68 VkDeviceSize paramsOffset{}; // This is set during Scene::compile()
69
70 std::string diffuseTexturePath;
71 std::string specularTexturePath;
72 std::string ambientTexturePath;
73 std::string emissionTexturePath;
74 std::string normalTexturePath;
75
76 ptr<Descriptor> pDescriptor;
77 };
78
79 struct InstanceData {
80 uint32_t verticesOffset; // Offset into global vertex buffer
81 uint32_t indicesOffset; // Offset into global index buffer
82 };
83
84 class Scene; // Forward declare scene so Node can befriend it
85 class Pipeline;
86
90 class Node
91 {
92 public:
97 MANDRILL_API Node();
98
103 MANDRILL_API ~Node();
104
112 MANDRILL_API void drawMeshes(VkCommandBuffer cmd, const ptr<const Scene> pScene) const;
113
121 MANDRILL_API void render(VkCommandBuffer cmd, const ptr<Camera> pCamera, const ptr<const Scene> pScene) const;
122
128 MANDRILL_API void addMesh(uint32_t meshIndex)
129 {
130 mMeshIndices.push_back(meshIndex);
131 }
132
138 MANDRILL_API void setPipeline(ptr<Pipeline> pPipeline)
139 {
140 mpPipeline = pPipeline;
141 }
142
147 MANDRILL_API glm::mat4 getTransform() const
148 {
149 return mTransform;
150 }
151
157 MANDRILL_API void setTransform(glm::mat4 transform)
158 {
159 mTransform = transform;
160 }
161
167 MANDRILL_API void setVisible(bool visible)
168 {
169 mVisible = visible;
170 }
171
176 MANDRILL_API bool getVisible() const
177 {
178 return mVisible;
179 }
180
185 MANDRILL_API std::vector<uint32_t>& getMeshIndices()
186 {
187 return mMeshIndices;
188 }
189
190 private:
191 friend Scene;
192
193 ptr<Pipeline> mpPipeline;
194
195 std::vector<uint32_t> mMeshIndices;
196
197 glm::mat4 mTransform;
198 glm::mat4* mpTransformDevice;
199 ptr<Descriptor> pDescriptor;
200 std::vector<VkDescriptorSet> mDescriptors;
201
202 bool mVisible;
203
204 std::vector<Node*> mChildren;
205 };
206
210 class Scene : public std::enable_shared_from_this<Scene>
211 {
212 public:
213 MANDRILL_NON_COPYABLE(Scene)
214
215
246 MANDRILL_API Scene(ptr<Device> pDevice, ptr<Swapchain> pSwapchain, bool supportRayTracing = false);
247
252 MANDRILL_API ~Scene();
253
260 MANDRILL_API void render(VkCommandBuffer cmd, const ptr<Camera> pCamera) const;
261
266 MANDRILL_API ptr<Node> addNode();
267
273 MANDRILL_API uint32_t addMaterial(Material material);
274
282 MANDRILL_API uint32_t addMesh(const std::vector<Vertex> vertices, const std::vector<uint32_t> indices,
283 uint32_t materialIndex);
284
292 MANDRILL_API std::vector<uint32_t> addMeshFromFile(const std::filesystem::path& path,
293 const std::filesystem::path& materialPath = "");
294
299 MANDRILL_API void compile();
300
312 MANDRILL_API void syncToDevice();
313
321 MANDRILL_API void updateAccelerationStructure(
322 VkBuildAccelerationStructureFlagsKHR flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR);
323
333 MANDRILL_API void bindRayTracingDescriptors(VkCommandBuffer cmd, ptr<Camera> pCamera, VkPipelineLayout layout);
334
340 MANDRILL_API void setSampler(const ptr<Sampler> pSampler);
341
370 MANDRILL_API ptr<Layout> getLayout();
371
376 MANDRILL_API std::vector<Node>& getNodes()
377 {
378 return mNodes;
379 }
380
385 MANDRILL_API uint32_t getVertexCount() const
386 {
387 return mVertexCount;
388 }
389
394 MANDRILL_API uint32_t getIndexCount() const
395 {
396 return mIndexCount;
397 }
398
403 MANDRILL_API uint32_t getMeshCount()
404 {
405 return count(mMeshes);
406 }
407
412 MANDRILL_API uint32_t getMaterialCount() const
413 {
414 return count(mMaterials);
415 }
416
421 MANDRILL_API uint32_t getTextureCount() const
422 {
423 return count(mTextures);
424 }
425
431 MANDRILL_API uint32_t getMeshVertexCount(uint32_t meshIndex) const
432 {
433 return count(mMeshes[meshIndex].vertices);
434 }
435
441 MANDRILL_API uint32_t getMeshIndexCount(uint32_t meshIndex) const
442 {
443 return count(mMeshes[meshIndex].indices);
444 }
445
451 MANDRILL_API VkDeviceAddress getMeshVertexAddress(uint32_t meshIndex) const
452 {
453 return mpVertexBuffer->getDeviceAddress() + mMeshes[meshIndex].deviceVerticesOffset;
454 }
455
461 MANDRILL_API VkDeviceAddress getMeshIndexAddress(uint32_t meshIndex) const
462 {
463 return mpIndexBuffer->getDeviceAddress() + mMeshes[meshIndex].deviceIndicesOffset;
464 }
465
471 MANDRILL_API uint32_t getMeshMaterialIndex(uint32_t meshIndex) const
472 {
473 return mMeshes[meshIndex].materialIndex;
474 }
475
480 MANDRILL_API ptr<AccelerationStructure> getAccelerationStructure() const
481 {
482 return mpAccelerationStructure;
483 }
484
490 MANDRILL_API void setEnvironmentMap(ptr<Texture> pTexture)
491 {
492 mpEnvironmentMap = pTexture;
493 }
494
495 private:
496 friend Node;
497
498 std::vector<uint32_t> loadFromOBJ(const std::filesystem::path& path,
499 const std::filesystem::path& materialPath = "");
500 std::vector<uint32_t> loadFromGLTF(const std::filesystem::path& path,
501 const std::filesystem::path& materialPath = "");
502 void addTexture(std::string texturePath);
503 void createDescriptors();
504
505 ptr<Device> mpDevice;
506 ptr<Swapchain> mpSwapchain;
507
508 std::vector<Mesh> mMeshes;
509 std::vector<Node> mNodes;
510 std::vector<Material> mMaterials;
511 std::unordered_map<std::string, ptr<Texture>> mTextures;
512 ptr<Texture> mpEnvironmentMap;
513 ptr<Descriptor> mpEnvironmentMapDescriptor;
514
515 ptr<Buffer> mpVertexBuffer;
516 ptr<Buffer> mpIndexBuffer;
517 ptr<Buffer> mpTransforms;
518 ptr<Buffer> mpMaterialParams;
519
520 ptr<Texture> mpMissingTexture;
521
522 bool mSupportRayTracing;
523 ptr<AccelerationStructure> mpAccelerationStructure;
524 ptr<Descriptor> mpRayTracingDescriptor;
525 ptr<Buffer> mpMaterialBuffer; // Almost same as mpMaterialParams but for ray tracing
526 ptr<Buffer> mpInstanceDataBuffer;
527
528 uint32_t mVertexCount;
529 uint32_t mIndexCount;
530 };
531}; // namespace Mandrill
Scene node class for managing a single node in a scene graph. This class can hold meshes and transfor...
Definition Scene.h:91
MANDRILL_API void addMesh(uint32_t meshIndex)
Add a mesh to the node.
Definition Scene.h:128
MANDRILL_API void render(VkCommandBuffer cmd, const ptr< Camera > pCamera, const ptr< const Scene > pScene) const
Render a node in the scene.
Definition Scene.cpp:48
MANDRILL_API glm::mat4 getTransform() const
Get the TRS transform of the node.
Definition Scene.h:147
MANDRILL_API void setTransform(glm::mat4 transform)
Set the TRS transform of the node.
Definition Scene.h:157
MANDRILL_API void setVisible(bool visible)
Set weather the node should be rendered or not.
Definition Scene.h:167
MANDRILL_API void setPipeline(ptr< Pipeline > pPipeline)
Set pipeline to use when rendering node.
Definition Scene.h:138
MANDRILL_API ~Node()
Destructor for scene node.
Definition Scene.cpp:28
MANDRILL_API Node()
Create a new scene node.
Definition Scene.cpp:21
MANDRILL_API void drawMeshes(VkCommandBuffer cmd, const ptr< const Scene > pScene) const
Bind the vertex and index buffers of the node's meshes and draw them. Use this instead of Node::rende...
Definition Scene.cpp:32
MANDRILL_API bool getVisible() const
Get the visibility of the node.
Definition Scene.h:176
MANDRILL_API std::vector< uint32_t > & getMeshIndices()
Get the mesh indices.
Definition Scene.h:185
Scene class that manages a collection of nodes, materials, meshes, and rendering operations.
Definition Scene.h:211
MANDRILL_API ptr< AccelerationStructure > getAccelerationStructure() const
Get the acceleration structure of the scene.
Definition Scene.h:480
MANDRILL_API VkDeviceAddress getMeshIndexAddress(uint32_t meshIndex) const
Get the address of a mesh's indices buffer.
Definition Scene.h:461
MANDRILL_API void updateAccelerationStructure(VkBuildAccelerationStructureFlagsKHR flags=VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR)
Build and update acceleration structure of the scene. This is needed for ray tracing the scene....
Definition Scene.cpp:344
MANDRILL_API std::vector< uint32_t > addMeshFromFile(const std::filesystem::path &path, const std::filesystem::path &materialPath="")
Add several meshes to a scene by reading them from an OBJ-file.
Definition Scene.cpp:182
MANDRILL_API ~Scene()
Destructor for scene.
Definition Scene.cpp:100
MANDRILL_API uint32_t getTextureCount() const
Get the number of textures in the scene.
Definition Scene.h:421
MANDRILL_API void setEnvironmentMap(ptr< Texture > pTexture)
Set an environment map for the scene.
Definition Scene.h:490
MANDRILL_API std::vector< Node > & getNodes()
Get the list of all nodes in the scene.
Definition Scene.h:376
MANDRILL_API void compile()
Calculate sizes of buffers and allocate resources. Call this after all nodes have been added.
Definition Scene.cpp:207
MANDRILL_API uint32_t getMeshMaterialIndex(uint32_t meshIndex) const
Get the matieral index of a mesh.
Definition Scene.h:471
MANDRILL_API uint32_t getIndexCount() const
Get the number of indices in the scene.
Definition Scene.h:394
MANDRILL_API void setSampler(const ptr< Sampler > pSampler)
Set sampler to use for rendering materials.
Definition Scene.cpp:379
MANDRILL_API uint32_t getMeshVertexCount(uint32_t meshIndex) const
Get the number of vertices in a mesh.
Definition Scene.h:431
MANDRILL_API void bindRayTracingDescriptors(VkCommandBuffer cmd, ptr< Camera > pCamera, VkPipelineLayout layout)
Bind descriptors for ray tracing.
Definition Scene.cpp:365
MANDRILL_API ptr< Node > addNode()
Add a node to the scene.
Definition Scene.cpp:111
MANDRILL_API ptr< Layout > getLayout()
Get the layout used by the scene (as described in the comments for the Scene() constructor above).
Definition Scene.cpp:388
MANDRILL_API void syncToDevice()
Synchronize buffers to device.
Definition Scene.cpp:315
MANDRILL_API uint32_t addMaterial(Material material)
Add a material to the scene.
Definition Scene.cpp:120
MANDRILL_API uint32_t addMesh(const std::vector< Vertex > vertices, const std::vector< uint32_t > indices, uint32_t materialIndex)
Add a mesh to the scene.
Definition Scene.cpp:144
MANDRILL_API uint32_t getMeshIndexCount(uint32_t meshIndex) const
Get the number of indices in a mesh.
Definition Scene.h:441
MANDRILL_API void render(VkCommandBuffer cmd, const ptr< Camera > pCamera) const
Render all the nodes in the scene.
Definition Scene.cpp:104
MANDRILL_API uint32_t getMaterialCount() const
Get the number of materials in the scene.
Definition Scene.h:412
MANDRILL_API uint32_t getMeshCount()
Get the number of meshes in the scene.
Definition Scene.h:403
MANDRILL_API uint32_t getVertexCount() const
Get the number of vertices in the scene.
Definition Scene.h:385
MANDRILL_API VkDeviceAddress getMeshVertexAddress(uint32_t meshIndex) const
Get the address of a mesh's vertices buffer.
Definition Scene.h:451