Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
rendering_engine::MeshDataGpu Class Reference

Manages mesh data in RAM and GPU, including upload and release operations. More...

#include <mesh_data_gpu.hpp>

Public Member Functions

 MeshDataGpu (IRenderer *renderer)
 Constructs an empty MeshDataGpu object. More...
 
 MeshDataGpu (const std::string &filename, IRenderer *renderer)
 Construct a MeshDataGpu instance. More...
 
 MeshDataGpu (std::vector< uint8_t > const &fileBytes, IRenderer *renderer)
 Constructs a MeshDataGpu instance from in-memory model data. More...
 
 ~MeshDataGpu ()
 Destructor. Releases GPU resources if allocated. More...
 
void UploadToGPU ()
 Upload mesh data to the GPU using the current mesh type. More...
 
void ReleaseFromGPU ()
 Release GPU resources associated with this mesh. More...
 
bool IsOnGPU () const
 Check if mesh data is currently uploaded to GPU. More...
 
void CreateQuad2D ()
 Creates a 1�1 unit quad centered at the origin. More...
 
void LoadCustomMesh (std::vector< glm::vec2 > positions2D, std::vector< glm::vec2 > texCoords, std::vector< glm::vec4 > colors, std::vector< std::uint32_t > indices)
 
size_t GetGpuVertexBufferSize () const
 Get the size (in bytes) of the vertex buffer on GPU. More...
 
size_t GetGpuIndexBufferSize () const
 Get the size (in bytes) of the index buffer on GPU. More...
 
size_t GetCpuVertexBufferSize () const
 Get the size (in bytes) of the vertex buffer in RAM. More...
 
size_t GetCpuIndexBufferSize () const
 Get the size (in bytes) of the index buffer in RAM. More...
 
IMeshRenderResourcesGetMeshRenderResources ()
 Get the interface for mesh GPU resources (Vulkan or other backend). More...
 
const std::vector< glm::vec2 > & GetPositions2D () const
 Returns a constant reference to the 2D vertex positions. More...
 
const std::vector< glm::vec3 > & GetPositions () const
 Returns a constant reference to the 3D vertex positions. More...
 
const std::vector< glm::vec4 > & GetColors () const
 Returns a constant reference to the vertex colors. More...
 
const std::vector< glm::vec3 > & GetNormals () const
 Returns a constant reference to the vertex normals. More...
 
const std::vector< glm::vec2 > & GetTexCoords () const
 Returns a constant reference to the texture coordinates. More...
 
const std::vector< glm::vec3 > & GetTangents () const
 Returns a constant reference to the vertex tangents. More...
 
const std::vector< uint32_t > & GetIndices () const
 Returns a constant reference to the mesh indices. More...
 

Protected Member Functions

void LoadModel (std::string path)
 Load a model from file and extract mesh data to RAM. More...
 
void LoadModel (std::vector< uint8_t > const &fileBytes)
 
void CalculateMeshParameters ()
 Calculate and cache sizes of RAM buffers for statistics or debugging. More...
 
std::vector< Vertex2DComposeVertex2DBuffer ()
 Compose a vertex buffer for 2D meshes (e.g., sprites). More...
 
std::vector< VertexPositionColorTextureComposeUnlitBuffer ()
 Compose a vertex buffer for unlit 3D meshes (e.g., billboards, particles). More...
 
std::vector< VertexPositionColorTextureNormalTangentComposeLitBuffer ()
 Compose a vertex buffer for lit 3D meshes (with normals, tangents). More...
 

Detailed Description

Manages mesh data in RAM and GPU, including upload and release operations.

Supports on-demand composition of different vertex buffer formats and maintains all intermediate mesh data required for rendering or further processing (e.g., collision).

Definition at line 36 of file mesh_data_gpu.hpp.

Constructor & Destructor Documentation

◆ MeshDataGpu() [1/3]

rendering_engine::MeshDataGpu::MeshDataGpu ( IRenderer renderer)

Constructs an empty MeshDataGpu object.

This constructor is typically used when the mesh data will be filled or generated procedurally before being uploaded to the GPU.

Parameters
rendererPointer to the rendering backend interface.

Definition at line 8 of file mesh_data_gpu.cpp.

9 :
10 mRenderer(renderer),
11 mGpuHandle(nullptr),
12 mSizeOfVerticesBytes(0),
13 mSizeOfIndicesBytes(0),
14 mMeshType(MeshType::None)
15{
16}
@ None
Undefined or not yet set.

