Cogs.Core
DracoMeshDecompressor.h
1#pragma once
2
3#include "GltfLoader.h"
4
5#if defined ( _WIN32 )
6#pragma warning(push)
7#pragma warning(disable:4127 4804)
8#elif defined ( __clang__ )
9#pragma clang diagnostic push
10#pragma clang diagnostic ignored "-Wignored-qualifiers"
11#pragma clang diagnostic ignored "-Wdeprecated-this-capture"
12#pragma clang diagnostic ignored "-Wignored-qualifiers"
13#endif
14#include "draco/mesh/mesh.h"
15#if defined ( _WIN32 )
16#pragma warning(pop)
17#elif defined ( __clang__ )
18#pragma clang diagnostic pop
19#endif
20
21#include "Serialization/JsonParser.h"
22#include "Foundation/Memory/MemoryBuffer.h"
23
24namespace Cogs::Core {
25
27 {
28 public:
31
32 void initAttributes(const GltfLoader::Object& properties);
33 bool decompress(GltfLoader::GltfModelDefinition& loadData, GltfLoader::VertexStreamsState& streamsState, std::vector<GltfLoader::VertexStream>& vertexStreams,
34 const GltfLoader::Object& attributeObject, bool skipNormals);
35 private:
36 void processVertexColors(draco::Mesh* dmesh, std::vector<Cogs::Core::GltfLoader::VertexStream>& vertexStreams, GltfLoader::VertexStreamsState& streamsState);
37 void processTextureCoords(draco::Mesh* dmesh, std::vector<Cogs::Core::GltfLoader::VertexStream>& vertexStreams, GltfLoader::VertexStreamsState& streamsState);
38 void processNormals(draco::Mesh* dmesh, std::vector<Cogs::Core::GltfLoader::VertexStream>& vertexStreams, Cogs::Core::GltfLoader::VertexStreamsState& streamsState);
39 void processTangents(draco::Mesh* dmesh, std::vector<Cogs::Core::GltfLoader::VertexStream>& vertexStreams, Cogs::Core::GltfLoader::VertexStreamsState& streamsState);
40
41 template<typename T>
42 void getAttributeData(const draco::Mesh* dmesh, const draco::PointAttribute* attr, GltfLoader::VertexStream& targetVertexStream)
43 {
44 assert(attr->IsValid() && "Internal error");
45
46 const uint32_t numComponents = attr->num_components();
47 const uint32_t numPoints = dmesh->num_points();
48 const uint32_t numValues = numPoints * numComponents;
49 const uint32_t stride = static_cast<uint32_t>(attr->byte_stride());
50
51 std::vector<T> vectors;
52 vectors.reserve(numValues);
53 std::vector<T> value(numComponents, -2);
54
55 for (draco::PointIndex i(0); i < numPoints; ++i) {
56 const draco::AttributeValueIndex val_index = attr->mapped_index(i);
57 if (!attr->ConvertValue<T>(val_index, &value[0])) {
58 assert(false && "Internal error. Could not convert Draco attribute value.");
59 return;
60 }
61 for (uint32_t j = 0; j < numComponents; ++j) {
62 vectors.emplace_back(value[j]);
63 }
64 }
65
66 this->decompressedAttributes.push_back(Memory::MemoryBuffer(numValues * sizeof(T)));
67 uint8_t* buf = reinterpret_cast<uint8_t*>(this->decompressedAttributes.back().data());
68 memcpy(buf, &vectors[0], numValues * sizeof(T));
69
70 targetVertexStream.dataView = GltfLoader::BufferDataView{ buf, stride, numPoints};
71 targetVertexStream.formatSize = stride;
72 targetVertexStream.offset = 0;
73 }
74
75 int bufferViewIdx = -1;
76 int attrPositionIdx = -1;
77 int attrNormalIdx = -1;
78 int attrTangentIdx = -1;
79 std::vector<int> attrTexCoordIndices; // TEX_0, TEX_1 etc.
80 std::vector<int> attrColorIndices; // COLOR_0, COLOR_1 etc.
81
82 // These are additional buffers which has been allocated. They will be deleted by the
83 // class destructor prevent memleaks.
84 Memory::MemoryBuffer decompressedIndices;
85 std::vector<Memory::MemoryBuffer> decompressedAttributes;
86 };
87
88}
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....