1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "vertexindexer_p.h" |
5 | |
6 | #include <QtCore/qmath.h> |
7 | #include <QtCore/qmap.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | int unique_vertices = 0; |
12 | |
13 | bool VertexIndexer::getSimilarVertexIndex_fast(const PackedVertex &packed, |
14 | QMap<PackedVertex, GLuint> &VertexToOutIndex, |
15 | GLuint &result) |
16 | { |
17 | QMap<PackedVertex, GLuint>::iterator it = VertexToOutIndex.find(key: packed); |
18 | if (it == VertexToOutIndex.end()) { |
19 | return false; |
20 | } else { |
21 | result = it.value(); |
22 | return true; |
23 | } |
24 | } |
25 | |
26 | void VertexIndexer::indexVBO(const QList<QVector3D> &in_vertices, const QList<QVector2D> &in_uvs, |
27 | const QList<QVector3D> &in_normals, QList<GLuint> &out_indices, |
28 | QList<QVector3D> &out_vertices, QList<QVector2D> &out_uvs, |
29 | QList<QVector3D> &out_normals) |
30 | { |
31 | unique_vertices = 0; |
32 | QMap<PackedVertex, GLuint> VertexToOutIndex; |
33 | |
34 | // For each input vertex |
35 | for (int i = 0; i < in_vertices.size(); i++) { |
36 | PackedVertex packed = {.position: in_vertices[i], .uv: in_uvs[i], .normal: in_normals[i]}; |
37 | |
38 | // Try to find a similar vertex in out_XXXX |
39 | GLuint index; |
40 | bool found = getSimilarVertexIndex_fast(packed, VertexToOutIndex, result&: index); |
41 | |
42 | if (found) { |
43 | out_indices.append(t: index); |
44 | } else { |
45 | unique_vertices++; |
46 | out_vertices.append(t: in_vertices[i]); |
47 | out_uvs.append(t: in_uvs[i]); |
48 | out_normals.append(t: in_normals[i]); |
49 | GLuint newindex = (GLuint)out_vertices.size() - 1; |
50 | out_indices.append(t: newindex); |
51 | VertexToOutIndex[packed] = newindex; |
52 | } |
53 | } |
54 | } |
55 | |
56 | QT_END_NAMESPACE |
57 | |