Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
Mesh Class Reference

GPU mesh representation with support for instanced rendering. More...

#include <Mesh.h>

Public Member Functions

 Mesh (const std::vector< Vertex > &verts, const std::vector< unsigned int > &idx, QOpenGLFunctions_4_5_Core *funcs)
 Constructs a mesh from vertex and index data.
 
 ~Mesh ()
 Destroys the mesh and releases GPU resources.
 
void draw () const
 Draws a single instance of this mesh.
 
void drawInstanced (const std::vector< Rendering::InstanceData > &instances)
 Draws multiple instances of this mesh in a single draw call.
 
std::span< const VertexgetVertices () const
 Provides read-only view of vertex data without copying.
 
std::span< const unsigned int > getIndices () const
 Provides read-only view of index data without copying.
 
const Physics::Bounding::AABBgetLocalAABB () const
 Gets the local-space axis-aligned bounding box.
 

Detailed Description

GPU mesh representation with support for instanced rendering.

Manages vertex data, indices, and GPU buffers for a single mesh. Supports both standard and instanced rendering. Meshes are immutable after construction - vertex and index data cannot be modified.

Note
This class assumes an active OpenGL context during construction.
The mesh automatically computes an axis-aligned bounding box (AABB) in local space for collision detection and culling.
See also
ResourceManager for mesh loading and management
Rendering::InstanceData for per-instance data layout

Definition at line 58 of file Mesh.h.

Constructor & Destructor Documentation

◆ Mesh()

Mesh::Mesh ( const std::vector< Vertex > &  verts,
const std::vector< unsigned int > &  idx,
QOpenGLFunctions_4_5_Core *  funcs 
)

Constructs a mesh from vertex and index data.

Uploads data to GPU and sets up vertex attributes for rendering. Automatically computes a local-space AABB for the mesh.

Parameters
vertsVertex data (positions, normals)
idxIndex buffer for indexed drawing
funcsOpenGL function pointers for the current context
Note
An OpenGL context must be current on this thread
funcs must not be nullptr
verts must not be empty
idx must not be empty and all indices must be valid

Definition at line 5 of file Mesh.cpp.

◆ ~Mesh()

Mesh::~Mesh ( )

Destroys the mesh and releases GPU resources.

Deletes VAO, VBO, EBO, and instance VBO if they exist.

Definition at line 29 of file Mesh.cpp.

Member Function Documentation

◆ draw()

void Mesh::draw ( ) const

Draws a single instance of this mesh.

Uses the currently bound shader. Model matrix and other uniforms must be set by the caller before drawing.

Note
A shader must be bound via glUseProgram
Appropriate uniforms must be set in the bound shader

Definition at line 72 of file Mesh.cpp.

◆ drawInstanced()

void Mesh::drawInstanced ( const std::vector< Rendering::InstanceData > &  instances)

Draws multiple instances of this mesh in a single draw call.

More efficient than multiple draw() calls for rendering many copies of the same mesh with different transforms/properties.

Parameters
instancesPer-instance data (transforms, IDs, colors, etc.)
Note
A shader must be bound that supports instanced rendering
instances must not be empty
Shader must have vertex attributes matching InstanceData layout
See also
Rendering::InstanceData for the expected data layout
setupInstanceAttributes() for attribute configuration

Definition at line 78 of file Mesh.cpp.

Here is the caller graph for this function:

◆ getIndices()

std::span< const unsigned int > Mesh::getIndices ( ) const

Provides read-only view of index data without copying.

Returns a non-owning view over the mesh's index array.

Returns
Non-owning span view of index data
Note
The returned span is only valid while this Mesh exists
The span is read-only - use const iteration
Cannot modify index data through this view (mesh is immutable after construction)
Example Usage:
std::span<const unsigned int> indices = mesh->getIndices();
size_t triangleCount = indices.size() / 3;

Definition at line 95 of file Mesh.cpp.

Here is the caller graph for this function:

◆ getLocalAABB()

const Physics::Bounding::AABB & Mesh::getLocalAABB ( ) const
inline

Gets the local-space axis-aligned bounding box.

The AABB is computed during construction and represents the tightest axis-aligned box that contains all vertices.

Returns
Reference to the cached local AABB
Note
To get a world-space AABB, call getTransformed() on the result with the object's model matrix

Definition at line 163 of file Mesh.h.

Here is the caller graph for this function:

◆ getVertices()

std::span< const Vertex > Mesh::getVertices ( ) const

Provides read-only view of vertex data without copying.

Returns a non-owning view over the mesh's vertex array.

Returns
Non-owning span view of vertex data
Note
The returned span is only valid while this Mesh exists
The span is read-only - use const iteration: for (const auto& v : span)
Cannot modify vertex data through this view (mesh is immutable after construction)
Example Usage:
std::span<const Vertex> verts = mesh->getVertices();
for (const auto& vertex : verts) {
// Process vertex.pos, vertex.normal, etc.
}

Definition at line 91 of file Mesh.cpp.

Here is the caller graph for this function:

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