From de13bc43a42c6200c3a6074d8f63828853e02d21 Mon Sep 17 00:00:00 2001 From: s473577 Date: Thu, 1 Feb 2024 15:11:52 +0100 Subject: [PATCH 1/4] Add drawing health bars for enemies --- grk/project/Enemy.h | 5 +- grk/project/SpriteRenderer.cpp | 33 +++++ grk/project/SpriteRenderer.h | 2 +- grk/project/grk-project.vcxproj | 2 + grk/project/grk-project.vcxproj.filters | 6 + grk/project/shaders/shader_sprite_bar.frag | 24 ++++ grk/project/shaders/shader_sprite_bar.vert | 20 +++ grk/project/src/ex_9_1.hpp | 52 ++++++- grk/project/src/ex_9_1.hpp.orig | 151 +++++++++++++++++---- 9 files changed, 257 insertions(+), 38 deletions(-) create mode 100644 grk/project/shaders/shader_sprite_bar.frag create mode 100644 grk/project/shaders/shader_sprite_bar.vert diff --git a/grk/project/Enemy.h b/grk/project/Enemy.h index 54da607..bef66a5 100644 --- a/grk/project/Enemy.h +++ b/grk/project/Enemy.h @@ -15,11 +15,12 @@ private: public: glm::mat4 modelMatrix; int hp; + int initialHp; int dmg; float aggroRange; - Enemy(int initialHp, glm::mat4 initialModelMatrix, int initialDmg, float initialAggroRange) - : hp(initialHp), modelMatrix(initialModelMatrix), dmg(initialDmg), aggroRange(initialAggroRange) + Enemy(int currHp,int initialHp, glm::mat4 initialModelMatrix, int initialDmg, float initialAggroRange) + : hp(currHp), initialHp(initialHp), modelMatrix(initialModelMatrix), dmg(initialDmg), aggroRange(initialAggroRange) {} diff --git a/grk/project/SpriteRenderer.cpp b/grk/project/SpriteRenderer.cpp index f27e154..0e3a965 100644 --- a/grk/project/SpriteRenderer.cpp +++ b/grk/project/SpriteRenderer.cpp @@ -12,6 +12,39 @@ Core::SpriteRenderer::~SpriteRenderer() glDeleteBuffers(1, &this->VBO); glDeleteBuffers(1, &this->EBO); } +void Core::SpriteRenderer::DrawSpriteBar(const glm::vec3 color, const glm::mat4 modelMatrix,const float progress, GLuint program) { + + Spaceship* spaceship = Spaceship::getInstance(); + + // Pobierz pozycję kamery + glm::vec3 cameraPosition = spaceship->cameraPos; + + glm::vec3 spritePosition = glm::vec3(modelMatrix[3]); + + // Oblicz wektor skierowany od sprita do kamery + glm::vec3 spriteToCamera = glm::normalize(cameraPosition - spritePosition); + + // Oblicz kąt rotacji w stopniach wokół osi Y + float angle = glm::degrees(atan2(spriteToCamera.x, spriteToCamera.z)); + + // Utwórz macierz rotacji wokł osi Y + glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(0.0f, 1.0f, 0.0f)); + // Utwórz macierz widoku-projekcji + glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix(); + + // Kombinuj macierze transformacji + glm::mat4 transformation = viewProjectionMatrix * modelMatrix * rotationMatrix; + + // Przekaż macierze do shadera + glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, glm::value_ptr(transformation)); + glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniform3fv(glGetUniformLocation(program, "activeColor"), 1, glm::value_ptr(color)); + glUniform1f(glGetUniformLocation(program, "progress"), progress); + + glBindVertexArray(this->VAO); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); +}; void Core::SpriteRenderer::DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, GLuint program) { Spaceship* spaceship = Spaceship::getInstance(); diff --git a/grk/project/SpriteRenderer.h b/grk/project/SpriteRenderer.h index b25f0af..ca15691 100644 --- a/grk/project/SpriteRenderer.h +++ b/grk/project/SpriteRenderer.h @@ -10,7 +10,7 @@ namespace Core { SpriteRenderer(); ~SpriteRenderer(); void DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, GLuint program); - + void DrawSpriteBar(const glm::vec3 color, const glm::mat4 modelMatrix,const float progress, GLuint program); private: unsigned int VAO; diff --git a/grk/project/grk-project.vcxproj b/grk/project/grk-project.vcxproj index af9222f..172044f 100644 --- a/grk/project/grk-project.vcxproj +++ b/grk/project/grk-project.vcxproj @@ -64,6 +64,8 @@ + + diff --git a/grk/project/grk-project.vcxproj.filters b/grk/project/grk-project.vcxproj.filters index ffb3d9c..406cd86 100644 --- a/grk/project/grk-project.vcxproj.filters +++ b/grk/project/grk-project.vcxproj.filters @@ -174,5 +174,11 @@ Shader Files + + Shader Files + + + Shader Files + \ No newline at end of file diff --git a/grk/project/shaders/shader_sprite_bar.frag b/grk/project/shaders/shader_sprite_bar.frag new file mode 100644 index 0000000..53e594c --- /dev/null +++ b/grk/project/shaders/shader_sprite_bar.frag @@ -0,0 +1,24 @@ +#version 430 core + +uniform float progress; +uniform vec3 activeColor; + +in vec3 worldPos; +in vec2 texCoord; +in vec3 color; + +out vec4 outColor; +void main() +{ + vec4 inactiveColor = vec4(0.5, 0.5, 0.5, 1.0); + + if (texCoord.x > progress) { + outColor = inactiveColor; // Ustawienie koloru dla obszaru nieaktywnego + return; + } + + outColor = vec4(activeColor,1.0); // Ustawienie koloru dla obszaru aktywnego + + + +} \ No newline at end of file diff --git a/grk/project/shaders/shader_sprite_bar.vert b/grk/project/shaders/shader_sprite_bar.vert new file mode 100644 index 0000000..0e99078 --- /dev/null +++ b/grk/project/shaders/shader_sprite_bar.vert @@ -0,0 +1,20 @@ +#version 430 core + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 aColor; +layout(location = 2) in vec2 vertexTexCoord; + +uniform mat4 transformation; +uniform mat4 modelMatrix; + +out vec3 worldPos; +out vec2 texCoord; +out vec3 color; + +void main() +{ + worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz; + gl_Position = transformation * vec4(vertexPosition, 1.0); + texCoord = vertexTexCoord; + color = aColor; +} \ No newline at end of file diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index c7cf77d..eebf9aa 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -63,6 +63,7 @@ GLuint programSprite; GLuint programCubemap; GLuint programParticle; GLuint programTex; +GLuint programSpriteBar; std::list planets; Sun* sun; @@ -93,7 +94,38 @@ void updateDeltaTime(float time) { if (deltaTime > 0.1) deltaTime = 0.1; lastTime = time; } +void renderEnemies() { + glUseProgram(programSpriteBar); + for (const auto& enemy : enemies) { + if (enemy->isAlive()) { + spriteRenderer->DrawSpriteBar( + glm::vec3(1.0f, 0.0f, 0.0f), + glm::scale( + glm::translate(enemy->modelMatrix, glm::vec3(0.0f, 0.7f, 0.0f) + ), + glm::vec3(1.0f, 0.2f, 1.0f) + ), + static_cast(enemy->hp / enemy->initialHp), + programSpriteBar); + } + } + glUseProgram(programSprite); + for (const auto& enemy : enemies) { + if (enemy->isAlive()) { + spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite); + } + } + glUseProgram(program); + for (const auto& enemy : enemies) { + if (enemy->isAlive()) { + enemy->attack(spaceship->spaceshipPos, glfwGetTime()); + enemy->renderBullets(glfwGetTime(), program); + } + + } + +} TextureTuple getRandomPlanetTexture() { int textureIndex = rand() % planetTextures.size(); TextureTuple selectedTextures = planetTextures[textureIndex]; @@ -102,7 +134,6 @@ TextureTuple getRandomPlanetTexture() { return planetTextures[textureIndex]; } - void renderShadowapSun() { float time = glfwGetTime(); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); @@ -155,6 +186,7 @@ void renderScene(GLFWwindow* window) spaceship->roughness, spaceship->metallic, programTex ); +<<<<<<< HEAD //Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); drawObjectPBRTexture(models::sphereContext, glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(1.0f)), texture::earthTexture, 0.3, 0.0, programTex); //glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * @@ -181,6 +213,12 @@ void renderScene(GLFWwindow* window) glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, spaceship->cameraDir)); glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp); spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); +======= + renderEnemies(); + + //drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program); + +>>>>>>> 6ccdec5 (Add drawing health bars for enemies) glUseProgram(0); glfwSwapBuffers(window); @@ -193,7 +231,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) HEIGHT = height; } - void createSuns() { createGalaxy(glm::vec3(0.f)); createGalaxy(glm::vec3(0.f, 50.f, 200.f)); @@ -226,9 +263,9 @@ void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int void createEnemies() { - enemies.push_back(new Enemy(100, glm::mat4(1.0f), 20, 5.0f)); - enemies.push_back(new Enemy(100, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 5.0f)); - enemies.push_back(new Enemy(100, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 25, 5.0f)); + enemies.push_back(new Enemy(100,100, glm::mat4(1.0f), 20, 5.0f)); + enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 5.0f)); + enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 25, 5.0f)); } @@ -262,7 +299,12 @@ void init(GLFWwindow* window) programTest = gameUtils->shaderLoader->CreateProgram("shaders/test.vert", "shaders/test.frag"); programSun = gameUtils->shaderLoader->CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); +<<<<<<< HEAD programTex = gameUtils->shaderLoader->CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag"); +======= + programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); + programSpriteBar = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite_bar.vert", "shaders/shader_sprite_bar.frag"); +>>>>>>> 6ccdec5 (Add drawing health bars for enemies) programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); diff --git a/grk/project/src/ex_9_1.hpp.orig b/grk/project/src/ex_9_1.hpp.orig index de240ec..db43d6d 100644 --- a/grk/project/src/ex_9_1.hpp.orig +++ b/grk/project/src/ex_9_1.hpp.orig @@ -21,19 +21,15 @@ #include "../Planet.h" #include "../GameObject.h" #include "../GameUtils.h" -<<<<<<< HEAD #include "../SpriteRenderer.h" #include "../Enemy.h" -======= #include "../ParticleSystem.h" #include "Camera.h" ->>>>>>> 2162820 (fire from ship) const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; int WIDTH = 500, HEIGHT = 500; - namespace models { Core::RenderContext marbleBustContext; Core::RenderContext spaceshipContext; @@ -42,9 +38,19 @@ namespace models { } namespace texture { GLuint cubemapTexture; + GLuint spaceshipTexture; GLuint spriteTexture; + GLuint earthTexture; } +struct TextureTuple { + GLuint textureID; + GLuint normalMapID; +}; + +std::vector planetTextures; + + void createGalaxy(glm::vec3 galaxyPosition); GLuint depthMapFBO; @@ -55,7 +61,12 @@ GLuint programSun; GLuint programTest; GLuint programSprite; GLuint programCubemap; +<<<<<<< HEAD GLuint programParticle; +GLuint programTex; +======= +GLuint programSpriteBar; +>>>>>>> 6ccdec5 (Add drawing health bars for enemies) std::list planets; Sun* sun; @@ -86,7 +97,51 @@ void updateDeltaTime(float time) { if (deltaTime > 0.1) deltaTime = 0.1; lastTime = time; } +void renderEnemies() { + glUseProgram(programSpriteBar); + for (const auto& enemy : enemies) { + if (enemy->isAlive()) { + spriteRenderer->DrawSpriteBar( + glm::vec3(1.0f, 0.0f, 0.0f), + glm::scale( + glm::translate(enemy->modelMatrix, glm::vec3(0.0f, 0.7f, 0.0f) + ), + glm::vec3(1.0f, 0.2f, 1.0f) + ), + static_cast(enemy->hp / enemy->initialHp), + programSpriteBar); + } +<<<<<<< HEAD +TextureTuple getRandomPlanetTexture() { + int textureIndex = rand() % planetTextures.size(); + TextureTuple selectedTextures = planetTextures[textureIndex]; + //GLuint textureID = selectedTextures.textureID; + //GLuint normalMapID = selectedTextures.normalMapID; + + return planetTextures[textureIndex]; +} + +======= + } + glUseProgram(programSprite); + for (const auto& enemy : enemies) { + if (enemy->isAlive()) { + spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite); + } + + } + glUseProgram(program); + for (const auto& enemy : enemies) { + if (enemy->isAlive()) { + enemy->attack(spaceship->spaceshipPos, glfwGetTime()); + enemy->renderBullets(glfwGetTime(), program); + } + + } + +} +>>>>>>> 6ccdec5 (Add drawing health bars for enemies) void renderShadowapSun() { float time = glfwGetTime(); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); @@ -97,6 +152,7 @@ void renderShadowapSun() { } void renderScene(GLFWwindow* window) { + glClearColor(0.4f, 0.4f, 0.8f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float time = glfwGetTime(); @@ -114,25 +170,37 @@ void renderScene(GLFWwindow* window) sun->draw(); } - glUseProgram(program); - + glUseProgram(programTex); for (Planet* p : planets) { - p->draw(time, program); + p->draw(time, programTex); } + glUseProgram(program); + spaceship->renderBullets(glfwGetTime(), program); + + drawObjectPBR(models::sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::translate(glm::vec3(1.f, 0, 0)) * glm::scale(glm::vec3(0.1f)), glm::vec3(0.5, 0.5, 0.5), 0.7, 0.0, program); - drawObjectPBR(models::spaceshipContext, + + glUseProgram(programTex); + //Core::SetActiveTexture(texture::spaceshipTexture, "textureSampler", programTex, 0); + drawObjectPBRTexture(models::spaceshipContext, spaceship->calculateModelMatrix(), - spaceship->color, - spaceship->roughness, spaceship->metallic, program + texture::spaceshipTexture, + spaceship->roughness, spaceship->metallic, programTex ); <<<<<<< HEAD + //Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); + drawObjectPBRTexture(models::sphereContext, glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(1.0f)), texture::earthTexture, 0.3, 0.0, programTex); + //glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * + Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); + drawObjectPBRTexture(models::sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), texture::earthTexture, 0.3, 0.0, programTex); + glUseProgram(programSprite); for (const auto& enemy : enemies) { if (enemy->isAlive()) { @@ -149,20 +217,16 @@ void renderScene(GLFWwindow* window) } - - - //drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program); - -======= - std::list* particleSystems = GameUtils::getInstance()->getParticleSystems(); glm::vec3 cameraSide = glm::normalize(glm::cross(spaceship->cameraDir, glm::vec3(0.f, 1.f, 0.f))); glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, spaceship->cameraDir)); glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp); spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); - /*for (auto particleSystem : *particleSystems) { - particleSystem->drawParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); - }*/ ->>>>>>> 2162820 (fire from ship) +======= + renderEnemies(); + + //drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program); + +>>>>>>> 6ccdec5 (Add drawing health bars for enemies) glUseProgram(0); glfwSwapBuffers(window); @@ -175,7 +239,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) HEIGHT = height; } - void createSuns() { createGalaxy(glm::vec3(0.f)); createGalaxy(glm::vec3(0.f, 50.f, 200.f)); @@ -197,26 +260,47 @@ void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int sun = new Sun(programSun, models::sphereContext, sunPos, glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(0.9f, 0.9f, 0.7f) * 5, sunScale); gu->getSuns()->push_back(sun); for (int i = 0; i < numberOfPlanets; i++) { + TextureTuple textures = getRandomPlanetTexture(); + GLuint texID = textures.textureID; + GLuint norMapID = textures.normalMapID; float distanceFromSum = (i + 1) * planetsDistance; - Planet* planet = new Planet(sun, distanceFromSum, i * 0.1f + planetSpeedCoef, planetSizes[i], models::sphereContext); + Planet* planet = new Planet(sun, distanceFromSum, i * 0.1f + planetSpeedCoef, planetSizes[i], models::sphereContext, texID, norMapID); planets.push_back(planet); } } void createEnemies() { - enemies.push_back(new Enemy(100, glm::mat4(1.0f), 20, 5.0f)); - enemies.push_back(new Enemy(100, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 5.0f)); - enemies.push_back(new Enemy(100, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 25, 5.0f)); + enemies.push_back(new Enemy(100,100, glm::mat4(1.0f), 20, 5.0f)); + enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 5.0f)); + enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 25, 5.0f)); } +void loadPlanetsTextures() { + planetTextures.clear(); + + std::vector> texturePaths = { + {"./textures/planets/planet1.png", "./textures/planets/planet1normal.png"}, + {"./textures/planets/planet2.png", "./textures/planets/planet2normal.png"}, + {"./textures/planets/planet3.png", "./textures/planets/planet3normal.png"}, + {"./textures/planets/planet4.png", "./textures/planets/planet4normal.png"} + }; + + for (const auto& paths : texturePaths) { + GLuint textureID = Core::LoadTexture(paths.first.c_str()); + GLuint normalMapID = Core::LoadTexture(paths.second.c_str()); + planetTextures.push_back({ textureID, normalMapID }); + } +} + + + void init(GLFWwindow* window) { GameUtils* gameUtils = GameUtils::getInstance(); spriteRenderer = new Core::SpriteRenderer(); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - glEnable(GL_DEPTH_TEST); program = gameUtils->shaderLoader->CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag"); @@ -224,14 +308,18 @@ void init(GLFWwindow* window) programSun = gameUtils->shaderLoader->CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); <<<<<<< HEAD - programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); - + programTex = gameUtils->shaderLoader->CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag"); ======= + programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); + programSpriteBar = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite_bar.vert", "shaders/shader_sprite_bar.frag"); +>>>>>>> 6ccdec5 (Add drawing health bars for enemies) + + + programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); programParticle = gameUtils->shaderLoader->CreateProgram("shaders/particle.vert", "shaders/particle.frag"); ->>>>>>> 2162820 (fire from ship) loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); - loadModelToContext("./models/spaceship.obj", models::spaceshipContext); + loadModelToContext("./models/StarShip2.obj", models::spaceshipContext); loadModelToContext("./models/sphere.obj", models::sphereContext); loadModelToContext("./models/cube.obj", models::cubeContext); Core::loadModelToContext("./models/sphere.obj", GameUtils::getInstance()->sphereContext); @@ -247,7 +335,10 @@ void init(GLFWwindow* window) "bkg2_back6.png" }; + loadPlanetsTextures(); texture::cubemapTexture = Core::LoadCubemap(cubeFaces); + texture::spaceshipTexture = Core::LoadTexture("./textures/spaceship/Material.001_Base_color.jpg"); + texture::earthTexture = Core::LoadTexture("./textures/planets/8k_earth_daymap.jpg"); spaceship->createParticles(); createSuns(); From 94b22aad78de85a9d5f9f90a1f652d2d2639c4d7 Mon Sep 17 00:00:00 2001 From: s473577 Date: Thu, 1 Feb 2024 23:27:42 +0100 Subject: [PATCH 2/4] Add drawing HUD HP bar --- grk/project/SpriteRenderer.cpp | 19 +++++++++++++++++++ grk/project/SpriteRenderer.h | 1 + grk/project/src/ex_9_1.hpp | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/grk/project/SpriteRenderer.cpp b/grk/project/SpriteRenderer.cpp index 0e3a965..c507f1c 100644 --- a/grk/project/SpriteRenderer.cpp +++ b/grk/project/SpriteRenderer.cpp @@ -12,6 +12,25 @@ Core::SpriteRenderer::~SpriteRenderer() glDeleteBuffers(1, &this->VBO); glDeleteBuffers(1, &this->EBO); } +void Core::SpriteRenderer::DrawHUDBar(const glm::vec3 color, const glm::mat4 modelMatrix, const float progress, GLuint program) { + + Spaceship* spaceship = Spaceship::getInstance(); + + glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix(); + + // Kombinuj macierze transformacji + glm::mat4 transformation = viewProjectionMatrix * modelMatrix; + + // Przekaż macierze do shadera + glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, glm::value_ptr(transformation)); + glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniform3fv(glGetUniformLocation(program, "activeColor"), 1, glm::value_ptr(color)); + glUniform1f(glGetUniformLocation(program, "progress"), progress); + + glBindVertexArray(this->VAO); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); +}; void Core::SpriteRenderer::DrawSpriteBar(const glm::vec3 color, const glm::mat4 modelMatrix,const float progress, GLuint program) { Spaceship* spaceship = Spaceship::getInstance(); diff --git a/grk/project/SpriteRenderer.h b/grk/project/SpriteRenderer.h index ca15691..2fdd1ff 100644 --- a/grk/project/SpriteRenderer.h +++ b/grk/project/SpriteRenderer.h @@ -11,6 +11,7 @@ namespace Core { ~SpriteRenderer(); void DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, GLuint program); void DrawSpriteBar(const glm::vec3 color, const glm::mat4 modelMatrix,const float progress, GLuint program); + void DrawHUDBar(const glm::vec3 color, const glm::mat4 modelMatrix, const float progress, GLuint program); private: unsigned int VAO; diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index eebf9aa..2db0268 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -95,7 +95,12 @@ void updateDeltaTime(float time) { lastTime = time; } void renderEnemies() { + glDisable(GL_DEPTH_TEST); glUseProgram(programSpriteBar); + glm::mat4 spaceshipModelMatrix = spaceship->calculateModelMatrix(); + glm::mat4 healthBarPosition = glm::translate(spaceshipModelMatrix, glm::vec3(12.0f, -9.0f, 0.0f)); + spriteRenderer->DrawHUDBar(glm::vec3(0.0f, 1.0f, 0.0f), glm::scale(healthBarPosition, glm::vec3(9.0f, 1.1f, 1.0f)), 1.f, programSpriteBar); + glEnable(GL_DEPTH_TEST); for (const auto& enemy : enemies) { if (enemy->isAlive()) { spriteRenderer->DrawSpriteBar( From 1f09c40bb5b8bc69255f29cee5c69f4fc5c66935 Mon Sep 17 00:00:00 2001 From: s473577 Date: Sat, 3 Feb 2024 20:20:01 +0100 Subject: [PATCH 3/4] Add collision detection and taking dmg --- grk/project/Bullet.h | 42 ++++++++++++++++- grk/project/Enemy.h | 29 ++++++++---- grk/project/GameEntity.h | 23 +++++++++ grk/project/Spaceship.h | 19 ++++++-- grk/project/grk-project.vcxproj | 1 + grk/project/grk-project.vcxproj.filters | 3 ++ grk/project/src/ex_9_1.hpp | 39 +++++++++++----- grk/project/src/ex_9_1.hpp.orig | 62 ++++++++++++++++--------- 8 files changed, 169 insertions(+), 49 deletions(-) create mode 100644 grk/project/GameEntity.h diff --git a/grk/project/Bullet.h b/grk/project/Bullet.h index ec40915..bbfea5b 100644 --- a/grk/project/Bullet.h +++ b/grk/project/Bullet.h @@ -1,5 +1,7 @@ #include "glm.hpp" #include "src/Shader_Loader.h" +#include +#include "GameEntity.h" #pragma once class Bullet @@ -39,14 +41,52 @@ public: return new Bullet(10, 10, directionNormalized, startPosition, birthTime, simpleRenderContext, 0.01f); } - bool shouldBeDestroyed(float time, GLuint program) { + bool shouldBeDestroyed(float time, GLuint program, std::vector& gameEntities, float attackerDmg) { float age = getAge(time); if (age > lifetime) { return true; } glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), startPosition) * glm::translate(directionNormalized * speed * age) * glm::scale(glm::vec3(scale)); Core::drawObjectPBR(renderContext, modelMatrix, glm::vec3(1.f, 0.f, 0.f), 0.3, 0, program); + //std::cout << "x: " << modelMatrix[3].x << std::endl; + //std::cout << "y: " << modelMatrix[3].y << std::endl; + //std::cout << "z: " << modelMatrix[3].z << std::endl; + if (checkCollisionWithGameEntities(gameEntities, modelMatrix, attackerDmg)) { + return true; + } + + return false; } + bool checkCollisionWithGameEntities(std::vector& gameEntities, glm::mat4 bulletModelMatrix, float attackerDmg) { + for (const auto& entity : gameEntities) { + glm::mat4 entityModelMatrix = entity->getModelMatrix(); + + // Sprawdź kolizję AABB między pociskiem a obiektem z wektora gameEntities + if (checkAABBCollision(bulletModelMatrix, entityModelMatrix)) { + entity->applyDamage(attackerDmg); + return true; + } + } + + return false; + } + bool checkAABBCollision(const glm::mat4& obj1ModelMatrix, const glm::mat4& obj2ModelMatrix) { + // Pobierz rozmiary obiektów z ich macierzy modelu + glm::vec3 obj1Min = glm::vec3(obj1ModelMatrix * glm::vec4(-0.5f, -0.5f, -0.5f, 1.0f)); + glm::vec3 obj1Max = glm::vec3(obj1ModelMatrix * glm::vec4(0.5f, 0.5f, 0.5f, 1.0f)); + + glm::vec3 obj2Min = glm::vec3(obj2ModelMatrix * glm::vec4(-0.5f, -0.5f, -0.5f, 1.0f)); + glm::vec3 obj2Max = glm::vec3(obj2ModelMatrix * glm::vec4(0.5f, 0.5f, 0.5f, 1.0f)); + + // Sprawdź kolizję wzdłuż trzech osi (x, y, z) + bool collisionX = obj1Max.x >= obj2Min.x && obj1Min.x <= obj2Max.x; + bool collisionY = obj1Max.y >= obj2Min.y && obj1Min.y <= obj2Max.y; + bool collisionZ = obj1Max.z >= obj2Min.z && obj1Min.z <= obj2Max.z; + + // Kolizja występuje tylko wtedy, gdy zachodzi kolizja na wszystkich trzech osiach + return collisionX && collisionY && collisionZ; + } + }; diff --git a/grk/project/Enemy.h b/grk/project/Enemy.h index bef66a5..24b3ed1 100644 --- a/grk/project/Enemy.h +++ b/grk/project/Enemy.h @@ -2,10 +2,11 @@ #include "ext.hpp" #include "src/Render_Utils.h" #include "Bullet.h" +#include "./GameEntity.h" #include "Spaceship.h" #pragma once -class Enemy +class Enemy : public GameEntity { private: std::list bullets; @@ -14,13 +15,13 @@ private: public: glm::mat4 modelMatrix; - int hp; - int initialHp; - int dmg; float aggroRange; - Enemy(int currHp,int initialHp, glm::mat4 initialModelMatrix, int initialDmg, float initialAggroRange) - : hp(currHp), initialHp(initialHp), modelMatrix(initialModelMatrix), dmg(initialDmg), aggroRange(initialAggroRange) + Enemy(float currHp,float initialHp, glm::mat4 initialModelMatrix, float initialDmg, float initialAggroRange) + : + aggroRange(initialAggroRange), + modelMatrix(initialModelMatrix), + GameEntity(currHp, initialHp, initialDmg) {} @@ -34,7 +35,7 @@ public: } virtual bool isAlive() const { - if (this->hp < 0) { + if (this->currentHP <= 0) { return false; } return true; @@ -48,11 +49,11 @@ public: return true; } - void renderBullets(float time, GLuint program) { + void renderBullets(float time, GLuint program, std::vector& gameEntities) { for (auto it = bullets.begin(); it != bullets.end();) { Bullet* bullet = *it; - if (bullet->shouldBeDestroyed(time, program)) { + if (bullet->shouldBeDestroyed(time, program, gameEntities, dmg)) { delete bullet; it = bullets.erase(it); } @@ -61,6 +62,15 @@ public: } } } + glm::vec3 getPosition() const override + { + return modelMatrix[3]; + } + glm::mat4 getModelMatrix() override + { + return modelMatrix; + } + private: void requestShoot(float time) { if (canShoot(time)) { @@ -75,6 +85,7 @@ private: void shoot(float time) { Spaceship* spaceship = Spaceship::getInstance(); glm::vec3 bulletDirection = glm::normalize(spaceship->spaceshipPos - glm::vec3(modelMatrix[3])); + this->bullets.push_back(Bullet::createSimpleBullet(bulletDirection, this->modelMatrix[3], time)); this->lastShootTime = time; } diff --git a/grk/project/GameEntity.h b/grk/project/GameEntity.h new file mode 100644 index 0000000..7a5c189 --- /dev/null +++ b/grk/project/GameEntity.h @@ -0,0 +1,23 @@ +#include "glm.hpp" + +#pragma once +class GameEntity +{ +public: + float currentHP; + float dmg; + float maxHP; + + GameEntity(float currentHP, float maxHP, float initialDmg) + : currentHP(currentHP), maxHP(maxHP), dmg(initialDmg) + { + + } + + virtual void applyDamage(float attackerDmg) { + currentHP = currentHP - attackerDmg; + }; + virtual glm::vec3 getPosition() const = 0; + virtual glm::mat4 getModelMatrix() = 0; +}; + diff --git a/grk/project/Spaceship.h b/grk/project/Spaceship.h index 2e1a0e4..06579a4 100644 --- a/grk/project/Spaceship.h +++ b/grk/project/Spaceship.h @@ -4,10 +4,11 @@ #include #include "Bullet.h" #include "ParticleSystem.h" +#include "GameEntity.h" #pragma once -class Spaceship +class Spaceship : public GameEntity { private: std::list bullets; @@ -16,8 +17,9 @@ private: ParticleSystem* leftParticle; ParticleSystem* rightParticle; - // Prywatny konstruktor, aby zapobiec zewnętrznemu tworzeniu instancji - Spaceship() { + // Prywatny konstruktor, aby zapobiec zewnďż˝trznemu tworzeniu instancji + Spaceship() : GameEntity(100.0f, 100.0f, 10.0f) + { } @@ -164,11 +166,11 @@ public: return cameraMatrix; } - void renderBullets(float time, GLuint program) { + void renderBullets(float time, GLuint program, std::vector& gameEntities) { for (auto it = bullets.begin(); it != bullets.end();) { Bullet* bullet = *it; - if (bullet->shouldBeDestroyed(time, program)) { + if (bullet->shouldBeDestroyed(time, program, gameEntities, dmg)) { delete bullet; it = bullets.erase(it); } @@ -194,4 +196,11 @@ public: bullets.push_back(Bullet::createSimpleBullet(cameraDir, spaceshipPos + perpendicularVector, time)); lastShootTime = time; } + + glm::vec3 getPosition() const override { + return spaceshipPos; + } + glm::mat4 getModelMatrix() override { + return calculateModelMatrix(); + } }; \ No newline at end of file diff --git a/grk/project/grk-project.vcxproj b/grk/project/grk-project.vcxproj index 172044f..e1e7e9f 100644 --- a/grk/project/grk-project.vcxproj +++ b/grk/project/grk-project.vcxproj @@ -29,6 +29,7 @@ + diff --git a/grk/project/grk-project.vcxproj.filters b/grk/project/grk-project.vcxproj.filters index 406cd86..d489eb7 100644 --- a/grk/project/grk-project.vcxproj.filters +++ b/grk/project/grk-project.vcxproj.filters @@ -128,6 +128,9 @@ Header Files + + Header Files + diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index 2db0268..03f09ca 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -70,7 +70,7 @@ Sun* sun; GLuint VAO,VBO; std::vector enemies; - +std::vector gameEntities; float exposition = 1.f; @@ -94,13 +94,21 @@ void updateDeltaTime(float time) { if (deltaTime > 0.1) deltaTime = 0.1; lastTime = time; } -void renderEnemies() { - glDisable(GL_DEPTH_TEST); +void renderHUD() { glUseProgram(programSpriteBar); + glDisable(GL_DEPTH_TEST); glm::mat4 spaceshipModelMatrix = spaceship->calculateModelMatrix(); glm::mat4 healthBarPosition = glm::translate(spaceshipModelMatrix, glm::vec3(12.0f, -9.0f, 0.0f)); - spriteRenderer->DrawHUDBar(glm::vec3(0.0f, 1.0f, 0.0f), glm::scale(healthBarPosition, glm::vec3(9.0f, 1.1f, 1.0f)), 1.f, programSpriteBar); + spriteRenderer->DrawHUDBar( + glm::vec3(0.0f, 1.0f, 0.0f), + glm::scale(healthBarPosition, glm::vec3(9.0f, 1.1f, 1.0f)), + spaceship->currentHP / spaceship->maxHP, + programSpriteBar); glEnable(GL_DEPTH_TEST); +} +void renderEnemies() { + + glUseProgram(programSpriteBar); for (const auto& enemy : enemies) { if (enemy->isAlive()) { spriteRenderer->DrawSpriteBar( @@ -110,12 +118,13 @@ void renderEnemies() { ), glm::vec3(1.0f, 0.2f, 1.0f) ), - static_cast(enemy->hp / enemy->initialHp), + enemy->currentHP / enemy->maxHP, programSpriteBar); } } glUseProgram(programSprite); for (const auto& enemy : enemies) { + if (enemy->isAlive()) { spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite); } @@ -125,7 +134,7 @@ void renderEnemies() { for (const auto& enemy : enemies) { if (enemy->isAlive()) { enemy->attack(spaceship->spaceshipPos, glfwGetTime()); - enemy->renderBullets(glfwGetTime(), program); + enemy->renderBullets(glfwGetTime(), program, gameEntities); } } @@ -174,9 +183,8 @@ void renderScene(GLFWwindow* window) glUseProgram(program); - spaceship->renderBullets(glfwGetTime(), program); - - + std::vector gameEntities(enemies.begin(), enemies.end()); + spaceship->renderBullets(glfwGetTime(), program, gameEntities); drawObjectPBR(models::sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::translate(glm::vec3(1.f, 0, 0)) * glm::scale(glm::vec3(0.1f)), @@ -190,6 +198,7 @@ void renderScene(GLFWwindow* window) texture::spaceshipTexture, spaceship->roughness, spaceship->metallic, programTex ); +<<<<<<< HEAD <<<<<<< HEAD //Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); @@ -219,6 +228,9 @@ void renderScene(GLFWwindow* window) glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp); spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); ======= +======= + renderHUD(); +>>>>>>> 9586849 (Add collision detection and taking dmg) renderEnemies(); //drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program); @@ -268,9 +280,12 @@ void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int void createEnemies() { - enemies.push_back(new Enemy(100,100, glm::mat4(1.0f), 20, 5.0f)); - enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 5.0f)); - enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 25, 5.0f)); + enemies.push_back(new Enemy(100.0f,100.0f, glm::mat4(1.0f), 1.0f, 5.0f)); + enemies.push_back(new Enemy(100.0f,100.0f, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 1.0f, 5.0f)); + enemies.push_back(new Enemy(100.0f,100.0f, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 1.0f, 5.0f)); + + //obiekty do ktorych bedzie sprawdzana kolizja dla pociskow enemy + gameEntities.push_back(spaceship); } diff --git a/grk/project/src/ex_9_1.hpp.orig b/grk/project/src/ex_9_1.hpp.orig index db43d6d..ac08dc5 100644 --- a/grk/project/src/ex_9_1.hpp.orig +++ b/grk/project/src/ex_9_1.hpp.orig @@ -61,19 +61,16 @@ GLuint programSun; GLuint programTest; GLuint programSprite; GLuint programCubemap; -<<<<<<< HEAD GLuint programParticle; GLuint programTex; -======= GLuint programSpriteBar; ->>>>>>> 6ccdec5 (Add drawing health bars for enemies) std::list planets; Sun* sun; GLuint VAO,VBO; std::vector enemies; - +std::vector gameEntities; float exposition = 1.f; @@ -97,7 +94,20 @@ void updateDeltaTime(float time) { if (deltaTime > 0.1) deltaTime = 0.1; lastTime = time; } +void renderHUD() { + glUseProgram(programSpriteBar); + glDisable(GL_DEPTH_TEST); + glm::mat4 spaceshipModelMatrix = spaceship->calculateModelMatrix(); + glm::mat4 healthBarPosition = glm::translate(spaceshipModelMatrix, glm::vec3(12.0f, -9.0f, 0.0f)); + spriteRenderer->DrawHUDBar( + glm::vec3(0.0f, 1.0f, 0.0f), + glm::scale(healthBarPosition, glm::vec3(9.0f, 1.1f, 1.0f)), + spaceship->currentHP / spaceship->maxHP, + programSpriteBar); + glEnable(GL_DEPTH_TEST); +} void renderEnemies() { + glUseProgram(programSpriteBar); for (const auto& enemy : enemies) { if (enemy->isAlive()) { @@ -108,24 +118,13 @@ void renderEnemies() { ), glm::vec3(1.0f, 0.2f, 1.0f) ), - static_cast(enemy->hp / enemy->initialHp), + enemy->currentHP / enemy->maxHP, programSpriteBar); } - -<<<<<<< HEAD -TextureTuple getRandomPlanetTexture() { - int textureIndex = rand() % planetTextures.size(); - TextureTuple selectedTextures = planetTextures[textureIndex]; - //GLuint textureID = selectedTextures.textureID; - //GLuint normalMapID = selectedTextures.normalMapID; - - return planetTextures[textureIndex]; -} - -======= } glUseProgram(programSprite); for (const auto& enemy : enemies) { + if (enemy->isAlive()) { spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite); } @@ -135,13 +134,20 @@ TextureTuple getRandomPlanetTexture() { for (const auto& enemy : enemies) { if (enemy->isAlive()) { enemy->attack(spaceship->spaceshipPos, glfwGetTime()); - enemy->renderBullets(glfwGetTime(), program); + enemy->renderBullets(glfwGetTime(), program, gameEntities); } } } ->>>>>>> 6ccdec5 (Add drawing health bars for enemies) +TextureTuple getRandomPlanetTexture() { + int textureIndex = rand() % planetTextures.size(); + TextureTuple selectedTextures = planetTextures[textureIndex]; + //GLuint textureID = selectedTextures.textureID; + //GLuint normalMapID = selectedTextures.normalMapID; + + return planetTextures[textureIndex]; +} void renderShadowapSun() { float time = glfwGetTime(); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); @@ -174,10 +180,15 @@ void renderScene(GLFWwindow* window) for (Planet* p : planets) { p->draw(time, programTex); } +<<<<<<< HEAD glUseProgram(program); spaceship->renderBullets(glfwGetTime(), program); +======= + std::vector gameEntities(enemies.begin(), enemies.end()); + spaceship->renderBullets(glfwGetTime(), program, gameEntities); +>>>>>>> 9586849 (Add collision detection and taking dmg) @@ -193,6 +204,7 @@ void renderScene(GLFWwindow* window) texture::spaceshipTexture, spaceship->roughness, spaceship->metallic, programTex ); +<<<<<<< HEAD <<<<<<< HEAD //Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); @@ -222,6 +234,9 @@ void renderScene(GLFWwindow* window) glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp); spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); ======= +======= + renderHUD(); +>>>>>>> 9586849 (Add collision detection and taking dmg) renderEnemies(); //drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program); @@ -271,9 +286,12 @@ void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int void createEnemies() { - enemies.push_back(new Enemy(100,100, glm::mat4(1.0f), 20, 5.0f)); - enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 5.0f)); - enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 25, 5.0f)); + enemies.push_back(new Enemy(100.0f,100.0f, glm::mat4(1.0f), 1.0f, 5.0f)); + enemies.push_back(new Enemy(100.0f,100.0f, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 1.0f, 5.0f)); + enemies.push_back(new Enemy(100.0f,100.0f, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 1.0f, 5.0f)); + + //obiekty do ktorych bedzie sprawdzana kolizja dla pociskow enemy + gameEntities.push_back(spaceship); } From 61170baa47da595e26d9b7e497a70470474baeaa Mon Sep 17 00:00:00 2001 From: s473577 Date: Sat, 3 Feb 2024 21:50:55 +0100 Subject: [PATCH 4/4] Adjust hud postion to the new scale --- grk/project/Enemy.h | 2 +- grk/project/src/ex_9_1.hpp | 43 +++++++++++++++++--------------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/grk/project/Enemy.h b/grk/project/Enemy.h index 24b3ed1..6249fc7 100644 --- a/grk/project/Enemy.h +++ b/grk/project/Enemy.h @@ -85,7 +85,7 @@ private: void shoot(float time) { Spaceship* spaceship = Spaceship::getInstance(); glm::vec3 bulletDirection = glm::normalize(spaceship->spaceshipPos - glm::vec3(modelMatrix[3])); - + //bulletDirection += glm::linearRand(glm::vec3(-0.01f), glm::vec3(0.1f)); // Modyfikacja kierunku o losowy szum this->bullets.push_back(Bullet::createSimpleBullet(bulletDirection, this->modelMatrix[3], time)); this->lastShootTime = time; } diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index 03f09ca..346e6a9 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -98,10 +98,10 @@ void renderHUD() { glUseProgram(programSpriteBar); glDisable(GL_DEPTH_TEST); glm::mat4 spaceshipModelMatrix = spaceship->calculateModelMatrix(); - glm::mat4 healthBarPosition = glm::translate(spaceshipModelMatrix, glm::vec3(12.0f, -9.0f, 0.0f)); + glm::mat4 healthBarPosition = glm::translate(spaceshipModelMatrix, glm::vec3(36.0f, -34.0f, 0.0f)); spriteRenderer->DrawHUDBar( glm::vec3(0.0f, 1.0f, 0.0f), - glm::scale(healthBarPosition, glm::vec3(9.0f, 1.1f, 1.0f)), + glm::scale(healthBarPosition, glm::vec3(26.0f, 4.f, 1.0f)), spaceship->currentHP / spaceship->maxHP, programSpriteBar); glEnable(GL_DEPTH_TEST); @@ -198,44 +198,40 @@ void renderScene(GLFWwindow* window) texture::spaceshipTexture, spaceship->roughness, spaceship->metallic, programTex ); -<<<<<<< HEAD -<<<<<<< HEAD //Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); drawObjectPBRTexture(models::sphereContext, glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(1.0f)), texture::earthTexture, 0.3, 0.0, programTex); //glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); drawObjectPBRTexture(models::sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), texture::earthTexture, 0.3, 0.0, programTex); - glUseProgram(programSprite); - for (const auto& enemy : enemies) { - if (enemy->isAlive()) { - spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite); - } + //glUseProgram(programSprite); + //for (const auto& enemy : enemies) { + // if (enemy->isAlive()) { + // spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite); + // } - } - glUseProgram(program); - for (const auto& enemy : enemies) { - if (enemy->isAlive()) { - enemy->attack(spaceship->spaceshipPos, glfwGetTime()); - enemy->renderBullets(glfwGetTime(), program); - } + //} + //glUseProgram(program); + //for (const auto& enemy : enemies) { + // if (enemy->isAlive()) { + // enemy->attack(spaceship->spaceshipPos, glfwGetTime()); + // enemy->renderBullets(glfwGetTime(), program, gameEntities); + // } - } + //} glm::vec3 cameraSide = glm::normalize(glm::cross(spaceship->cameraDir, glm::vec3(0.f, 1.f, 0.f))); glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, spaceship->cameraDir)); glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp); spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); -======= -======= + + renderHUD(); ->>>>>>> 9586849 (Add collision detection and taking dmg) renderEnemies(); //drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program); ->>>>>>> 6ccdec5 (Add drawing health bars for enemies) glUseProgram(0); glfwSwapBuffers(window); @@ -319,12 +315,11 @@ void init(GLFWwindow* window) programTest = gameUtils->shaderLoader->CreateProgram("shaders/test.vert", "shaders/test.frag"); programSun = gameUtils->shaderLoader->CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); -<<<<<<< HEAD + programTex = gameUtils->shaderLoader->CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag"); -======= + programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); programSpriteBar = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite_bar.vert", "shaders/shader_sprite_bar.frag"); ->>>>>>> 6ccdec5 (Add drawing health bars for enemies) programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag");