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
38QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) {38QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) {
39 // figure out the size of the resulting mesh39 // figure out the size of the resulting mesh
40 size_t totalVertexCount { 0 };40 size_t totalVertexCount { 0 };
41 size_t totalAttributeCount { 0 };41 size_t totalColorCount { 0 };
42 size_t totalNormalCount { 0 };
42 size_t totalIndexCount { 0 };43 size_t totalIndexCount { 0 };
43 foreach (const MeshProxy* meshProxy, in) {44 foreach (const MeshProxy* meshProxy, in) {
44 MeshPointer mesh = meshProxy->getMeshPointer();45 MeshPointer mesh = meshProxy->getMeshPointer();
45 totalVertexCount += mesh->getNumVertices();46 totalVertexCount += mesh->getNumVertices();
46 47
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
47 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h53 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
48 const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal);54 const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal);
49 gpu::BufferView::Index numNormals = (gpu::BufferView::Index)normalsBufferView.getNumElements();55 gpu::BufferView::Index numNormals = (gpu::BufferView::Index)normalsBufferView.getNumElements();
50 totalAttributeCount += numNormals;56 totalNormalCount += numNormals;
51 57
52 totalIndexCount += mesh->getNumIndices();58 totalIndexCount += mesh->getNumIndices();
53 }59 }
@@ -57,7 +63,11 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 3 unmodified lines
57 unsigned char* combinedVertexData = new unsigned char[combinedVertexSize];63 unsigned char* combinedVertexData = new unsigned char[combinedVertexSize];
58 unsigned char* combinedVertexDataCursor = combinedVertexData;64 unsigned char* combinedVertexDataCursor = combinedVertexData;
59 65
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);
61 unsigned char* combinedNormalData = new unsigned char[combinedNormalSize];71 unsigned char* combinedNormalData = new unsigned char[combinedNormalSize];
62 unsigned char* combinedNormalDataCursor = combinedNormalData;72 unsigned char* combinedNormalDataCursor = combinedNormalData;
63 73
@@ -74,6 +84,10 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 10 unmodified lines
74 memcpy(combinedVertexDataCursor, &position, sizeof(position));84 memcpy(combinedVertexDataCursor, &position, sizeof(position));
75 combinedVertexDataCursor += sizeof(position);85 combinedVertexDataCursor += sizeof(position);
76 },86 },
87 [&](glm::vec3 color){
88 memcpy(combinedColorDataCursor, &color, sizeof(color));
89 combinedColorDataCursor += sizeof(color);
90 },
77 [&](glm::vec3 normal){91 [&](glm::vec3 normal){
78 memcpy(combinedNormalDataCursor, &normal, sizeof(normal));92 memcpy(combinedNormalDataCursor, &normal, sizeof(normal));
79 combinedNormalDataCursor += sizeof(normal);93 combinedNormalDataCursor += sizeof(normal);
@@ -96,6 +110,13 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) { · 16 unmodified lines
96 gpu::BufferView combinedVertexBufferView(combinedVertexBufferPointer, vertexElement);110 gpu::BufferView combinedVertexBufferView(combinedVertexBufferPointer, vertexElement);
97 result->setVertexBuffer(combinedVertexBufferView);111 result->setVertexBuffer(combinedVertexBufferView);
98 112
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
99 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h120 int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
100 gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);121 gpu::Element normalElement = gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ);
101 gpu::Buffer* combinedNormalsBuffer = new gpu::Buffer(combinedNormalSize, combinedNormalData);122 gpu::Buffer* combinedNormalsBuffer = new gpu::Buffer(combinedNormalSize, combinedNormalData);
@@ -132,6 +153,7 @@ QScriptValue ModelScriptingInterface::transformMesh(glm::mat4 transform, MeshPro · 30 unmodified lines
132 }153 }
133 154
134 model::MeshPointer result = mesh->map([&](glm::vec3 position){ return glm::vec3(transform * glm::vec4(position, 1.0f)); },155 model::MeshPointer result = mesh->map([&](glm::vec3 position){ return glm::vec3(transform * glm::vec4(position, 1.0f)); },
156 [&](glm::vec3 color){ return color; },
135 [&](glm::vec3 normal){ return glm::vec3(transform * glm::vec4(normal, 0.0f)); },157 [&](glm::vec3 normal){ return glm::vec3(transform * glm::vec4(normal, 0.0f)); },
136 [&](uint32_t index){ return index; });158 [&](uint32_t index){ return index; });
137 MeshProxy* resultProxy = new SimpleMeshProxy(result);159 MeshProxy* resultProxy = new SimpleMeshProxy(result);