From d1c4bde677bb5949751488eeb8ed8937c83fe38a Mon Sep 17 00:00:00 2001 From: humbletim Date: Thu, 22 Feb 2018 20:53:27 -0500 Subject: [PATCH] revert OBJWriter.cpp --- libraries/fbx/src/OBJWriter.cpp | 85 +++++++++++---------------------- 1 file changed, 28 insertions(+), 57 deletions(-) diff --git a/libraries/fbx/src/OBJWriter.cpp b/libraries/fbx/src/OBJWriter.cpp index ffc34573d4..4441ae6649 100644 --- a/libraries/fbx/src/OBJWriter.cpp +++ b/libraries/fbx/src/OBJWriter.cpp @@ -9,12 +9,10 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "OBJWriter.h" - #include #include -#include -#include +#include "graphics/Geometry.h" +#include "OBJWriter.h" #include "ModelFormatLogging.h" static QString formatFloat(double n) { @@ -48,12 +46,9 @@ bool writeOBJToTextStream(QTextStream& out, QList meshes) { QList meshNormalStartOffset; int currentVertexStartOffset = 0; int currentNormalStartOffset = 0; - int subMeshIndex = 0; - out << "# OBJWriter::writeOBJToTextStream\n"; // write out vertices (and maybe colors) foreach (const MeshPointer& mesh, meshes) { - out << "# vertices::subMeshIndex " << subMeshIndex++ << "\n"; meshVertexStartOffset.append(currentVertexStartOffset); const gpu::BufferView& vertexBuffer = mesh->getVertexBuffer(); @@ -70,10 +65,7 @@ bool writeOBJToTextStream(QTextStream& out, QList meshes) { out << formatFloat(v[1]) << " "; out << formatFloat(v[2]); if (colorIndex < numColors) { - glm::vec3 color = buffer_helpers::convert(colorsBufferView, colorIndex); - // TODO: still verifying that the above decodes properly; previous variations were: - // glm::vec3 color = buffer_helpers::glmVecFromVariant(buffer_helpers::toVariant(colorsBufferView, colorIndex)); - // glm::vec3 color = colorsBufferView.get(colorIndex); + glm::vec3 color = colorsBufferView.get(colorIndex); out << " " << formatFloat(color[0]); out << " " << formatFloat(color[1]); out << " " << formatFloat(color[2]); @@ -89,17 +81,12 @@ bool writeOBJToTextStream(QTextStream& out, QList meshes) { // write out normals bool haveNormals = true; - subMeshIndex = 0; foreach (const MeshPointer& mesh, meshes) { - out << "# normals::subMeshIndex " << subMeshIndex++ << "\n"; meshNormalStartOffset.append(currentNormalStartOffset); const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(gpu::Stream::InputSlot::NORMAL); gpu::BufferView::Index numNormals = (gpu::BufferView::Index)normalsBufferView.getNumElements(); for (gpu::BufferView::Index i = 0; i < numNormals; i++) { - glm::vec3 normal = buffer_helpers::convert(normalsBufferView, i); - // TODO: still verifying that the above decodes properly; previous variations were: - // glm::vec3 normal = buffer_helpers::glmVecFromVariant(buffer_helpers::toVariant(normalsBufferView, i)); - // glm::vec3 normal = normalsBufferView.get(i); + glm::vec3 normal = normalsBufferView.get(i); out << "vn "; out << formatFloat(normal[0]) << " "; out << formatFloat(normal[1]) << " "; @@ -111,9 +98,7 @@ bool writeOBJToTextStream(QTextStream& out, QList meshes) { // write out faces int nth = 0; - subMeshIndex = 0; foreach (const MeshPointer& mesh, meshes) { - out << "# faces::subMeshIndex " << subMeshIndex++ << "\n"; currentVertexStartOffset = meshVertexStartOffset.takeFirst(); currentNormalStartOffset = meshNormalStartOffset.takeFirst(); @@ -121,25 +106,35 @@ bool writeOBJToTextStream(QTextStream& out, QList meshes) { const gpu::BufferView& indexBuffer = mesh->getIndexBuffer(); graphics::Index partCount = (graphics::Index)mesh->getNumParts(); - QString name = (!mesh->displayName.size() ? QString("mesh-%1-part").arg(nth) : QString::fromStdString(mesh->displayName)) - .replace(QRegExp("[^-_a-zA-Z0-9]"), "_"); for (int partIndex = 0; partIndex < partCount; partIndex++) { const graphics::Mesh::Part& part = partBuffer.get(partIndex); - out << QString("g %1-%2-%3\n").arg(subMeshIndex, 3, 10, QChar('0')).arg(name).arg(partIndex); + out << "g part-" << nth++ << "\n"; - const bool shorts = indexBuffer._element == gpu::Element::INDEX_UINT16; - auto face = [&](uint32_t i0, uint32_t i1, uint32_t i2) { - uint32_t index0, index1, index2; - if (shorts) { - index0 = indexBuffer.get(i0); - index1 = indexBuffer.get(i1); - index2 = indexBuffer.get(i2); - } else { - index0 = indexBuffer.get(i0); - index1 = indexBuffer.get(i1); - index2 = indexBuffer.get(i2); + // graphics::Mesh::TRIANGLES + // TODO -- handle other formats + gpu::BufferView::Iterator indexItr = indexBuffer.cbegin(); + indexItr += part._startIndex; + + int indexCount = 0; + while (indexItr != indexBuffer.cend() && indexCount < part._numIndices) { + uint32_t index0 = *indexItr; + indexItr++; + indexCount++; + if (indexItr == indexBuffer.cend() || indexCount >= part._numIndices) { + qCDebug(modelformat) << "OBJWriter -- index buffer length isn't multiple of 3"; + break; } + uint32_t index1 = *indexItr; + indexItr++; + indexCount++; + if (indexItr == indexBuffer.cend() || indexCount >= part._numIndices) { + qCDebug(modelformat) << "OBJWriter -- index buffer length isn't multiple of 3"; + break; + } + uint32_t index2 = *indexItr; + indexItr++; + indexCount++; out << "f "; if (haveNormals) { @@ -151,30 +146,6 @@ bool writeOBJToTextStream(QTextStream& out, QList meshes) { out << currentVertexStartOffset + index1 + 1 << " "; out << currentVertexStartOffset + index2 + 1 << "\n"; } - }; - - // graphics::Mesh::TRIANGLES / graphics::Mesh::QUADS - // TODO -- handle other formats - uint32_t len = part._startIndex + part._numIndices; - qCDebug(modelformat) << "OBJWriter -- part" << partIndex << "topo" << part._topology << "index elements" << (shorts ? "uint16_t" : "uint32_t"); - if (part._topology == graphics::Mesh::TRIANGLES && len % 3 != 0) { - qCDebug(modelformat) << "OBJWriter -- index buffer length isn't a multiple of 3" << len; - } - if (part._topology == graphics::Mesh::QUADS && len % 4 != 0) { - qCDebug(modelformat) << "OBJWriter -- index buffer length isn't a multiple of 4" << len; - } - if (len > indexBuffer.getNumElements()) { - qCDebug(modelformat) << "OBJWriter -- len > index size" << len << indexBuffer.getNumElements(); - } - if (part._topology == graphics::Mesh::QUADS) { - for (uint32_t idx = part._startIndex; idx+3 < len; idx += 4) { - face(idx+0, idx+1, idx+3); - face(idx+1, idx+2, idx+3); - } - } else if (part._topology == graphics::Mesh::TRIANGLES) { - for (uint32_t idx = part._startIndex; idx+2 < len; idx += 3) { - face(idx+0, idx+1, idx+2); - } } out << "\n"; }