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

Protected Member Functions

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

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 229 of file mesh_data_gpu.cpp.

230{
231 size_t sizeOfVerticesBytes = 0;
232 size_t sizeOfIndicesBytes = 0;
233
234 sizeOfVerticesBytes += (mPositions2D.size() * sizeof(glm::vec2));
235 sizeOfVerticesBytes += (mPositions.size() * sizeof(glm::vec3));
236 sizeOfVerticesBytes += (mColor.size() * sizeof(glm::vec4));
237 sizeOfVerticesBytes += (mNormals.size() * sizeof(glm::vec3));
238 sizeOfVerticesBytes += (mTexCoords.size() * sizeof(glm::vec2));
239 sizeOfVerticesBytes += (mTangents.size() * sizeof(glm::vec3));
240
241 sizeOfIndicesBytes += (mIndices.size() * sizeof(uint32_t));
242
243 mSizeOfVerticesBytes = sizeOfVerticesBytes;
244 mSizeOfIndicesBytes = sizeOfIndicesBytes;
245}

◆ 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 285 of file mesh_data_gpu.cpp.

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

◆ 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 266 of file mesh_data_gpu.cpp.

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

◆ 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 247 of file mesh_data_gpu.cpp.

248{
249 std::vector<Vertex2D> result;
250
251 unsigned int index = 0;
252 for (const auto& position : mPositions2D)
253 {
254 Vertex2D vert;
255 vert.position = position;
256 vert.color = mColor.empty() ? glm::vec4(1.0f) : mColor[index];
257 vert.textureCoordinates = mTexCoords.empty() ? glm::vec2(0.0f) : mTexCoords[index];
258
259 result.push_back(vert);
260 ++index;
261 }
262
263 return result;
264}

◆ 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 mTexCoords = { {0.0f, 1.0f}, {1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 0.0f} };
175 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} };
176 mIndices = {
177 0, 1, 2, // first triangle
178 2, 3, 0 // second triangle
179 };
180
182
183 mMeshType = MeshType::Sprite2D;
184}
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 142 of file mesh_data_gpu.hpp.

142{ 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 215 of file mesh_data_gpu.cpp.

216{
217 return mSizeOfIndicesBytes;
218}

◆ 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 210 of file mesh_data_gpu.cpp.

211{
212 return mSizeOfVerticesBytes;
213}

◆ 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 198 of file mesh_data_gpu.cpp.

199{
200 size_t result = 0;
201
202 if (mGpuHandle)
203 {
204 result = mGpuHandle->GetIndexBufferSize();
205 }
206
207 return result;
208}

◆ 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 186 of file mesh_data_gpu.cpp.

187{
188 size_t result = 0;
189
190 if (mGpuHandle)
191 {
192 result = mGpuHandle->GetVertexBufferSize();
193 }
194
195 return result;
196}

◆ GetIndices()

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

Returns a constant reference to the mesh indices.

Definition at line 162 of file mesh_data_gpu.hpp.

162{ 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 220 of file mesh_data_gpu.cpp.

221{
222 if (!IsOnGPU())
223 {
224 UploadToGPU();
225 }
226 return mGpuHandle.get();
227}
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 147 of file mesh_data_gpu.hpp.

147{ 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 137 of file mesh_data_gpu.hpp.

137{ 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 132 of file mesh_data_gpu.hpp.

132{ 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 157 of file mesh_data_gpu.hpp.

157{ 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 152 of file mesh_data_gpu.hpp.

152{ 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}

◆ 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}
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: