SolarSim
Loading...
Searching...
No Matches
mesh.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <glad/glad.h>
4#include <vector>
5
6namespace solarsim {
7
14 struct Mesh {
16 uint32_t vao = 0;
18 uint32_t vbo = 0;
20 uint32_t ebo = 0;
22 uint32_t vertexCount = 0;
23
25 GLenum drawMode = GL_TRIANGLES;
27 bool useElements = false;
28
30 bool useNormals = true;
31
33 std::vector<float> vertices;
35 std::vector<uint32_t> indices;
36
43 void setupBuffers() {
44 glGenVertexArrays(1, &vao);
45 glBindVertexArray(vao);
46
47 glGenBuffers(1, &vbo);
48 glBindBuffer(GL_ARRAY_BUFFER, vbo);
49 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
50
51 if (useElements && !indices.empty()) {
52 glGenBuffers(1, &ebo);
53 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
54 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint32_t), indices.data(), GL_STATIC_DRAW);
55 }
56
57 size_t fields = useNormals ? 2 : 1;
58 size_t stride = fields * 3;
59
60 // TODO: Add more vertex data
61 glEnableVertexAttribArray(0);
62 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride * sizeof(float), (void*)0);
63
64 if (useNormals) {
65 glEnableVertexAttribArray(1);
66 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride * sizeof(float), (void*)(3 * sizeof(float)));
67 }
68
69 glBindVertexArray(0);
70 }
71 };
72}
Definition engine.cpp:23
Represents 3D geometry with OpenGL buffers.
Definition mesh.hpp:14
void setupBuffers()
Initialize OpenGL buffers and vertex attributes.
Definition mesh.hpp:43
std::vector< uint32_t > indices
Definition mesh.hpp:35
uint32_t vbo
Definition mesh.hpp:18
bool useNormals
Definition mesh.hpp:30
uint32_t ebo
Definition mesh.hpp:20
GLenum drawMode
Definition mesh.hpp:25
std::vector< float > vertices
Definition mesh.hpp:33
uint32_t vao
Definition mesh.hpp:16
bool useElements
Definition mesh.hpp:27
uint32_t vertexCount
Definition mesh.hpp:22