-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcube.cpp
103 lines (92 loc) · 3.22 KB
/
cube.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
#include "cube.h"
#include <QMatrix4x4>
#include <QColor>
Cube::Cube(int _id, float x, float y, float z)
: id(_id)
, position(x, y, z)
{
vertices << QVector3D(-0.48, -0.48, 0.48) << QVector3D(0.48, -0.48, 0.48) << QVector3D(0.48, 0.48, 0.48) // Front
<< QVector3D(0.48, 0.48, 0.48) << QVector3D(-0.48, 0.48, 0.48) << QVector3D(-0.48, -0.48, 0.48)
<< QVector3D(0.48, -0.48, -0.48) << QVector3D(-0.48, -0.48, -0.48) << QVector3D(-0.48, 0.48, -0.48) // Back
<< QVector3D(-0.48, 0.48, -0.48) << QVector3D(0.48, 0.48, -0.48) << QVector3D(0.48, -0.48, -0.48)
<< QVector3D(-0.48, -0.48, -0.48) << QVector3D(-0.48, -0.48, 0.48) << QVector3D(-0.48, 0.48, 0.48) // Left
<< QVector3D(-0.48, 0.48, 0.48) << QVector3D(-0.48, 0.48, -0.48) << QVector3D(-0.48, -0.48, -0.48)
<< QVector3D(0.48, -0.48, 0.48) << QVector3D(0.48, -0.48, -0.48) << QVector3D(0.48, 0.48, -0.48) // Right
<< QVector3D(0.48, 0.48, -0.48) << QVector3D(0.48, 0.48, 0.48) << QVector3D(0.48, -0.48, 0.48)
<< QVector3D(-0.48, 0.48, 0.48) << QVector3D(0.48, 0.48, 0.48) << QVector3D(0.48, 0.48, -0.48) // Top
<< QVector3D(0.48, 0.48, -0.48) << QVector3D(-0.48, 0.48, -0.48) << QVector3D(-0.48, 0.48, 0.48)
<< QVector3D(-0.48, -0.48, -0.48) << QVector3D(0.48, -0.48, -0.48) << QVector3D(0.48, -0.48, 0.48) // Bottom
<< QVector3D(0.48, -0.48, 0.48) << QVector3D(-0.48, -0.48, 0.48) << QVector3D(-0.48, -0.48, -0.48);
setToStdColor();
}
QQuaternion Cube::getRotation() const
{
return rotation;
}
int Cube::getId() const
{
return id;
}
QVector<QVector3D> Cube::getVertices() const
{
return vertices;
}
QVector<QVector3D> Cube::getColorVectorById() const
{
QVector<QVector3D> colorsById;
for (int i = 0; i < 36; ++i) {
colorsById << QVector3D(id / 255.0f, id / 255.0f, id / 255.0f);
}
return colorsById;
}
QVector<QVector3D> Cube::getColors() const
{
return colors;
}
void Cube::setToStdColor()
{
colors.clear();
for (int i = 0; i < 6; ++i) {
colors << QVector3D(0.6, 0, 0); //FRONT
}
for (int i = 0; i < 6; ++i) {
colors << QVector3D(0.9, 0.4, 0); //BACK
}
for (int i = 0; i < 6; ++i) {
colors << QVector3D(0, 0.4, 0); //LEFT
}
for (int i = 0; i < 6; ++i) {
colors << QVector3D(0, 0, 0.4); //RIGHT
}
for (int i = 0; i < 6; ++i) {
colors << QVector3D(0.9, 0.9, 0.9); //TOP
}
for (int i = 0; i < 6; ++i) {
colors << QVector3D(0.8, 0.8, 0); //BOTTOM
}
}
//darkens the colors of the whole cube
void Cube::setColorHighlight()
{
QVector<QVector3D> highlightedColors;
for (int i = 0; i < colors.size(); ++i) {
highlightedColors << colors.at(i) * QVector3D(0.3, 0.3, 0.3);
}
colors = highlightedColors;
}
QVector3D Cube::getPosition() const
{
return position;
}
void Cube::rotateX(int angle)
{
rotation = QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), angle) * rotation;
}
void Cube::rotateY(int angle)
{
rotation = QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), angle) * rotation;
}
void Cube::rotateZ(int angle)
{
rotation = QQuaternion::fromAxisAndAngle(QVector3D(0, 0, 1), angle) * rotation;
}