From bf240d7eeaa89657462b705849fde56e54e237db Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Tue, 6 Feb 2024 17:37:20 -0300 Subject: refa Use quaternion for orientation --- src/vk/graphics_pipeline_3d.cpp | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'src/vk/graphics_pipeline_3d.cpp') diff --git a/src/vk/graphics_pipeline_3d.cpp b/src/vk/graphics_pipeline_3d.cpp index f657a54..2b6812d 100644 --- a/src/vk/graphics_pipeline_3d.cpp +++ b/src/vk/graphics_pipeline_3d.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2023 Frederico de Oliveira Linhares + * Copyright 2022-2024 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -296,14 +296,10 @@ GraphicsPipeline3D::draw( for(auto &instance: instances) { // Object matrix. - glm::mat4 base_matrix{1.0f}; - base_matrix = glm::translate(base_matrix, *instance->position); - base_matrix = glm::rotate( - base_matrix, instance->rotation->x, glm::vec3{1.0, 0.0, 0.0}); - base_matrix = glm::rotate( - base_matrix, instance->rotation->y, glm::vec3{0.0, 1.0, 0.0}); - base_matrix = glm::rotate( - base_matrix, instance->rotation->z, glm::vec3{0.0, 0.0, 1.0}); + glm::mat4 translation_matrix{1.0f}; + translation_matrix = glm::translate( + translation_matrix, *instance->position); + glm::mat4 rotation_matrix{glm::toMat4(*instance->orientation)}; std::array vk_descriptor_sets{ cg_core.vk_light->descriptor_sets_world[image_index], @@ -320,7 +316,7 @@ GraphicsPipeline3D::draw( draw_command_buffer, static_mesh->index_count, 1, 0, 0, 0); VK::UDOStaticModel udo_static_model{}; - udo_static_model.base_matrix = base_matrix; + udo_static_model.base_matrix = translation_matrix * rotation_matrix; instance->uniform_buffers[image_index].copy_data(&udo_static_model); } } @@ -329,16 +325,11 @@ GraphicsPipeline3D::draw( VK::UDOView3D ubo_view_3d{}; // View matrix. - ubo_view_3d.view = glm::mat4{1.0f}; - ubo_view_3d.view = glm::translate( - ubo_view_3d.view, *view->camera_position); - ubo_view_3d.view = glm::rotate( - ubo_view_3d.view, view->camera_rotation->y, glm::vec3{0.0, 1.0, 0.0}); - ubo_view_3d.view = glm::rotate( - ubo_view_3d.view, view->camera_rotation->x, glm::vec3{1.0, 0.0, 0.0}); - ubo_view_3d.view = glm::rotate( - ubo_view_3d.view, view->camera_rotation->z, glm::vec3{0.0, 0.0, 1.0}); - ubo_view_3d.view = glm::inverse(ubo_view_3d.view); + glm::mat4 translation_matrix{1.0f}; + translation_matrix = glm::translate( + translation_matrix, *view->camera_position); + glm::mat4 rotation_matrix{glm::toMat4(*view->camera_orientation)}; + ubo_view_3d.view = glm::inverse(translation_matrix * rotation_matrix); // Projection matrix. ubo_view_3d.proj = glm::perspective( -- cgit v1.2.3