Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Mesh.cpp
Go to the documentation of this file.
1#include "Engine/Mesh/Mesh.h"
7#include "tiny_gltf.h"
8#include <cstring>
9#include <iostream>
10#include <plog/Log.h>
11
12namespace EngineCore
13{
15 : meshData(
16 GltfLoader::GltfMeshData( tinygltf::Model(), tinygltf::Mesh(), tinygltf::Node(), glm::mat4( 1 ) )
17 ) {
18
19 };
20
23 {
24 PLOGI << "=== Mesh CONSTRUCTOR STARTED ===";
25 PLOGI << "Mesh name: " << name;
26 PLOGI << "Mesh primitives count: " << meshData.getPrimitives().size();
27
28 for ( const auto & meshPrimitiveData : meshData.getPrimitives() )
29 {
30 PLOGI << "MeshPrimitive color texture ptr: "
31 << ( meshPrimitiveData.getColorTexturePtr() ? " is Valid" : " is Invalid" );
32 }
33
34 PLOGI << "=== Mesh CONSTRUCTOR FINISHED ===";
35 }
36
38 {
39 TRACY_ZONE_SCOPED_NAMED( "Create mesh resources" );
40 PLOGI << "=== Create Mesh resources ===";
41 PLOGI << "Mesh name: " << name;
42 meshPrimitives.resize( meshData.getPrimitives().size() );
43 const uint32_t meshPrimitiveCount = meshData.getPrimitives().size();
44 PLOGI << "Creating " << meshPrimitiveCount << " mesh primitives";
45
46 for ( uint32_t i = 0; i < meshPrimitiveCount; i++ )
47 {
48 PLOGI << "=== Creating mesh primitive " << i + 1 << " of " << meshPrimitiveCount << " ===";
49 try
50 {
51 meshPrimitives[i] = MeshPrimitive( meshData.getPrimitives()[i], context, renderer );
52 PLOGI << "=== Mesh primitive " << i + 1 << " created successfully ===";
53 }
54 catch ( const std::exception & e )
55 {
56 PLOGE << "=== ERROR creating mesh primitive " << i + 1 << ": " << e.what() << " ===";
57 throw;
58 }
59 }
60 PLOGI << "=== Mesh primitive data loaded ===";
61 glm::mat4 matrix = meshData.getTransform();
62 sceneTransform = Transform( matrix );
63
64 // Calculate bounding sphere for the entire mesh
65 if ( !meshPrimitives.empty() )
66 {
67 glm::vec3 minCoordinates( std::numeric_limits<float>::max() );
68 glm::vec3 maxCoordinates( std::numeric_limits<float>::lowest() );
69
70 for ( const auto & primitive : meshPrimitives )
71 {
72 for ( const auto & vertex : primitive.getVertices() )
73 {
74 minCoordinates = glm::min( minCoordinates, glm::vec3(vertex.position) );
75 maxCoordinates = glm::max( maxCoordinates, glm::vec3(vertex.position) );
76 }
77 }
78 glm::vec3 extent = maxCoordinates - minCoordinates;
79 boundingSphereRadius = glm::length( extent ) / 2.0f;
80 }
81
82 isReady = true;
83 }
84
85 void Mesh::cleanup() {}
86
88 {
89 meshPrimitives.clear();
90 }
91
92 uint32_t Mesh::getIndexCount() const
93 {
94 uint32_t indicesCount = 0u;
95 for ( const MeshPrimitive & primitive : meshPrimitives )
96 {
97 indicesCount += primitive.getIndices().size();
98 }
99 return indicesCount;
100 }
101
103 {
105 uint32_t primitiveCount = 0u;
106 for ( const MeshPrimitive & primitive : meshPrimitives )
107 {
108 primitiveCount += primitive.getMeshletTriangleCount();
109 }
110 return primitiveCount;
111 }
112
113 uint32_t Mesh::getMeshletCount() const
114 {
115 TRACY_ZONE_SCOPED_NAMED( "Get meshlet count" );
116 uint32_t count = 0u;
117 for ( const MeshPrimitive & primitive : meshPrimitives )
118 {
119 count += primitive.getMeshletsCount();
120 }
121 return count;
122 }
123
124 std::vector<MeshPrimitive> Mesh::getPrimitives() const
125 {
126 return meshPrimitives;
127 }
128
129 const std::vector<MeshPrimitive> & Mesh::getPrimitivesByReference() const
130 {
131 return meshPrimitives;
132 }
133
134 std::vector<MeshPrimitive> & Mesh::getPrimitivesByReference()
135 {
136 return meshPrimitives;
137 }
138
139 std::string Mesh::getName() const
140 {
141 return name;
142 }
143
144 std::string Mesh::createMeshName( const std::filesystem::path & path, const std::string & meshName )
145 {
146 std::stringstream name;
147 name << path.generic_string() << ";" << meshName;
148 return name.str();
149 }
150
152 {
153 return sceneTransform;
154 }
155
157 {
159 }
160
162 {
163 if ( materialName.has_value() )
164 {
165 return materialName.value();
166 }
168 }
169
171 {
172 return textureCount;
173 }
174
175} // namespace EngineCore
#define TRACY_ZONE_SCOPED_FUNCTION
#define TRACY_ZONE_SCOPED_NAMED(name)
The application context is the core class which stores the basic openxr and vulkan objects.
std::optional< PipelineNames > materialName
Definition Mesh.h:71
static const uint32_t textureCount
Definition Mesh.h:29
std::atomic< bool > isReady
Definition Mesh.h:42
ApplicationContext * context
Definition Mesh.h:79
Transform sceneTransform
Definition Mesh.h:84
std::string getName() const
Definition Mesh.cpp:139
Renderer * renderer
Definition Mesh.h:78
const std::vector< MeshPrimitive > & getPrimitivesByReference() const
Definition Mesh.cpp:129
uint32_t getIndexCount() const
Definition Mesh.cpp:92
static std::string createMeshName(const std::filesystem::path &path, const std::string &meshName)
Definition Mesh.cpp:144
uint32_t getMeshletCount() const
Definition Mesh.cpp:113
GltfLoader::GltfMeshData meshData
Definition Mesh.h:76
uint32_t getMeshletTriangleCount() const
Definition Mesh.cpp:102
float boundingSphereRadius
Definition Mesh.h:74
static uint32_t getTextureCount()
Definition Mesh.cpp:170
Transform getSceneTransform() const
Definition Mesh.cpp:151
std::string name
Definition Mesh.h:73
void createResources()
Definition Mesh.cpp:37
void reset()
Definition Mesh.cpp:87
std::vector< MeshPrimitive > getPrimitives() const
Definition Mesh.cpp:124
void cleanup()
Definition Mesh.cpp:85
std::vector< MeshPrimitive > meshPrimitives
Definition Mesh.h:86
PipelineNames getShaderName() const
Gets the name of the shader assigned to this object.
Definition Mesh.cpp:161
float getBoundingSphereRadius() const
Definition Mesh.cpp:156
Log category system implementation.
Converters from other number / vector systems into glm.
Definition Converter.cpp:24