◆ MeshDataGpu() [2/3]

rendering_engine::MeshDataGpu::MeshDataGpu ( const std::string &  filename,
IRenderer renderer 
)

Construct a MeshDataGpu instance.

Parameters
filenamePath to the model file
rendererPointer to the rendering backend interface.

Definition at line 17 of file mesh_data_gpu.cpp.

18 :
19 mRenderer(renderer),
20 mGpuHandle(nullptr),
21 mSizeOfVerticesBytes(0),
22 mSizeOfIndicesBytes(0),
23 mMeshType(MeshType::None)
24{
25 LoadModel(filename);
26}
void LoadModel(std::string path)
Load a model from file and extract mesh data to RAM.

◆ MeshDataGpu() [3/3]

rendering_engine::MeshDataGpu::MeshDataGpu ( std::vector< uint8_t > const &  fileBytes,
IRenderer renderer 
)

Constructs a MeshDataGpu instance from in-memory model data.

This version loads a model directly from a raw file buffer in memory. It allows meshes to be created from packed assets, archives, network transfers, or any other non-filesystem source. After loading the model, vertex and index data are uploaded to GPU buffers the same way as in the file-based constructor.

Parameters
fileBytesRaw contents of the model file.
rendererPointer to the rendering backend interface.

Definition at line 28 of file mesh_data_gpu.cpp.

29 :
30 mRenderer(renderer),
31 mGpuHandle(nullptr),
32 mSizeOfVerticesBytes(0),
33 mSizeOfIndicesBytes(0),
34 mMeshType(MeshType::None)
35{
36 LoadModel(fileBytes);
37}

◆ ~MeshDataGpu()

rendering_engine::MeshDataGpu::~MeshDataGpu ( )

Destructor. Releases GPU resources if allocated.

Definition at line 39 of file mesh_data_gpu.cpp.

40{
42}
void ReleaseFromGPU()
Release GPU resources associated with this mesh.

Member Function Documentation

◆ CalculateMeshParameters()

void rendering_engine::MeshDataGpu::CalculateMeshParameters ( )
protected

Calculate and cache sizes of RAM buffers for statistics or debugging.

Definition at line 250 of file mesh_data_gpu.cpp.

251{
252 size_t sizeOfVerticesBytes = 0;
253 size_t sizeOfIndicesBytes = 0;
254
255 sizeOfVerticesBytes += (mPositions2D.size() * sizeof(glm::vec2));
256 sizeOfVerticesBytes += (mPositions.size() * sizeof(glm::vec3));
257 sizeOfVerticesBytes += (mColor.size() * sizeof(glm::vec4));
258 sizeOfVerticesBytes += (mNormals.size() * sizeof(glm::vec3));
259 sizeOfVerticesBytes += (mTexCoords.size() * sizeof(glm::vec2));
260 sizeOfVerticesBytes += (mTangents.size() * sizeof(glm::vec3));
261
262 sizeOfIndicesBytes += (mIndices.size() * sizeof(uint32_t));
263
264 mSizeOfVerticesBytes = sizeOfVerticesBytes;
265 mSizeOfIndicesBytes = sizeOfIndicesBytes;
266}

◆ ComposeLitBuffer()

std::vector< VertexPositionColorTextureNormalTangent > rendering_engine::MeshDataGpu::ComposeLitBuffer ( )
protected

Compose a vertex buffer for lit 3D meshes (with normals, tangents).

Returns
Vector of VertexPositionTextureColorNormalTangent structures.

Definition at line 306 of file mesh_data_gpu.cpp.

307{
308 std::vector<VertexPositionColorTextureNormalTangent> result;
309
310 unsigned int index = 0;
311 for (const auto& position : mPositions)
312 {
313 VertexPositionColorTextureNormalTangent vert;
314 vert.position = position;
315 vert.color = mColor.empty() ? glm::vec4(1.0f) : mColor[index];
316 vert.textureCoordinates = mTexCoords.empty() ? glm::vec2(0.0f) : mTexCoords[index];
317 vert.normal = mNormals.empty() ? glm::vec3(0.0f, 0.0f, 1.0f) : mNormals[index];
318 vert.tangent = mTangents.empty() ? glm::vec3(1.0f, 0.0f, 0.0f) : mTangents[index];
319
320 result.push_back(vert);
321 ++index;
322 }
323
324 return result;
325}

