Showing the changes to libraries/script-engine/src/ModelScriptingInterface.cpp (1 of 4 files). Show all
Unified · Split
| @@ -12,11 +12,12 @@ · 11 unmodified lines | ||
| 12 | 12 | #include <QScriptEngine> |
| 13 | 13 | #include <QScriptValueIterator> |
| 14 | 14 | #include <QtScript/QScriptValue> |
| 15 | +#include "ScriptEngine.h" | |
| 15 | 16 | #include "ModelScriptingInterface.h" |
| 16 | 17 | #include "OBJWriter.h" |
| 17 | 18 | |
| 18 | − | |
| 19 | 19 | ModelScriptingInterface::ModelScriptingInterface(QObject* parent) : QObject(parent) { |
| 20 | + _modelScriptEngine = qobject_cast<ScriptEngine*>(parent); | |
| 20 | 21 | } |
| 21 | 22 | |
| 22 | 23 | QScriptValue meshToScriptValue(QScriptEngine* engine, MeshProxy* const &in) { |
| @@ -51,3 +52,89 @@ QString ModelScriptingInterface::meshToOBJ(MeshProxyList in) { · 28 unmodified lines | ||
| 51 | 52 | |
| 52 | 53 | return writeOBJToString(meshes); |
| 53 | 54 | } |
| 55 | + | |
| 56 | + | |
| 57 | +QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { | |
| 58 | + model::MeshPointer result(new model::Mesh()); | |
| 59 | + int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h | |
| 60 | + | |
| 61 | + size_t totalVertexCount { 0 }; | |
| 62 | + size_t totalAttributeCount { 0 }; | |
| 63 | + size_t totalIndexCount { 0 }; | |
| 64 | + foreach (const MeshProxy* meshProxy, in) { | |
| 65 | + MeshPointer mesh = meshProxy->getMeshPointer(); | |
| 66 | + totalVertexCount += mesh->getNumVertices(); | |
| 67 | + totalAttributeCount += mesh->getNumAttributes(); | |
| 68 | + totalIndexCount += mesh->getNumIndices(); | |
| 69 | + } | |
| 70 | + | |
| 71 | + gpu::Resource::Size combinedVertexSize = totalVertexCount * sizeof(glm::vec3); | |
| 72 | + unsigned char* combinedVertexData = new unsigned char[combinedVertexSize]; | |
| 73 | + unsigned char* combinedVertexDataCursor = combinedVertexData; | |
| 74 | + | |
| 75 | + gpu::Resource::Size combinedNormalSize = totalVertexCount * sizeof(glm::vec3); | |
| 76 | + unsigned char* combinedNormalData = new unsigned char[combinedNormalSize]; | |
| 77 | + unsigned char* combinedNormalDataCursor = combinedNormalData; | |
| 78 | + | |
| 79 | + gpu::Resource::Size combinedIndexSize = totalVertexCount * sizeof(uint32_t); | |
| 80 | + unsigned char* combinedIndexData = new unsigned char[combinedIndexSize]; | |
| 81 | + unsigned char* combinedIndexDataCursor = combinedIndexData; | |
| 82 | + | |
| 83 | + uint32_t indexStartOffset { 0 }; | |
| 84 | + | |
| 85 | + foreach (const MeshProxy* meshProxy, in) { | |
| 86 | + MeshPointer mesh = meshProxy->getMeshPointer(); | |
| 87 | + | |
| 88 | + // vertex data | |
| 89 | + const gpu::BufferView& vertexBufferView = mesh->getVertexBuffer(); | |
| 90 | + gpu::BufferView::Index numVertices = (gpu::BufferView::Index)mesh->getNumVertices(); | |
| 91 | + for (gpu::BufferView::Index i = 0; i < numVertices; i ++) { | |
| 92 | + glm::vec3 pos = vertexBufferView.get<glm::vec3>(i); | |
| 93 | + memcpy(combinedVertexDataCursor, &pos, sizeof(pos)); | |
| 94 | + combinedVertexDataCursor += sizeof(pos); | |
| 95 | + } | |
| 96 | + | |
| 97 | + // normal data | |
| 98 | + const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal); | |
| 99 | + gpu::BufferView::Index numNormals = (gpu::BufferView::Index)mesh->getNumAttributes(); | |
| 100 | + for (gpu::BufferView::Index i = 0; i < numNormals; i ++) { | |
| 101 | + glm::vec3 normal = normalsBufferView.get<glm::vec3>(i); | |
| 102 | + memcpy(combinedNormalDataCursor, &normal, sizeof(normal)); | |
| 103 | + combinedNormalDataCursor += sizeof(normal); | |
| 104 | + } | |
| 105 | + // TODO -- other attributes | |
| 106 | + | |
| 107 | + // face data | |
| 108 | + const gpu::BufferView& indexBufferView = mesh->getIndexBuffer(); | |
| 109 | + gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)mesh->getNumAttributes(); | |
| 110 | + for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) { | |
| 111 | + uint32_t index = indexBufferView.get<uint32_t>(i); | |
| 112 | + index += indexStartOffset; | |
| 113 | + memcpy(combinedIndexDataCursor, &index, sizeof(index)); | |
| 114 | + combinedIndexDataCursor += sizeof(index); | |
| 115 | + } | |
| 116 | + | |
| 117 | + indexStartOffset += numVertices; | |
| 118 | + } | |
| 119 | + | |
| 120 | + gpu::Element vertexElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ); | |
| 121 | + gpu::Buffer* combinedVertexBuffer = new gpu::Buffer(combinedVertexSize, combinedVertexData); | |
| 122 | + gpu::BufferPointer combinedVertexBufferPointer(combinedVertexBuffer); | |
| 123 | + gpu::BufferView combinedVertexBufferView(combinedVertexBufferPointer, vertexElement); | |
| 124 | + result->setVertexBuffer(combinedVertexBufferView); | |
| 125 | + | |
| 126 | + gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ); | |
| 127 | + gpu::Buffer* combinedNormalsBuffer = new gpu::Buffer(combinedNormalSize, combinedNormalData); | |
| 128 | + gpu::BufferPointer combinedNormalsBufferPointer(combinedNormalsBuffer); | |
| 129 | + gpu::BufferView combinedNormalsBufferView(combinedNormalsBufferPointer, normalElement); | |
| 130 | + result->addAttribute(attributeTypeNormal, combinedNormalsBufferView); | |
| 131 | + | |
| 132 | + gpu::Element indexElement = gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::RAW); | |
| 133 | + gpu::Buffer* combinedIndexesBuffer = new gpu::Buffer(combinedIndexSize, combinedIndexData); | |
| 134 | + gpu::BufferPointer combinedIndexesBufferPointer(combinedIndexesBuffer); | |
| 135 | + gpu::BufferView combinedIndexesBufferView(combinedIndexesBufferPointer, indexElement); | |
| 136 | + result->setIndexBuffer(combinedIndexesBufferView); | |
| 137 | + | |
| 138 | + MeshProxy* resultProxy = new MeshProxy(result); | |
| 139 | + return meshToScriptValue(_modelScriptEngine, resultProxy); | |
| 140 | +} | |