From dc73f3b5f1b92ab0dcbb6fb7b6ec79e7cd1a8b5f Mon Sep 17 00:00:00 2001 From: s473577 Date: Wed, 7 Feb 2024 15:09:10 +0100 Subject: [PATCH] Fix hud bars are no longer moving and rotating with spaceship movement --- grk/project/Spaceship.h | 31 +++++++++++++++++++++++++++++++ grk/project/SpriteRenderer.cpp | 2 +- grk/project/src/ex_9_1.hpp | 7 ++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/grk/project/Spaceship.h b/grk/project/Spaceship.h index 186231b..8876620 100644 --- a/grk/project/Spaceship.h +++ b/grk/project/Spaceship.h @@ -49,6 +49,7 @@ public: glm::vec3 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f); glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f); + glm::vec3 cameraPosHUDBar = glm::vec3(0.479490f, 1.250000f, -2.124680f); glm::mat4 additionalHorRotationMatrix; glm::mat4 additionalVerRotationMatrix; @@ -255,6 +256,8 @@ public: requestShoot(time); cameraPos = spaceshipPos - calculateCameraDistanceHor(w, shiftState, deltaTime) * spaceshipDir + glm::vec3(0, calculateCameraDistanceVer(deltaTime), 0); + cameraPosHUDBar = spaceshipPos - 1.f * spaceshipDir + glm::vec3(0, 1, 0) * 0.2f; + cameraDir = spaceshipDir; glm::vec3 perpendicularVector = 0.04f * glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.0f, 1.0f, 0.0f))); if (leftParticle != nullptr && rightParticle != nullptr) { @@ -340,4 +343,32 @@ public: glm::mat4 getModelMatrix() override { return calculateModelMatrix(); } + + glm::mat4 calculateModelMatrixForHUDBar() { + glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); + glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir)); + + glm::mat4 spaceshipCameraRotationMatrix = glm::mat4({ + spaceshipSide.x, spaceshipSide.y, spaceshipSide.z, 0, + spaceshipUp.x, spaceshipUp.y, spaceshipUp.z, 0, + -spaceshipDir.x, -spaceshipDir.y, -spaceshipDir.z, 0, + 0., 0., 0., 1. + }); + return glm::translate(spaceshipPos) * spaceshipCameraRotationMatrix * glm::eulerAngleY(glm::pi()) * glm::scale(glm::vec3(0.02f)); + } + glm::mat4 createCameraMatrixForHUDBar() + { + glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f))); + glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, cameraDir)); + glm::mat4 cameraRotrationMatrix = glm::mat4({ + cameraSide.x,cameraSide.y,cameraSide.z,0, + cameraUp.x,cameraUp.y,cameraUp.z ,0, + -cameraDir.x,-cameraDir.y,-cameraDir.z,0, + 0.,0.,0.,1., + }); + cameraRotrationMatrix = glm::transpose(cameraRotrationMatrix); + glm::mat4 cameraMatrix = cameraRotrationMatrix * glm::translate(-cameraPosHUDBar); + + return cameraMatrix; + } }; \ No newline at end of file diff --git a/grk/project/SpriteRenderer.cpp b/grk/project/SpriteRenderer.cpp index c507f1c..7a375f1 100644 --- a/grk/project/SpriteRenderer.cpp +++ b/grk/project/SpriteRenderer.cpp @@ -16,7 +16,7 @@ void Core::SpriteRenderer::DrawHUDBar(const glm::vec3 color, const glm::mat4 mod Spaceship* spaceship = Spaceship::getInstance(); - glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix(); + glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrixForHUDBar(); // Kombinuj macierze transformacji glm::mat4 transformation = viewProjectionMatrix * modelMatrix; diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index cf1f207..f7ad488 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -104,13 +104,14 @@ void updateDeltaTime(float time) { lastTime = time; } void renderHUD() { + glUseProgram(programSpriteBar); glDisable(GL_DEPTH_TEST); - glm::mat4 spaceshipModelMatrix = spaceship->calculateModelMatrix(); - glm::mat4 healthBarPosition = glm::translate(spaceshipModelMatrix, glm::vec3(36.0f, -34.0f, 0.0f)); + glm::mat4 modelMatrixHUD = spaceship->calculateModelMatrixForHUDBar(); + glm::mat4 healthBarPosition = glm::translate(modelMatrixHUD, glm::vec3(38.0f, -35.0f, 0.0f)); spriteRenderer->DrawHUDBar( glm::vec3(0.0f, 1.0f, 0.0f), - glm::scale(healthBarPosition, glm::vec3(26.0f, 4.f, 1.0f)), + glm::scale(healthBarPosition, glm::vec3(22.0f, 4.f, 1.0f)), spaceship->currentHP / spaceship->maxHP, programSpriteBar); glEnable(GL_DEPTH_TEST);