| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Data Visualization module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 or (at your option) any later version |
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include "meshloader_p.h" |
| 31 | |
| 32 | #include <QtCore/QFile> |
| 33 | #include <QtCore/QStringList> |
| 34 | #include <QtCore/QVector> |
| 35 | #include <QtGui/QVector2D> |
| 36 | |
| 37 | QT_BEGIN_NAMESPACE_DATAVISUALIZATION |
| 38 | |
| 39 | QString slashTag = QStringLiteral("/" ); |
| 40 | |
| 41 | bool MeshLoader::loadOBJ(const QString &path, |
| 42 | QVector<QVector3D> &out_vertices, |
| 43 | QVector<QVector2D> &out_uvs, |
| 44 | QVector<QVector3D> &out_normals) |
| 45 | { |
| 46 | QVector<unsigned int> vertexIndices, uvIndices, normalIndices; |
| 47 | QVector<QVector3D> temp_vertices; |
| 48 | QVector<QVector2D> temp_uvs; |
| 49 | QVector<QVector3D> temp_normals; |
| 50 | |
| 51 | QFile file(path); |
| 52 | if (!file.open(flags: QIODevice::ReadOnly | QIODevice::Text)) { |
| 53 | qWarning(msg: "Cannot open the file" ); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | QTextStream textIn(&file); |
| 58 | while (!textIn.atEnd()) { |
| 59 | QString line = textIn.readLine(); |
| 60 | QStringList lineContents = line.split(QStringLiteral(" " )); |
| 61 | if (!lineContents.at(i: 0).compare(QStringLiteral("v" ))) { |
| 62 | QVector3D vertex; |
| 63 | vertex.setX(lineContents.at(i: 1).toFloat()); |
| 64 | vertex.setY(lineContents.at(i: 2).toFloat()); |
| 65 | vertex.setZ(lineContents.at(i: 3).toFloat()); |
| 66 | temp_vertices.append(t: vertex); |
| 67 | } |
| 68 | else if (!lineContents.at(i: 0).compare(QStringLiteral("vt" ))) { |
| 69 | QVector2D uv; |
| 70 | uv.setX(lineContents.at(i: 1).toFloat()); |
| 71 | uv.setY(lineContents.at(i: 2).toFloat()); // invert this if using DDS textures |
| 72 | temp_uvs.append(t: uv); |
| 73 | } |
| 74 | else if (!lineContents.at(i: 0).compare(QStringLiteral("vn" ))) { |
| 75 | QVector3D normal; |
| 76 | normal.setX(lineContents.at(i: 1).toFloat()); |
| 77 | normal.setY(lineContents.at(i: 2).toFloat()); |
| 78 | normal.setZ(lineContents.at(i: 3).toFloat()); |
| 79 | temp_normals.append(t: normal); |
| 80 | } |
| 81 | else if (!lineContents.at(i: 0).compare(QStringLiteral("f" ))) { |
| 82 | unsigned int vertexIndex[3], uvIndex[3], normalIndex[3]; |
| 83 | QStringList set1 = lineContents.at(i: 1).split(sep: slashTag); |
| 84 | QStringList set2 = lineContents.at(i: 2).split(sep: slashTag); |
| 85 | QStringList set3 = lineContents.at(i: 3).split(sep: slashTag); |
| 86 | vertexIndex[0] = set1.at(i: 0).toUInt(); |
| 87 | vertexIndex[1] = set2.at(i: 0).toUInt(); |
| 88 | vertexIndex[2] = set3.at(i: 0).toUInt(); |
| 89 | uvIndex[0] = set1.at(i: 1).toUInt(); |
| 90 | uvIndex[1] = set2.at(i: 1).toUInt(); |
| 91 | uvIndex[2] = set3.at(i: 1).toUInt(); |
| 92 | normalIndex[0] = set1.at(i: 2).toUInt(); |
| 93 | normalIndex[1] = set2.at(i: 2).toUInt(); |
| 94 | normalIndex[2] = set3.at(i: 2).toUInt(); |
| 95 | vertexIndices.append(t: vertexIndex[0]); |
| 96 | vertexIndices.append(t: vertexIndex[1]); |
| 97 | vertexIndices.append(t: vertexIndex[2]); |
| 98 | uvIndices.append(t: uvIndex[0]); |
| 99 | uvIndices.append(t: uvIndex[1]); |
| 100 | uvIndices.append(t: uvIndex[2]); |
| 101 | normalIndices.append(t: normalIndex[0]); |
| 102 | normalIndices.append(t: normalIndex[1]); |
| 103 | normalIndices.append(t: normalIndex[2]); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // For each vertex of each triangle |
| 108 | for (int i = 0; i < vertexIndices.size(); i++) { |
| 109 | // Get the indices of its attributes |
| 110 | unsigned int vertexIndex = vertexIndices[i]; |
| 111 | unsigned int uvIndex = uvIndices[i]; |
| 112 | unsigned int normalIndex = normalIndices[i]; |
| 113 | |
| 114 | // Get the attributes thanks to the index |
| 115 | QVector3D vertex = temp_vertices[vertexIndex - 1]; |
| 116 | QVector2D uv = temp_uvs[uvIndex - 1]; |
| 117 | QVector3D normal = temp_normals[normalIndex - 1]; |
| 118 | |
| 119 | // Put the attributes in buffers |
| 120 | out_vertices.append(t: vertex); |
| 121 | out_uvs.append(t: uv); |
| 122 | out_normals.append(t: normal); |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | QT_END_NAMESPACE_DATAVISUALIZATION |
| 129 | |