-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfigure.cpp
117 lines (97 loc) · 4.16 KB
/
figure.cpp
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
#include "figure.h"
void createFigureVAO(Figure* fig)
{
glGenVertexArrays(1, &fig->VAO);
glBindVertexArray(fig->VAO);
// Genero, rendo attivo, riempio il VBO della geometria dei vertici
glGenBuffers(1, &fig->VBO_Geom);
glBindBuffer(GL_ARRAY_BUFFER, fig->VBO_Geom);
glBufferData(GL_ARRAY_BUFFER, fig->vertices.size() * sizeof(Point3D), fig->vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
// Genero, rendo attivo, riempio il VBO dei colori
glGenBuffers(1, &fig->VBO_Col);
glBindBuffer(GL_ARRAY_BUFFER, fig->VBO_Col);
glBufferData(GL_ARRAY_BUFFER, fig->colors.size() * sizeof(ColorRGBA), fig->colors.data(), GL_STATIC_DRAW);
// Adesso carico il VBO dei colori nel layer 2
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
}
void updateFigureVertices(Figure* fig)
{
glBindBuffer(GL_ARRAY_BUFFER, fig->VBO_Geom);
glBufferData(GL_ARRAY_BUFFER, fig->vertices.size() * sizeof(Point3D), fig->vertices.data(), GL_STATIC_DRAW);
}
void buildCircle(Figure* fig, Point3D center, float radius, int numTriangles, ColorRGBA colorExtern, ColorRGBA colorIntern)
{
// PI * 2 = complete circle => divide by num of triangles we want to use
float stepA = (PI * 2) / numTriangles;
for (int i = 0; i < numTriangles; i ++)
{
// Extern vertices
Point3D v0 = { center.x + radius * cos((double)i * stepA), center.y + radius * sin((double)i * stepA), 0.0f };
fig->vertices.push_back(v0);
fig->colors.push_back(colorExtern);
Point3D v1 = { center.x + radius * cos((double)(i + 1) * stepA), center.y + radius * sin((double)(i + 1) * stepA), 0.0f };
fig->vertices.push_back(v1);
fig->colors.push_back(colorExtern);
// Intern vertices
Point3D v2 = center;
fig->vertices.push_back(v2);
fig->colors.push_back(colorIntern);
}
}
void buildSemiCircle(Figure* fig, Point3D center, float radius, float startAngle, int numTriangles, ColorRGBA colorExtern, ColorRGBA colorIntern)
{
// PI = half circle => divide by num of triangles we want to use
float stepA = (PI * 2) / numTriangles;
for (int i = 0; i < numTriangles; i++)
{
float t0 = (float)i * stepA + startAngle;
float t1 = (float)(i+1) * stepA + startAngle;
// Extern vertices
Point3D v0 = { center.x + radius * cos(t0), center.y + radius * sin(t0), 0.0f };
fig->vertices.push_back(v0);
fig->colors.push_back(colorExtern);
Point3D v1 = { center.x + radius * cos(t1), center.y + radius * sin(t1), 0.0f };
fig->vertices.push_back(v1);
fig->colors.push_back(colorExtern);
// Intern vertices
Point3D v2 = center;
fig->vertices.push_back(v2);
fig->colors.push_back(colorIntern);
}
}
void buildHollowCircle(Figure* fig, Point3D center, float radiusExtern, float radiusIntern, int numTriangles, ColorRGBA colorExtern, ColorRGBA colorIntern)
{
float stepA = (PI * 2) / numTriangles;
for (int i = 0; i < numTriangles; i++)
{
// Extern vertices
Point3D v0 = { center.x + radiusExtern * cos((double)i * stepA), center.y + radiusExtern * sin((double)i * stepA), 0.0f };
fig->vertices.push_back(v0);
fig->colors.push_back(colorExtern);
Point3D v1 = { center.x + radiusExtern * cos((double)(i + 1) * stepA), center.y + radiusExtern * sin((double)(i + 1) * stepA), 0.0f };
fig->vertices.push_back(v1);
fig->colors.push_back(colorExtern);
// Intern vertices
Point3D v2 = { center.x + radiusIntern * cos((double)i * stepA), center.y + radiusIntern * sin((double)i * stepA), 0.0f };
fig->vertices.push_back(v2);
fig->colors.push_back(colorIntern);
Point3D v3 = { center.x + radiusIntern * cos((double)(i + 1) * stepA), center.y + radiusIntern * sin((double)(i + 1) * stepA), 0.0f };
fig->vertices.push_back(v3);
fig->colors.push_back(colorIntern);
}
}
void buildCircumference(Figure* fig, Point3D center, float radius, int numSegments, ColorRGBA color)
{
// PI * 2 = complete circle => divide by num of triangles we want to use
float stepA = (PI * 2) / numSegments;
for (int i = 0; i <= numSegments; i++)
{
// Extern vertices
Point3D v0 = { center.x + radius * cos((double)i * stepA), center.y + radius * sin((double)i * stepA), 0.0f };
fig->vertices.push_back(v0);
fig->colors.push_back(color);
}
}