const char* mode_str;
const char* type_str;
ComponentType component_type;
+std::list<o3dgc::Real*> float_attributes_indices;// See above about "floatAttributesIndexes".
+
+ /**********************************************************/
+ /************** Read data from JSON-document **************/
+ /**********************************************************/
#define MESH_READ_COMPRESSEDDATA_MEMBER(pFieldName, pOut) \
if(!ReadMember(pJSON_Object_CompressedData, pFieldName, pOut)) { throw DeadlyImportError(std::string("GLTF: \"compressedData\" must has \"") + pFieldName + "\"."); }
#undef MESH_READ_COMPRESSEDDATA_MEMBER
+ //
+ // Check for float attributes
+ //
+ // Object example:
+ // "floatAttributesIndexes": {
+ // "accessor_1356": 0,
+ // "accessor_1358": 1,
+ // "accessor_1360": 2
+ // },
+ //
+ // Find object "floatAttributesIndexes"
+ Value* float_attr_ind = FindObject(pJSON_Object_CompressedData, "floatAttributesIndexes");
+
+ if(float_attr_ind != nullptr)
+ {
+ size_t attr_num = 0;
+
+ // Walk thru children and get accessors and numbers of attributes.
+ for(Value::MemberIterator memb_it = float_attr_ind->MemberBegin(); memb_it != float_attr_ind->MemberEnd(); ++memb_it)
+ {
+ if(!memb_it->name.IsString()) throw DeadlyImportError("GLTF: name of the member of \"floatAttributesIndexes\" must be a string.");
+ if(!memb_it->value.IsUint()) throw DeadlyImportError(std::string("GLTF: value of the member (\"") + memb_it->name.GetString() + \
+ "\") in \"floatAttributesIndexes\" must be an unsigned integer.");
+ /*if(attr_num != memb_it->value.GetUint()) throw DeadlyImportError(std::string("GLTF: invalid number of float attribute index. Member (\"") + \
+ memb_it->name.GetString() + "\".");*/
+
+ attr_num++;
+ // Checks passed, extract data.
+ Ref<Accessor> attr_cur_acc = pAsset_Root.accessors.Get(memb_it->name.GetString());
+
+ float_attributes_indices.push_back((float*)attr_cur_acc->GetPointer());
+ }
+ }// if(float_attr_ind != nullptr)
+
// Check some values
if(strcmp(type_str, "SCALAR")) throw DeadlyImportError("GLTF: only \"SCALAR\" type is supported for compressed data.");
if(component_type != ComponentType_UNSIGNED_BYTE) throw DeadlyImportError("GLTF: only \"UNSIGNED_BYTE\" component type is supported for compressed data.");
throw DeadlyImportError(std::string("GLTF: for compressed data supported modes is: \"ascii\", \"binary\". Not the: \"") + mode_str + "\".");
}
+ /**********************************************************/
+ /********************* Decoding data **********************/
+ /**********************************************************/
+
// Search for "bufferView" by ID.
Ref<BufferView> bufview = pAsset_Root.bufferViews.Get(bufview_id);
//
// void testDecode(shared_ptr <GLTFMesh> mesh, BinaryStream &bstream)
o3dgc::SC3DMCDecoder<uint16_t> decoder;
- o3dgc::IndexedFaceSet <unsigned short> ifs;
+ o3dgc::IndexedFaceSet<uint16_t> ifs;
uint8_t* output_data;
- size_t size_vertex, size_normal, size_texcoord, size_indices, output_data_size;
+ size_t size_vertex, size_normal, size_indices, output_data_size;
o3dgc::BinaryStream bstream;
+ float* tarrays[6];
// Read data from buffer and place it in BinaryStream for decoder.
bstream.LoadFromBuffer(&bufview->buffer->GetPointer()[bufview->byteOffset + byte_offset], count);
// After decoding header we can get size of primitives
if(decoder.DecodeHeader(ifs, bstream) != o3dgc::O3DGC_OK) throw DeadlyImportError("GLTF: can not decode Open3DGC header.");
+ size_indices = ifs.GetNCoordIndex() * 3 * sizeof(unsigned short);
size_vertex = ifs.GetNCoord() * 3 * sizeof(float);
size_normal = ifs.GetNNormal() * 3 * sizeof(float);
- size_texcoord = ifs.GetNFloatAttribute(0) * 2 * sizeof(float);
- size_indices = ifs.GetNCoordIndex() * 3 * sizeof(unsigned short);
- output_data_size = size_vertex + size_normal + size_texcoord + size_indices;
+ for(size_t idx_attr = 0, idx_attr_end = ifs.GetNumFloatAttributes(); idx_attr < idx_attr_end; idx_attr++)
+ {
+ size_t qty_attr = ifs.GetNFloatAttribute(idx_attr);
+
+ if(qty_attr == 0) continue;
+
+ tarrays[idx_attr] = new float[qty_attr * ifs.GetFloatAttributeDim(idx_attr)];
+ ifs.SetFloatAttribute(idx_attr, tarrays[idx_attr]);
+ /*
+ switch(ifs.GetFloatAttributeType(idx_attr))
+ {
+ // Unknown for Open3DGC,
+ // but not for COLLADA2GLTF - GLTF::Semantic::JOINT. What's mean "JOINT"? Good question, but lim(comments in COLLADA2GLTF) = 0.
+ case o3dgc::O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_UNKOWN:// 0
+ break;
+ case o3dgc::O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_POSITION:// 1
+ break;
+ case o3dgc::O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_NORMAL:// 2
+ break;
+ case o3dgc::O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_COLOR:// 3
+ break;
+ case o3dgc::O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_TEXCOORD:// 4
+ break;
+ case o3dgc::O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_WEIGHT:// 5
+ break;
+ default:
+ throw DeadlyImportError("GLTF: Unknown type of float attribute (" + std::to_string(idx_attr) + ") for Open3DGC encoding.");
+ }*/
+ }
+
+ //size_texcoord = ifs.GetNFloatAttribute(0) * 2 * sizeof(float);
+
+ output_data_size = size_vertex + size_normal + /*size_texcoord*/ + size_indices;
output_data = new uint8_t[output_data_size];
float* uncompressed_vertices = (float* const)(output_data + size_indices);// size_indices => vertex offset
if(ifs.GetNNormal() > 0) ifs.SetNormal((float* const)(output_data + size_indices + size_vertex));
- if(ifs.GetNFloatAttribute(0)) ifs.SetFloatAttribute(0, (float* const)(output_data + size_indices + size_vertex + size_normal));
+ //if(ifs.GetNFloatAttribute(0)) ifs.SetFloatAttribute(0, (float* const)(output_data + size_indices + size_vertex + size_normal));
// Decode data
if(decoder.DecodePlayload(ifs, bstream) != o3dgc::O3DGC_OK) throw DeadlyImportError("GLTF: can not decode Open3DGC data.");
-/*
+/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
#include <assimp/material.h>
#include <assimp/scene.h>
+// Header files, standart library.
#include <memory>
+#include <inttypes.h>
#include "glTFAssetWriter.h"
+// Header files, Open3DGC.
+#include <Open3DGC/o3dgcSC3DMCEncoder.h>
+
using namespace rapidjson;
using namespace Assimp;
void glTFExporter::ExportMeshes()
{
+// Not for
+// using IndicesType = decltype(aiFace::mNumIndices);
+// But yes for
+// using IndicesType = unsigned short;
+// because "ComponentType_UNSIGNED_SHORT" used for indices. And its maximal type according to glTF specification.
+using IndicesType = unsigned short;
+
+// Variables needed for compression. BEGIN.
+// Indices, not pointers - because pointer to buffer is changin while writing to it.
+size_t idx_srcdata_begin;// Index of buffer before writing mesh data. Also, index of begin of coordinates array in buffer.
+size_t idx_srcdata_normal = SIZE_MAX;// Index of begin of normals array in buffer. SIZE_MAX - mean that mesh has no normals.
+std::vector<size_t> idx_srcdata_tc;// Array of indices. Every index point to begin of texture coordinates array in buffer.
+size_t idx_srcdata_ind;// Index of begin of coordinates indices array in buffer.
+bool comp_allow;// Point that data of current mesh can be compressed.
+// Variables needed for compression. END.
+
for (unsigned int i = 0; i < mScene->mNumMeshes; ++i) {
const aiMesh* aim = mScene->mMeshes[i];
+ // Check if compressing requested and mesh can be encoded.
+ if((aim->mPrimitiveTypes == aiPrimitiveType_TRIANGLE) && (aim->mNumVertices > 0))///TODO: export properties if(compression is needed)
+ {
+ comp_allow = true;
+ idx_srcdata_tc.reserve(AI_MAX_NUMBER_OF_TEXTURECOORDS);
+ }
+ else
+ {
+ comp_allow = false;
+ }
+
std::string meshId = mAsset->FindUniqueID(aim->mName.C_Str(), "mesh");
Ref<Mesh> m = mAsset->meshes.Create(meshId);
m->primitives.resize(1);
b = mAsset->buffers.Create(bufferId);
}
+ /******************* Vertices ********************/
+ // If compression is used then you need parameters of uncompressed region: begin and size. At this step "begin" is stored.
+ if(comp_allow) idx_srcdata_begin = b->byteLength;
+
Ref<Accessor> v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
- if (v) p.attributes.position.push_back(v);
+ if (v) p.attributes.position.push_back(v);
- Ref<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
- if (n) p.attributes.normal.push_back(n);
+ /******************** Normals ********************/
+ if(comp_allow && (aim->mNormals > 0)) idx_srcdata_normal = b->byteLength;// Store index of normals array.
+ Ref<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
+ if (n) p.attributes.normal.push_back(n);
+
+ /************** Texture coordinates **************/
for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
if (aim->mNumUVComponents[i] > 0) {
AttribType::Value type = (aim->mNumUVComponents[i] == 2) ? AttribType::VEC2 : AttribType::VEC3;
- Ref<Accessor> tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, true);
- if (tc) p.attributes.texcoord.push_back(tc);
- }
- }
- if (aim->mNumFaces > 0) {
- unsigned int nIndicesPerFace = aim->mFaces[0].mNumIndices;
- std::vector<uint16_t> indices;
+ if(comp_allow) idx_srcdata_tc.push_back(b->byteLength);// Store index of texture coordinates array.
+
+ Ref<Accessor> tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, true);
+ if (tc) p.attributes.texcoord.push_back(tc);
+ }
+ }
+
+ /*************** Vertices indices ****************/
+ idx_srcdata_ind = b->byteLength;// Store index of indices array.
+
+ if (aim->mNumFaces > 0) {
+ std::vector<IndicesType> indices;
+ unsigned int nIndicesPerFace = aim->mFaces[0].mNumIndices;
indices.resize(aim->mNumFaces * nIndicesPerFace);
for (size_t i = 0; i < aim->mNumFaces; ++i) {
for (size_t j = 0; j < nIndicesPerFace; ++j) {
indices[i*nIndicesPerFace + j] = uint16_t(aim->mFaces[i].mIndices[j]);
}
}
- p.indices = ExportData(*mAsset, meshId, b, unsigned(indices.size()), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_SHORT, true);
- }
+
+ p.indices = ExportData(*mAsset, meshId, b, unsigned(indices.size()), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_SHORT, true);
+ }
switch (aim->mPrimitiveTypes) {
case aiPrimitiveType_POLYGON:
default: // aiPrimitiveType_TRIANGLE
p.mode = PrimitiveMode_TRIANGLES;
}
- }
+
+ /****************** Compression ******************/
+ ///TODO: animation: weights, joints.
+ if(comp_allow)
+ {
+ o3dgc::BinaryStream bs;
+ o3dgc::SC3DMCEncoder<IndicesType> encoder;
+ o3dgc::IndexedFaceSet<IndicesType> comp_o3dgc_ifs;
+ o3dgc::SC3DMCEncodeParams comp_o3dgc_params;
+ unsigned qcoord = 12;///TODO: dbg
+ unsigned qnormal = 10;///TODO: dbg
+ unsigned qtexCoord = 10;///TODO: dbg
+
+ o3dgc::O3DGCSC3DMCPredictionMode positionPrediction = o3dgc::O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION;///TODO: dbg
+ o3dgc::O3DGCSC3DMCPredictionMode normalPrediction = o3dgc::O3DGC_SC3DMC_SURF_NORMALS_PREDICTION;///TODO: dbg
+ o3dgc::O3DGCSC3DMCPredictionMode texcoordPrediction = o3dgc::O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION;///TODO: dbg
+
+ // IndexedFacesSet: "Crease angle", "solid", "convex" are set to default.
+ comp_o3dgc_ifs.SetCCW(true);
+ comp_o3dgc_ifs.SetIsTriangularMesh(true);
+ comp_o3dgc_ifs.SetNumFloatAttributes(0);
+ // Coordinates
+ comp_o3dgc_params.SetCoordQuantBits(qcoord);///TODO: IME
+ comp_o3dgc_params.SetCoordPredMode(positionPrediction);///TODO: IME
+ comp_o3dgc_ifs.SetNCoord(aim->mNumVertices);
+ comp_o3dgc_ifs.SetCoord((o3dgc::Real* const)&b->GetPointer()[idx_srcdata_begin]);
+ // Normals
+ if(idx_srcdata_normal != SIZE_MAX)
+ {
+ comp_o3dgc_params.SetNormalQuantBits(qnormal);///TODO: IME
+ comp_o3dgc_params.SetNormalPredMode(normalPrediction);///TODO: IME
+ comp_o3dgc_ifs.SetNNormal(aim->mNumVertices);
+ comp_o3dgc_ifs.SetNormal((o3dgc::Real* const)&b->GetPointer()[idx_srcdata_normal]);
+ }
+
+ // Texture coordinates
+ for(size_t num_tc = 0; num_tc < idx_srcdata_tc.size(); num_tc++)
+ {
+ size_t num = comp_o3dgc_ifs.GetNumFloatAttributes();
+
+ comp_o3dgc_params.SetFloatAttributeQuantBits(num, qtexCoord);///TODO: IME
+ comp_o3dgc_params.SetFloatAttributePredMode(num, texcoordPrediction);///TODO: IME
+ comp_o3dgc_ifs.SetNFloatAttribute(num, aim->mNumVertices);// number of elements.
+ comp_o3dgc_ifs.SetFloatAttributeDim(num, aim->mNumUVComponents[i]);// components per element: aiVector3D => x * float
+ comp_o3dgc_ifs.SetFloatAttributeType(num, o3dgc::O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_TEXCOORD);
+ comp_o3dgc_ifs.SetFloatAttribute(num, (o3dgc::Real* const)&b->GetPointer()[idx_srcdata_tc[num_tc]]);
+ comp_o3dgc_ifs.SetNumFloatAttributes(num + 1);
+ }
+
+ // Coordinates indices
+ comp_o3dgc_ifs.SetNCoordIndex(aim->mNumFaces);
+ comp_o3dgc_ifs.SetCoordIndex((IndicesType* const)&b->GetPointer()[idx_srcdata_ind]);
+ // Prepare to enconding
+ comp_o3dgc_params.SetNumFloatAttributes(comp_o3dgc_ifs.GetNumFloatAttributes());
+ comp_o3dgc_params.SetStreamType(o3dgc::O3DGC_STREAM_TYPE_BINARY);///TODO: exporter params
+ // Encoding
+ encoder.Encode(comp_o3dgc_params, comp_o3dgc_ifs, bs);
+
+ ///TODO: replace data in buffer
+ }// if(comp_allow)
+ }// for (unsigned int i = 0; i < mScene->mNumMeshes; ++i) {
+
+ ///TODO: export properties if(compression is used)
}
unsigned int glTFExporter::ExportNode(const aiNode* n)