History

vertex color support in ModelScriptingInterface

Seth Alves committed 9 years ago

Commit 47ea32f1 parent 6e039485 files · +70 −10Browse files

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

Unified · Split

libraries/script-engine/src/ModelScriptingInterface.cpp+25−3
@@ -38,16 +38,22 @@ QString ModelScriptingInterface::meshToOBJ(MeshProxyList in) { · 37 unmodified lines
3838 QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) {
3939 // figure out the size of the resulting mesh
4040 size_t totalVertexCount { 0 };
41− size_t totalAttributeCount { 0 };
41+ size_t totalColorCount { 0 };
42+ size_t totalNormalCount { 0 };
4243 size_t totalIndexCount { 0 };
4344 foreach (const MeshProxy* meshProxy, in) {
4445 MeshPointer mesh = meshProxy->getMeshPointer();
4546 totalVertexCount += mesh->getNumVertices();
4647
48+ int attributeTypeColor = gpu::Stream::InputSlot::COLOR; // libraries/gpu/src/gpu/Stream.h
49+ const gpu::BufferView& colorsBufferView = mesh->getAttributeBuffer(attributeTypeColor);
50+ gpu::BufferView::Index numColors = (gpu::BufferView::Index)colorsBufferView.getNumElements();
51+ totalColorCount += numColors;
52+
4753 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
4854 const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal);
4955 gpu::BufferView::Index numNormals = (gpu::BufferView::Index)normalsBufferView.getNumElements();
50− totalAttributeCount += numNormals;
56+ totalNormalCount += numNormals;
5157
5258 totalIndexCount += mesh->getNumIndices();
5359 }
@@ -57,7 +63,11 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 3 unmodified lines
5763 unsigned char* combinedVertexData = new unsigned char[combinedVertexSize];
5864 unsigned char* combinedVertexDataCursor = combinedVertexData;
5965
60− gpu::Resource::Size combinedNormalSize = totalAttributeCount * sizeof(glm::vec3);
66+ gpu::Resource::Size combinedColorSize = totalColorCount * sizeof(glm::vec3);
67+ unsigned char* combinedColorData = new unsigned char[combinedColorSize];
68+ unsigned char* combinedColorDataCursor = combinedColorData;
69+
70+ gpu::Resource::Size combinedNormalSize = totalNormalCount * sizeof(glm::vec3);
6171 unsigned char* combinedNormalData = new unsigned char[combinedNormalSize];
6272 unsigned char* combinedNormalDataCursor = combinedNormalData;
6373
@@ -74,6 +84,10 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 10 unmodified lines
7484 memcpy(combinedVertexDataCursor, &position, sizeof(position));
7585 combinedVertexDataCursor += sizeof(position);
7686 },
87+ [&](glm::vec3 color){
88+ memcpy(combinedColorDataCursor, &color, sizeof(color));
89+ combinedColorDataCursor += sizeof(color);
90+ },
7791 [&](glm::vec3 normal){
7892 memcpy(combinedNormalDataCursor, &normal, sizeof(normal));
7993 combinedNormalDataCursor += sizeof(normal);
@@ -96,6 +110,13 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 16 unmodified lines
96110 gpu::BufferView combinedVertexBufferView(combinedVertexBufferPointer, vertexElement);
97111 result->setVertexBuffer(combinedVertexBufferView);
98112
113+ int attributeTypeColor = gpu::Stream::InputSlot::COLOR; // libraries/gpu/src/gpu/Stream.h
114+ gpu::Element colorElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);
115+ gpu::Buffer* combinedColorsBuffer = new gpu::Buffer(combinedColorSize, combinedColorData);
116+ gpu::BufferPointer combinedColorsBufferPointer(combinedColorsBuffer);
117+ gpu::BufferView combinedColorsBufferView(combinedColorsBufferPointer, colorElement);
118+ result->addAttribute(attributeTypeColor, combinedColorsBufferView);
119+
99120 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
100121 gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);
101122 gpu::Buffer* combinedNormalsBuffer = new gpu::Buffer(combinedNormalSize, combinedNormalData);
@@ -132,6 +153,7 @@ QScriptValue ModelScriptingInterface::transformMesh(glm::mat4 transform, MeshPro · 30 unmodified lines
132153 }
133154
134155 model::MeshPointer result = mesh->map([&](glm::vec3 position){ return glm::vec3(transform * glm::vec4(position, 1.0f)); },
156+ [&](glm::vec3 color){ return color; },
135157 [&](glm::vec3 normal){ return glm::vec3(transform * glm::vec4(normal, 0.0f)); },
136158 [&](uint32_t index){ return index; });
137159 MeshProxy* resultProxy = new SimpleMeshProxy(result);