◆ ComposeUnlitBuffer()

std::vector< VertexPositionColorTexture > rendering_engine::MeshDataGpu::ComposeUnlitBuffer ( )
protected

Compose a vertex buffer for unlit 3D meshes (e.g., billboards, particles).

Returns
Vector of VertexPositionTextureColor structures.

Definition at line 287 of file mesh_data_gpu.cpp.

288{
289 std::vector<VertexPositionColorTexture> result;
290
291 unsigned int index = 0;
292 for (const auto& position : mPositions)
293 {
294 VertexPositionColorTexture vert;
295 vert.position = position;
296 vert.color = mColor.empty() ? glm::vec4(1) : mColor[index];
297 vert.textureCoordinates = mTexCoords.empty() ? glm::vec2(0.0f) : mTexCoords[index];
298
299 result.push_back(vert);
300 ++index;
301 }
302
303 return result;
304}

◆ ComposeVertex2DBuffer()

std::vector< Vertex2D > rendering_engine::MeshDataGpu::ComposeVertex2DBuffer ( )
protected

Compose a vertex buffer for 2D meshes (e.g., sprites).

Returns
Vector of Vertex2D structures.

Definition at line 268 of file mesh_data_gpu.cpp.

269{
270 std::vector<Vertex2D> result;
271
272 unsigned int index = 0;
273 for (const auto& position : mPositions2D)
274 {
275 Vertex2D vert;
276 vert.position = position;
277 vert.color = mColor.empty() ? glm::vec4(1.0f) : mColor[index];
278 vert.textureCoordinates = mTexCoords.empty() ? glm::vec2(0.0f) : mTexCoords[index];
279
280 result.push_back(vert);
281 ++index;
282 }
283
284 return result;
285}

◆ CreateQuad2D()

void rendering_engine::MeshDataGpu::CreateQuad2D ( )

Creates a 1�1 unit quad centered at the origin.

The quad spans from -0.5 to +0.5 on both X and Y axes and uses counterclockwise winding. Suitable as a base mesh for sprites or 2D UI elements.

Definition at line 171 of file mesh_data_gpu.cpp.

172{
173 mPositions2D = { {-0.5f, -0.5f}, { 0.5f, -0.5f}, { 0.5f, 0.5f}, {-0.5f, 0.5f} };
174
175 // TOP-LEFT origin UVs
176 mTexCoords = {
177 {0.0f, 0.0f}, // bottom-left
178 {1.0f, 0.0f}, // bottom-right
179 {1.0f, 1.0f}, // top-right
180 {0.0f, 1.0f} // top-left
181 };
182
183 mColor = { {1.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f} };
184
185 mIndices = {
186 0, 1, 2, // first triangle
187 2, 3, 0 // second triangle
188 };
189
191
192 mMeshType = MeshType::Sprite2D;
193}
void CalculateMeshParameters()
Calculate and cache sizes of RAM buffers for statistics or debugging.
@ Sprite2D
2D sprite mesh (typically for UI or simple quads).

◆ GetColors()

const std::vector< glm::vec4 > & rendering_engine::MeshDataGpu::GetColors ( ) const
inline

Returns a constant reference to the vertex colors.

Definition at line 147 of file mesh_data_gpu.hpp.

147{ return mColor; }

◆ GetCpuIndexBufferSize()

size_t rendering_engine::MeshDataGpu::GetCpuIndexBufferSize ( ) const

Get the size (in bytes) of the index buffer in RAM.

Returns
Size in bytes of the CPU-side index data.

Definition at line 236 of file mesh_data_gpu.cpp.

237{
238 return mSizeOfIndicesBytes;
239}

◆ GetCpuVertexBufferSize()

size_t rendering_engine::MeshDataGpu::GetCpuVertexBufferSize ( ) const

Get the size (in bytes) of the vertex buffer in RAM.

Returns
Size in bytes of the CPU-side vertex data.

Definition at line 231 of file mesh_data_gpu.cpp.

232{
233 return mSizeOfVerticesBytes;
234}

◆ GetGpuIndexBufferSize()

size_t rendering_engine::MeshDataGpu::GetGpuIndexBufferSize ( ) const

Get the size (in bytes) of the index buffer on GPU.

Returns
Size in bytes of the GPU index buffer.

