summaryrefslogtreecommitdiffstats
path: root/src/datavisualization/utils/surfaceobject.cpp
blob: 0f9bb7e03051b280310552b6673bdbcbe5ccac7d (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the QtDataVisualization module.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "surfaceobject_p.h"
#include "abstractobjecthelper_p.h"

#include <QVector3D>
#include <QVector2D>

#include <QDebug>

QT_DATAVISUALIZATION_BEGIN_NAMESPACE

SurfaceObject::SurfaceObject()
{
    m_indicesType = GL_UNSIGNED_INT;
    initializeOpenGLFunctions();
    glGenBuffers(1, &m_vertexbuffer);
    glGenBuffers(1, &m_normalbuffer);
    glGenBuffers(1, &m_uvbuffer);
    glGenBuffers(1, &m_elementbuffer);
    glGenBuffers(1, &m_gridElementbuffer);
}

SurfaceObject::~SurfaceObject()
{
    glDeleteBuffers(1, &m_gridElementbuffer);
}

void SurfaceObject::setUpSmoothData(const QSurfaceDataArray &dataArray, const QRect &space,
                                    GLfloat yRange, bool changeGeometry, bool needTexture)
{
    int columns = space.width();
    int rows = space.height();
    int totalSize = rows * columns;
    GLfloat width = (GLfloat(columns) - 1.0f) / 2.0f;
    GLfloat depth = (GLfloat(rows) - 1.0f) / -2.0f;
    GLfloat height = yRange / 2.0f;

    // Create/populate vertice table
    if (changeGeometry)
        m_vertices.resize(totalSize);

    QVector<QVector2D> uvs;
    if (needTexture)
        uvs.resize(totalSize);
    float uvX = 1.0 / float(columns - 1);
    float uvY = 1.0 / float(rows - 1);
    int totalIndex = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            m_vertices[totalIndex] = QVector3D(float(j) / width - 1.0f,
                                               float(dataArray.at(i)->at(j)) / height - 1.0f,
                                               float(i) / depth + 1.0f);
            if (needTexture)
                uvs[totalIndex] = QVector2D(float(j) * uvX, float(i) * uvY);
            totalIndex++;
        }
    }

    // Create normals
    int rowLimit = rows - 1;
    int colLimit = columns - 1;
    int rowColLimit = rowLimit * columns;
    int totalLimit = totalSize - 1;
    if (changeGeometry)
        m_normals.resize(totalSize);

    totalIndex = 0;
    for (int row = 0; row < rowColLimit; row += columns) {
        for (int j = 0; j < colLimit; j++) {
            m_normals[totalIndex++] = normal(m_vertices.at(row + j),
                                             m_vertices.at(row + j + 1),
                                             m_vertices.at(row + columns + j));
        }
        int p = row + colLimit;
        m_normals[totalIndex++] = normal(m_vertices.at(p),
                                         m_vertices.at(p + columns),
                                         m_vertices.at(p - 1));
    }
    for (int j = rowColLimit; j < totalLimit; j++) {
        m_normals[totalIndex++] = normal(m_vertices.at(j),
                                         m_vertices.at(j - columns),
                                         m_vertices.at(j + 1));
    }
    int p = rows * colLimit;
    m_normals[totalIndex++] = normal(m_vertices.at(p),
                                     m_vertices.at(p - 1),
                                     m_vertices.at(p - columns - 1));

    // Create indices table
    GLint *indices = 0;
    if (changeGeometry) {
        m_indexCount = 6 * colLimit * rowLimit;
        indices = new GLint[m_indexCount];
        p = 0;
        for (int row = 0; row < rowLimit * columns; row += columns) {
            for (int j = 0; j < colLimit; j++) {
                // Left triangle
                indices[p++] = row + j + 1;
                indices[p++] = row + columns + j;
                indices[p++] = row + j;

                // Right triangle
                indices[p++] = row + columns + j + 1;
                indices[p++] = row + columns + j;
                indices[p++] = row + j + 1;
            }
        }
    }

    // Create line element indices
    GLint *gridIndices = 0;
    if (changeGeometry) {
        m_gridIndexCount = 2 * columns * rowLimit + 2 * rows * colLimit;
        gridIndices = new GLint[m_gridIndexCount];
        p = 0;
        for (int i = 0, row = 0; i < rows; i++, row += columns) {
            for (int j = 0; j < colLimit; j++) {
                gridIndices[p++] = row + j;
                gridIndices[p++] = row + j + 1;
            }
        }
        for (int i = 0, row = 0; i < rowLimit; i++, row += columns) {
            for (int j = 0; j < columns; j++) {
                gridIndices[p++] = row + j;
                gridIndices[p++] = row + j + columns;
            }
        }
    }

    createBuffers(m_vertices, uvs, m_normals, indices, gridIndices, changeGeometry);

    delete[] indices;
    delete[] gridIndices;
}


