Fix hud bars are no longer moving and rotating with spaceship movement

This commit is contained in:
s473577 2024-02-07 15:09:10 +01:00
parent 6f85812a35
commit dc73f3b5f1
3 changed files with 36 additions and 4 deletions

View File

@ -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<float>()) * 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;
}
};

View File

@ -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;

View File

@ -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);