summaryrefslogtreecommitdiffstats
path: root/src/render/io/objloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/render/io/objloader.cpp')
-rw-r--r--src/render/io/objloader.cpp99
1 files changed, 56 insertions, 43 deletions
diff --git a/src/render/io/objloader.cpp b/src/render/io/objloader.cpp
index 87085c1de..da1af4003 100644
--- a/src/render/io/objloader.cpp
+++ b/src/render/io/objloader.cpp
@@ -129,48 +129,61 @@ bool ObjLoader::load(::QIODevice *ioDev, const QString &subMesh)
line = line.simplified();
if (line.length() > 0 && line.at(0) != QChar::fromLatin1('#')) {
- QTextStream lineStream(&line, QIODevice::ReadOnly);
- QString token;
- lineStream >> token;
-
- if (token == QStringLiteral("v")) {
- if (!skipping) {
- float x, y, z;
- lineStream >> x >> y >> z;
- positions.append(QVector3D( x, y, z ));
+ const QVector<QStringRef> tokens = line.splitRef(QChar::fromLatin1(' '));
+
+ if (tokens.first() == QStringLiteral("v")) {
+ if (tokens.size() < 4) {
+ qCWarning(Render::Io) << "Unsupported number of components in vertex";
} else {
- positionsOffset++;
+ if (!skipping) {
+ float x = tokens.at(1).toFloat();
+ float y = tokens.at(2).toFloat();
+ float z = tokens.at(3).toFloat();
+ positions.append(QVector3D( x, y, z ));
+ } else {
+ positionsOffset++;
+ }
}
- } else if (token == QStringLiteral("vt") && m_loadTextureCoords) {
- if (!skipping) {
- // Process texture coordinate
- float s,t;
- lineStream >> s >> t;
- //FlipUVs
- t = 1.0f - t;
- texCoords.append(QVector2D(s, t));
+ } else if (tokens.first() == QStringLiteral("vt") && m_loadTextureCoords) {
+ if (tokens.size() < 3) {
+ qCWarning(Render::Io) << "Unsupported number of components in texture coordinate";
} else {
- texCoordsOffset++;
+ if (!skipping) {
+ // Process texture coordinate
+ float s = tokens.at(1).toFloat();
+ float t = tokens.at(2).toFloat();
+ //FlipUVs
+ t = 1.0f - t;
+ texCoords.append(QVector2D( s, t ));
+ } else {
+ texCoordsOffset++;
+ }
}
- } else if (token == QStringLiteral("vn")) {
- if (!skipping) {
- float x, y, z;
- lineStream >> x >> y >> z;
- normals.append(QVector3D( x, y, z ));
+ } else if (tokens.first() == QStringLiteral("vn")) {
+ if (tokens.size() < 4) {
+ qCWarning(Render::Io) << "Unsupported number of components in vertex normal";
} else {
- normalsOffset++;
+ if (!skipping) {
+ float x = tokens.at(1).toFloat();
+ float y = tokens.at(2).toFloat();
+ float z = tokens.at(3).toFloat();
+ normals.append(QVector3D( x, y, z ));
+ } else {
+ normalsOffset++;
+ }
}
- } else if (!skipping && token == QStringLiteral("f")) {
+ } else if (!skipping && tokens.first() == QStringLiteral("f")) {
// Process face
++faceCount;
+
+ int faceVertices = tokens.size() - 1;
+
QVector<FaceIndices> face;
- int faceVertices = 0;
- while (!lineStream.atEnd()) {
- QString faceString;
- lineStream >> faceString;
+ face.reserve(faceVertices);
+ for (int i = 0; i < faceVertices; i++) {
FaceIndices faceIndices;
- QStringList indices = faceString.split(QChar::fromLatin1('/'));
+ const QVector<QStringRef> indices = tokens.at(i + 1).split(QChar::fromLatin1('/'));
switch (indices.size()) {
case 3:
faceIndices.normalIndex = indices.at(2).toInt() - 1 - normalsOffset; // fall through
@@ -184,7 +197,6 @@ bool ObjLoader::load(::QIODevice *ioDev, const QString &subMesh)
}
face.append(faceIndices);
- ++faceVertices;
}
// If number of edges in face is greater than 3,
@@ -207,11 +219,14 @@ bool ObjLoader::load(::QIODevice *ioDev, const QString &subMesh)
}
// end of face
- } else if ( token == QStringLiteral("o") ) {
- if (!subMesh.isEmpty() ) {
- QString objName;
- lineStream >> objName;
- skipping = subMeshMatch.indexIn(objName) < 0;
+ } else if (tokens.first() == QStringLiteral("o")) {
+ if (tokens.size() < 2) {
+ qCWarning(Render::Io) << "Missing submesh name";
+ } else {
+ if (!subMesh.isEmpty() ) {
+ QString objName = tokens.at(1).toString();
+ skipping = subMeshMatch.indexIn(objName) < 0;
+ }
}
}
} // end of input line
@@ -346,14 +361,12 @@ void ObjLoader::updateIndices(const QVector<QVector3D> &positions,
if (hasNormals)
m_normals.resize(vertexCount);
- foreach (const FaceIndices &faceIndices, faceIndexMap.keys()) {
- const int i = faceIndexMap.value(faceIndices);
-
- m_points[i] = positions[faceIndices.positionIndex];
+ for (QHash<FaceIndices, unsigned int>::const_iterator it = faceIndexMap.begin(), endIt = faceIndexMap.end(); it != endIt; ++it) {
+ m_points[it.value()] = positions[it.key().positionIndex];
if (hasTexCoords)
- m_texCoords[i] = std::numeric_limits<unsigned int>::max() != faceIndices.texCoordIndex ? texCoords[faceIndices.texCoordIndex] : QVector2D();
+ m_texCoords[it.value()] = std::numeric_limits<unsigned int>::max() != it.key().texCoordIndex ? texCoords[it.key().texCoordIndex] : QVector2D();
if (hasNormals)
- m_normals[i] = normals[faceIndices.normalIndex];
+ m_normals[it.value()] = normals[it.key().normalIndex];
}
// Now iterate over the face indices and lookup the unique vertex index