Add drawing health bars for enemies

This commit is contained in:
s473577 2024-02-01 15:11:52 +01:00
parent 057d07c3b3
commit de13bc43a4
9 changed files with 257 additions and 38 deletions

View File

@ -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)
{}

View File

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

View File

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

View File

@ -64,6 +64,8 @@
<None Include="shaders\shader_sprite.vert" />
<None Include="shaders\shader_tex.frag" />
<None Include="shaders\shader_tex.vert" />
<None Include="shaders\shader_sprite_bar.frag" />
<None Include="shaders\shader_sprite_bar.vert" />
<None Include="shaders\test.frag" />
<None Include="shaders\test.vert" />
</ItemGroup>

View File

@ -174,5 +174,11 @@
<None Include="particle.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_sprite_bar.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_sprite_bar.vert">
<Filter>Shader Files</Filter>
</None>
</ItemGroup>
</Project>

View File

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

View File

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

View File

@ -63,6 +63,7 @@ GLuint programSprite;
GLuint programCubemap;
GLuint programParticle;
GLuint programTex;
GLuint programSpriteBar;
std::list<Planet*> 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<float>(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");

View File

@ -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<TextureTuple> 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<Planet*> 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<float>(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<ParticleSystem*>* 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<std::pair<std::string, std::string>> 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();