Definition at line 219 of file mesh_data_gpu.cpp.

220{
221 size_t result = 0;
222
223 if (mGpuHandle)
224 {
225 result = mGpuHandle->GetIndexBufferSize();
226 }
227
228 return result;
229}

◆ GetGpuVertexBufferSize()

size_t rendering_engine::MeshDataGpu::GetGpuVertexBufferSize ( ) const

Get the size (in bytes) of the vertex buffer on GPU.

Returns
Size in bytes of the GPU vertex buffer.

Definition at line 207 of file mesh_data_gpu.cpp.

208{
209 size_t result = 0;
210
211 if (mGpuHandle)
212 {
213 result = mGpuHandle->GetVertexBufferSize();
214 }
215
216 return result;
217}

◆ GetIndices()

const std::vector< uint32_t > & rendering_engine::MeshDataGpu::GetIndices ( ) const
inline

Returns a constant reference to the mesh indices.

Definition at line 167 of file mesh_data_gpu.hpp.

167{ return mIndices; }

◆ GetMeshRenderResources()

IMeshRenderResources * rendering_engine::MeshDataGpu::GetMeshRenderResources ( )

Get the interface for mesh GPU resources (Vulkan or other backend).

Returns
Pointer to the backend-specific IMeshRenderResources implementation.

Definition at line 241 of file mesh_data_gpu.cpp.

242{
243 if (!IsOnGPU())
244 {
245 UploadToGPU();
246 }
247 return mGpuHandle.get();
248}
void UploadToGPU()
Upload mesh data to the GPU using the current mesh type.
bool IsOnGPU() const
Check if mesh data is currently uploaded to GPU.

◆ GetNormals()

const std::vector< glm::vec3 > & rendering_engine::MeshDataGpu::GetNormals ( ) const
inline

Returns a constant reference to the vertex normals.

Definition at line 152 of file mesh_data_gpu.hpp.

152{ return mNormals; }

◆ GetPositions()

const std::vector< glm::vec3 > & rendering_engine::MeshDataGpu::GetPositions ( ) const
inline

Returns a constant reference to the 3D vertex positions.

Definition at line 142 of file mesh_data_gpu.hpp.

142{ return mPositions; }

◆ GetPositions2D()

const std::vector< glm::vec2 > & rendering_engine::MeshDataGpu::GetPositions2D ( ) const
inline

Returns a constant reference to the 2D vertex positions.

Definition at line 137 of file mesh_data_gpu.hpp.

137{ return mPositions2D; }

◆ GetTangents()

const std::vector< glm::vec3 > & rendering_engine::MeshDataGpu::GetTangents ( ) const
inline

Returns a constant reference to the vertex tangents.

Definition at line 162 of file mesh_data_gpu.hpp.

162{ return mTangents; }

◆ GetTexCoords()

const std::vector< glm::vec2 > & rendering_engine::MeshDataGpu::GetTexCoords ( ) const
inline

Returns a constant reference to the texture coordinates.

Definition at line 157 of file mesh_data_gpu.hpp.

157{ return mTexCoords; }

◆ IsOnGPU()

bool rendering_engine::MeshDataGpu::IsOnGPU ( ) const

Check if mesh data is currently uploaded to GPU.

Returns
True if GPU resources are resident, false otherwise.

Definition at line 103 of file mesh_data_gpu.cpp.

104{
105 bool result = false;
106
107 if (mGpuHandle)
108 {
109 result = mGpuHandle->IsOnGPU();
110 }
111
112 return result;
113}

◆ LoadCustomMesh()

void rendering_engine::MeshDataGpu::LoadCustomMesh ( std::vector< glm::vec2 >  positions2D,
std::vector< glm::vec2 >  texCoords,
std::vector< glm::vec4 >  colors,
std::vector< std::uint32_t >  indices 
)

Definition at line 195 of file mesh_data_gpu.cpp.

196{
197 mPositions2D = positions2D;
198 mTexCoords = texCoords;
199 mColor = colors;
200 mIndices = indices;
201
203
204 mMeshType = MeshType::Sprite2D;
205}

◆ LoadModel() [1/2]

void rendering_engine::MeshDataGpu::LoadModel ( std::string  path)
protected

Load a model from file and extract mesh data to RAM.

Parameters
pathPath to the model file (e.g., FBX).

Definition at line 115 of file mesh_data_gpu.cpp.

