Projekt

Obecné

Profil

Stáhnout (3.19 KB) Statistiky
| Větev: | Tag: | Revize:
1
#include "sphere.hpp"
2

    
3
const float Sphere::RADIUS = 300.0f;
4
const float Sphere::MAX_SCALING = 1.3f;
5
const float Sphere::MIN_SCALING = 0.8f;
6
const int Sphere::SMOOTHING = 5;
7

    
8
const QVector3D Sphere::middlePoint(const QVector3D &a, const QVector3D &b)
9
{
10
    return QVector3D((a + b) / 2.0f);
11
}
12

    
13
float Sphere::getMaxRadius()
14
{
15
    return RADIUS * MAX_SCALING;
16
}
17

    
18
Sphere::Sphere(bool moving): Element(moving ? Element::SPHERE : Element::SPHERE_FIXED)
19
{
20
    // Predni polokoule.
21
    QVector3D v1(0.0f, RADIUS, 0.0f);
22
    QVector3D v2(0.0f, 0.0f, RADIUS);
23
    QVector3D v3(RADIUS, 0.0f, 0.0f);
24
    subdivide(v1, v2, v3, SMOOTHING);
25
    
26
    QVector3D v4(0.0f, RADIUS, 0.0f);
27
    QVector3D v5(-1.0f * RADIUS, 0.0f, 0.0f);
28
    QVector3D v6(0.0f, 0.0f, RADIUS);
29
    subdivide(v4, v5, v6, SMOOTHING);
30
    
31
    QVector3D v13(0.0f, -1.0f * RADIUS, 0.0f);
32
    QVector3D v14(RADIUS, 0.0f, 0.0f);
33
    QVector3D v15(0.0f, 0.0f, RADIUS);
34
    subdivide(v13, v14, v15, SMOOTHING);
35
    
36
    QVector3D v16(0.0f, -1.0f * RADIUS, 0.0f);
37
    QVector3D v17(0.0f, 0.0f, RADIUS);
38
    QVector3D v18(-1.0f * RADIUS, 0.0f, 0.0f);
39
    subdivide(v16, v17, v18, SMOOTHING);
40
    
41
    // Zadni polokoule.
42
    QVector3D v7(0.0f, RADIUS, 0.0f);
43
    QVector3D v8(RADIUS, 0.0f, 0.0f);
44
    QVector3D v9(0.0f, 0.0f, -1.0f * RADIUS);
45
    subdivide(v7, v8, v9, SMOOTHING);
46
    
47
    QVector3D v10(0.0f, RADIUS, 0.0f);
48
    QVector3D v11(0.0f, 0.0f, -1.0f * RADIUS);
49
    QVector3D v12(-1.0f * RADIUS, 0.0f, 0.0f);
50
    subdivide(v10, v11, v12, SMOOTHING);
51
    
52
    QVector3D v19(0.0f, -1.0f * RADIUS, 0.0f);
53
    QVector3D v20(0.0f, 0.0f, -1.0f * RADIUS);
54
    QVector3D v21(RADIUS, 0.0f, 0.0f);
55
    subdivide(v19, v20, v21, SMOOTHING);
56
    
57
    QVector3D v22(0.0f, -1.0f * RADIUS, 0.0f);
58
    QVector3D v23(-1.0f * RADIUS, 0.0f, 0.0f);
59
    QVector3D v24(0.0f, 0.0f, -1.0f * RADIUS);
60
    subdivide(v22, v23, v24, SMOOTHING);
61
    
62
    set = false;
63
}
64

    
65
void Sphere::setRangeZ(const float minimumZ, const float maximumZ)
66
{
67
    this->minimumZ = minimumZ;
68
    this->maximumZ = maximumZ;
69
    set = false;
70
}
71

    
72
float Sphere::getCurrentScaling()
73
{
74
    return MIN_SCALING + (position.z() - minimumZ) / (maximumZ - minimumZ) * (MAX_SCALING - MIN_SCALING);
75
}
76

    
77
void Sphere::setPosition(const QVector3D &position)
78
{
79
    this->position = position;
80
    set = true;
81
}
82

    
83
const QVector3D Sphere::getPosition()
84
{
85
    return position;
86
}
87

    
88
bool Sphere::isSet()
89
{
90
    return set;
91
}
92

    
93
void Sphere::subdivide(const QVector3D &a, const QVector3D &b, const QVector3D &c, const int level)
94
{
95
    if (level < 1) {
96
        // Jedna se o konec rekurzivniho volani, pouze vytvoreni trojuhelnika.
97
        addTriangle(a, b, c);
98
    } else {
99
        // Stredni body.
100
        QVector3D mAB = middlePoint(a, b);
101
        QVector3D mBC = middlePoint(b, c);
102
        QVector3D mCA = middlePoint(c, a);
103
        
104
        // Nastaveni velikosti vektoru (velikost polomeru koule).
105
        mAB *= RADIUS / mAB.length();
106
        mBC *= RADIUS / mBC.length();
107
        mCA *= RADIUS / mCA.length();
108
        
109
        // Rozdeleni trojuhelnika na dalsi ctyri.
110
        subdivide(a,  mAB, mCA, level - 1);
111
        subdivide(b, mBC,  mAB, level - 1);
112
        subdivide(c, mCA, mBC, level - 1);
113
        subdivide(mAB, mBC, mCA, level - 1); // Trojuhelnik ve stredu.
114
    }
115
}
(5-5/10)