2023-08-06 08:48:11 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-04-09 15:28:00 -04:00
|
|
|
#include "mdlpart.h"
|
|
|
|
#include "glm/gtx/transform.hpp"
|
|
|
|
|
2023-07-07 16:01:39 -04:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QResizeEvent>
|
|
|
|
#include <QVBoxLayout>
|
2023-04-09 15:28:00 -04:00
|
|
|
#include <QVulkanInstance>
|
|
|
|
#include <QVulkanWindow>
|
2023-07-07 16:01:39 -04:00
|
|
|
#include <QWindow>
|
2023-09-26 20:42:45 -04:00
|
|
|
#include <cmath>
|
2023-04-09 15:28:00 -04:00
|
|
|
#include <glm/gtc/quaternion.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.inl>
|
|
|
|
|
2023-07-09 10:54:27 -04:00
|
|
|
#include "filecache.h"
|
2023-09-26 17:09:12 -04:00
|
|
|
#include "imgui.h"
|
2023-09-23 14:08:41 -04:00
|
|
|
#include "tiny_gltf.h"
|
2023-07-09 10:54:27 -04:00
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
class VulkanWindow : public QWindow
|
|
|
|
{
|
2023-04-09 15:28:00 -04:00
|
|
|
public:
|
2023-10-12 23:45:34 -04:00
|
|
|
VulkanWindow(MDLPart *part, Renderer *renderer, QVulkanInstance *instance)
|
2023-12-09 15:01:20 -05:00
|
|
|
: m_renderer(renderer)
|
2023-10-12 23:45:34 -04:00
|
|
|
, m_instance(instance)
|
2023-12-09 15:01:20 -05:00
|
|
|
, part(part)
|
2023-10-12 23:45:34 -04:00
|
|
|
{
|
2023-07-09 10:54:27 -04:00
|
|
|
setSurfaceType(VulkanSurface);
|
|
|
|
setVulkanInstance(instance);
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void exposeEvent(QExposeEvent *)
|
|
|
|
{
|
2023-07-09 10:54:27 -04:00
|
|
|
if (isExposed()) {
|
|
|
|
if (!m_initialized) {
|
|
|
|
m_initialized = true;
|
|
|
|
|
|
|
|
auto surface = m_instance->surfaceForWindow(this);
|
2023-12-09 15:01:00 -05:00
|
|
|
if (!m_renderer->initSwapchain(surface, width() * screen()->devicePixelRatio(), height() * screen()->devicePixelRatio()))
|
2023-07-09 10:54:27 -04:00
|
|
|
m_initialized = false;
|
|
|
|
else
|
|
|
|
render();
|
2023-07-06 17:36:22 -04:00
|
|
|
}
|
2023-07-09 10:54:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
bool event(QEvent *e)
|
|
|
|
{
|
2023-07-09 10:54:27 -04:00
|
|
|
switch (e->type()) {
|
2023-10-12 23:45:34 -04:00
|
|
|
case QEvent::UpdateRequest:
|
|
|
|
render();
|
|
|
|
break;
|
|
|
|
case QEvent::Resize: {
|
|
|
|
QResizeEvent *resizeEvent = (QResizeEvent *)e;
|
|
|
|
auto surface = m_instance->surfaceForWindow(this);
|
2023-12-09 15:01:00 -05:00
|
|
|
m_renderer->resize(surface,
|
|
|
|
resizeEvent->size().width() * screen()->devicePixelRatio(),
|
|
|
|
resizeEvent->size().height() * screen()->devicePixelRatio());
|
2023-10-12 23:45:34 -04:00
|
|
|
} break;
|
|
|
|
case QEvent::MouseButtonPress: {
|
|
|
|
auto mouseEvent = dynamic_cast<QMouseEvent *>(e);
|
|
|
|
|
|
|
|
if (mouseEvent->button() == Qt::MouseButton::LeftButton || mouseEvent->button() == Qt::MouseButton::RightButton) {
|
|
|
|
part->lastX = mouseEvent->position().x();
|
|
|
|
part->lastY = mouseEvent->position().y();
|
|
|
|
part->cameraMode = mouseEvent->button() == Qt::MouseButton::LeftButton ? MDLPart::CameraMode::Orbit : MDLPart::CameraMode::Move;
|
|
|
|
|
|
|
|
setKeyboardGrabEnabled(true);
|
|
|
|
setCursor(Qt::BlankCursor);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case QEvent::MouseButtonRelease: {
|
|
|
|
part->cameraMode = MDLPart::CameraMode::None;
|
|
|
|
|
|
|
|
setKeyboardGrabEnabled(false);
|
|
|
|
setCursor({});
|
|
|
|
} break;
|
|
|
|
case QEvent::MouseMove: {
|
|
|
|
auto mouseEvent = dynamic_cast<QMouseEvent *>(e);
|
|
|
|
if (part->cameraMode != MDLPart::CameraMode::None) {
|
|
|
|
const int deltaX = mouseEvent->position().x() - part->lastX;
|
|
|
|
const int deltaY = mouseEvent->position().y() - part->lastY;
|
|
|
|
|
|
|
|
if (part->cameraMode == MDLPart::CameraMode::Orbit) {
|
|
|
|
part->yaw += deltaX * 0.01f; // TODO: remove these magic numbers
|
|
|
|
part->pitch += deltaY * 0.01f;
|
|
|
|
} else {
|
|
|
|
const glm::vec3 position(part->cameraDistance * std::sin(part->yaw),
|
|
|
|
part->cameraDistance * part->pitch,
|
|
|
|
part->cameraDistance * std::cos(part->yaw));
|
|
|
|
|
|
|
|
const glm::quat rot = glm::quatLookAt((part->position + position) - part->position, {0, 1, 0});
|
|
|
|
|
|
|
|
part->position += glm::vec3{0, 1, 0} * (float)deltaY * 0.01f;
|
|
|
|
part->position.y = std::clamp(part->position.y, 0.0f, 10.0f);
|
2023-07-09 10:54:27 -04:00
|
|
|
}
|
2023-07-06 17:36:22 -04:00
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
part->lastX = mouseEvent->position().x();
|
|
|
|
part->lastY = mouseEvent->position().y();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case QEvent::Wheel: {
|
|
|
|
auto scrollEvent = dynamic_cast<QWheelEvent *>(e);
|
2023-07-06 17:36:22 -04:00
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
part->cameraDistance -= (scrollEvent->angleDelta().y() / 120.0f) * 0.1f; // FIXME: why 120?
|
|
|
|
part->cameraDistance = std::clamp(part->cameraDistance, 1.0f, 4.0f);
|
|
|
|
} break;
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return QWindow::event(e);
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void render()
|
|
|
|
{
|
2023-09-26 17:09:12 -04:00
|
|
|
ImGui::SetCurrentContext(m_renderer->ctx);
|
|
|
|
|
|
|
|
auto &io = ImGui::GetIO();
|
|
|
|
io.DisplaySize = ImVec2(width(), height());
|
|
|
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
2023-09-25 23:48:03 -04:00
|
|
|
if (part->requestUpdate)
|
2023-10-12 23:45:34 -04:00
|
|
|
part->requestUpdate();
|
2023-09-25 23:48:03 -04:00
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
ImGui::Render();
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
glm::vec3 position(part->cameraDistance * sin(part->yaw), part->cameraDistance * part->pitch, part->cameraDistance * cos(part->yaw));
|
2023-07-06 17:36:22 -04:00
|
|
|
|
|
|
|
m_renderer->view = glm::lookAt(part->position + position, part->position, glm::vec3(0, -1, 0));
|
|
|
|
|
2023-04-09 15:28:00 -04:00
|
|
|
m_renderer->render(models);
|
|
|
|
m_instance->presentQueued(this);
|
|
|
|
requestUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RenderModel> models;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_initialized = false;
|
2023-10-12 23:45:34 -04:00
|
|
|
Renderer *m_renderer;
|
|
|
|
QVulkanInstance *m_instance;
|
|
|
|
MDLPart *part;
|
2023-04-09 15:28:00 -04:00
|
|
|
};
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
MDLPart::MDLPart(GameData *data, FileCache &cache)
|
|
|
|
: data(data)
|
|
|
|
, cache(cache)
|
|
|
|
{
|
2023-04-09 15:28:00 -04:00
|
|
|
auto viewportLayout = new QVBoxLayout();
|
2023-07-09 11:04:30 -04:00
|
|
|
viewportLayout->setContentsMargins(0, 0, 0, 0);
|
2023-04-09 15:28:00 -04:00
|
|
|
setLayout(viewportLayout);
|
|
|
|
|
2023-10-13 15:03:17 -04:00
|
|
|
pbd = physis_parse_pbd(physis_gamedata_extract_file(data, "chara/xls/bonedeformer/human.pbd"));
|
|
|
|
|
2023-04-09 15:28:00 -04:00
|
|
|
renderer = new Renderer();
|
|
|
|
|
|
|
|
auto inst = new QVulkanInstance();
|
|
|
|
inst->setVkInstance(renderer->instance);
|
|
|
|
inst->setFlags(QVulkanInstance::Flag::NoDebugOutputRedirect);
|
|
|
|
inst->create();
|
|
|
|
|
2023-07-06 17:36:22 -04:00
|
|
|
vkWindow = new VulkanWindow(this, renderer, inst);
|
2023-04-09 15:28:00 -04:00
|
|
|
vkWindow->setVulkanInstance(inst);
|
|
|
|
|
|
|
|
auto widget = QWidget::createWindowContainer(vkWindow);
|
|
|
|
|
|
|
|
viewportLayout->addWidget(widget);
|
|
|
|
|
|
|
|
connect(this, &MDLPart::modelChanged, this, &MDLPart::reloadRenderer);
|
|
|
|
connect(this, &MDLPart::skeletonChanged, this, &MDLPart::reloadBoneData);
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void MDLPart::exportModel(const QString &fileName)
|
|
|
|
{
|
2023-09-23 14:08:41 -04:00
|
|
|
const int selectedModel = 0;
|
|
|
|
const int selectedLod = 0;
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
const physis_MDL &model = models[selectedModel].model;
|
|
|
|
const physis_LOD &lod = model.lods[selectedLod];
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-23 14:08:41 -04:00
|
|
|
tinygltf::Model gltfModel;
|
|
|
|
gltfModel.asset.generator = "Novus";
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-12-09 14:49:31 -05:00
|
|
|
// TODO: just write the code better! dummy!!
|
|
|
|
gltfModel.nodes.reserve(1 + model.num_affected_bones + lod.num_parts);
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &gltfSkeletonNode = gltfModel.nodes.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
gltfSkeletonNode.name = skeleton->root_bone->name;
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-23 14:08:41 -04:00
|
|
|
for (int i = 0; i < model.num_affected_bones; i++) {
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &node = gltfModel.nodes.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
node.name = model.affected_bone_names[i];
|
2023-04-09 15:28:00 -04:00
|
|
|
|
|
|
|
int real_bone_id = 0;
|
2023-07-09 10:54:27 -04:00
|
|
|
for (int k = 0; k < skeleton->num_bones; k++) {
|
2023-09-23 14:08:41 -04:00
|
|
|
if (strcmp(skeleton->bones[k].name, model.affected_bone_names[i]) == 0) {
|
2023-07-09 10:54:27 -04:00
|
|
|
real_bone_id = k;
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &real_bone = skeleton->bones[real_bone_id];
|
2023-09-23 14:08:41 -04:00
|
|
|
node.translation = {real_bone.position[0], real_bone.position[1], real_bone.position[2]};
|
|
|
|
node.rotation = {real_bone.rotation[0], real_bone.rotation[1], real_bone.rotation[2], real_bone.rotation[3]};
|
|
|
|
node.scale = {real_bone.scale[0], real_bone.scale[1], real_bone.scale[2]};
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup parenting
|
2023-09-23 14:08:41 -04:00
|
|
|
for (int i = 0; i < model.num_affected_bones; i++) {
|
2023-04-09 15:28:00 -04:00
|
|
|
int real_bone_id = 0;
|
2023-07-09 10:54:27 -04:00
|
|
|
for (int k = 0; k < skeleton->num_bones; k++) {
|
2023-09-23 14:08:41 -04:00
|
|
|
if (strcmp(skeleton->bones[k].name, model.affected_bone_names[i]) == 0) {
|
2023-07-09 10:54:27 -04:00
|
|
|
real_bone_id = k;
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &real_bone = skeleton->bones[real_bone_id];
|
2023-07-09 10:54:27 -04:00
|
|
|
if (real_bone.parent_bone != nullptr) {
|
2023-12-09 14:49:31 -05:00
|
|
|
bool found = false;
|
2023-09-23 14:08:41 -04:00
|
|
|
for (int k = 0; k < model.num_affected_bones; k++) {
|
|
|
|
if (strcmp(model.affected_bone_names[k], real_bone.parent_bone->name) == 0) {
|
|
|
|
gltfModel.nodes[k + 1].children.push_back(i + 1); // +1 for the skeleton node taking up the first index
|
2023-12-09 14:49:31 -05:00
|
|
|
found = true;
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
2023-12-09 14:49:31 -05:00
|
|
|
|
|
|
|
// Find the next closest bone that isn't a direct descendant
|
|
|
|
// of n_root, but won't have a parent anyway
|
|
|
|
if (!found) {
|
|
|
|
gltfSkeletonNode.children.push_back(i + 1);
|
|
|
|
}
|
2023-09-23 14:08:41 -04:00
|
|
|
} else {
|
|
|
|
gltfSkeletonNode.children.push_back(i + 1);
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &gltfSkin = gltfModel.skins.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
gltfSkin.name = gltfSkeletonNode.name;
|
2023-10-10 18:32:30 -04:00
|
|
|
gltfSkin.skeleton = 0;
|
2023-09-23 14:08:41 -04:00
|
|
|
for (int i = 1; i < gltfModel.nodes.size(); i++) {
|
|
|
|
gltfSkin.joints.push_back(i);
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
|
2023-09-23 14:08:41 -04:00
|
|
|
// Inverse bind matrices
|
|
|
|
{
|
|
|
|
gltfSkin.inverseBindMatrices = gltfModel.accessors.size();
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &inverseAccessor = gltfModel.accessors.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
inverseAccessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
inverseAccessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT;
|
|
|
|
inverseAccessor.count = model.num_affected_bones;
|
|
|
|
inverseAccessor.type = TINYGLTF_TYPE_MAT4;
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &inverseBufferView = gltfModel.bufferViews.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
inverseBufferView.buffer = gltfModel.buffers.size();
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &inverseBuffer = gltfModel.buffers.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
for (int i = 0; i < model.num_affected_bones; i++) {
|
|
|
|
int real_bone_id = 0;
|
|
|
|
for (int k = 0; k < skeleton->num_bones; k++) {
|
|
|
|
if (strcmp(skeleton->bones[k].name, model.affected_bone_names[i]) == 0) {
|
|
|
|
real_bone_id = k;
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &real_bone = skeleton->bones[real_bone_id];
|
2023-09-23 14:08:41 -04:00
|
|
|
auto inverseMatrix = boneData[real_bone.index].inversePose;
|
2023-10-12 23:45:34 -04:00
|
|
|
auto inverseMatrixCPtr = reinterpret_cast<uint8_t *>(glm::value_ptr(inverseMatrix));
|
2023-09-23 14:08:41 -04:00
|
|
|
|
|
|
|
inverseBuffer.data.insert(inverseBuffer.data.end(), inverseMatrixCPtr, inverseMatrixCPtr + sizeof(float) * 16);
|
|
|
|
}
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-23 14:08:41 -04:00
|
|
|
inverseBufferView.byteLength = inverseBuffer.data.size();
|
|
|
|
}
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-23 14:08:41 -04:00
|
|
|
for (int i = 0; i < lod.num_parts; i++) {
|
2023-12-09 14:49:31 -05:00
|
|
|
gltfSkeletonNode.children.push_back(gltfModel.nodes.size());
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &gltfNode = gltfModel.nodes.emplace_back();
|
2023-12-09 14:49:31 -05:00
|
|
|
|
2023-09-26 21:21:04 -04:00
|
|
|
gltfNode.name = models[0].name.toStdString() + " Part " + std::to_string(i) + ".0";
|
2023-09-23 14:08:41 -04:00
|
|
|
gltfNode.skin = 0;
|
|
|
|
|
|
|
|
gltfNode.mesh = gltfModel.meshes.size();
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &gltfMesh = gltfModel.meshes.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
|
2023-09-26 21:21:04 -04:00
|
|
|
gltfMesh.name = gltfNode.name + " Mesh Attribute";
|
2023-09-23 14:08:41 -04:00
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &gltfPrimitive = gltfMesh.primitives.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
gltfPrimitive.attributes["POSITION"] = gltfModel.accessors.size();
|
|
|
|
gltfPrimitive.attributes["TEXCOORD_0"] = gltfModel.accessors.size() + 1;
|
2023-12-09 14:49:31 -05:00
|
|
|
gltfPrimitive.attributes["TEXCOORD_1"] = gltfModel.accessors.size() + 2;
|
|
|
|
gltfPrimitive.attributes["NORMAL"] = gltfModel.accessors.size() + 3;
|
|
|
|
gltfPrimitive.attributes["COLOR_0"] = gltfModel.accessors.size() + 6;
|
|
|
|
gltfPrimitive.attributes["WEIGHTS_0"] = gltfModel.accessors.size() + 7;
|
|
|
|
gltfPrimitive.attributes["JOINTS_0"] = gltfModel.accessors.size() + 8;
|
2023-09-23 14:08:41 -04:00
|
|
|
gltfPrimitive.mode = TINYGLTF_MODE_TRIANGLES;
|
|
|
|
|
|
|
|
// Vertices
|
|
|
|
{
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &positionAccessor = gltfModel.accessors.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
positionAccessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
positionAccessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT;
|
|
|
|
positionAccessor.count = lod.parts[i].num_vertices;
|
|
|
|
positionAccessor.type = TINYGLTF_TYPE_VEC3;
|
|
|
|
|
2023-12-09 14:49:31 -05:00
|
|
|
auto &uv0Accessor = gltfModel.accessors.emplace_back();
|
|
|
|
uv0Accessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
uv0Accessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT;
|
|
|
|
uv0Accessor.count = lod.parts[i].num_vertices;
|
|
|
|
uv0Accessor.type = TINYGLTF_TYPE_VEC2;
|
|
|
|
uv0Accessor.byteOffset = offsetof(Vertex, uv0);
|
|
|
|
|
|
|
|
auto &uv1Accessor = gltfModel.accessors.emplace_back();
|
|
|
|
uv1Accessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
uv1Accessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT;
|
|
|
|
uv1Accessor.count = lod.parts[i].num_vertices;
|
|
|
|
uv1Accessor.type = TINYGLTF_TYPE_VEC2;
|
|
|
|
uv1Accessor.byteOffset = offsetof(Vertex, uv1);
|
2023-09-23 14:08:41 -04:00
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &normalAccessor = gltfModel.accessors.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
normalAccessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
normalAccessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT;
|
|
|
|
normalAccessor.count = lod.parts[i].num_vertices;
|
|
|
|
normalAccessor.type = TINYGLTF_TYPE_VEC3;
|
|
|
|
normalAccessor.byteOffset = offsetof(Vertex, normal);
|
|
|
|
|
2023-12-09 14:49:31 -05:00
|
|
|
auto &tangent1Accessor = gltfModel.accessors.emplace_back();
|
|
|
|
tangent1Accessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
tangent1Accessor.componentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
|
|
|
|
tangent1Accessor.count = lod.parts[i].num_vertices;
|
|
|
|
tangent1Accessor.type = TINYGLTF_TYPE_VEC4;
|
|
|
|
tangent1Accessor.byteOffset = offsetof(Vertex, tangent1);
|
|
|
|
|
|
|
|
auto &tangent2Accessor = gltfModel.accessors.emplace_back();
|
|
|
|
tangent2Accessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
tangent2Accessor.componentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
|
|
|
|
tangent2Accessor.count = lod.parts[i].num_vertices;
|
|
|
|
tangent2Accessor.type = TINYGLTF_TYPE_VEC4;
|
|
|
|
tangent2Accessor.byteOffset = offsetof(Vertex, tangent2);
|
|
|
|
|
|
|
|
auto &colorAccessor = gltfModel.accessors.emplace_back();
|
|
|
|
colorAccessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
colorAccessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT;
|
|
|
|
colorAccessor.count = lod.parts[i].num_vertices;
|
|
|
|
colorAccessor.type = TINYGLTF_TYPE_VEC4;
|
|
|
|
colorAccessor.byteOffset = offsetof(Vertex, color);
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &boneWeightAccessor = gltfModel.accessors.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
boneWeightAccessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
boneWeightAccessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT;
|
|
|
|
boneWeightAccessor.count = lod.parts[i].num_vertices;
|
|
|
|
boneWeightAccessor.type = TINYGLTF_TYPE_VEC4;
|
|
|
|
boneWeightAccessor.byteOffset = offsetof(Vertex, bone_weight);
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &boneIdAccessor = gltfModel.accessors.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
boneIdAccessor.bufferView = gltfModel.bufferViews.size();
|
2023-12-09 14:49:31 -05:00
|
|
|
boneIdAccessor.componentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
|
2023-09-23 14:08:41 -04:00
|
|
|
boneIdAccessor.count = lod.parts[i].num_vertices;
|
|
|
|
boneIdAccessor.type = TINYGLTF_TYPE_VEC4;
|
|
|
|
boneIdAccessor.byteOffset = offsetof(Vertex, bone_id);
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &vertexBufferView = gltfModel.bufferViews.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
vertexBufferView.buffer = gltfModel.buffers.size();
|
|
|
|
vertexBufferView.target = TINYGLTF_TARGET_ARRAY_BUFFER;
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &vertexBuffer = gltfModel.buffers.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
vertexBuffer.data.resize(lod.parts[i].num_vertices * sizeof(Vertex));
|
|
|
|
memcpy(vertexBuffer.data.data(), lod.parts[i].vertices, vertexBuffer.data.size());
|
|
|
|
|
|
|
|
vertexBufferView.byteLength = vertexBuffer.data.size();
|
|
|
|
vertexBufferView.byteStride = sizeof(Vertex);
|
|
|
|
}
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-23 14:08:41 -04:00
|
|
|
// Indices
|
|
|
|
{
|
|
|
|
gltfPrimitive.indices = gltfModel.accessors.size();
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &indexAccessor = gltfModel.accessors.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
indexAccessor.bufferView = gltfModel.bufferViews.size();
|
|
|
|
indexAccessor.componentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT;
|
|
|
|
indexAccessor.count = lod.parts[i].num_indices;
|
|
|
|
indexAccessor.type = TINYGLTF_TYPE_SCALAR;
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &indexBufferView = gltfModel.bufferViews.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
indexBufferView.buffer = gltfModel.buffers.size();
|
|
|
|
indexBufferView.target = TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER;
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
auto &indexBuffer = gltfModel.buffers.emplace_back();
|
2023-09-23 14:08:41 -04:00
|
|
|
indexBuffer.data.resize(lod.parts[i].num_indices * sizeof(uint16_t));
|
|
|
|
memcpy(indexBuffer.data.data(), lod.parts[i].indices, indexBuffer.data.size());
|
|
|
|
|
|
|
|
indexBufferView.byteLength = indexBuffer.data.size();
|
|
|
|
indexBufferView.byteStride = sizeof(uint16_t);
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 18:32:30 -04:00
|
|
|
auto &scene = gltfModel.scenes.emplace_back();
|
|
|
|
scene.name = models[0].name.toStdString();
|
|
|
|
scene.nodes = {0};
|
|
|
|
|
2023-09-23 14:08:41 -04:00
|
|
|
tinygltf::TinyGLTF loader;
|
|
|
|
loader.WriteGltfSceneToFile(&gltfModel, fileName.toStdString(), true, true, false, true);
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
|
2023-12-09 14:49:31 -05:00
|
|
|
RenderModel &MDLPart::getModel(const int index)
|
|
|
|
{
|
|
|
|
return models[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void MDLPart::reloadModel(const int index)
|
|
|
|
{
|
|
|
|
renderer->reloadModel(models[index], 0);
|
|
|
|
|
|
|
|
Q_EMIT modelChanged();
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void MDLPart::clear()
|
|
|
|
{
|
2023-04-09 15:28:00 -04:00
|
|
|
models.clear();
|
|
|
|
|
|
|
|
Q_EMIT modelChanged();
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:03:17 -04:00
|
|
|
void MDLPart::addModel(physis_MDL mdl, const QString &name, std::vector<physis_Material> materials, int lod, uint16_t fromBodyId, uint16_t toBodyId)
|
2023-09-26 21:21:04 -04:00
|
|
|
{
|
2023-04-09 15:28:00 -04:00
|
|
|
qDebug() << "Adding model to MDLPart";
|
|
|
|
|
|
|
|
auto model = renderer->addModel(mdl, lod);
|
2023-09-26 21:21:04 -04:00
|
|
|
model.name = name;
|
2023-10-13 15:03:17 -04:00
|
|
|
model.from_body_id = fromBodyId;
|
|
|
|
model.to_body_id = toBodyId;
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
std::transform(materials.begin(), materials.end(), std::back_inserter(model.materials), [this](const physis_Material &mat) {
|
|
|
|
return createMaterial(mat);
|
|
|
|
});
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-07-09 11:52:59 -04:00
|
|
|
if (materials.empty()) {
|
|
|
|
model.materials.push_back(createMaterial(physis_Material{}));
|
|
|
|
}
|
|
|
|
|
2023-04-09 15:28:00 -04:00
|
|
|
models.push_back(model);
|
|
|
|
|
|
|
|
Q_EMIT modelChanged();
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void MDLPart::setSkeleton(physis_Skeleton newSkeleton)
|
|
|
|
{
|
2023-07-06 17:36:22 -04:00
|
|
|
skeleton = std::make_unique<physis_Skeleton>(newSkeleton);
|
|
|
|
|
|
|
|
firstTimeSkeletonDataCalculated = false;
|
2023-04-09 15:28:00 -04:00
|
|
|
|
|
|
|
Q_EMIT skeletonChanged();
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void MDLPart::clearSkeleton()
|
|
|
|
{
|
2023-04-09 15:28:00 -04:00
|
|
|
skeleton.reset();
|
|
|
|
|
2023-07-06 17:36:22 -04:00
|
|
|
firstTimeSkeletonDataCalculated = false;
|
|
|
|
|
2023-04-09 15:28:00 -04:00
|
|
|
Q_EMIT skeletonChanged();
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void MDLPart::reloadRenderer()
|
|
|
|
{
|
2023-04-09 15:28:00 -04:00
|
|
|
reloadBoneData();
|
|
|
|
|
|
|
|
vkWindow->models = models;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void MDLPart::reloadBoneData()
|
|
|
|
{
|
2023-07-09 10:54:27 -04:00
|
|
|
if (skeleton) {
|
2023-07-06 17:36:22 -04:00
|
|
|
if (!firstTimeSkeletonDataCalculated) {
|
2023-07-07 16:01:39 -04:00
|
|
|
if (boneData.empty()) {
|
|
|
|
boneData.resize(skeleton->num_bones);
|
|
|
|
}
|
|
|
|
|
2023-07-06 17:36:22 -04:00
|
|
|
calculateBoneInversePose(*skeleton, *skeleton->root_bone, nullptr);
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
for (auto &bone : boneData) {
|
2023-07-06 17:36:22 -04:00
|
|
|
bone.inversePose = glm::inverse(bone.inversePose);
|
|
|
|
}
|
|
|
|
firstTimeSkeletonDataCalculated = true;
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update data
|
|
|
|
calculateBone(*skeleton, *skeleton->root_bone, nullptr);
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
for (auto &model : models) {
|
2023-04-09 15:28:00 -04:00
|
|
|
// we want to map the actual affected bones to bone ids
|
|
|
|
std::map<int, int> boneMapping;
|
|
|
|
for (int i = 0; i < model.model.num_affected_bones; i++) {
|
|
|
|
for (int k = 0; k < skeleton->num_bones; k++) {
|
2023-10-12 23:45:34 -04:00
|
|
|
if (std::string_view{skeleton->bones[k].name} == std::string_view{model.model.affected_bone_names[i]}) {
|
2023-04-09 15:28:00 -04:00
|
|
|
boneMapping[i] = k;
|
2023-07-06 17:36:22 -04:00
|
|
|
}
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:03:17 -04:00
|
|
|
std::vector<glm::mat4> deformBones(model.model.num_affected_bones);
|
|
|
|
for (int i = 0; i < model.model.num_affected_bones; i++) {
|
|
|
|
deformBones[i] = glm::mat4(1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get deform matrices
|
|
|
|
auto deform = physis_pbd_get_deform_matrix(pbd, model.from_body_id, model.to_body_id);
|
|
|
|
if (deform.num_bones != 0) {
|
|
|
|
for (int i = 0; i < deform.num_bones; i++) {
|
|
|
|
auto deformBone = deform.bones[i];
|
|
|
|
|
|
|
|
for (int k = 0; k < model.model.num_affected_bones; k++) {
|
|
|
|
if (std::string_view{model.model.affected_bone_names[k]} == std::string_view{deformBone.name}) {
|
|
|
|
deformBones[k] = glm::mat4{deformBone.deform[0],
|
|
|
|
deformBone.deform[1],
|
|
|
|
deformBone.deform[2],
|
|
|
|
deformBone.deform[3],
|
|
|
|
deformBone.deform[4],
|
|
|
|
deformBone.deform[5],
|
|
|
|
deformBone.deform[6],
|
|
|
|
deformBone.deform[7],
|
|
|
|
deformBone.deform[8],
|
|
|
|
deformBone.deform[9],
|
|
|
|
deformBone.deform[10],
|
|
|
|
deformBone.deform[11],
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
1.0f};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 15:28:00 -04:00
|
|
|
for (int i = 0; i < model.model.num_affected_bones; i++) {
|
2023-10-13 15:03:17 -04:00
|
|
|
const int originalBoneId = boneMapping[i];
|
|
|
|
model.boneData[i] = boneData[originalBoneId].localTransform * deformBones[i] * boneData[originalBoneId].inversePose;
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
RenderMaterial MDLPart::createMaterial(const physis_Material &material)
|
|
|
|
{
|
2023-04-09 15:28:00 -04:00
|
|
|
RenderMaterial newMaterial;
|
|
|
|
|
|
|
|
for (int i = 0; i < material.num_textures; i++) {
|
|
|
|
std::string t = material.textures[i];
|
|
|
|
|
|
|
|
if (t.find("skin") != std::string::npos) {
|
|
|
|
newMaterial.type = MaterialType::Skin;
|
|
|
|
}
|
|
|
|
|
|
|
|
char type = t[t.length() - 5];
|
|
|
|
|
2023-07-09 10:54:27 -04:00
|
|
|
switch (type) {
|
2023-10-12 23:45:34 -04:00
|
|
|
case 'm': {
|
2023-09-26 00:37:55 -04:00
|
|
|
auto texture = physis_texture_parse(cache.lookupFile(QLatin1String(material.textures[i])));
|
|
|
|
auto tex = renderer->addTexture(texture.width, texture.height, texture.rgba, texture.rgba_size);
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-26 00:37:55 -04:00
|
|
|
newMaterial.multiTexture = new RenderTexture(tex);
|
2023-10-12 23:45:34 -04:00
|
|
|
}
|
|
|
|
case 'd': {
|
2023-09-26 00:37:55 -04:00
|
|
|
auto texture = physis_texture_parse(cache.lookupFile(QLatin1String(material.textures[i])));
|
|
|
|
auto tex = renderer->addTexture(texture.width, texture.height, texture.rgba, texture.rgba_size);
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-26 00:37:55 -04:00
|
|
|
newMaterial.diffuseTexture = new RenderTexture(tex);
|
2023-10-12 23:45:34 -04:00
|
|
|
} break;
|
|
|
|
case 'n': {
|
2023-09-26 00:37:55 -04:00
|
|
|
auto texture = physis_texture_parse(cache.lookupFile(QLatin1String(material.textures[i])));
|
|
|
|
auto tex = renderer->addTexture(texture.width, texture.height, texture.rgba, texture.rgba_size);
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-26 00:37:55 -04:00
|
|
|
newMaterial.normalTexture = new RenderTexture(tex);
|
2023-10-12 23:45:34 -04:00
|
|
|
} break;
|
|
|
|
case 's': {
|
2023-09-26 00:37:55 -04:00
|
|
|
auto texture = physis_texture_parse(cache.lookupFile(QLatin1String(material.textures[i])));
|
|
|
|
auto tex = renderer->addTexture(texture.width, texture.height, texture.rgba, texture.rgba_size);
|
2023-04-09 15:28:00 -04:00
|
|
|
|
2023-09-26 00:37:55 -04:00
|
|
|
newMaterial.specularTexture = new RenderTexture(tex);
|
2023-10-12 23:45:34 -04:00
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
qDebug() << "unhandled type" << type;
|
|
|
|
break;
|
2023-04-09 15:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newMaterial;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void MDLPart::calculateBoneInversePose(physis_Skeleton &skeleton, physis_Bone &bone, physis_Bone *parent_bone)
|
|
|
|
{
|
2023-04-09 15:28:00 -04:00
|
|
|
const glm::mat4 parentMatrix = parent_bone == nullptr ? glm::mat4(1.0f) : boneData[parent_bone->index].inversePose;
|
|
|
|
|
2023-07-07 16:01:39 -04:00
|
|
|
glm::mat4 local = glm::mat4(1.0f);
|
2023-04-09 15:28:00 -04:00
|
|
|
local = glm::translate(local, glm::vec3(bone.position[0], bone.position[1], bone.position[2]));
|
|
|
|
local *= glm::mat4_cast(glm::quat(bone.rotation[3], bone.rotation[0], bone.rotation[1], bone.rotation[2]));
|
|
|
|
local = glm::scale(local, glm::vec3(bone.scale[0], bone.scale[1], bone.scale[2]));
|
|
|
|
|
|
|
|
boneData[bone.index].inversePose = parentMatrix * local;
|
|
|
|
|
2023-07-09 10:54:27 -04:00
|
|
|
for (int i = 0; i < skeleton.num_bones; i++) {
|
2023-10-12 23:45:34 -04:00
|
|
|
if (skeleton.bones[i].parent_bone != nullptr && std::string_view{skeleton.bones[i].parent_bone->name} == std::string_view{bone.name}) {
|
2023-04-09 15:28:00 -04:00
|
|
|
calculateBoneInversePose(skeleton, skeleton.bones[i], &bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:34 -04:00
|
|
|
void MDLPart::calculateBone(physis_Skeleton &skeleton, physis_Bone &bone, const physis_Bone *parent_bone)
|
|
|
|
{
|
2023-10-13 15:03:17 -04:00
|
|
|
const glm::mat4 parent_matrix = parent_bone == nullptr ? glm::mat4(1.0f) : (boneData[parent_bone->index].localTransform);
|
2023-04-09 15:28:00 -04:00
|
|
|
|
|
|
|
glm::mat4 local = glm::mat4(1.0f);
|
2023-07-09 10:54:27 -04:00
|
|
|
local = glm::translate(local, glm::vec3(bone.position[0], bone.position[1], bone.position[2]));
|
|
|
|
local *= glm::mat4_cast(glm::quat(bone.rotation[3], bone.rotation[0], bone.rotation[1], bone.rotation[2]));
|
|
|
|
local = glm::scale(local, glm::vec3(bone.scale[0], bone.scale[1], bone.scale[2]));
|
2023-04-09 15:28:00 -04:00
|
|
|
|
|
|
|
boneData[bone.index].localTransform = parent_matrix * local;
|
2023-10-13 15:03:17 -04:00
|
|
|
boneData[bone.index].finalTransform = parent_matrix;
|
2023-07-07 16:01:39 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < skeleton.num_bones; i++) {
|
2023-10-12 23:45:34 -04:00
|
|
|
if (skeleton.bones[i].parent_bone != nullptr && std::string_view{skeleton.bones[i].parent_bone->name} == std::string_view{bone.name}) {
|
2023-04-09 15:28:00 -04:00
|
|
|
calculateBone(skeleton, skeleton.bones[i], &bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 23:48:03 -04:00
|
|
|
void MDLPart::removeModel(const physis_MDL &mdl)
|
|
|
|
{
|
|
|
|
models.erase(std::remove_if(models.begin(),
|
|
|
|
models.end(),
|
|
|
|
[mdl](const RenderModel other) {
|
|
|
|
return mdl.lods == other.model.lods;
|
|
|
|
}),
|
|
|
|
models.end());
|
|
|
|
}
|
|
|
|
|
2023-04-09 15:28:00 -04:00
|
|
|
#include "moc_mdlpart.cpp"
|