void SurfaceObject::setUpData(const QSurfaceDataArray &dataArray, const QRect &space,
                              GLfloat yRange, bool changeGeometry, bool needTexture)
{
    int columns = space.width();
    int rows = space.height();
    int totalSize = rows * columns * 2;
    GLfloat width = (GLfloat(columns) - 1.0f) / 2.0f;
    GLfloat depth = (GLfloat(rows) - 1.0f) / -2.0f;
    GLfloat height = yRange / 2.0f;
    float uvX = 1.0 / float(columns - 1);
    float uvY = 1.0 / float(rows - 1);

    // Create vertice table
    if (changeGeometry)
        m_vertices.resize(totalSize);

    QVector<QVector2D> uvs;
    if (needTexture)
        uvs.resize(totalSize);

    int totalIndex = 0;
    int rowLimit = rows - 1;
    int colLimit = columns - 1;
    int doubleColumns = columns * 2 - 2;
    int rowColLimit = rowLimit * doubleColumns;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            m_vertices[totalIndex] = QVector3D(float(j) / width - 1.0f,
                                               float(dataArray.at(i)->at(j)) / height - 1.0f,
                                               float(i) / depth + 1.0f);

            if (needTexture)
                uvs[totalIndex] = QVector2D(float(j) * uvX, float(i) * uvY);

            totalIndex++;

            if (j > 0 && j < colLimit) {
                m_vertices[totalIndex] = m_vertices[totalIndex - 1];
                if (needTexture)
                    uvs[totalIndex] = uvs[totalIndex - 1];
                totalIndex++;
            }
        }
    }

    // Create normals & indices table
    GLint *indices = 0;
    int p = 0;
    if (changeGeometry) {
        int normalCount = 2 * colLimit * rowLimit;
        m_indexCount = 3 * normalCount;
        indices = new GLint[m_indexCount];
        m_normals.resize(normalCount);
    }

    totalIndex = 0;
    for (int row = 0, upperRow = doubleColumns;
         row < rowColLimit;
         row += doubleColumns, upperRow += doubleColumns) {
        for (int j = 0; j < doubleColumns; j += 2) {
            // Normal for the left triangle
            m_normals[totalIndex++] = normal(m_vertices.at(row + j),
                                             m_vertices.at(row + j + 1),
                                             m_vertices.at(upperRow + j));

            // Normal for the right triangle
            m_normals[totalIndex++] = normal(m_vertices.at(row + j + 1),
                                             m_vertices.at(upperRow + j + 1),
                                             m_vertices.at(upperRow + j));

            if (changeGeometry) {
                // Left triangle
                indices[p++] = row + j + 1;
                indices[p++] = upperRow + j;
                indices[p++] = row + j;

                // Right triangle
                indices[p++] = upperRow + j + 1;
                indices[p++] = upperRow + j;
                indices[p++] = row + j + 1;
            }
        }
    }

    // Create grid line element indices
    GLint *gridIndices = 0;
    if (changeGeometry) {
        m_gridIndexCount = 2 * columns * rowLimit + 2 * rows * colLimit;
        gridIndices = new GLint[m_gridIndexCount];
        p = 0;
        int fullRowLimit = rows * doubleColumns;
        for (int row = 0; row < fullRowLimit; row += doubleColumns) {
            for (int j = 0; j < doubleColumns; j += 2) {
                gridIndices[p++] = row + j;
                gridIndices[p++] = row + j + 1;

                if (row < rowColLimit) {
                    gridIndices[p++] = row + j;
                    gridIndices[p++] = row + j + doubleColumns;
                }
            }
        }
        for (int i = doubleColumns - 1; i < rowColLimit; i += doubleColumns) {
            gridIndices[p++] = i;
            gridIndices[p++] = i  + doubleColumns;
        }
    }

    createBuffers(m_vertices, uvs, m_normals, indices, gridIndices, changeGeometry);

    delete[] indices;
    delete[] gridIndices;
}

void SurfaceObject::createBuffers(const QVector<QVector3D> &vertices, const QVector<QVector2D> &uvs,
                                  const QVector<QVector3D> &normals, const GLint *indices,
                                  const GLint *gridIndices, bool changeGeometry)
{
    // Move to buffers
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(QVector3D),
                 &vertices.at(0), GL_DYNAMIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, m_normalbuffer);
    glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(QVector3D),
                 &normals.at(0), GL_DYNAMIC_DRAW);

    if (changeGeometry) {
        glBindBuffer(GL_ARRAY_BUFFER, m_uvbuffer);
        glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(QVector2D),
                     &uvs.at(0), GL_STATIC_DRAW);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementbuffer);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount * sizeof(GLint),
                     indices, GL_STATIC_DRAW);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_gridElementbuffer);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_gridIndexCount * sizeof(GLint),
                     gridIndices, GL_STATIC_DRAW);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    }

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    m_meshDataLoaded = true;
}

GLuint SurfaceObject::gridElementBuf()
{
    if (!m_meshDataLoaded)
        qFatal("No loaded object");
    return m_gridElementbuffer;
}

GLuint SurfaceObject::gridIndexCount()
{
    return m_gridIndexCount;
}

QVector3D SurfaceObject::normal(const QVector3D &a, const QVector3D &b, const QVector3D &c)
{
    QVector3D v1 = b - a;
    QVector3D v2 = c - a;
    return QVector3D::crossProduct(v1, v2);
}

QT_DATAVISUALIZATION_END_NAMESPACE