Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
Mesh.cpp
Go to the documentation of this file.
1#include "Mesh.h"
2#include <vector>
3#include <iostream>
4
5Mesh::Mesh(const std::vector<Vertex> &verts, const std::vector<unsigned int> &idx, QOpenGLFunctions_4_5_Core* glFuncs)
6 : indexCount(idx.size()), vertices(verts), indices(idx), funcs(glFuncs), localAABB(){
7 assert(QOpenGLContext::currentContext() != nullptr && "GL context is NOT current during Mesh construction!");
8 assert(funcs != nullptr);
9
10 funcs->glGenVertexArrays(1, &VAO);
11 assert(VAO != 0 && "VAO generation failed — is GL context current?");
12 funcs->glGenBuffers(1, &VBO);
13 funcs->glGenBuffers(1, &EBO);
14
15 funcs->glBindVertexArray(VAO);
16 funcs->glBindBuffer(GL_ARRAY_BUFFER, VBO);
17 funcs->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
18
19 funcs->glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
20 funcs->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
21 funcs->glEnableVertexAttribArray(0);
22 funcs->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
23 funcs->glEnableVertexAttribArray(1);
24 funcs->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
25 setupInstanceAttributes();
26 createLocalAABB();
27}
28
30 funcs->glDeleteVertexArrays(1, &VAO);
31 funcs->glDeleteBuffers(1, &VBO);
32 funcs->glDeleteBuffers(1, &EBO);
33}
34
35void Mesh::setupInstanceAttributes() {
36 funcs->glGenBuffers(1, &instanceVBO);
37 funcs->glBindVertexArray(VAO);
38 funcs->glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
39
40 // Model matrix = 4 vec4s = locations 2, 3, 4, 5
41 for (int i = 0; i < 4; ++i) {
42 funcs->glEnableVertexAttribArray(2 + i);
43 funcs->glVertexAttribPointer(2 + i, 4, GL_FLOAT, GL_FALSE, sizeof(Rendering::InstanceData), (void*)(sizeof(glm::vec4) * i));
44 funcs->glVertexAttribDivisor(2 + i, 1);
45 }
46
47 // objectID = location 6
48 funcs->glEnableVertexAttribArray(6);
49 funcs->glVertexAttribIPointer(6, 1, GL_UNSIGNED_INT, sizeof(Rendering::InstanceData), (void*)offsetof(Rendering::InstanceData, objectID));
50 funcs->glVertexAttribDivisor(6, 1);
51
52 // color = location 7
53 funcs->glEnableVertexAttribArray(7);
54 funcs->glVertexAttribPointer(7, 3, GL_FLOAT, GL_FALSE, sizeof(Rendering::InstanceData), (void*)offsetof(Rendering::InstanceData, color));
55 funcs->glVertexAttribDivisor(7, 1);
56
57 funcs->glBindVertexArray(0);
58}
59
60void Mesh::createLocalAABB() {
61 glm::vec3 minV(std::numeric_limits<float>::max()), maxV(-std::numeric_limits<float>::max());
62 for (const Vertex& vertex : vertices) {
63 minV = glm::min(minV, vertex.pos);
64 maxV = glm::max(maxV, vertex.pos);
65 }
66 glm::vec3 center = (minV + maxV) * 0.5f;
67 glm::vec3 halfExtents = (maxV - minV) * 0.5f;
68 localAABB = Physics::Bounding::AABB(center, halfExtents);
69}
70
71
72void Mesh::draw() const {
73 funcs->glBindVertexArray(VAO);
74 glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr);
75 funcs->glBindVertexArray(0);
76}
77
78void Mesh::drawInstanced(const std::vector<Rendering::InstanceData>& instances) {
79 assert(VAO != 0 && "asdVAO generation failed — is GL context current?");
80 funcs->glBindVertexArray(VAO);
81
82 funcs->glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
83 funcs->glBufferData(GL_ARRAY_BUFFER, instances.size() * sizeof(Rendering::InstanceData), instances.data(), GL_DYNAMIC_DRAW);
84
85 funcs->glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr,
86 static_cast<GLsizei>(instances.size()));
87
88 funcs->glBindVertexArray(0);
89}
90
91std::span<const Vertex> Mesh::getVertices() const {
92 return vertices;
93}
94
95std::span<const unsigned int> Mesh::getIndices() const {
96 return indices;
97}
~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
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.
Definition Mesh.cpp:5
std::span< const Vertex > getVertices() const
Provides read-only view of vertex data without copying.
Definition Mesh.cpp:91
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
Per-instance data for instanced rendering.
Vertex data for a single vertex in a mesh.
Definition Mesh.h:13