mirror of
https://github.com/redstrate/Novus.git
synced 2025-04-26 13:47:46 +00:00
mdlpart: Reformat code, misc cleanup
This commit is contained in:
parent
9481c3e120
commit
1a97441647
2 changed files with 158 additions and 149 deletions
|
@ -21,15 +21,20 @@
|
||||||
#include "tiny_gltf.h"
|
#include "tiny_gltf.h"
|
||||||
|
|
||||||
#ifndef USE_STANDALONE_WINDOW
|
#ifndef USE_STANDALONE_WINDOW
|
||||||
class VulkanWindow : public QWindow {
|
class VulkanWindow : public QWindow
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
VulkanWindow(MDLPart *part, Renderer *renderer, QVulkanInstance *instance)
|
VulkanWindow(MDLPart *part, Renderer *renderer, QVulkanInstance *instance)
|
||||||
: part(part), m_renderer(renderer), m_instance(instance) {
|
: part(part)
|
||||||
|
, m_renderer(renderer)
|
||||||
|
, m_instance(instance)
|
||||||
|
{
|
||||||
setSurfaceType(VulkanSurface);
|
setSurfaceType(VulkanSurface);
|
||||||
setVulkanInstance(instance);
|
setVulkanInstance(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
void exposeEvent(QExposeEvent*) {
|
void exposeEvent(QExposeEvent *)
|
||||||
|
{
|
||||||
if (isExposed()) {
|
if (isExposed()) {
|
||||||
if (!m_initialized) {
|
if (!m_initialized) {
|
||||||
m_initialized = true;
|
m_initialized = true;
|
||||||
|
@ -43,7 +48,8 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool event(QEvent* e) {
|
bool event(QEvent *e)
|
||||||
|
{
|
||||||
switch (e->type()) {
|
switch (e->type()) {
|
||||||
case QEvent::UpdateRequest:
|
case QEvent::UpdateRequest:
|
||||||
render();
|
render();
|
||||||
|
@ -106,7 +112,8 @@ public:
|
||||||
return QWindow::event(e);
|
return QWindow::event(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void render() {
|
void render()
|
||||||
|
{
|
||||||
ImGui::SetCurrentContext(m_renderer->ctx);
|
ImGui::SetCurrentContext(m_renderer->ctx);
|
||||||
|
|
||||||
auto &io = ImGui::GetIO();
|
auto &io = ImGui::GetIO();
|
||||||
|
@ -119,10 +126,7 @@ public:
|
||||||
|
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
|
|
||||||
glm::vec3 position(
|
glm::vec3 position(part->cameraDistance * sin(part->yaw), part->cameraDistance * part->pitch, part->cameraDistance * cos(part->yaw));
|
||||||
part->cameraDistance * sin(part->yaw),
|
|
||||||
part->cameraDistance * part->pitch,
|
|
||||||
part->cameraDistance * cos(part->yaw));
|
|
||||||
|
|
||||||
m_renderer->view = glm::lookAt(part->position + position, part->position, glm::vec3(0, -1, 0));
|
m_renderer->view = glm::lookAt(part->position + position, part->position, glm::vec3(0, -1, 0));
|
||||||
|
|
||||||
|
@ -145,7 +149,10 @@ private:
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MDLPart::MDLPart(GameData* data, FileCache& cache) : data(data), cache(cache) {
|
MDLPart::MDLPart(GameData *data, FileCache &cache)
|
||||||
|
: data(data)
|
||||||
|
, cache(cache)
|
||||||
|
{
|
||||||
auto viewportLayout = new QVBoxLayout();
|
auto viewportLayout = new QVBoxLayout();
|
||||||
viewportLayout->setContentsMargins(0, 0, 0, 0);
|
viewportLayout->setContentsMargins(0, 0, 0, 0);
|
||||||
setLayout(viewportLayout);
|
setLayout(viewportLayout);
|
||||||
|
@ -179,7 +186,8 @@ MDLPart::MDLPart(GameData* data, FileCache& cache) : data(data), cache(cache) {
|
||||||
connect(this, &MDLPart::skeletonChanged, this, &MDLPart::reloadBoneData);
|
connect(this, &MDLPart::skeletonChanged, this, &MDLPart::reloadBoneData);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::exportModel(const QString& fileName) {
|
void MDLPart::exportModel(const QString &fileName)
|
||||||
|
{
|
||||||
const int selectedModel = 0;
|
const int selectedModel = 0;
|
||||||
const int selectedLod = 0;
|
const int selectedLod = 0;
|
||||||
|
|
||||||
|
@ -270,7 +278,8 @@ void MDLPart::exportModel(const QString& fileName) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < lod.num_parts; i++) {
|
for (int i = 0; i < lod.num_parts; i++) {
|
||||||
auto& gltfNode = gltfModel.nodes.emplace_back();;
|
auto &gltfNode = gltfModel.nodes.emplace_back();
|
||||||
|
;
|
||||||
gltfNode.name = models[0].name.toStdString() + " Part " + std::to_string(i) + ".0";
|
gltfNode.name = models[0].name.toStdString() + " Part " + std::to_string(i) + ".0";
|
||||||
gltfNode.skin = 0;
|
gltfNode.skin = 0;
|
||||||
|
|
||||||
|
@ -367,7 +376,8 @@ void MDLPart::exportModel(const QString& fileName) {
|
||||||
loader.WriteGltfSceneToFile(&gltfModel, fileName.toStdString(), true, true, false, true);
|
loader.WriteGltfSceneToFile(&gltfModel, fileName.toStdString(), true, true, false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::clear() {
|
void MDLPart::clear()
|
||||||
|
{
|
||||||
models.clear();
|
models.clear();
|
||||||
|
|
||||||
Q_EMIT modelChanged();
|
Q_EMIT modelChanged();
|
||||||
|
@ -380,8 +390,7 @@ void MDLPart::addModel(physis_MDL mdl, const QString &name, std::vector<physis_M
|
||||||
auto model = renderer->addModel(mdl, lod);
|
auto model = renderer->addModel(mdl, lod);
|
||||||
model.name = name;
|
model.name = name;
|
||||||
|
|
||||||
std::transform(
|
std::transform(materials.begin(), materials.end(), std::back_inserter(model.materials), [this](const physis_Material &mat) {
|
||||||
materials.begin(), materials.end(), std::back_inserter(model.materials), [this](const physis_Material& mat) {
|
|
||||||
return createMaterial(mat);
|
return createMaterial(mat);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -394,7 +403,8 @@ void MDLPart::addModel(physis_MDL mdl, const QString &name, std::vector<physis_M
|
||||||
Q_EMIT modelChanged();
|
Q_EMIT modelChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::setSkeleton(physis_Skeleton newSkeleton) {
|
void MDLPart::setSkeleton(physis_Skeleton newSkeleton)
|
||||||
|
{
|
||||||
skeleton = std::make_unique<physis_Skeleton>(newSkeleton);
|
skeleton = std::make_unique<physis_Skeleton>(newSkeleton);
|
||||||
|
|
||||||
firstTimeSkeletonDataCalculated = false;
|
firstTimeSkeletonDataCalculated = false;
|
||||||
|
@ -402,7 +412,8 @@ void MDLPart::setSkeleton(physis_Skeleton newSkeleton) {
|
||||||
Q_EMIT skeletonChanged();
|
Q_EMIT skeletonChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::loadRaceDeformMatrices(physis_Buffer buffer) {
|
void MDLPart::loadRaceDeformMatrices(physis_Buffer buffer)
|
||||||
|
{
|
||||||
QJsonDocument document = QJsonDocument::fromJson(QByteArray((const char *)buffer.data, buffer.size));
|
QJsonDocument document = QJsonDocument::fromJson(QByteArray((const char *)buffer.data, buffer.size));
|
||||||
for (auto boneObj : document.object()[QLatin1String("Data")].toArray()) {
|
for (auto boneObj : document.object()[QLatin1String("Data")].toArray()) {
|
||||||
QJsonArray matrix = boneObj.toObject()[QLatin1String("Matrix")].toArray();
|
QJsonArray matrix = boneObj.toObject()[QLatin1String("Matrix")].toArray();
|
||||||
|
@ -426,7 +437,8 @@ void MDLPart::loadRaceDeformMatrices(physis_Buffer buffer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::clearSkeleton() {
|
void MDLPart::clearSkeleton()
|
||||||
|
{
|
||||||
skeleton.reset();
|
skeleton.reset();
|
||||||
|
|
||||||
firstTimeSkeletonDataCalculated = false;
|
firstTimeSkeletonDataCalculated = false;
|
||||||
|
@ -434,7 +446,8 @@ void MDLPart::clearSkeleton() {
|
||||||
Q_EMIT skeletonChanged();
|
Q_EMIT skeletonChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::reloadRenderer() {
|
void MDLPart::reloadRenderer()
|
||||||
|
{
|
||||||
reloadBoneData();
|
reloadBoneData();
|
||||||
|
|
||||||
#ifndef USE_STANDALONE_WINDOW
|
#ifndef USE_STANDALONE_WINDOW
|
||||||
|
@ -444,7 +457,8 @@ void MDLPart::reloadRenderer() {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::reloadBoneData() {
|
void MDLPart::reloadBoneData()
|
||||||
|
{
|
||||||
if (skeleton) {
|
if (skeleton) {
|
||||||
if (!firstTimeSkeletonDataCalculated) {
|
if (!firstTimeSkeletonDataCalculated) {
|
||||||
if (boneData.empty()) {
|
if (boneData.empty()) {
|
||||||
|
@ -467,8 +481,7 @@ void MDLPart::reloadBoneData() {
|
||||||
std::map<int, int> boneMapping;
|
std::map<int, int> boneMapping;
|
||||||
for (int i = 0; i < model.model.num_affected_bones; i++) {
|
for (int i = 0; i < model.model.num_affected_bones; i++) {
|
||||||
for (int k = 0; k < skeleton->num_bones; k++) {
|
for (int k = 0; k < skeleton->num_bones; k++) {
|
||||||
if (std::string_view{skeleton->bones[k].name} ==
|
if (std::string_view{skeleton->bones[k].name} == std::string_view{model.model.affected_bone_names[i]}) {
|
||||||
std::string_view{model.model.affected_bone_names[i]}) {
|
|
||||||
boneMapping[i] = k;
|
boneMapping[i] = k;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -481,7 +494,8 @@ void MDLPart::reloadBoneData() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderMaterial MDLPart::createMaterial(const physis_Material& material) {
|
RenderMaterial MDLPart::createMaterial(const physis_Material &material)
|
||||||
|
{
|
||||||
RenderMaterial newMaterial;
|
RenderMaterial newMaterial;
|
||||||
|
|
||||||
for (int i = 0; i < material.num_textures; i++) {
|
for (int i = 0; i < material.num_textures; i++) {
|
||||||
|
@ -527,7 +541,8 @@ RenderMaterial MDLPart::createMaterial(const physis_Material& material) {
|
||||||
return newMaterial;
|
return newMaterial;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::calculateBoneInversePose(physis_Skeleton& skeleton, physis_Bone& bone, physis_Bone* parent_bone) {
|
void MDLPart::calculateBoneInversePose(physis_Skeleton &skeleton, physis_Bone &bone, physis_Bone *parent_bone)
|
||||||
|
{
|
||||||
const glm::mat4 parentMatrix = parent_bone == nullptr ? glm::mat4(1.0f) : boneData[parent_bone->index].inversePose;
|
const glm::mat4 parentMatrix = parent_bone == nullptr ? glm::mat4(1.0f) : boneData[parent_bone->index].inversePose;
|
||||||
|
|
||||||
glm::mat4 local = glm::mat4(1.0f);
|
glm::mat4 local = glm::mat4(1.0f);
|
||||||
|
@ -538,16 +553,15 @@ void MDLPart::calculateBoneInversePose(physis_Skeleton& skeleton, physis_Bone& b
|
||||||
boneData[bone.index].inversePose = parentMatrix * local;
|
boneData[bone.index].inversePose = parentMatrix * local;
|
||||||
|
|
||||||
for (int i = 0; i < skeleton.num_bones; i++) {
|
for (int i = 0; i < skeleton.num_bones; i++) {
|
||||||
if (skeleton.bones[i].parent_bone != nullptr &&
|
if (skeleton.bones[i].parent_bone != nullptr && std::string_view{skeleton.bones[i].parent_bone->name} == std::string_view{bone.name}) {
|
||||||
std::string_view{skeleton.bones[i].parent_bone->name} == std::string_view{bone.name}) {
|
|
||||||
calculateBoneInversePose(skeleton, skeleton.bones[i], &bone);
|
calculateBoneInversePose(skeleton, skeleton.bones[i], &bone);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDLPart::calculateBone(physis_Skeleton& skeleton, physis_Bone& bone, const physis_Bone* parent_bone) {
|
void MDLPart::calculateBone(physis_Skeleton &skeleton, physis_Bone &bone, const physis_Bone *parent_bone)
|
||||||
const glm::mat4 parent_matrix =
|
{
|
||||||
parent_bone == nullptr ? glm::mat4(1.0f) : boneData[parent_bone->index].localTransform;
|
const glm::mat4 parent_matrix = parent_bone == nullptr ? glm::mat4(1.0f) : boneData[parent_bone->index].localTransform;
|
||||||
|
|
||||||
glm::mat4 local = glm::mat4(1.0f);
|
glm::mat4 local = glm::mat4(1.0f);
|
||||||
local = glm::translate(local, glm::vec3(bone.position[0], bone.position[1], bone.position[2]));
|
local = glm::translate(local, glm::vec3(bone.position[0], bone.position[1], bone.position[2]));
|
||||||
|
@ -555,12 +569,10 @@ void MDLPart::calculateBone(physis_Skeleton& skeleton, physis_Bone& bone, const
|
||||||
local = glm::scale(local, glm::vec3(bone.scale[0], bone.scale[1], bone.scale[2]));
|
local = glm::scale(local, glm::vec3(bone.scale[0], bone.scale[1], bone.scale[2]));
|
||||||
|
|
||||||
boneData[bone.index].localTransform = parent_matrix * local;
|
boneData[bone.index].localTransform = parent_matrix * local;
|
||||||
boneData[bone.index].finalTransform =
|
boneData[bone.index].finalTransform = boneData[bone.index].localTransform * boneData[bone.index].deformRaceMatrix * boneData[bone.index].inversePose;
|
||||||
boneData[bone.index].localTransform * boneData[bone.index].deformRaceMatrix * boneData[bone.index].inversePose;
|
|
||||||
|
|
||||||
for (int i = 0; i < skeleton.num_bones; i++) {
|
for (int i = 0; i < skeleton.num_bones; i++) {
|
||||||
if (skeleton.bones[i].parent_bone != nullptr &&
|
if (skeleton.bones[i].parent_bone != nullptr && std::string_view{skeleton.bones[i].parent_bone->name} == std::string_view{bone.name}) {
|
||||||
std::string_view{skeleton.bones[i].parent_bone->name} == std::string_view{bone.name}) {
|
|
||||||
calculateBone(skeleton, skeleton.bones[i], &bone);
|
calculateBone(skeleton, skeleton.bones[i], &bone);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,8 @@ class VulkanWindow;
|
||||||
class StandaloneWindow;
|
class StandaloneWindow;
|
||||||
class FileCache;
|
class FileCache;
|
||||||
|
|
||||||
class MDLPart : public QWidget {
|
class MDLPart : public QWidget
|
||||||
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -26,11 +27,7 @@ public:
|
||||||
int lastX = -1;
|
int lastX = -1;
|
||||||
int lastY = -1;
|
int lastY = -1;
|
||||||
|
|
||||||
enum class CameraMode {
|
enum class CameraMode { None, Orbit, Move };
|
||||||
None,
|
|
||||||
Orbit,
|
|
||||||
Move
|
|
||||||
};
|
|
||||||
|
|
||||||
CameraMode cameraMode = CameraMode::None;
|
CameraMode cameraMode = CameraMode::None;
|
||||||
float pitch = 0.0f;
|
float pitch = 0.0f;
|
||||||
|
|
Loading…
Add table
Reference in a new issue