Loading...
Searching...
No Matches
Mesh.cpp
Go to the documentation of this file.
1#include "hellfire/graphics/Mesh.h"
2#include <unordered_map>
3#include "hellfire/core/Application.h"
4#include "hellfire/graphics/Vertex.h"
5#include "hellfire/graphics/material/Material.h"
6
7namespace hellfire {
8 Mesh::Mesh() : vao_(nullptr), vbo_(nullptr), ibo_(nullptr), index_count_(0) {
9 }
10
11 Mesh::Mesh(const std::vector<Vertex> &vertices,
12 const std::vector<unsigned int> &indices) : vertices(vertices), indices(indices), index_count_(0) {
14 }
15
16 Mesh::Mesh(const std::vector<Vertex> &vertices, const std::vector<unsigned int> &indices, bool defer_build) : vertices(vertices), indices(indices), index_count_(0) {
17 if (!defer_build) {
19 }
20 }
21
22 void Mesh::bind() const {
23 vao_->bind();
24 }
25
26 void Mesh::unbind() const {
27 vao_->unbind();
28 }
29
30 void Mesh::build() {
32 }
33
34 void Mesh::create_mesh() {
35 vao_ = std::make_unique<VA>();
36 vbo_ = std::make_unique<VB>();
37 ibo_ = std::make_unique<IB>();
38
39 vao_->bind();
40
41 vbo_->bind();
42 vbo_->pass_data(vertices);
43
44 ibo_->bind();
45 ibo_->pass_data(indices);
46
47 // Layout 0: Position
48 glEnableVertexAttribArray(0);
49 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
50 reinterpret_cast<void *>(offsetof(Vertex, position)));
51
52 // Layout 1: Normal
53 glEnableVertexAttribArray(1);
54 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
55 reinterpret_cast<void *>(offsetof(Vertex, normal)));
56
57 // Layout 2: Color
58 glEnableVertexAttribArray(2);
59 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
60 reinterpret_cast<void *>(offsetof(Vertex, color)));
61
62 // Layout 3: texCoords (Uv's)
63 glEnableVertexAttribArray(3);
64 glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
65 reinterpret_cast<void *>(offsetof(Vertex, texCoords)));
66
67 // Layout 4: Tangent
68 glEnableVertexAttribArray(4);
69 glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
70 reinterpret_cast<void *>(offsetof(Vertex, tangent)));
71
72 // Layout 5: Bi Tangent
73 glEnableVertexAttribArray(5);
74 glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
75 reinterpret_cast<void *>(offsetof(Vertex, bitangent)));
76
77 // Unbind the buffers
78 vao_->unbind();
79 vbo_->unbind();
80 ibo_->unbind();
81 }
82
83 void Mesh::draw() const {
84 vao_->bind();
85 glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, nullptr);
86 vao_->unbind();
87 }
88
89 void Mesh::draw_instanced(const size_t amount) const {
90 vao_->bind();
91 glDrawElementsInstanced(GL_TRIANGLES, get_index_count(),
92 GL_UNSIGNED_INT, nullptr, amount);
93 vao_->unbind();
94 }
95
96 int Mesh::get_index_count() const {
97 return indices.size();
98 }
99}
Mesh(const std::vector< Vertex > &vertices, const std::vector< unsigned int > &indices)
Definition Mesh.cpp:11
int get_index_count() const
Definition Mesh.cpp:96
void bind() const
Definition Mesh.cpp:22
void build()
Definition Mesh.cpp:30
void create_mesh()
Definition Mesh.cpp:34
void draw() const
Definition Mesh.cpp:83
void unbind() const
Definition Mesh.cpp:26
Mesh(const std::vector< Vertex > &vertices, const std::vector< unsigned int > &indices, bool defer_build)
Definition Mesh.cpp:16
int index_count_
Definition Mesh.h:45