-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawablemesh.h
executable file
·152 lines (131 loc) · 4.54 KB
/
drawablemesh.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
#ifndef DRAWABLEMESH_H
#define DRAWABLEMESH_H
#include "material.h"
#include "polygonalmesh.h"
#include "shader.h"
class DrawableMesh : public PolygonalMesh {
public:
DrawableMesh() {
for (auto &color : colorPalette) {
color /= 255.0;
}
}
void drawMesh() {
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT,
0);
glBindVertexArray(0);
}
void setMaterial(const Material &value) { material = value; }
glm::mat4 getModelMatrix() const { return m_modelMatrix; }
double alphaValue{1.0f};
protected:
uint VAO, VBO, EBO;
Material material{
glm::vec3(0.05, 0.05, 0.05),
glm::vec3(1.0, 0.75, 0.5), // should be static constexpr
glm::vec3(0.1, 0.075, 0.05), 80 * 0.85};
glm::mat4 m_modelMatrix{1.0}; // modelSpace->worldSpace
std::vector<glm::vec3> colorPalette{
// this gets normalized in the constructor!
// NOTE this should be static
glm::vec3(128.0, 0, 0), glm::vec3(255, 0, 0),
glm::vec3(255, 200, 220), glm::vec3(170.0, 110, 40),
glm::vec3(255.0, 150, 0), glm::vec3(255.0, 215.0, 180),
glm::vec3(128, 128, 0), glm::vec3(255, 235, 0),
glm::vec3(255, 250, 200), glm::vec3(190, 255, 0),
glm::vec3(0, 190, 0), glm::vec3(170, 255, 195),
glm::vec3(0, 128, 128), glm::vec3(100, 255, 255),
glm::vec3(0, 0, 128), glm::vec3(67, 133, 255),
glm::vec3(130, 0, 150), glm::vec3(230, 190, 255),
glm::vec3(255, 0, 255), glm::vec3(128, 128, 128)};
protected:
virtual void setUniforms(Shader *shader) = 0;
void updateMeshBuffers() {
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER,
m_vertices.size() * sizeof(MyVertex),
&m_vertices[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
m_indices.size() * sizeof(GLuint), &m_indices[0],
GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
sizeof(MyVertex), (GLvoid *)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
sizeof(MyVertex),
(GLvoid *)offsetof(MyVertex, Normal));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE,
sizeof(MyVertex),
(GLvoid *)offsetof(MyVertex, Color));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
void initializeDrawingBuffers() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER,
m_vertices.size() * sizeof(MyVertex),
&m_vertices[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
m_indices.size() * sizeof(GLuint), &m_indices[0],
GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
sizeof(MyVertex), (GLvoid *)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
sizeof(MyVertex),
(GLvoid *)offsetof(MyVertex, Normal));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE,
sizeof(MyVertex),
(GLvoid *)offsetof(MyVertex, Color));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
// std::cout << "printDebugInformation was called in
// "<<__func__<<std::endl;
// // printDebugInformation();
//
}
void printDebugInformation() const {
glBindVertexArray(VAO);
for (int i = 0; i <= 1; i++) {
GLint ival;
GLvoid *pval;
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED,
&ival);
printf("Attr %d: ENABLED = %d\n", i,
ival);
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_SIZE,
&ival);
printf("Attr %d: SIZE = %d\n", i, ival);
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_STRIDE,
&ival);
printf("Attr %d: STRIDE = %d\n", i, ival);
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_TYPE,
&ival);
printf("Attr %d: TYPE = 0x%x\n", i, ival);
glGetVertexAttribiv(
i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &ival);
printf("Attr %d: NORMALIZED = %d\n", i,
ival);
glGetVertexAttribiv(
i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &ival);
printf("Attr %d: BUFFER = %d\n", i, ival);
glGetVertexAttribPointerv(
i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pval);
printf("Attr %d: POINTER = %d\n", i,
ival);
}
// Also print the numeric handle of the VAO:
printf("VAO = %ld\n", long(VAO));
}
};
#endif // DRAWABLEMESH_H