aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/gridgeometry.cpp
blob: 4b548239ca8600851f51c96fbe52f93eb1088fe0 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#ifdef QUICK3D_MODULE

#include "gridgeometry.h"

#include <QtQuick3DRuntimeRender/private/qssgrendergeometry_p.h>

namespace QmlDesigner {
namespace Internal {

GridGeometry::GridGeometry()
    : QQuick3DGeometry()
{
}

GridGeometry::~GridGeometry()
{
}

int GridGeometry::lines() const
{
    return m_lines;
}

float GridGeometry::step() const
{
    return m_step;
}

bool GridGeometry::isCenterLine() const
{
    return m_isCenterLine;
}

// Number of lines on each side of the center lines.
// These lines are not drawn if m_isCenterLine is true; lines and step are simply used to calculate
// the length of the center line in that case.
void GridGeometry::setLines(int count)
{
    count = qMax(count, 1);
    if (m_lines == count)
        return;
    m_lines = qMax(count, 1);
    emit linesChanged();
    update();
}

// Space between lines
void GridGeometry::setStep(float step)
{
    step = qMax(step, 0.0f);
    if (qFuzzyCompare(m_step, step))
        return;
    m_step = step;
    emit stepChanged();
    update();
}

void GridGeometry::setIsCenterLine(bool enabled)
{
    if (m_isCenterLine == enabled)
        return;

    m_isCenterLine = enabled;
    emit isCenterLineChanged();
    update();
}

QSSGRenderGraphObject *GridGeometry::updateSpatialNode(QSSGRenderGraphObject *node)
{
    node = QQuick3DGeometry::updateSpatialNode(node);
    QSSGRenderGeometry *geometry = static_cast<QSSGRenderGeometry *>(node);
    geometry->clear();

    QByteArray vertexData;
    fillVertexData(vertexData);

    geometry->addAttribute(QSSGRenderGeometry::Attribute::PositionSemantic, 0,
                           QSSGRenderGeometry::Attribute::ComponentType::F32Type);
    geometry->setStride(12);
    geometry->setVertexData(vertexData);
    geometry->setPrimitiveType(QSSGRenderGeometry::Lines);

    int lastIndex = (vertexData.size() - 1) / int(sizeof(QVector3D));
    auto vertexPtr = reinterpret_cast<QVector3D *>(vertexData.data());
    geometry->setBounds(QVector3D(vertexPtr[0][0], vertexPtr[0][1], 0.0),
                        QVector3D(vertexPtr[lastIndex][0], vertexPtr[lastIndex][1], 0.0));
    return node;
}

void GridGeometry::fillVertexData(QByteArray &vertexData)
{
    const int numSubdivs = 1; // number of subdivision lines (i.e. lines between main grid lines)
    const int vtxSize = int(sizeof(float)) * 3 * 2;
    const int size = m_isCenterLine ? vtxSize
                  : m_isSubdivision ? 4 * m_lines * vtxSize * numSubdivs
                                    : 4 * m_lines * vtxSize;
    vertexData.resize(size);
    float *dataPtr = reinterpret_cast<float *>(vertexData.data());

    float x0 = -float(m_lines) * m_step;
    float y0 = x0;
    float x1 = -x0;
    float y1 = x1;

    if (m_isCenterLine) {
        // start position
        dataPtr[0] = 0.f;
        dataPtr[1] = y0;
        dataPtr[2] = 0.f;
        // end position
        dataPtr[3] = 0.f;
        dataPtr[4] = y1;
        dataPtr[5] = 0.f;
    } else {
        // Lines are created so that bounding box can later be calculated from first and last vertex
        if (m_isSubdivision) {
            const float subdivStep = m_step / float(numSubdivs + 1);
            const int subdivMainLines = m_lines * 2;
            auto generateSubLines = [&](float x0, float y0, float x1, float y1, bool vertical) {
                for (int i = 0; i < subdivMainLines; ++i) {
                    for (int j = 1; j <= numSubdivs; ++j) {
                        // start position
                        dataPtr[0] = vertical ? x0 + i * m_step + j * subdivStep : x0;
                        dataPtr[1] = vertical ? y0 : y0 + i * m_step + j * subdivStep;
                        dataPtr[2] = .0f;
                        // end position
                        dataPtr[3] = vertical ? x0 + i * m_step + j * subdivStep : x1;
                        dataPtr[4] = vertical ? y1 : y0 + i * m_step + j * subdivStep;
                        dataPtr[5] = .0f;
                        dataPtr += 6;
                    }
                }
            };
            generateSubLines(x0, y0, x1, y1, true);
            generateSubLines(x0, y0, x1, y1, false);
        } else {
            auto generateLines = [this, &dataPtr](float x0, float y0, float x1, float y1, bool vertical) {
                for (int i = 0; i < m_lines; ++i) {
                    // start position
                    dataPtr[0] = vertical ? x0 + i * m_step : x0;
                    dataPtr[1] = vertical ? y0 : y0 + i * m_step;
                    dataPtr[2] = .0f;
                    // end position
                    dataPtr[3] = vertical ? x0 + i * m_step : x1;
                    dataPtr[4] = vertical ? y1 : y0 + i * m_step;
                    dataPtr[5] = .0f;
                    dataPtr += 6;
                }
            };
            generateLines(x0, y0, x1, y1, true);
            generateLines(x0, y0, x1, y1, false);
            generateLines(x0, m_step, x1, y1, false);
            generateLines(m_step, y0, x1, y1, true);
        }
    }
}

}
}

#endif // QUICK3D_MODULE