-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cpp
More file actions
130 lines (108 loc) · 2.84 KB
/
Sphere.cpp
File metadata and controls
130 lines (108 loc) · 2.84 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
#include "Sphere.h"
#include <qmath.h>
Sphere::Sphere(Node *parent) : Mesh(parent)
{
setName("Ball");
setType("Sphere");
createDOF(DOF::RotationX, true, false, false, 0, 0, 0);
createDOF(DOF::RotationY, true, false, false, 1, 0, 0);
createDOF(DOF::RotationZ, true, false, false, 0, 0, 0);
createDOF(DOF::Speed, true, true, true, 0.5, -4 * M_PI, 4 * M_PI);
}
Sphere::~Sphere()
{
}
void Sphere::init()
{
Mesh::init();
set(QVector3D(), 1, 10);
}
void Sphere::set(const QVector3D ¢er, float radius, int precision)
{
_center = center;
_radius = radius;
_precision = precision;
_set();
}
const QVector3D &Sphere::center() const
{
return _center;
}
void Sphere::setCenter(const QVector3D ¢er)
{
_center = center;
_set();
}
float Sphere::radius() const
{
return _radius;
}
void Sphere::setRadius(float radius)
{
_radius = radius;
_set();
}
int Sphere::precision() const
{
return _precision;
}
void Sphere::setPrecision(float precision)
{
_precision = precision;
_set();
}
QVector3D Sphere::axe() const
{
return QVector3D(DOFValue(DOF::RotationX), DOFValue(DOF::RotationY), DOFValue(DOF::RotationZ));
}
void Sphere::setAxe(const QVector3D &axe)
{
setDOFValue(DOF::RotationX, axe.x());
setDOFValue(DOF::RotationY, axe.y());
setDOFValue(DOF::RotationZ, axe.z());
}
float Sphere::speed() const
{
return DOFValue(DOF::Speed);
}
void Sphere::setSpeed(float speed)
{
setDOFValue(DOF::Speed, speed);
}
void Sphere::_set()
{
_vertices.resize((_precision + 1) * (_precision + 1) * 3);
_normals.resize(_vertices.size());
_faces.resize(_precision * _precision * 6);
int vIdx = 0;
int fIdx = -1;
for (int sl = 0; sl <= _precision; ++sl) {
float theta = sl * M_PI / _precision;
for (int st = 0; st <= _precision; ++st) {
float phi = st * 2.0 * M_PI / _precision;
float x = sin(theta) * cos(phi);
float y = cos(theta);
float z = sin(theta) * sin(phi);
_vertices[vIdx] = x * _radius + _center.x();
_vertices[vIdx + 1] = y * _radius + _center.y();
_vertices[vIdx + 2] = z * _radius + _center.z();
_normals[vIdx] = x;
_normals[vIdx + 1] = y;
_normals[vIdx + 2] = z;
vIdx += 3;
if (sl != _precision && st != _precision) {
float indice1 = sl * (_precision + 1) + st;
float indice2 = indice1 + _precision + 1;
_faces[++fIdx] = indice1;
_faces[++fIdx] = indice2;
_faces[++fIdx] = indice1 + 1;
_faces[++fIdx] = indice2;
_faces[++fIdx] = indice2 + 1;
_faces[++fIdx] = indice1 + 1;
}
}
}
setVertices(_vertices);
setNormals(_normals);
setFaces(_faces);
}