-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimitiveCylinder.cpp
More file actions
162 lines (128 loc) · 4.27 KB
/
Copy pathPrimitiveCylinder.cpp
File metadata and controls
162 lines (128 loc) · 4.27 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
157
158
159
160
161
162
#include "PrimitiveCylinder.h"
constexpr float PI = 3.141592653589f;
constexpr float PI2 = PI * 2;
PrimitiveCylinder::PrimitiveCylinder(Dimensions dims, glm::vec3 colour) : PrimitiveCylinder() {
this->dimensions = dims;
this->rgb = colour;
}
void PrimitiveCylinder::makeObject()
{
constructVertices(this->vertex_positions, this->vertex_normals, numberOfvertices, this->dimensions);
constructIndices(this->indices, this->numberOfvertices, this->sizeOfIndices);
constructVertexColours(this->vertex_colours, this->rgb, numberOfvertices);
Object3D::makeObject();
glGenBuffers(1, &this->indexBufferObj);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBufferObj);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void PrimitiveCylinder::drawObject(bool clockwise)
{
Object3D::drawObject(clockwise);
int numfanvertices = this->dimensions.sections + 2;
int numsidevertices = numfanvertices * 2;
int side_offset = this->dimensions.sections * 2 + 2;
// Draw the cylinder using filled triangles
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBufferObj);
// Draw the top lid
glDrawElements(GL_TRIANGLE_FAN, numfanvertices, GL_UNSIGNED_INT, nullptr);
// Draw the bottom lid
glDrawElements(GL_TRIANGLE_FAN, numfanvertices, GL_UNSIGNED_INT, (GLvoid*)(numfanvertices * sizeof(GLuint)));
// Draw the sides
glDrawElements(GL_TRIANGLE_STRIP, side_offset, GL_UNSIGNED_INT, (GLvoid*)(numsidevertices * sizeof(GLuint)));
}
void constructVertices(
std::vector<glm::vec3>& vert_pos,
std::vector<glm::vec3>& vert_normals,
const unsigned int const vert_count,
const Dimensions const dims
)
{
// Initialize vectors
vert_pos = std::vector<glm::vec3>(vert_count, { 0,0,0 });
vert_normals = std::vector<glm::vec3>(vert_count, { 0,0,0 });
GLfloat halfLength = dims.length / 2;
vert_pos[0] = { 0, halfLength, 0 };
vert_normals[0] = { 0.0, 1.0, 0.0 };
// Create a parallel region - https://stackoverflow.com/questions/1448318/omp-parallel-vs-omp-parallel-for - 09/11/2023
#pragma omp parallel
{
#pragma omp for // Parallelize for loop
for (size_t i = 1; i < dims.sections + 1; i++) {
GLfloat theta = (PI2) / dims.sections * i;
GLfloat x = dims.radius * cos(theta);
GLfloat y = halfLength;
GLfloat z = dims.radius * sin(theta);
vert_pos[i] = { x,y,z };
vert_normals[i] = { 0, 1, 0 };
}
#pragma omp single // Run section on a single thread
{
vert_pos[dims.sections + 1] = { 0, -halfLength, 0 };
vert_normals[dims.sections + 1] = { 0, -1, 0 };
}
//for every point around the circle
#pragma omp for // Parallelize for loop
for (int i = 102; i < (dims.sections * 2) + 2; i++)
{
GLfloat theta = (2 * PI) / dims.sections * (i - 102);
GLfloat x = dims.radius * cos(theta);
GLfloat y = -halfLength;
GLfloat z = dims.radius * sin(theta);
vert_pos[i] = { x, y, z };
vert_normals[i] = { 0.0, -1.0, 0.0 };
}
unsigned int top_edge = 1;
unsigned int bottom_edge = 102;
#pragma omp for // Parallelize for loop
for (int i = ((dims.sections * 2) + 2); i < vert_count; i += 2)
{
vert_pos[i] = vert_pos[top_edge];
vert_normals[i] = { vert_pos[top_edge].x, 0.0, vert_pos[top_edge].z };
vert_pos[i + 1] = vert_pos[bottom_edge];
vert_normals[i + 1] = { vert_pos[bottom_edge].x, 0.0, vert_pos[bottom_edge].z };
top_edge++;
bottom_edge++;
}
}
}
void constructIndices(
std::vector<GLuint>& indices,
const unsigned int const vert_count,
GLuint& sizeofIndices
)
{
const unsigned int index_count = vert_count + 4;
// Parallelize region of code
#pragma omp parallel
{
indices = std::vector<GLuint>(index_count, 0);
#pragma omp for
for (int i = 0; i < 101; i++)
{
indices[i] = i;
}
#pragma omp for
for (int i = 102; i < 203; i++)
{
indices[i] = i - 1;
}
#pragma omp for
for (int i = 204; i < 404; i++)
{
indices[i] = i - 2;
}
#pragma omp single
{
indices[101] = 1;
indices[203] = 102;
indices[404] = 202;
indices[405] = 203;
sizeofIndices = (sizeof(indices) / sizeof(*&indices[0]));
}
}
}
void constructVertexColours(std::vector<glm::vec4>& vert_colours, const glm::vec3 const rgb, const unsigned int const vert_count)
{
vert_colours = std::vector<glm::vec4>(vert_count, { rgb.r, rgb.g, rgb.b, 1 });
}