Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
Mesh.h
Go to the documentation of this file.
1#pragma once
2#include <QOpenGLFunctions_4_5_Core>
3#include <vector>
4#include <glm/glm.hpp>
7
13struct Vertex {
14 glm::vec3 pos;
15 glm::vec3 normal;
16
22 bool operator==(const Vertex& other) const {
23 return pos == other.pos &&
24 normal == other.normal;
25 }
26};
27
33namespace std {
34 template <>
35 struct hash<Vertex> {
36 size_t operator()(const Vertex& v) const {
37 size_t h1 = hash<float>()(v.pos.x) ^ (hash<float>()(v.pos.y) << 1) ^ (hash<float>()(v.pos.z) << 2);
38 size_t h2 = hash<float>()(v.normal.x) ^ (hash<float>()(v.normal.y) << 1) ^ (hash<float>()(v.normal.z) << 2);
39 return h1 ^ (h2 << 1);
40 }
41 };
42}
43
58class Mesh {
59public:
75 Mesh(const std::vector<Vertex>& verts, const std::vector<unsigned int>& idx, QOpenGLFunctions_4_5_Core* funcs);
76
82 ~Mesh();
83
93 void draw() const;
94
110 void drawInstanced(const std::vector<Rendering::InstanceData>& instances);
111
131 std::span<const Vertex> getVertices() const;
132
150 std::span<const unsigned int> getIndices() const;
151
163 const Physics::Bounding::AABB& getLocalAABB() const { return localAABB; }
164private:
165 QOpenGLFunctions_4_5_Core* funcs;
166 std::vector<Vertex> vertices;
167 std::vector<unsigned int> indices;
168 Physics::Bounding::AABB localAABB;
169
170 unsigned int VAO;
171 unsigned int VBO;
172 unsigned int instanceVBO;
173 unsigned int EBO;
174 unsigned int indexCount;
175
189 void setupInstanceAttributes();
190
199 void createLocalAABB();
200};
201
GPU mesh representation with support for instanced rendering.
Definition Mesh.h:58
~Mesh()
Destroys the mesh and releases GPU resources.
Definition Mesh.cpp:29
void draw() const
Draws a single instance of this mesh.
Definition Mesh.cpp:72
std::span< const Vertex > getVertices() const
Provides read-only view of vertex data without copying.
Definition Mesh.cpp:91
const Physics::Bounding::AABB & getLocalAABB() const
Gets the local-space axis-aligned bounding box.
Definition Mesh.h:163
std::span< const unsigned int > getIndices() const
Provides read-only view of index data without copying.
Definition Mesh.cpp:95
void drawInstanced(const std::vector< Rendering::InstanceData > &instances)
Draws multiple instances of this mesh in a single draw call.
Definition Mesh.cpp:78
Hash specialization for Vertex to enable use in unordered containers.
Definition Mesh.h:33
Vertex data for a single vertex in a mesh.
Definition Mesh.h:13
glm::vec3 normal
Surface normal.
Definition Mesh.h:15
bool operator==(const Vertex &other) const
Equality comparison for vertex deduplication.
Definition Mesh.h:22
glm::vec3 pos
Position in local/model space.
Definition Mesh.h:14
size_t operator()(const Vertex &v) const
Definition Mesh.h:36