Showing the changes to libraries/script-engine/src/ModelScriptingInterface.cpp (1 of 4 files). Show all
Unified · Split
| @@ -55,7 +55,6 @@ QString ModelScriptingInterface::meshToOBJ(MeshProxyList in) { · 54 unmodified lines | ||
| 55 | 55 | |
| 56 | 56 | |
| 57 | 57 | QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { |
| 58 | − model::MeshPointer result(new model::Mesh()); | |
| 59 | 58 | int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h |
| 60 | 59 | |
| 61 | 60 | size_t totalVertexCount { 0 }; |
| @@ -72,11 +71,11 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 10 unmodified lines | ||
| 72 | 71 | unsigned char* combinedVertexData = new unsigned char[combinedVertexSize]; |
| 73 | 72 | unsigned char* combinedVertexDataCursor = combinedVertexData; |
| 74 | 73 | |
| 75 | − gpu::Resource::Size combinedNormalSize = totalVertexCount * sizeof(glm::vec3); | |
| 74 | + gpu::Resource::Size combinedNormalSize = totalAttributeCount * sizeof(glm::vec3); | |
| 76 | 75 | unsigned char* combinedNormalData = new unsigned char[combinedNormalSize]; |
| 77 | 76 | unsigned char* combinedNormalDataCursor = combinedNormalData; |
| 78 | 77 | |
| 79 | − gpu::Resource::Size combinedIndexSize = totalVertexCount * sizeof(uint32_t); | |
| 78 | + gpu::Resource::Size combinedIndexSize = totalIndexCount * sizeof(uint32_t); | |
| 80 | 79 | unsigned char* combinedIndexData = new unsigned char[combinedIndexSize]; |
| 81 | 80 | unsigned char* combinedIndexDataCursor = combinedIndexData; |
| 82 | 81 | |
| @@ -96,7 +95,7 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 13 unmodified lines | ||
| 96 | 95 | |
| 97 | 96 | // normal data |
| 98 | 97 | const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal); |
| 99 | − gpu::BufferView::Index numNormals = (gpu::BufferView::Index)mesh->getNumAttributes(); | |
| 98 | + gpu::BufferView::Index numNormals = (gpu::BufferView::Index)mesh->getNumAttributes(); | |
| 100 | 99 | for (gpu::BufferView::Index i = 0; i < numNormals; i ++) { |
| 101 | 100 | glm::vec3 normal = normalsBufferView.get<glm::vec3>(i); |
| 102 | 101 | memcpy(combinedNormalDataCursor, &normal, sizeof(normal)); |
| @@ -106,7 +105,7 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 3 unmodified lines | ||
| 106 | 105 | |
| 107 | 106 | // face data |
| 108 | 107 | const gpu::BufferView& indexBufferView = mesh->getIndexBuffer(); |
| 109 | − gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)mesh->getNumAttributes(); | |
| 108 | + gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)mesh->getNumIndices(); | |
| 110 | 109 | for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) { |
| 111 | 110 | uint32_t index = indexBufferView.get<uint32_t>(i); |
| 112 | 111 | index += indexStartOffset; |
| @@ -117,6 +116,8 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 4 unmodified lines | ||
| 117 | 116 | indexStartOffset += numVertices; |
| 118 | 117 | } |
| 119 | 118 | |
| 119 | + model::MeshPointer result(new model::Mesh()); | |
| 120 | + | |
| 120 | 121 | gpu::Element vertexElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ); |
| 121 | 122 | gpu::Buffer* combinedVertexBuffer = new gpu::Buffer(combinedVertexSize, combinedVertexData); |
| 122 | 123 | gpu::BufferPointer combinedVertexBufferPointer(combinedVertexBuffer); |
| @@ -135,6 +136,99 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 12 unmodified lines | ||
| 135 | 136 | gpu::BufferView combinedIndexesBufferView(combinedIndexesBufferPointer, indexElement); |
| 136 | 137 | result->setIndexBuffer(combinedIndexesBufferView); |
| 137 | 138 | |
| 139 | + | |
| 140 | + std::vector<model::Mesh::Part> parts; | |
| 141 | + parts.emplace_back(model::Mesh::Part((model::Index)0, // startIndex | |
| 142 | + (model::Index)result->getNumIndices(), // numIndices | |
| 143 | + (model::Index)0, // baseVertex | |
| 144 | + model::Mesh::TRIANGLES)); // topology | |
| 145 | + result->setPartBuffer(gpu::BufferView(new gpu::Buffer(parts.size() * sizeof(model::Mesh::Part), | |
| 146 | + (gpu::Byte*) parts.data()), gpu::Element::PART_DRAWCALL)); | |
| 147 | + | |
| 148 | + | |
| 149 | + MeshProxy* resultProxy = new MeshProxy(result); | |
| 150 | + return meshToScriptValue(_modelScriptEngine, resultProxy); | |
| 151 | +} | |
| 152 | + | |
| 153 | + | |
| 154 | +QScriptValue ModelScriptingInterface::transformMesh(glm::mat4 transform, MeshProxy* meshProxy) { | |
| 155 | + int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h | |
| 156 | + | |
| 157 | + MeshPointer mesh = meshProxy->getMeshPointer(); | |
| 158 | + | |
| 159 | + gpu::Resource::Size vertexSize = mesh->getNumVertices() * sizeof(glm::vec3); | |
| 160 | + unsigned char* resultVertexData = new unsigned char[vertexSize]; | |
| 161 | + unsigned char* vertexDataCursor = resultVertexData; | |
| 162 | + | |
| 163 | + gpu::Resource::Size normalSize = mesh->getNumAttributes() * sizeof(glm::vec3); | |
| 164 | + unsigned char* resultNormalData = new unsigned char[normalSize]; | |
| 165 | + unsigned char* normalDataCursor = resultNormalData; | |
| 166 | + | |
| 167 | + gpu::Resource::Size indexSize = mesh->getNumIndices() * sizeof(uint32_t); | |
| 168 | + unsigned char* resultIndexData = new unsigned char[indexSize]; | |
| 169 | + unsigned char* indexDataCursor = resultIndexData; | |
| 170 | + | |
| 171 | + // vertex data | |
| 172 | + const gpu::BufferView& vertexBufferView = mesh->getVertexBuffer(); | |
| 173 | + gpu::BufferView::Index numVertices = (gpu::BufferView::Index)mesh->getNumVertices(); | |
| 174 | + for (gpu::BufferView::Index i = 0; i < numVertices; i ++) { | |
| 175 | + glm::vec3 pos = vertexBufferView.get<glm::vec3>(i); | |
| 176 | + pos = glm::vec3(transform * glm::vec4(pos, 0.0f)); | |
| 177 | + memcpy(vertexDataCursor, &pos, sizeof(pos)); | |
| 178 | + vertexDataCursor += sizeof(pos); | |
| 179 | + } | |
| 180 | + | |
| 181 | + // normal data | |
| 182 | + const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal); | |
| 183 | + gpu::BufferView::Index numNormals = (gpu::BufferView::Index)mesh->getNumAttributes(); | |
| 184 | + for (gpu::BufferView::Index i = 0; i < numNormals; i ++) { | |
| 185 | + glm::vec3 normal = normalsBufferView.get<glm::vec3>(i); | |
| 186 | + normal = glm::vec3(transform * glm::vec4(normal, 0.0f)); | |
| 187 | + memcpy(normalDataCursor, &normal, sizeof(normal)); | |
| 188 | + normalDataCursor += sizeof(normal); | |
| 189 | + } | |
| 190 | + // TODO -- other attributes | |
| 191 | + | |
| 192 | + // face data | |
| 193 | + const gpu::BufferView& indexBufferView = mesh->getIndexBuffer(); | |
| 194 | + gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)mesh->getNumIndices(); | |
| 195 | + for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) { | |
| 196 | + uint32_t index = indexBufferView.get<uint32_t>(i); | |
| 197 | + memcpy(indexDataCursor, &index, sizeof(index)); | |
| 198 | + indexDataCursor += sizeof(index); | |
| 199 | + } | |
| 200 | + | |
| 201 | + model::MeshPointer result(new model::Mesh()); | |
| 202 | + | |
| 203 | + gpu::Element vertexElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ); | |
| 204 | + gpu::Buffer* resultVertexBuffer = new gpu::Buffer(vertexSize, resultVertexData); | |
| 205 | + gpu::BufferPointer resultVertexBufferPointer(resultVertexBuffer); | |
| 206 | + gpu::BufferView resultVertexBufferView(resultVertexBufferPointer, vertexElement); | |
| 207 | + result->setVertexBuffer(resultVertexBufferView); | |
| 208 | + | |
| 209 | + gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ); | |
| 210 | + gpu::Buffer* resultNormalsBuffer = new gpu::Buffer(normalSize, resultNormalData); | |
| 211 | + gpu::BufferPointer resultNormalsBufferPointer(resultNormalsBuffer); | |
| 212 | + gpu::BufferView resultNormalsBufferView(resultNormalsBufferPointer, normalElement); | |
| 213 | + result->addAttribute(attributeTypeNormal, resultNormalsBufferView); | |
| 214 | + | |
| 215 | + gpu::Element indexElement = gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::RAW); | |
| 216 | + gpu::Buffer* resultIndexesBuffer = new gpu::Buffer(indexSize, resultIndexData); | |
| 217 | + gpu::BufferPointer resultIndexesBufferPointer(resultIndexesBuffer); | |
| 218 | + gpu::BufferView resultIndexesBufferView(resultIndexesBufferPointer, indexElement); | |
| 219 | + result->setIndexBuffer(resultIndexesBufferView); | |
| 220 | + | |
| 221 | + | |
| 222 | + std::vector<model::Mesh::Part> parts; | |
| 223 | + parts.emplace_back(model::Mesh::Part((model::Index)0, // startIndex | |
| 224 | + (model::Index)result->getNumIndices(), // numIndices | |
| 225 | + (model::Index)0, // baseVertex | |
| 226 | + model::Mesh::TRIANGLES)); // topology | |
| 227 | + result->setPartBuffer(gpu::BufferView(new gpu::Buffer(parts.size() * sizeof(model::Mesh::Part), | |
| 228 | + (gpu::Byte*) parts.data()), gpu::Element::PART_DRAWCALL)); | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 138 | 232 | MeshProxy* resultProxy = new MeshProxy(result); |
| 139 | 233 | return meshToScriptValue(_modelScriptEngine, resultProxy); |
| 140 | 234 | } |