-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCylinder.cpp
More file actions
156 lines (133 loc) · 4.23 KB
/
Cylinder.cpp
File metadata and controls
156 lines (133 loc) · 4.23 KB
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
#include <glm/ext.hpp>
#include "Cylinder.h"
Cylinder::Cylinder() : m_id(255), m_aabb(glm::vec3(-1,-1,0), glm::vec3(1, 1, 1)) {
m_nDivisions = 50;
m_nVertices = (m_nDivisions + 1) * 2 + 1;
const int floatsPerVertex = 14;
const float lu = 2;
const float lv = 6;
const int vboSize = floatsPerVertex * m_nVertices * sizeof(float);
float* vboData = new float[floatsPerVertex * m_nVertices];
int idx = 0;
for (int i = 0; i <= m_nDivisions; ++i) {
float theta = float(i) / float(m_nDivisions) * 2 * glm::pi<float>();
/* top vertex */
// pos
vboData[idx++] = glm::cos(theta);
vboData[idx++] = glm::sin(theta);
vboData[idx++] = 1.0f;
// normal
vboData[idx++] = glm::cos(theta);
vboData[idx++] = glm::sin(theta);
vboData[idx++] = 0.0f;
// uv
vboData[idx++] = float(i) / float(m_nDivisions) * lu;
vboData[idx++] = 1.0f * lv;
// udir
vboData[idx++] = -glm::sin(theta);
vboData[idx++] = glm::cos(theta);
vboData[idx++] = 0.0f;
// vdir
vboData[idx++] = 0.0f;
vboData[idx++] = 0.0f;
vboData[idx++] = 1.0f;
/*bottom vertex*/
// pos
vboData[idx++] = glm::cos(theta);
vboData[idx++] = glm::sin(theta);
vboData[idx++] = 0.0f;
// normal
vboData[idx++] = glm::cos(theta);
vboData[idx++] = glm::sin(theta);
vboData[idx++] = 0.0f;
// uv
vboData[idx++] = float(i) / float(m_nDivisions) * lu;
vboData[idx++] = 0.0f;
// udir
vboData[idx++] = -glm::sin(theta);
vboData[idx++] = glm::cos(theta);
vboData[idx++] = 0.0f;
// vdir
vboData[idx++] = 0.0f;
vboData[idx++] = 0.0f;
vboData[idx++] = 1.0f;
}
/* top-center vertex */
// pos
vboData[idx++] = 0.0f;
vboData[idx++] = 0.0f;
vboData[idx++] = 1.0f;
// normal
vboData[idx++] = 0.0f;
vboData[idx++] = 0.0f;
vboData[idx++] = 0.0f;
// uv
vboData[idx++] = 0.0f;
vboData[idx++] = 0.0f;
// udir
vboData[idx++] = 1.0f;
vboData[idx++] = 0.0f;
vboData[idx++] = 0.0f;
// vdir
vboData[idx++] = 0.0f;
vboData[idx++] = 1.0f;
vboData[idx++] = 0.0f;
ushort* iboData = new ushort[m_nVertices + m_nDivisions + 1];
size_t iboSize = (m_nVertices + m_nDivisions + 1) * sizeof(ushort);
for (int i = 0; i < m_nVertices; ++i) {
iboData[i] = i;
}
for (int i = 0; i <= m_nDivisions; ++i) {
iboData[i + m_nVertices] = i*2;
}
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, vboSize, (void*)vboData, GL_STATIC_DRAW);
delete[] vboData;
glGenBuffers(1, &m_ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iboSize, (void*)iboData, GL_STATIC_DRAW);
delete[] iboData;
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, floatsPerVertex * sizeof(float), (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, floatsPerVertex * sizeof(float), (void*)(3 * sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, floatsPerVertex * sizeof(float), (void*)(6 * sizeof(float)));
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, floatsPerVertex * sizeof(float), (void*)(8 * sizeof(float)));
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, floatsPerVertex * sizeof(float), (void*)(11 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
Cylinder::~Cylinder() {
glDeleteVertexArrays(1, &m_vao);
glDeleteBuffers(1, &m_vbo);
glDeleteBuffers(1, &m_ibo);
}
CylinderInstance::CylinderInstance(Cylinder* cylinder) :
m_cyl(cylinder),
m_M(1.0f),
m_aabb(m_cyl->getAABB()) {}
CylinderInstance::CylinderInstance() :
m_cyl(nullptr),
m_M(1.0f) {}
void CylinderInstance::draw() const {
assert(m_cyl);
glBindVertexArray(m_cyl->getVao());
glDrawElements(GL_TRIANGLE_STRIP, m_cyl->getNumVertices() - 1, GL_UNSIGNED_SHORT, (void*)0);
glDrawElements(GL_TRIANGLE_FAN, m_cyl->getNumDivisions() + 2, GL_UNSIGNED_SHORT, (void*)((m_cyl->getNumVertices() - 1)*sizeof(ushort)));
glBindVertexArray(0);
}
void CylinderInstance::setM(const glm::mat4& M) {
m_M = M;
m_aabb = m_cyl->getAABB().transform(m_M);
}
void CylinderInstance::transform(const glm::mat4& T) {
m_M = T * m_M;
m_aabb = m_cyl->getAABB().transform(m_M);
}