2023-12-09 15:39:35 -05:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "mdlimport.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
|
|
|
|
#include "tiny_gltf.h"
|
|
|
|
|
|
|
|
void importModel(physis_MDL &existingModel, const QString &filename)
|
|
|
|
{
|
|
|
|
tinygltf::Model model;
|
|
|
|
|
|
|
|
std::string error, warning;
|
|
|
|
|
|
|
|
tinygltf::TinyGLTF loader;
|
|
|
|
if (!loader.LoadBinaryFromFile(&model, &error, &warning, filename.toStdString())) {
|
|
|
|
qInfo() << "Error when loading glTF model:" << error;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!warning.empty()) {
|
|
|
|
qInfo() << "Warnings when loading glTF model:" << warning;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &node : model.nodes) {
|
|
|
|
// Detect if it's a mesh node
|
|
|
|
if (node.mesh >= 0) {
|
|
|
|
qInfo() << "Importing" << node.name;
|
|
|
|
|
|
|
|
const QStringList parts = QString::fromStdString(node.name).split(QLatin1Char(' '));
|
|
|
|
const QString &name = parts[0];
|
|
|
|
const QStringList lodPartNumber = parts[2].split(QLatin1Char('.'));
|
|
|
|
|
2023-12-09 16:57:08 -05:00
|
|
|
// const int lodNumber = lodPartNumber[0].toInt();
|
|
|
|
const int lodNumber = 0;
|
|
|
|
const int partNumber = lodPartNumber[0].toInt();
|
2023-12-09 15:39:35 -05:00
|
|
|
|
|
|
|
qInfo() << "- LOD:" << lodNumber;
|
|
|
|
qInfo() << "- Part:" << partNumber;
|
|
|
|
|
|
|
|
auto &mesh = model.meshes[node.mesh];
|
|
|
|
auto &primitive = mesh.primitives[0];
|
|
|
|
|
|
|
|
// All of the accessors are mapped to the same buffer vertex view
|
|
|
|
const auto &vertexAccessor = model.accessors[primitive.attributes["POSITION"]];
|
|
|
|
const auto &vertexView = model.bufferViews[vertexAccessor.bufferView];
|
|
|
|
const auto &vertexBuffer = model.buffers[vertexView.buffer];
|
|
|
|
|
|
|
|
const auto &indexAccessor = model.accessors[primitive.indices];
|
|
|
|
const auto &indexView = model.bufferViews[indexAccessor.bufferView];
|
|
|
|
const auto &indexBuffer = model.buffers[indexView.buffer];
|
|
|
|
|
|
|
|
qInfo() << "- Importing mesh of" << vertexAccessor.count << "vertices and" << indexAccessor.count << "indices.";
|
|
|
|
|
|
|
|
std::vector<Vertex> newVertices;
|
|
|
|
for (int i = 0; i < vertexAccessor.count; i++) {
|
2023-12-09 16:57:08 -05:00
|
|
|
auto vertexData = (glm::vec3 *)(vertexBuffer.data.data() + (vertexView.byteStride * i) + vertexView.byteOffset + vertexAccessor.byteOffset);
|
|
|
|
|
2023-12-09 15:39:35 -05:00
|
|
|
// Replace position data
|
|
|
|
auto vertex = existingModel.lods[lodNumber].parts[partNumber].vertices[i];
|
2023-12-09 16:57:08 -05:00
|
|
|
vertex.position[0] = vertexData->x;
|
|
|
|
vertex.position[1] = vertexData->y;
|
|
|
|
vertex.position[2] = vertexData->z;
|
2023-12-09 15:39:35 -05:00
|
|
|
|
|
|
|
newVertices.push_back(vertex);
|
|
|
|
}
|
|
|
|
|
2023-12-09 16:57:08 -05:00
|
|
|
auto indexData = (const uint16_t *)(&indexBuffer.data.at(0) + indexView.byteOffset + indexAccessor.byteOffset);
|
2023-12-09 15:39:35 -05:00
|
|
|
|
|
|
|
physis_mdl_replace_vertices(&existingModel, lodNumber, partNumber, vertexAccessor.count, newVertices.data(), indexAccessor.count, indexData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qInfo() << "Successfully imported model!";
|
|
|
|
}
|