116{
117 std::unique_ptr<Model> model = std::make_unique<Model>(path, true);
118
119 if (model)
120 {
121 if (model->HasMeshes())
122 {
123 mPositions = model->Meshes()[0]->Vertices();
124
125 if (model->Meshes()[0]->VertexColors().size() >= 1)
126 mColor = model->Meshes()[0]->VertexColors()[0];
127
128 mNormals = model->Meshes()[0]->Normals();
129 for (const auto& texCoord : model->Meshes()[0]->TextureCoordinates()[0])
130 {
131 mTexCoords.push_back(glm::vec2(texCoord.x, texCoord.y));
132 }
133 mTangents = model->Meshes()[0]->Tangents();
134 mIndices = model->Meshes()[0]->Indices();
135 }
136 }
137
139
140 mMeshType = MeshType::Surface;
141}
@ Surface
Full 3D surface mesh (models with normals/tangents, etc.).

◆ LoadModel() [2/2]

void rendering_engine::MeshDataGpu::LoadModel ( std::vector< uint8_t > const &  fileBytes)
protected

Definition at line 143 of file mesh_data_gpu.cpp.

144{
145 std::unique_ptr<Model> model = std::make_unique<Model>(fileBytes, true);
146
147 if (model)
148 {
149 if (model->HasMeshes())
150 {
151 mPositions = model->Meshes()[0]->Vertices();
152
153 if (model->Meshes()[0]->VertexColors().size() >= 1)
154 mColor = model->Meshes()[0]->VertexColors()[0];
155
156 mNormals = model->Meshes()[0]->Normals();
157 for (const auto& texCoord : model->Meshes()[0]->TextureCoordinates()[0])
158 {
159 mTexCoords.push_back(glm::vec2(texCoord.x, texCoord.y));
160 }
161 mTangents = model->Meshes()[0]->Tangents();
162 mIndices = model->Meshes()[0]->Indices();
163 }
164 }
165
167
168 mMeshType = MeshType::Surface;
169}

◆ ReleaseFromGPU()

void rendering_engine::MeshDataGpu::ReleaseFromGPU ( )

Release GPU resources associated with this mesh.

Definition at line 87 of file mesh_data_gpu.cpp.

88{
89 if (!mRenderer)
90 {
91 return;
92 }
93 if (!mGpuHandle)
94 {
95 return;
96 }
97
98 mGpuHandle->Shutdown();
99 mGpuHandle.release();
100 mGpuHandle = nullptr;
101}

◆ UploadToGPU()

void rendering_engine::MeshDataGpu::UploadToGPU ( )

Upload mesh data to the GPU using the current mesh type.

Definition at line 44 of file mesh_data_gpu.cpp.

45{
46 if (!mRenderer)
47 {
48 return;
49 }
50 if (mGpuHandle)
51 {
52 if (mGpuHandle->IsOnGPU())
53 {
55 }
56 }
57
58 mGpuHandle = std::unique_ptr<IMeshRenderResources>(mRenderer->ProvideMeshRenderResources());
59
60 switch (mMeshType)
61 {
62 case MeshType::None:
63 {
64 break;
65 }
67 {
68 mGpuHandle->CreateVertexBuffer(ComposeVertex2DBuffer());
69 mGpuHandle->CreateIndexBuffer(mIndices);
70 break;
71 }
73 {
74 mGpuHandle->CreateVertexBuffer(ComposeUnlitBuffer());
75 mGpuHandle->CreateIndexBuffer(mIndices);
76 break;
77 }
79 {
80 mGpuHandle->CreateVertexBuffer(ComposeLitBuffer());
81 mGpuHandle->CreateIndexBuffer(mIndices);
82 break;
83 }
84 }
85}
virtual IMeshRenderResources * ProvideMeshRenderResources() const =0
Provides access to mesh-related GPU resources.
std::vector< VertexPositionColorTextureNormalTangent > ComposeLitBuffer()
Compose a vertex buffer for lit 3D meshes (with normals, tangents).
std::vector< Vertex2D > ComposeVertex2DBuffer()
Compose a vertex buffer for 2D meshes (e.g., sprites).
std::vector< VertexPositionColorTexture > ComposeUnlitBuffer()
Compose a vertex buffer for unlit 3D meshes (e.g., billboards, particles).
@ Billboard
3D billboard mesh (sprites that face the camera).

The documentation for this class was generated from the following files: