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
6060
6161 // alloc the resulting mesh
6262 gpu::Resource::Size combinedVertexSize = totalVertexCount * sizeof(glm::vec3);
63− unsigned char* combinedVertexData = new unsigned char[combinedVertexSize];
64− unsigned char* combinedVertexDataCursor = combinedVertexData;
63+ std::unique_ptr<unsigned char> combinedVertexData{ new unsigned char[combinedVertexSize] };
64+ unsigned char* combinedVertexDataCursor = combinedVertexData.get();
6565
6666 gpu::Resource::Size combinedColorSize = totalColorCount * sizeof(glm::vec3);
67− unsigned char* combinedColorData = new unsigned char[combinedColorSize];
68− unsigned char* combinedColorDataCursor = combinedColorData;
67+ std::unique_ptr<unsigned char> combinedColorData{ new unsigned char[combinedColorSize] };
68+ unsigned char* combinedColorDataCursor = combinedColorData.get();
6969
7070 gpu::Resource::Size combinedNormalSize = totalNormalCount * sizeof(glm::vec3);
71− unsigned char* combinedNormalData = new unsigned char[combinedNormalSize];
72− unsigned char* combinedNormalDataCursor = combinedNormalData;
71+ std::unique_ptr<unsigned char> combinedNormalData{ new unsigned char[combinedNormalSize] };
72+ unsigned char* combinedNormalDataCursor = combinedNormalData.get();
7373
7474 gpu::Resource::Size combinedIndexSize = totalIndexCount * sizeof(uint32_t);
75− unsigned char* combinedIndexData = new unsigned char[combinedIndexSize];
76− unsigned char* combinedIndexDataCursor = combinedIndexData;
75+ std::unique_ptr<unsigned char> combinedIndexData{ new unsigned char[combinedIndexSize] };
76+ unsigned char* combinedIndexDataCursor = combinedIndexData.get();
7777
7878 uint32_t indexStartOffset { 0 };
7979
@@ -105,27 +105,27 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 25 unmodified lines
105105 model::MeshPointer result(new model::Mesh());
106106
107107 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());
109109 gpu::BufferPointer combinedVertexBufferPointer(combinedVertexBuffer);
110110 gpu::BufferView combinedVertexBufferView(combinedVertexBufferPointer, vertexElement);
111111 result->setVertexBuffer(combinedVertexBufferView);
112112
113113 int attributeTypeColor = gpu::Stream::InputSlot::COLOR; // libraries/gpu/src/gpu/Stream.h
114114 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());
116116 gpu::BufferPointer combinedColorsBufferPointer(combinedColorsBuffer);
117117 gpu::BufferView combinedColorsBufferView(combinedColorsBufferPointer, colorElement);
118118 result->addAttribute(attributeTypeColor, combinedColorsBufferView);
119119
120120 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
121121 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());
123123 gpu::BufferPointer combinedNormalsBufferPointer(combinedNormalsBuffer);
124124 gpu::BufferView combinedNormalsBufferView(combinedNormalsBufferPointer, normalElement);
125125 result->addAttribute(attributeTypeNormal, combinedNormalsBufferView);
126126
127127 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());
129129 gpu::BufferPointer combinedIndexesBufferPointer(combinedIndexesBuffer);
130130 gpu::BufferView combinedIndexesBufferView(combinedIndexesBufferPointer, indexElement);
131131 result->setIndexBuffer(combinedIndexesBufferView);
@@ -152,9 +152,10 @@ QScriptValue ModelScriptingInterface::transformMesh(glm::mat4 transform, MeshPro · 20 unmodified lines
152152 return QScriptValue(false);
153153 }
154154
155+ const auto inverseTransposeTransform = glm::inverse(glm::transpose(transform));
155156 model::MeshPointer result = mesh->map([&](glm::vec3 position){ return glm::vec3(transform * glm::vec4(position, 1.0f)); },
156157 [&](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)); },
158159 [&](uint32_t index){ return index; });
159160 MeshProxy* resultProxy = new SimpleMeshProxy(result);
160161 return meshToScriptValue(_modelScriptEngine, resultProxy);