Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ fmtlib Library - see the LICENSE file in the libs/libfmt folder (BSD 2-clause "S
OpenFBX library - see the LICENSE file in radiantcore/model/import/openfbx (MIT license)
pugixml library - see the LICENSE file in the libs/pugixml folder (MIT license)
SHA256 implementation - by Brad Conte, released into the public domain, see this URL:
(https://github.com/B-Con/crypto-algorithms/tree/master#readme)
(https://github.com/B-Con/crypto-algorithms/tree/master#readme)
cgltf library - see the LICENSE file in the radiantcore/model/import/gltf folder (MIT license)
4 changes: 4 additions & 0 deletions radiantcore/model/ModelFormatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

#include "module/StaticModule.h"

#include "import/GltfModelLoader.h"
#include "import/FbxModelLoader.h"
#include "export/AseExporter.h"
#include "export/Lwo2Exporter.h"
#include "export/WavefrontExporter.h"
#include "command/ExecutionFailure.h"



namespace model
{

Expand All @@ -40,6 +43,7 @@ void ModelFormatManager::initialiseModule(const IApplicationContext& ctx)

// Register the built-in model importers
registerImporter(std::make_shared<FbxModelLoader>());
registerImporter(std::make_shared<GltfModelLoader>());

// Register the built-in model exporters
registerExporter(std::make_shared<AseExporter>());
Expand Down
271 changes: 271 additions & 0 deletions radiantcore/model/import/GltfModelLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
#include "GltfModelLoader.h"

#include <istream>
//#include "AseModel.h"

#include "os/path.h"
#include "string/case_conv.h"
#include "stream/ScopedArchiveBuffer.h"

#include "../StaticModel.h"
#include "parser/ParseException.h"
#include "../picomodel/PicoModelLoader.h"

//https://github.com/jkuhlmann/cgltf
#define CGLTF_IMPLEMENTATION
#include "gltf/cgltf.h"
#include "../StaticModelSurface.h"

namespace
{
class ScopeGuard
{
std::function<void()> mExitFunc;
public:
ScopeGuard(std::function<void()> exitFunc)
:mExitFunc(exitFunc)
{

}

~ScopeGuard()
{
mExitFunc();
}
};
}

namespace model
{

GltfModelLoader::GltfModelLoader() :
ModelImporterBase("GLB")
{}

IModelPtr GltfModelLoader::loadModelFromPath(const std::string& path)
{
// Open an ArchiveFile to load
auto file = path_is_absolute(path.c_str()) ?
GlobalFileSystem().openFileInAbsolutePath(path) :
GlobalFileSystem().openFile(path);

std::string fileName = path_is_absolute(path.c_str()) ?
path : GlobalFileSystem().findFile(path) + path;

cgltf_options options = {};
cgltf_data* gltfData = NULL;
cgltf_result result = cgltf_parse_file(&options, fileName.c_str(), &gltfData);

if (result != cgltf_result_success)
{
//Failed to load
rError() << "Failed to parse GLB file " << path << std::endl;
return IModelPtr();
}

ScopeGuard freeCGLTF([gltfData] {
cgltf_free(gltfData);
});

result = cgltf_load_buffers(&options, gltfData, fileName.c_str());

if (result != cgltf_result_success)
{
//Failed to load
rError() << "Failed to parse GLTF buffers " << path << std::endl;
return IModelPtr();
}

//Note: "mesh" = how many individual blender objects there are.
//so if the model consists of 3 objects, then: you have 3 meshes.

if (gltfData->meshes_count == 0)
{
//No mesh
rError() << "GLB file has no meshes " << path << std::endl;
return IModelPtr();
}

// Maps to help with hierarchy
std::unordered_map<cgltf_mesh*, cgltf_node*> meshToNodeMap;

for (size_t i = 0; i < gltfData->nodes_count; i++)
{
cgltf_node* node = &gltfData->nodes[i];
if (node->mesh)
{
meshToNodeMap.emplace(node->mesh, node);
}
}

std::vector<StaticModelSurfacePtr> staticSurfaces;

for (size_t i = 0; i < gltfData->meshes_count; i++)
{
cgltf_mesh* mesh = &gltfData->meshes[i];
if (mesh->primitives_count <= 0)
{
continue;
}

// Handle transformation of the mesh in the node, including parents
cgltf_node* node = meshToNodeMap[mesh];
float mtx[16];
cgltf_node_transform_world(node, mtx);
auto transform = Matrix4::byColumns(mtx[0], mtx[1], mtx[2], mtx[3],
mtx[4], mtx[5], mtx[6], mtx[7],
mtx[8], mtx[9], mtx[10], mtx[11],
mtx[12], mtx[13], mtx[14], mtx[15]);

for (size_t j = 0; j < mesh->primitives_count; j++)
{
auto& primitive = mesh->primitives[j];
size_t numVerts = 0;
size_t numIndices = 0;
const uint8_t* positions = nullptr;
const uint8_t* normals = nullptr;
const uint8_t* texcoords = nullptr;

for (size_t k = 0; k < primitive.attributes_count; k++)
{
cgltf_attribute& attribute = primitive.attributes[k];
std::string attributeName = attribute.name;
if (attributeName == "POSITION")
{
cgltf_accessor* accessor = attribute.data;
if (accessor->component_type != cgltf_component_type_r_32f)
{
rError() << "GLB file has a position attribute that doesn't use floats " << path << std::endl;
return IModelPtr();
}
if (accessor->type != cgltf_type_vec3)
{
rError() << "GLB file has a position attribute that isn't a Vec3 " << path << std::endl;
return IModelPtr();
}

numVerts = accessor->count;
positions = reinterpret_cast<const uint8_t*>(cgltf_buffer_view_data(accessor->buffer_view));

}
else if (attributeName == "NORMAL")
{
cgltf_accessor* accessor = attribute.data;
if (accessor->component_type != cgltf_component_type_r_32f)
{
rError() << "GLB file has a normal attribute that doesn't use floats " << path << std::endl;
return IModelPtr();
}
if (accessor->type != cgltf_type_vec3)
{
rError() << "GLB file has a normal attribute that isn't a Vec3 " << path << std::endl;
return IModelPtr();
}

normals = reinterpret_cast<const uint8_t*>(cgltf_buffer_view_data(accessor->buffer_view));
}
else if (attributeName == "TEXCOORD_0")
{
cgltf_accessor* accessor = attribute.data;
if (accessor->component_type != cgltf_component_type_r_32f)
{
rError() << "GLB file has a texcoord_0 attribute that doesn't use floats " << path << std::endl;
return IModelPtr();
}
if (accessor->type != cgltf_type_vec2)
{
rError() << "GLB file has a texcoord_0 attribute that isn't a Vec2 " << path << std::endl;
return IModelPtr();
}

texcoords = reinterpret_cast<const uint8_t*>(cgltf_buffer_view_data(accessor->buffer_view));
}
}

if (primitive.indices)
{
const uint8_t* indicesPtr = reinterpret_cast<const uint8_t*>(cgltf_buffer_view_data(primitive.indices->buffer_view));
numIndices = primitive.indices->count;
cgltf_component_type indexType = primitive.indices->component_type;

std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
indices.resize(numIndices);

if (indexType == cgltf_component_type_r_8u)
{
for (size_t index = 0; index < numIndices; index++)
{
indices[index] = indicesPtr[index];
}
}
else if (indexType == cgltf_component_type_r_16u)
{
const uint16_t* ptrAs16 = reinterpret_cast<const uint16_t*>(indicesPtr);
for (size_t index = 0; index < numIndices; index++)
{
indices[index] = ptrAs16[index];
}
}
else if (indexType == cgltf_component_type_r_32u)
{
const uint32_t* ptrAs32 = reinterpret_cast<const uint32_t*>(indicesPtr);
for (size_t index = 0; index < numIndices; index++)
{
indices[index] = ptrAs32[index];
}
}
else
{
rError() << "GLB file doesn't indices of an unsigned type " << path << std::endl;
return IModelPtr();
}

// GLTF is CCW winding order, but dark radiant wants CW
size_t numTris = numIndices / 3;
for (size_t tri = 0; tri < numTris; tri++)
{
std::swap(indices[tri * 3], indices[tri * 3 + 1]);
}

vertices.reserve(numVerts);
const std::array<float, 3>* posVec3 = reinterpret_cast<const std::array<float, 3>*>(positions);
const std::array<float, 3>* normalsVec3 = reinterpret_cast<const std::array<float, 3>*>(normals);
const std::array<float, 2>* texcoordsVec2 = reinterpret_cast<const std::array<float, 2>*>(texcoords);
for (size_t vertex = 0; vertex < numVerts; vertex++)
{
Vector3 pos = posVec3 ? Vertex3(posVec3[vertex][0], posVec3[vertex][1], posVec3[vertex][2]) : Vertex3();
Vector3 normal = normalsVec3 ? Normal3(-normalsVec3[vertex][0], -normalsVec3[vertex][1], -normalsVec3[vertex][2]) : Normal3();

pos = transform.transformPoint(pos);
normal = transform.transformDirection(normal);
// NOTE: Normals are negated due to winding order swap from CCW to CW
vertices.emplace_back(pos, normal,
texcoordsVec2 ? TexCoord2f(texcoordsVec2[vertex][0], texcoordsVec2[vertex][1]) : TexCoord2f());
}

auto& staticSurface = staticSurfaces.emplace_back(std::make_shared<StaticModelSurface>(std::move(vertices), std::move(indices)));
if (primitive.material && primitive.material->name)
{
staticSurface->setDefaultMaterial(primitive.material->name);
staticSurface->setActiveMaterial(staticSurface->getActiveMaterial());
}
}
else
{
rError() << "GLB file doesn't seem to have indices " << path << std::endl;
return IModelPtr();
}
}
}

auto staticModel = std::make_shared<StaticModel>(staticSurfaces);

// Set the filename
staticModel->setFilename(os::getFilename(fileName));
staticModel->setModelPath(path);

return staticModel; //Return the model.
}

}
18 changes: 18 additions & 0 deletions radiantcore/model/import/GltfModelLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "ModelImporterBase.h"

namespace model
{

class GltfModelLoader :
public ModelImporterBase
{
public:
GltfModelLoader();

// Load the given model from the path, VFS or absolute
IModelPtr loadModelFromPath(const std::string& name) override;
};

} // namespace model
7 changes: 7 additions & 0 deletions radiantcore/model/import/gltf/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2018-2021 Johannes Kuhlmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading