Showing the changes to libraries/script-engine/src/ModelScriptingInterface.cpp (1 of 3 files). Show all
Unified · Split
| @@ -53,10 +53,8 @@ QString ModelScriptingInterface::meshToOBJ(MeshProxyList in) { · 52 unmodified lines | ||
| 53 | 53 | return writeOBJToString(meshes); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | − | |
| 57 | 56 | QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { |
| 58 | − int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h | |
| 59 | − | |
| 57 | + // figure out the size of the resulting mesh | |
| 60 | 58 | size_t totalVertexCount { 0 }; |
| 61 | 59 | size_t totalAttributeCount { 0 }; |
| 62 | 60 | size_t totalIndexCount { 0 }; |
| @@ -67,6 +65,7 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 4 unmodified lines | ||
| 67 | 65 | totalIndexCount += mesh->getNumIndices(); |
| 68 | 66 | } |
| 69 | 67 | |
| 68 | + // alloc the resulting mesh | |
| 70 | 69 | gpu::Resource::Size combinedVertexSize = totalVertexCount * sizeof(glm::vec3); |
| 71 | 70 | unsigned char* combinedVertexData = new unsigned char[combinedVertexSize]; |
| 72 | 71 | unsigned char* combinedVertexDataCursor = combinedVertexData; |
| @@ -83,36 +82,22 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 10 unmodified lines | ||
| 83 | 82 | |
| 84 | 83 | foreach (const MeshProxy* meshProxy, in) { |
| 85 | 84 | MeshPointer mesh = meshProxy->getMeshPointer(); |
| 85 | + mesh->forEach( | |
| 86 | + [&](glm::vec3 position){ | |
| 87 | + memcpy(combinedVertexDataCursor, &position, sizeof(position)); | |
| 88 | + combinedVertexDataCursor += sizeof(position); | |
| 89 | + }, | |
| 90 | + [&](glm::vec3 normal){ | |
| 91 | + memcpy(combinedNormalDataCursor, &normal, sizeof(normal)); | |
| 92 | + combinedNormalDataCursor += sizeof(normal); | |
| 93 | + }, | |
| 94 | + [&](uint32_t index){ | |
| 95 | + index += indexStartOffset; | |
| 96 | + memcpy(combinedIndexDataCursor, &index, sizeof(index)); | |
| 97 | + combinedIndexDataCursor += sizeof(index); | |
| 98 | + }); | |
| 86 | 99 | |
| 87 | − // vertex data | |
| 88 | − const gpu::BufferView& vertexBufferView = mesh->getVertexBuffer(); | |
| 89 | 100 | gpu::BufferView::Index numVertices = (gpu::BufferView::Index)mesh->getNumVertices(); |
| 90 | − for (gpu::BufferView::Index i = 0; i < numVertices; i ++) { | |
| 91 | − glm::vec3 pos = vertexBufferView.get<glm::vec3>(i); | |
| 92 | − memcpy(combinedVertexDataCursor, &pos, sizeof(pos)); | |
| 93 | − combinedVertexDataCursor += sizeof(pos); | |
| 94 | − } | |
| 95 | − | |
| 96 | − // normal data | |
| 97 | − const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal); | |
| 98 | − gpu::BufferView::Index numNormals = (gpu::BufferView::Index)mesh->getNumAttributes(); | |
| 99 | − for (gpu::BufferView::Index i = 0; i < numNormals; i ++) { | |
| 100 | − glm::vec3 normal = normalsBufferView.get<glm::vec3>(i); | |
| 101 | − memcpy(combinedNormalDataCursor, &normal, sizeof(normal)); | |
| 102 | − combinedNormalDataCursor += sizeof(normal); | |
| 103 | − } | |
| 104 | − // TODO -- other attributes | |
| 105 | − | |
| 106 | − // face data | |
| 107 | − const gpu::BufferView& indexBufferView = mesh->getIndexBuffer(); | |
| 108 | − gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)mesh->getNumIndices(); | |
| 109 | − for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) { | |
| 110 | − uint32_t index = indexBufferView.get<uint32_t>(i); | |
| 111 | − index += indexStartOffset; | |
| 112 | − memcpy(combinedIndexDataCursor, &index, sizeof(index)); | |
| 113 | − combinedIndexDataCursor += sizeof(index); | |
| 114 | − } | |
| 115 | − | |
| 116 | 101 | indexStartOffset += numVertices; |
| 117 | 102 | } |
| 118 | 103 | |
| @@ -124,6 +109,7 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 5 unmodified lines | ||
| 124 | 109 | gpu::BufferView combinedVertexBufferView(combinedVertexBufferPointer, vertexElement); |
| 125 | 110 | result->setVertexBuffer(combinedVertexBufferView); |
| 126 | 111 | |
| 112 | + int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h | |
| 127 | 113 | gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ); |
| 128 | 114 | gpu::Buffer* combinedNormalsBuffer = new gpu::Buffer(combinedNormalSize, combinedNormalData); |
| 129 | 115 | gpu::BufferPointer combinedNormalsBufferPointer(combinedNormalsBuffer); |
| @@ -136,7 +122,6 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 6 unmodified lines | ||
| 136 | 122 | gpu::BufferView combinedIndexesBufferView(combinedIndexesBufferPointer, indexElement); |
| 137 | 123 | result->setIndexBuffer(combinedIndexesBufferView); |
| 138 | 124 | |
| 139 | − | |
| 140 | 125 | std::vector<model::Mesh::Part> parts; |
| 141 | 126 | parts.emplace_back(model::Mesh::Part((model::Index)0, // startIndex |
| 142 | 127 | (model::Index)result->getNumIndices(), // numIndices |
| @@ -151,92 +136,6 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 8 unmodified lines | ||
| 151 | 136 | } |
| 152 | 137 | |
| 153 | 138 | |
| 154 | −// QScriptValue ModelScriptingInterface::transformMesh(glm::mat4 transform, MeshProxy* meshProxy) { | |
| 155 | −// int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h | |
| 156 | − | |
| 157 | −// if (!meshProxy) { | |
| 158 | −// return QScriptValue(false); | |
| 159 | −// } | |
| 160 | − | |
| 161 | −// MeshPointer mesh = meshProxy->getMeshPointer(); | |
| 162 | − | |
| 163 | −// gpu::Resource::Size vertexSize = mesh->getNumVertices() * sizeof(glm::vec3); | |
| 164 | −// unsigned char* resultVertexData = new unsigned char[vertexSize]; | |
| 165 | −// unsigned char* vertexDataCursor = resultVertexData; | |
| 166 | − | |
| 167 | −// gpu::Resource::Size normalSize = mesh->getNumAttributes() * sizeof(glm::vec3); | |
| 168 | −// unsigned char* resultNormalData = new unsigned char[normalSize]; | |
| 169 | −// unsigned char* normalDataCursor = resultNormalData; | |
| 170 | − | |
| 171 | −// gpu::Resource::Size indexSize = mesh->getNumIndices() * sizeof(uint32_t); | |
| 172 | −// unsigned char* resultIndexData = new unsigned char[indexSize]; | |
| 173 | −// unsigned char* indexDataCursor = resultIndexData; | |
| 174 | − | |
| 175 | −// // vertex data | |
| 176 | −// const gpu::BufferView& vertexBufferView = mesh->getVertexBuffer(); | |
| 177 | −// gpu::BufferView::Index numVertices = (gpu::BufferView::Index)mesh->getNumVertices(); | |
| 178 | −// for (gpu::BufferView::Index i = 0; i < numVertices; i ++) { | |
| 179 | −// glm::vec3 pos = vertexBufferView.get<glm::vec3>(i); | |
| 180 | −// pos = glm::vec3(transform * glm::vec4(pos, 1.0f)); | |
| 181 | −// memcpy(vertexDataCursor, &pos, sizeof(pos)); | |
| 182 | −// vertexDataCursor += sizeof(pos); | |
| 183 | −// } | |
| 184 | − | |
| 185 | −// // normal data | |
| 186 | −// const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal); | |
| 187 | −// gpu::BufferView::Index numNormals = (gpu::BufferView::Index)mesh->getNumAttributes(); | |
| 188 | −// for (gpu::BufferView::Index i = 0; i < numNormals; i ++) { | |
| 189 | −// glm::vec3 normal = normalsBufferView.get<glm::vec3>(i); | |
| 190 | −// normal = glm::vec3(transform * glm::vec4(normal, 0.0f)); | |
| 191 | −// memcpy(normalDataCursor, &normal, sizeof(normal)); | |
| 192 | −// normalDataCursor += sizeof(normal); | |
| 193 | −// } | |
| 194 | −// // TODO -- other attributes | |
| 195 | − | |
| 196 | −// // face data | |
| 197 | −// const gpu::BufferView& indexBufferView = mesh->getIndexBuffer(); | |
| 198 | −// gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)mesh->getNumIndices(); | |
| 199 | −// for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) { | |
| 200 | −// uint32_t index = indexBufferView.get<uint32_t>(i); | |
| 201 | −// memcpy(indexDataCursor, &index, sizeof(index)); | |
| 202 | −// indexDataCursor += sizeof(index); | |
| 203 | −// } | |
| 204 | − | |
| 205 | −// model::MeshPointer result(new model::Mesh()); | |
| 206 | − | |
| 207 | −// gpu::Element vertexElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ); | |
| 208 | −// gpu::Buffer* resultVertexBuffer = new gpu::Buffer(vertexSize, resultVertexData); | |
| 209 | −// gpu::BufferPointer resultVertexBufferPointer(resultVertexBuffer); | |
| 210 | −// gpu::BufferView resultVertexBufferView(resultVertexBufferPointer, vertexElement); | |
| 211 | −// result->setVertexBuffer(resultVertexBufferView); | |
| 212 | − | |
| 213 | −// gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ); | |
| 214 | −// gpu::Buffer* resultNormalsBuffer = new gpu::Buffer(normalSize, resultNormalData); | |
| 215 | −// gpu::BufferPointer resultNormalsBufferPointer(resultNormalsBuffer); | |
| 216 | −// gpu::BufferView resultNormalsBufferView(resultNormalsBufferPointer, normalElement); | |
| 217 | −// result->addAttribute(attributeTypeNormal, resultNormalsBufferView); | |
| 218 | − | |
| 219 | −// gpu::Element indexElement = gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::RAW); | |
| 220 | −// gpu::Buffer* resultIndexesBuffer = new gpu::Buffer(indexSize, resultIndexData); | |
| 221 | −// gpu::BufferPointer resultIndexesBufferPointer(resultIndexesBuffer); | |
| 222 | −// gpu::BufferView resultIndexesBufferView(resultIndexesBufferPointer, indexElement); | |
| 223 | −// result->setIndexBuffer(resultIndexesBufferView); | |
| 224 | − | |
| 225 | − | |
| 226 | −// std::vector<model::Mesh::Part> parts; | |
| 227 | −// parts.emplace_back(model::Mesh::Part((model::Index)0, // startIndex | |
| 228 | −// (model::Index)result->getNumIndices(), // numIndices | |
| 229 | −// (model::Index)0, // baseVertex | |
| 230 | −// model::Mesh::TRIANGLES)); // topology | |
| 231 | −// result->setPartBuffer(gpu::BufferView(new gpu::Buffer(parts.size() * sizeof(model::Mesh::Part), | |
| 232 | −// (gpu::Byte*) parts.data()), gpu::Element::PART_DRAWCALL)); | |
| 233 | − | |
| 234 | − | |
| 235 | − | |
| 236 | −// MeshProxy* resultProxy = new MeshProxy(result); | |
| 237 | −// return meshToScriptValue(_modelScriptEngine, resultProxy); | |
| 238 | −// } | |
| 239 | − | |
| 240 | 139 | |
| 241 | 140 | QScriptValue ModelScriptingInterface::transformMesh(glm::mat4 transform, MeshProxy* meshProxy) { |
| 242 | 141 | if (!meshProxy) { |