-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcylinder_geometry.h
199 lines (158 loc) · 7 KB
/
cylinder_geometry.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
#ifndef CYLINDER_GEOMETRY_H
#define CYLINDER_GEOMETRY_H
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "model_renderer.h"
#include "compute_normals.h"
class CylinderGeometry : public IGeometry
{
public:
CylinderGeometry(float radius, float height, glm::vec3 color_top, glm::vec3 color_bottom, glm::vec3 color_side)
{
const int SAMPLES = 30;
m_vertices = new GLfloat[3 * (4 * SAMPLES + 2)];
m_colors = new GLfloat[3 * (4 * SAMPLES + 2)];
m_normals = new GLfloat[3 * (4 * SAMPLES + 2)];
m_faces = new GLuint[3 * (2 * SAMPLES + SAMPLES + SAMPLES)];
int face_counter = 0;
int vertex_counter = 0;
int zero_vertex;
// top face (circle)
zero_vertex = vertex_counter; // save initial vertex index
m_vertices[vertex_counter * 3 + 0] = 0.0f;
m_vertices[vertex_counter * 3 + 1] = 0.0f;
m_vertices[vertex_counter * 3 + 2] = height / 2.0f;
m_colors[vertex_counter * 3 + 0] = color_top.r;
m_colors[vertex_counter * 3 + 1] = color_top.g;
m_colors[vertex_counter * 3 + 2] = color_top.b;
m_normals[vertex_counter * 3 + 0] = 0.0f;
m_normals[vertex_counter * 3 + 1] = 0.0f;
m_normals[vertex_counter * 3 + 2] = 1.0f;
vertex_counter++;
for (int i = 0; i < SAMPLES; i++)
{
const float angle = float(i) / float(SAMPLES) * glm::pi<float>() * 2.0f;
const float x = radius * std::cos(angle);
const float y = radius * std::sin(angle);
const float z = height / 2.0f;
m_vertices[vertex_counter * 3 + 0] = x;
m_vertices[vertex_counter * 3 + 1] = y;
m_vertices[vertex_counter * 3 + 2] = z;
m_colors[vertex_counter * 3 + 0] = color_top.r;
m_colors[vertex_counter * 3 + 1] = color_top.g;
m_colors[vertex_counter * 3 + 2] = color_top.b;
m_normals[vertex_counter * 3 + 0] = 0.0f;
m_normals[vertex_counter * 3 + 1] = 0.0f;
m_normals[vertex_counter * 3 + 2] = 1.0f;
vertex_counter++;
}
for (int i = 0; i < SAMPLES; i++)
{
m_faces[face_counter * 3 + 0] = zero_vertex;
m_faces[face_counter * 3 + 1] = zero_vertex + 1 + i;
m_faces[face_counter * 3 + 2] = zero_vertex + 1 + (i + 1) % SAMPLES;
face_counter++;
}
// bottom face (circle)
zero_vertex = vertex_counter; // save initial vertex index
m_vertices[vertex_counter * 3 + 0] = 0.0f;
m_vertices[vertex_counter * 3 + 1] = 0.0f;
m_vertices[vertex_counter * 3 + 2] = -height / 2.0f;
m_colors[vertex_counter * 3 + 0] = color_bottom.r;
m_colors[vertex_counter * 3 + 1] = color_bottom.g;
m_colors[vertex_counter * 3 + 2] = color_bottom.b;
m_normals[vertex_counter * 3 + 0] = 0.0f;
m_normals[vertex_counter * 3 + 1] = 0.0f;
m_normals[vertex_counter * 3 + 2] = -1.0f;
vertex_counter++;
for (int i = 0; i < SAMPLES; i++)
{
const float angle = float(i) / float(SAMPLES) * glm::pi<float>() * 2.0f;
const float x = radius * std::cos(angle);
const float y = radius * std::sin(angle);
const float z = -height / 2.0f;
m_vertices[vertex_counter * 3 + 0] = x;
m_vertices[vertex_counter * 3 + 1] = y;
m_vertices[vertex_counter * 3 + 2] = z;
m_colors[vertex_counter * 3 + 0] = color_bottom.r;
m_colors[vertex_counter * 3 + 1] = color_bottom.g;
m_colors[vertex_counter * 3 + 2] = color_bottom.b;
m_normals[vertex_counter * 3 + 0] = 0.0f;
m_normals[vertex_counter * 3 + 1] = 0.0f;
m_normals[vertex_counter * 3 + 2] = -1.0f;
vertex_counter++;
}
for (int i = 0; i < SAMPLES; i++)
{
m_faces[face_counter * 3 + 0] = zero_vertex;
m_faces[face_counter * 3 + 1] = zero_vertex + 1 + (i + 1) % SAMPLES;
m_faces[face_counter * 3 + 2] = zero_vertex + 1 + i;
face_counter++;
}
// duplicate top and bottom points (color is different), connect them
zero_vertex = vertex_counter;
for (int i = 0; i < SAMPLES; i++)
{
const float angle = float(i) / float(SAMPLES) * glm::pi<float>() * 2.0f;
const float x = radius * std::cos(angle);
const float y = radius * std::sin(angle);
const float zt = height / 2.0f;
const float zb = -height / 2.0f;
glm::vec3 normal(x,y,0.0f);
normal = glm::normalize(normal);
m_vertices[vertex_counter * 3 + 0] = x;
m_vertices[vertex_counter * 3 + 1] = y;
m_vertices[vertex_counter * 3 + 2] = zt;
m_colors[vertex_counter * 3 + 0] = color_side.r;
m_colors[vertex_counter * 3 + 1] = color_side.g;
m_colors[vertex_counter * 3 + 2] = color_side.b;
m_normals[vertex_counter * 3 + 0] = normal.x;
m_normals[vertex_counter * 3 + 1] = normal.y;
m_normals[vertex_counter * 3 + 2] = normal.z;
vertex_counter++;
m_vertices[vertex_counter * 3 + 0] = x;
m_vertices[vertex_counter * 3 + 1] = y;
m_vertices[vertex_counter * 3 + 2] = zb;
m_colors[vertex_counter * 3 + 0] = color_side.r;
m_colors[vertex_counter * 3 + 1] = color_side.g;
m_colors[vertex_counter * 3 + 2] = color_side.b;
m_normals[vertex_counter * 3 + 0] = normal.x;
m_normals[vertex_counter * 3 + 1] = normal.y;
m_normals[vertex_counter * 3 + 2] = normal.z;
vertex_counter++;
m_faces[face_counter * 3 + 0] = zero_vertex + i * 2;
m_faces[face_counter * 3 + 1] = zero_vertex + i * 2 + 1;
m_faces[face_counter * 3 + 2] = zero_vertex + ((i + 1) % SAMPLES) * 2 + 1;
face_counter++;
m_faces[face_counter * 3 + 0] = zero_vertex + i * 2;
m_faces[face_counter * 3 + 1] = zero_vertex + ((i + 1) % SAMPLES) * 2 + 1;
m_faces[face_counter * 3 + 2] = zero_vertex + ((i + 1) % SAMPLES) * 2;
face_counter++;
}
m_faces_size = face_counter;
m_vertices_size = vertex_counter;
}
~CylinderGeometry()
{
delete[] m_vertices;
delete[] m_colors;
delete[] m_faces;
delete[] m_normals;
}
const GLfloat * vertices() { return m_vertices; }
const GLfloat * colors() { return m_colors; }
const GLfloat * normals() { return m_normals; }
const GLuint * faces() { return m_faces; }
GLsizei verticesSize() { return m_vertices_size; }
GLsizei size() { return m_faces_size * 3; }
GLenum type() { return GL_TRIANGLES; }
private:
GLfloat * m_vertices;
GLfloat * m_colors;
GLfloat * m_normals;
GLsizei m_vertices_size;
GLuint * m_faces;
GLsizei m_faces_size;
};
#endif // CYLINDER_GEOMETRY_H