History

Fixed memory leak in ModelScriptingInterface::appendMeshes and wrong normal transformation in ModelScriptingInterface

Olivier Prat committed 8 years ago

Commit 75295251 parent 40f83a61 file · +14 −13Browse files

Showing the changes to libraries/script-engine/src/ModelScriptingInterface.cpp (1 of 1 file). Show all

Unified · Split

libraries/script-engine/src/ModelScriptingInterface.cpp+14−13
@@ -60,20 +60,20 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 59 unmodified lines
60 60
61 // alloc the resulting mesh61 // alloc the resulting mesh
62 gpu::Resource::Size combinedVertexSize = totalVertexCount * sizeof(glm::vec3);62 gpu::Resource::Size combinedVertexSize = totalVertexCount * sizeof(glm::vec3);
63 unsigned char* combinedVertexData = new unsigned char[combinedVertexSize];63 std::unique_ptr<unsigned char> combinedVertexData{ new unsigned char[combinedVertexSize] };
64 unsigned char* combinedVertexDataCursor = combinedVertexData;64 unsigned char* combinedVertexDataCursor = combinedVertexData.get();
65 65
66 gpu::Resource::Size combinedColorSize = totalColorCount * sizeof(glm::vec3);66 gpu::Resource::Size combinedColorSize = totalColorCount * sizeof(glm::vec3);
67 unsigned char* combinedColorData = new unsigned char[combinedColorSize];67 std::unique_ptr<unsigned char> combinedColorData{ new unsigned char[combinedColorSize] };
68 unsigned char* combinedColorDataCursor = combinedColorData;68 unsigned char* combinedColorDataCursor = combinedColorData.get();
69 69
70 gpu::Resource::Size combinedNormalSize = totalNormalCount * sizeof(glm::vec3);70 gpu::Resource::Size combinedNormalSize = totalNormalCount * sizeof(glm::vec3);
71 unsigned char* combinedNormalData = new unsigned char[combinedNormalSize];71 std::unique_ptr<unsigned char> combinedNormalData{ new unsigned char[combinedNormalSize] };
72 unsigned char* combinedNormalDataCursor = combinedNormalData;72 unsigned char* combinedNormalDataCursor = combinedNormalData.get();
73 73
74 gpu::Resource::Size combinedIndexSize = totalIndexCount * sizeof(uint32_t);74 gpu::Resource::Size combinedIndexSize = totalIndexCount * sizeof(uint32_t);
75 unsigned char* combinedIndexData = new unsigned char[combinedIndexSize];75 std::unique_ptr<unsigned char> combinedIndexData{ new unsigned char[combinedIndexSize] };
76 unsigned char* combinedIndexDataCursor = combinedIndexData;76 unsigned char* combinedIndexDataCursor = combinedIndexData.get();
77 77
78 uint32_t indexStartOffset { 0 };78 uint32_t indexStartOffset { 0 };
79 79
@@ -105,27 +105,27 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 25 unmodified lines
105 model::MeshPointer result(new model::Mesh());105 model::MeshPointer result(new model::Mesh());
106 106
107 gpu::Element vertexElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);107 gpu::Element vertexElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);
108 gpu::Buffer* combinedVertexBuffer = new gpu::Buffer(combinedVertexSize, combinedVertexData);108 gpu::Buffer* combinedVertexBuffer = new gpu::Buffer(combinedVertexSize, combinedVertexData.get());
109 gpu::BufferPointer combinedVertexBufferPointer(combinedVertexBuffer);109 gpu::BufferPointer combinedVertexBufferPointer(combinedVertexBuffer);
110 gpu::BufferView combinedVertexBufferView(combinedVertexBufferPointer, vertexElement);110 gpu::BufferView combinedVertexBufferView(combinedVertexBufferPointer, vertexElement);
111 result->setVertexBuffer(combinedVertexBufferView);111 result->setVertexBuffer(combinedVertexBufferView);
112 112
113 int attributeTypeColor = gpu::Stream::InputSlot::COLOR; // libraries/gpu/src/gpu/Stream.h113 int attributeTypeColor = gpu::Stream::InputSlot::COLOR; // libraries/gpu/src/gpu/Stream.h
114 gpu::Element colorElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);114 gpu::Element colorElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);
115 gpu::Buffer* combinedColorsBuffer = new gpu::Buffer(combinedColorSize, combinedColorData);115 gpu::Buffer* combinedColorsBuffer = new gpu::Buffer(combinedColorSize, combinedColorData.get());
116 gpu::BufferPointer combinedColorsBufferPointer(combinedColorsBuffer);116 gpu::BufferPointer combinedColorsBufferPointer(combinedColorsBuffer);
117 gpu::BufferView combinedColorsBufferView(combinedColorsBufferPointer, colorElement);117 gpu::BufferView combinedColorsBufferView(combinedColorsBufferPointer, colorElement);
118 result->addAttribute(attributeTypeColor, combinedColorsBufferView);118 result->addAttribute(attributeTypeColor, combinedColorsBufferView);
119 119
120 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h120 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
121 gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);121 gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);
122 gpu::Buffer* combinedNormalsBuffer = new gpu::Buffer(combinedNormalSize, combinedNormalData);122 gpu::Buffer* combinedNormalsBuffer = new gpu::Buffer(combinedNormalSize, combinedNormalData.get());
123 gpu::BufferPointer combinedNormalsBufferPointer(combinedNormalsBuffer);123 gpu::BufferPointer combinedNormalsBufferPointer(combinedNormalsBuffer);
124 gpu::BufferView combinedNormalsBufferView(combinedNormalsBufferPointer, normalElement);124 gpu::BufferView combinedNormalsBufferView(combinedNormalsBufferPointer, normalElement);
125 result->addAttribute(attributeTypeNormal, combinedNormalsBufferView);125 result->addAttribute(attributeTypeNormal, combinedNormalsBufferView);
126 126
127 gpu::Element indexElement = gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::RAW);127 gpu::Element indexElement = gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::RAW);
128 gpu::Buffer* combinedIndexesBuffer = new gpu::Buffer(combinedIndexSize, combinedIndexData);128 gpu::Buffer* combinedIndexesBuffer = new gpu::Buffer(combinedIndexSize, combinedIndexData.get());
129 gpu::BufferPointer combinedIndexesBufferPointer(combinedIndexesBuffer);129 gpu::BufferPointer combinedIndexesBufferPointer(combinedIndexesBuffer);
130 gpu::BufferView combinedIndexesBufferView(combinedIndexesBufferPointer, indexElement);130 gpu::BufferView combinedIndexesBufferView(combinedIndexesBufferPointer, indexElement);
131 result->setIndexBuffer(combinedIndexesBufferView);131 result->setIndexBuffer(combinedIndexesBufferView);
@@ -152,9 +152,10 @@ QScriptValue ModelScriptingInterface::transformMesh(glm::mat4 transform, MeshPro · 20 unmodified lines
152 return QScriptValue(false);152 return QScriptValue(false);
153 }153 }
154 154
155 const auto inverseTransposeTransform = glm::inverse(glm::transpose(transform));
155 model::MeshPointer result = mesh->map([&](glm::vec3 position){ return glm::vec3(transform * glm::vec4(position, 1.0f)); },156 model::MeshPointer result = mesh->map([&](glm::vec3 position){ return glm::vec3(transform * glm::vec4(position, 1.0f)); },
156 [&](glm::vec3 color){ return color; },157 [&](glm::vec3 color){ return color; },
157 [&](glm::vec3 normal){ return glm::vec3(transform * glm::vec4(normal, 0.0f)); },158 [&](glm::vec3 normal){ return glm::vec3(inverseTransposeTransform * glm::vec4(normal, 0.0f)); },
158 [&](uint32_t index){ return index; });159 [&](uint32_t index){ return index; });
159 MeshProxy* resultProxy = new SimpleMeshProxy(result);160 MeshProxy* resultProxy = new SimpleMeshProxy(result);
160 return meshToScriptValue(_modelScriptEngine, resultProxy);161 return meshToScriptValue(_modelScriptEngine, resultProxy);