summaryrefslogtreecommitdiffstats
path: root/src/gldisc.cpp
blob: 05222a0a3649c5aad2d5bb51fa87ae013127e8fa (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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "gldisc.h"

GLDisc::GLDisc(double radius, double height, QVector3D lowerMiddlePoint, int sides, GLColorRgba color,
               QString textureFile)
    : GLBody(sqrt(pow(radius, 2) + pow(0.5 * height, 2)), color, textureFile)
{
    m_height = height;
    m_sides = sides;

    m_center = v_Z * 0.5 * m_height;

    m_circleRadius = radius;

    m_lowerMiddlePoint = lowerMiddlePoint;
}

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

    m_firstPoint = m_points->size();

    m_points->append(GLPoint(v_Zero, -v_Z, QVector3D(0.5, 0.5, 0.0), m_color));

    for (int side = 0; side < m_sides; side++)
    {
        QVector3D tmp = calculatePoint(side * 2 * M_PI / m_sides);

        QVector3D tmpTextCoo = (0.5 * tmp) + QVector3D(0.5, 0.5, 0.0);

        m_points->append(GLPoint(m_circleRadius * tmp, -v_Z, tmpTextCoo, m_color));
        m_points->append(GLPoint(m_circleRadius * tmp, tmp, tmpTextCoo, m_color));
        m_points->append(GLPoint(QVector3D(m_circleRadius * tmp .x(), m_circleRadius * tmp .y(), m_height), tmp,
                                 tmpTextCoo, m_color));
        m_points->append(GLPoint(QVector3D(m_circleRadius * tmp .x(), m_circleRadius * tmp .y(), m_height), v_Z,
                                 tmpTextCoo, m_color));
    }

    m_points->append(GLPoint(m_height * v_Z, v_Z, QVector3D(0.5, 0.5, 0.0),
                             m_color));

    m_nextPoint = m_points->size();

    m_firstIndex = m_indices->size();

    for (int side = 0; side < m_sides - 1; side++)
    {
        m_indices->append(m_firstPoint);
        m_indices->append(m_firstPoint + 1 + 4 * side);
        m_indices->append(m_firstPoint + 5 + 4 * side);

        m_indices->append(m_firstPoint + 2 + 4 * side);
        m_indices->append(m_firstPoint + 6 + 4 * side);
        m_indices->append(m_firstPoint + 3 + 4 * side);

        m_indices->append(m_firstPoint + 3 + 4 * side);
        m_indices->append(m_firstPoint + 7 + 4 * side);
        m_indices->append(m_firstPoint + 6 + 4 * side);

        m_indices->append(m_firstPoint + 4 + 4 * side);
        m_indices->append(m_firstPoint + 8 + 4 * side);
        m_indices->append(m_nextPoint - 1);
    }

    m_indices->append(m_firstPoint);
    m_indices->append(m_firstPoint + 1 + 4 * (m_sides - 1));
    m_indices->append(m_firstPoint + 1);

    m_indices->append(m_firstPoint + 2 + 4 * (m_sides - 1));
    m_indices->append(m_firstPoint + 2);
    m_indices->append(m_firstPoint + 3 + 4 * (m_sides - 1));

    m_indices->append(m_firstPoint + 3 + 4 * (m_sides - 1));
    m_indices->append(m_firstPoint + 3);
    m_indices->append(m_firstPoint + 2);

    m_indices->append(m_firstPoint + 4 + 4 * (m_sides - 1));
    m_indices->append(m_firstPoint + 4);
    m_indices->append(m_nextPoint - 1);

    m_nextIndex = m_indices->size();
}

bool GLDisc::isHit(QVector3D p1, QVector3D p2)
{

    QVector3D p1New = p1 - m_lowerMiddlePoint;
    QVector3D p2New = p2 - m_lowerMiddlePoint;

    if (!GLBody::isHit(p1New, p2New))
    {
        return false;
    }

    QVector3D lineVector = p2New - p1New;

    // Calculate intersection point with the xy plane
    QVector3D vecBottom = p1New + lineVector * (-p1New.z() / lineVector.z());

    if (vecBottom.length() < m_circleRadius)
    {
        return true;
    }

    // Calculate intersection point with the xy + m_heigth plane
    // Then drop z so that we get a projection onto the xy plane
    QVector3D vecTop = (p1New + lineVector * ((m_height - p1New.z()) /
                                           lineVector.z())).toVector2D();

    if (vecTop.length() < m_circleRadius)
    {
        return true;
    }

    // Check if the mouse line is hitting the outer disc side
    QVector2D vecXYProjection = lineVector.toVector2D();
    QVector2D vecXYStart = p1New.toVector2D();

    // Solved via the "Mitternachtsformel"
    float a = QVector2D::dotProduct(vecXYProjection, vecXYProjection);
    float b = 2 * QVector2D::dotProduct(vecXYStart, vecXYProjection);
    float c = QVector2D::dotProduct(vecXYStart,
                                    vecXYStart) - m_circleRadius * m_circleRadius;

    float discriminant = b * b - 4 * a * c;

    if (discriminant >= 0)
    {
        discriminant = sqrt(discriminant);

        float lambda1 = (-b - discriminant) / (2*a);

        float z1 = (p1New + lambda1 * lineVector).z();

        if(z1 >= 0 && z1 <= m_height)
        {
            return true;
        }

        float lambda2 = (-b + discriminant) / (2*a);

        float z2 = (p1New + lambda2 * lineVector).z();

        if(z2 >= 0 && z2 <= m_height)
        {
            return true;
        }
    }

    return false;
}

QVector3D GLDisc::calculatePoint(double sideAngle)
{
    double x = sin(sideAngle);
    double y = cos(sideAngle);

    return QVector3D(x, y, 0.0);
}


void GLDisc::draw(GLESRenderer *renderer)
{
    renderer->pushMvMatrix();

    renderer->translate(m_lowerMiddlePoint);

    GLBody::draw(renderer);

    renderer->popMvMatrix();
}