From d6fd47f10d3c705f70d18ecacfe239fb9418ec6e Mon Sep 17 00:00:00 2001 From: Mara Vogt <mvogt@uni-koblenz.de> Date: Wed, 16 Jun 2021 08:23:59 +0200 Subject: [PATCH] [#63] first scene wip improved computeModelMatrix --- .../src/vkcv/asset/asset_loader.cpp | 87 +++++++++---------- projects/first_scene/src/main.cpp | 4 - 2 files changed, 41 insertions(+), 50 deletions(-) diff --git a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp index f546e21c..41002a3c 100644 --- a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp +++ b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp @@ -4,7 +4,7 @@ #include <stdlib.h> // calloc(3) #include <fx/gltf.h> #define STB_IMAGE_IMPLEMENTATION -#define STBI_ONLY_JPEG +//#define STBI_ONLY_JPEG // should not just be jpeg #include <stb_image.h> #include <vkcv/Logger.hpp> @@ -70,39 +70,38 @@ enum IndexType getIndexType(const enum fx::gltf::Accessor::ComponentType &t) } } -std::array<float, 16> computeModelMatrix(std::array<float, 3> translation, std::array<float, 3> scale, std::array<float, 4> rotation){ - std::array<float, 16> modelMatrix; - // row 4 - modelMatrix[12] = 0; - modelMatrix[13] = 0; - modelMatrix[14] = 0; - modelMatrix[15] = 1; - // translation - modelMatrix[3] = translation[0]; - modelMatrix[7] = translation[1]; - modelMatrix[11] = translation[2]; - // scale - //modelMatrix[0] = scale[0]; - //modelMatrix[5] = scale[1]; - //modelMatrix[10] = scale[2]; - // rotation and scale - auto a = rotation[0]; - auto q1 = rotation[1]; - auto q2 = rotation[2]; - auto q3 = rotation[3]; - - modelMatrix[0] = (2 * (a * a + q1 * q1) - 1) * scale[0]; - modelMatrix[1] = (2 * (q1 * q2 - a * q3)) * scale[1]; - modelMatrix[2] = (2 * (q1 * q3 + a * q2)) * scale[2]; - - modelMatrix[4] = (2 * (q1 * q2 + a * q3)) * scale[0]; - modelMatrix[5] = (2 * (a * a + q2 * q2) - 1) * scale[1]; - modelMatrix[6] = (2 * (q2 * q3 - a * q1)) * scale[2]; - - modelMatrix[8] = (2 * (q1 * q3 - a * q2)) * scale[0]; - modelMatrix[9] = (2 * (q2 * q3 + a * q1)) * scale[1]; - modelMatrix[10] = (2 * (a * a + q3 * q3) - 1) * scale[2]; - return modelMatrix; +std::array<float, 16> computeModelMatrix(std::array<float, 3> translation, std::array<float, 3> scale, std::array<float, 4> rotation, std::array<float, 16> matrix){ + std::array<float, 16> modelMatrix = {1,0,0,0, + 0,1,0,0, + 0,0,1,0, + 0,0,0,1}; + if (matrix != modelMatrix){ + return matrix; + } else { + // translation + modelMatrix[3] = translation[0]; + modelMatrix[7] = translation[1]; + modelMatrix[11] = translation[2]; + // rotation and scale + auto a = rotation[0]; + auto q1 = rotation[1]; + auto q2 = rotation[2]; + auto q3 = rotation[3]; + + modelMatrix[0] = (2 * (a * a + q1 * q1) - 1) * scale[0]; + modelMatrix[1] = (2 * (q1 * q2 - a * q3)) * scale[1]; + modelMatrix[2] = (2 * (q1 * q3 + a * q2)) * scale[2]; + + modelMatrix[4] = (2 * (q1 * q2 + a * q3)) * scale[0]; + modelMatrix[5] = (2 * (a * a + q2 * q2) - 1) * scale[1]; + modelMatrix[6] = (2 * (q2 * q3 - a * q1)) * scale[2]; + + modelMatrix[8] = (2 * (q1 * q3 - a * q2)) * scale[0]; + modelMatrix[9] = (2 * (q2 * q3 + a * q1)) * scale[1]; + modelMatrix[10] = (2 * (a * a + q3 * q3) - 1) * scale[2]; + return modelMatrix; + } + } int loadMesh(const std::string &path, Mesh &mesh) { @@ -427,22 +426,18 @@ int loadScene(const std::string &path, Scene &scene){ vertexGroupsIndex.push_back(groupCount); } - std::array<float, 16> modelMatrix = computeModelMatrix(sceneObjects.nodes[i].translation, sceneObjects.nodes[i].scale, sceneObjects.nodes[i].rotation); - mesh = { - sceneObjects.meshes[i].name, - modelMatrix, - vertexGroupsIndex, - }; - - /*mesh.name = sceneObjects.meshes[i].name; - mesh.vertexGroups = vertexGroupsIndex;*/ + mesh.name = sceneObjects.meshes[i].name; + mesh.vertexGroups = vertexGroupsIndex; meshes.push_back(mesh); } - /*for(int m = 0; m < sceneObjects.nodes.size(); m++){ - meshes[sceneObjects.nodes[m].mesh].modelMatrix = sceneObjects.nodes[m].matrix; - }*/ + for(int m = 0; m < sceneObjects.nodes.size(); m++) { + meshes[sceneObjects.nodes[m].mesh].modelMatrix = computeModelMatrix(sceneObjects.nodes[m].translation, + sceneObjects.nodes[m].scale, + sceneObjects.nodes[m].rotation, + sceneObjects.nodes[m].matrix); + } if (sceneObjects.textures.size() > 0){ textures.reserve(sceneObjects.textures.size()); diff --git a/projects/first_scene/src/main.cpp b/projects/first_scene/src/main.cpp index 51993a35..f7f4aaea 100644 --- a/projects/first_scene/src/main.cpp +++ b/projects/first_scene/src/main.cpp @@ -225,10 +225,6 @@ int main(int argc, const char** argv) { mvp.push_back(vp * m); } - //vkcv::PushConstantData pushConstantData((void*)mainPassMatrices.data(), 2 * sizeof(glm::mat4)); - - //std::vector<glm::mat4> pushConstantDataVector(drawcalls.size(), mvp); - vkcv::PushConstantData pushConstantData((void*)mvp.data(), sizeof(glm::mat4)); const std::vector<vkcv::ImageHandle> renderTargets = { swapchainInput, depthBuffer }; -- GitLab