summaryrefslogtreecommitdiffstats
path: root/src/glsphere.cpp
blob: 8b68b7c44f12731fc9d0bac3bf340ca508439899 (plain)
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
#include "glsphere.h"

GLSphere::GLSphere(double radius, int stacks, int slices)
    : GLBody(radius)
{
    m_stacks = stacks;
    m_slices = slices;
}

void GLSphere::makeSurface(QVector<GLPoint> *pointContainer,
                           QVector<GLshort> *indexContainer)
{
    GLBody::makeSurface(pointContainer, indexContainer);

    QVector3D n0 = v_XYZ; //dummy normal
    QVector3D t0 = QVector3D(0.0, 0.0, 0.0); //dummy texture

    QVector3D northpol = v_Y * m_radius;
    QVector3D southpol = -v_Y * m_radius;

    m_firstPoint = m_points->size();

    for (int slice = 1; slice < m_slices; slice += 2)
        {
            m_points->append(GLPoint(northpol, n0, t0, GLColorRgba::clMagenta));

            for (int stack = 1; stack < m_stacks; stack++)
            {
                m_points->append(GLPoint(calculatePoint(2 * M_PI * slice / m_slices,
                                                        stack * M_PI / m_stacks), n0, t0,
                                         m_color));

                m_points->append(GLPoint(calculatePoint(2 * M_PI * (slice - 1) / m_slices,
                                                        stack * M_PI / m_stacks), n0, t0,
                                         m_color));

            }

            m_points->append(GLPoint(southpol, n0, t0, GLColorRgba::clMagenta));

            for (int stack = m_stacks - 1; stack > 1; stack--)
            {
                m_points->append(GLPoint(calculatePoint(2 * M_PI * slice / m_slices,
                                                        stack * M_PI / m_stacks), n0, t0,
                                         m_color));

                m_points->append(GLPoint(calculatePoint(2 * M_PI * (slice + 1) / m_slices,
                                                        stack * M_PI / m_stacks), n0, t0,
                                         m_color));

            }
        }

    m_nextPoint = m_points->size();
}

QVector3D GLSphere::calculatePoint(double sliceRotation, double stackRotation)
{
    double x = m_radius * sin(stackRotation) * sin(sliceRotation);
    double y = m_radius * cos(stackRotation);
    double z = m_radius * sin(stackRotation) * cos(sliceRotation);

    return QVector3D(x, y, z);
}