-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.h
265 lines (214 loc) · 7.66 KB
/
mesh.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#pragma once
// openGL functions
#include <GL/glew.h>
// matrix math
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vector>
#include "shader.h"
extern std::vector<GLuint> cubeElements;
extern std::vector<GLuint> pyramidElements;
struct Vertex {
Vertex(glm::vec3 position) :
position(position) {
}
glm::vec3 position;
};
struct TVertex : Vertex {
TVertex(glm::vec3 position, glm::vec2 texcoord) :
Vertex(position), texcoord(texcoord) {
}
glm::vec2 texcoord;
};
struct CVertex : Vertex {
CVertex(glm::vec3 position, glm::vec3 color) :
Vertex(position), color(color) {
}
glm::vec3 color;
};
struct CTVertex : Vertex {
CTVertex(CVertex cvert, glm::vec2 texcoord) :
Vertex(cvert.position), color(cvert.color), texcoord(texcoord) {
}
CTVertex(glm::vec3 position, glm::vec3 color, glm::vec2 texcoord) :
Vertex(position), color(color), texcoord(texcoord) {
}
glm::vec3 color;
glm::vec2 texcoord;
};
template<class Vertex>
class Mesh {
public:
const GLuint TRIS;
virtual void render() = 0;
Mesh(std::vector<Vertex>& vertices, std::vector<GLuint>& indices) : TRIS(indices.size()) {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind array to remember attributes
glBindVertexArray(VAO);
// load vertex buffer onto GPU
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
// load element buffer onto GPU
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
// set up attribute pointers
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
glBindVertexArray(0);
}
~Mesh() {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
}
protected:
// render handles
GLuint VAO, VBO, EBO;
};
// used for standard models (none in game yet) and terrain (currently hardcoded for this)
class StandardMesh : Mesh<CTVertex> {
// bool manual mesh binding?
public:
StandardMesh(std::vector<CTVertex>& vertices, std::vector<GLuint> indices) : Mesh(vertices, indices) {
// set up attribute pointers
glBindVertexArray(VAO);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(CTVertex), (GLvoid*)offsetof(CTVertex, color));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(CTVertex), (GLvoid*)offsetof(CTVertex, texcoord));
glBindVertexArray(0);
}
// hard coded batching for now
void bindTextures(GLuint texture, GLuint noise) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, noise);
}
void unbind() {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
}
void render() override {
// set bindings
glBindVertexArray(VAO);
// draw
glDrawElements(GL_TRIANGLES, TRIS, GL_UNSIGNED_INT, 0);
// unbind stuff
glBindVertexArray(0);
}
};
template <class Vertex>
class IMesh : public Mesh<Vertex> {
public:
GLuint instanceCount, colorBuffer, modelBuffer;
bool built = false;
IMesh(std::vector<Vertex>& vertices, std::vector<GLuint>& indices) : Mesh<Vertex>(vertices, indices) {
}
~IMesh() {
if (built) {
glDeleteBuffers(1, &colorBuffer);
glDeleteBuffers(1, &modelBuffer);
}
}
// sets up instancing buffers starting at attribute 'start'
// could probably just make constructor do this but couldn't figure out how
// to handle adding additional attributes correctly in child classes
void setInstanceBuffers(GLuint attribStart) {
if (built) {
std::cout << "ERROR::ALREADY_BUILT_INSTANCE_BUFFER" << std::endl;
return;
}
glBindVertexArray(VAO);
// build color buffer
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glEnableVertexAttribArray(attribStart);
glVertexAttribPointer(attribStart, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (GLvoid*)0);
glVertexAttribDivisor(attribStart, 1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// build model buffer
glGenBuffers(1, &modelBuffer);
glBindBuffer(GL_ARRAY_BUFFER, modelBuffer);
for (int i = 0; i < 4; i++) {
glEnableVertexAttribArray(++attribStart);
glVertexAttribPointer(attribStart, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(i * sizeof(glm::vec4)));
glVertexAttribDivisor(attribStart, 1);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
built = true;
}
// stream should be set if this function is getting called each frame
void setColors(std::vector<glm::vec3>& colors, bool stream, int count = 0) {
if (colors.size() == 0 || !built) {
return;
}
if (count == 0) {
count = colors.size();
}
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, count * sizeof(glm::vec3), &colors[0], stream ? GL_STREAM_DRAW : GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void setModels(std::vector<glm::mat4>& models, bool stream, int count = 0) {
if (models.size() == 0 || !built) {
instanceCount = 0;
return;
}
if (count == 0) {
count = models.size();
}
instanceCount = count;
glBindBuffer(GL_ARRAY_BUFFER, modelBuffer);
glBufferData(GL_ARRAY_BUFFER, count * sizeof(glm::mat4), &models[0], stream ? GL_STREAM_DRAW : GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
};
class PIMesh : public IMesh <Vertex> {
public:
PIMesh(std::vector<Vertex>& vertices, std::vector<GLuint>& indices) : IMesh<Vertex>(vertices, indices) {
setInstanceBuffers(1);
}
void render() override {
if (instanceCount <= 0) {
return;
}
glBindVertexArray(VAO);
glDrawElementsInstanced(GL_TRIANGLES, TRIS, GL_UNSIGNED_INT, 0, instanceCount);
glBindVertexArray(0);
}
static std::vector<Vertex> cubeVertices;
};
class TIMesh : public IMesh<TVertex> {
public:
GLuint texture;
TIMesh(std::vector<TVertex>& vertices, std::vector<GLuint>& indices, GLuint texture) : IMesh<TVertex>(vertices, indices) {
this->texture = texture;
glBindVertexArray(VAO);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TVertex), (GLvoid*)offsetof(TVertex, texcoord));
glBindVertexArray(0);
setInstanceBuffers(2);
}
void render() override {
if (instanceCount <= 0) {
return;
}
// set bindings
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO);
glDrawElementsInstanced(GL_TRIANGLES, TRIS, GL_UNSIGNED_INT, 0, instanceCount);
// unbind stuff
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
static std::vector<TVertex> cubeVertices;
static std::vector<TVertex> offsetCubeVertices;
static std::vector<TVertex> pyramidVertices; // triangular pyramid
};