health_bar #11

Merged
s473577 merged 4 commits from health_bar into master 2024-02-03 21:51:30 +01:00
12 changed files with 428 additions and 69 deletions

View File

@ -1,5 +1,7 @@
#include "glm.hpp" #include "glm.hpp"
#include "src/Shader_Loader.h" #include "src/Shader_Loader.h"
#include <vector>
#include "GameEntity.h"
#pragma once #pragma once
class Bullet class Bullet
@ -39,14 +41,52 @@ public:
return new Bullet(10, 10, directionNormalized, startPosition, birthTime, simpleRenderContext, 0.01f); 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<GameEntity*>& gameEntities, float attackerDmg) {
float age = getAge(time); float age = getAge(time);
if (age > lifetime) { if (age > lifetime) {
return true; return true;
} }
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), startPosition) * glm::translate(directionNormalized * speed * age) * glm::scale(glm::vec3(scale)); 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); 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; return false;
} }
bool checkCollisionWithGameEntities(std::vector<GameEntity*>& 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;
}
}; };

View File

@ -2,10 +2,11 @@
#include "ext.hpp" #include "ext.hpp"
#include "src/Render_Utils.h" #include "src/Render_Utils.h"
#include "Bullet.h" #include "Bullet.h"
#include "./GameEntity.h"
#include "Spaceship.h" #include "Spaceship.h"
#pragma once #pragma once
class Enemy class Enemy : public GameEntity
{ {
private: private:
std::list<Bullet*> bullets; std::list<Bullet*> bullets;
@ -14,12 +15,13 @@ private:
public: public:
glm::mat4 modelMatrix; glm::mat4 modelMatrix;
int hp;
int dmg;
float aggroRange; float aggroRange;
Enemy(int initialHp, glm::mat4 initialModelMatrix, int initialDmg, float initialAggroRange) Enemy(float currHp,float initialHp, glm::mat4 initialModelMatrix, float initialDmg, float initialAggroRange)
: hp(initialHp), modelMatrix(initialModelMatrix), dmg(initialDmg), aggroRange(initialAggroRange) :
aggroRange(initialAggroRange),
modelMatrix(initialModelMatrix),
GameEntity(currHp, initialHp, initialDmg)
{} {}
@ -33,7 +35,7 @@ public:
} }
virtual bool isAlive() const { virtual bool isAlive() const {
if (this->hp < 0) { if (this->currentHP <= 0) {
return false; return false;
} }
return true; return true;
@ -47,11 +49,11 @@ public:
return true; return true;
} }
void renderBullets(float time, GLuint program) { void renderBullets(float time, GLuint program, std::vector<GameEntity*>& gameEntities) {
for (auto it = bullets.begin(); it != bullets.end();) { for (auto it = bullets.begin(); it != bullets.end();) {
Bullet* bullet = *it; Bullet* bullet = *it;
if (bullet->shouldBeDestroyed(time, program)) { if (bullet->shouldBeDestroyed(time, program, gameEntities, dmg)) {
delete bullet; delete bullet;
it = bullets.erase(it); it = bullets.erase(it);
} }
@ -60,6 +62,15 @@ public:
} }
} }
} }
glm::vec3 getPosition() const override
{
return modelMatrix[3];
}
glm::mat4 getModelMatrix() override
{
return modelMatrix;
}
private: private:
void requestShoot(float time) { void requestShoot(float time) {
if (canShoot(time)) { if (canShoot(time)) {
@ -74,6 +85,7 @@ private:
void shoot(float time) { void shoot(float time) {
Spaceship* spaceship = Spaceship::getInstance(); Spaceship* spaceship = Spaceship::getInstance();
glm::vec3 bulletDirection = glm::normalize(spaceship->spaceshipPos - glm::vec3(modelMatrix[3])); 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->bullets.push_back(Bullet::createSimpleBullet(bulletDirection, this->modelMatrix[3], time));
this->lastShootTime = time; this->lastShootTime = time;
} }

23
grk/project/GameEntity.h Normal file
View File

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

View File

@ -4,10 +4,11 @@
#include <list> #include <list>
#include "Bullet.h" #include "Bullet.h"
#include "ParticleSystem.h" #include "ParticleSystem.h"
#include "GameEntity.h"
#pragma once #pragma once
class Spaceship class Spaceship : public GameEntity
{ {
private: private:
std::list<Bullet*> bullets; std::list<Bullet*> bullets;
@ -16,8 +17,9 @@ private:
ParticleSystem* leftParticle; ParticleSystem* leftParticle;
ParticleSystem* rightParticle; ParticleSystem* rightParticle;
// Prywatny konstruktor, aby zapobiec zewnêtrznemu tworzeniu instancji // Prywatny konstruktor, aby zapobiec zewn<77>trznemu tworzeniu instancji
Spaceship() { Spaceship() : GameEntity(100.0f, 100.0f, 10.0f)
{
} }
@ -164,11 +166,11 @@ public:
return cameraMatrix; return cameraMatrix;
} }
void renderBullets(float time, GLuint program) { void renderBullets(float time, GLuint program, std::vector<GameEntity*>& gameEntities) {
for (auto it = bullets.begin(); it != bullets.end();) { for (auto it = bullets.begin(); it != bullets.end();) {
Bullet* bullet = *it; Bullet* bullet = *it;
if (bullet->shouldBeDestroyed(time, program)) { if (bullet->shouldBeDestroyed(time, program, gameEntities, dmg)) {
delete bullet; delete bullet;
it = bullets.erase(it); it = bullets.erase(it);
} }
@ -194,4 +196,11 @@ public:
bullets.push_back(Bullet::createSimpleBullet(cameraDir, spaceshipPos + perpendicularVector, time)); bullets.push_back(Bullet::createSimpleBullet(cameraDir, spaceshipPos + perpendicularVector, time));
lastShootTime = time; lastShootTime = time;
} }
glm::vec3 getPosition() const override {
return spaceshipPos;
}
glm::mat4 getModelMatrix() override {
return calculateModelMatrix();
}
}; };

View File

@ -12,6 +12,58 @@ Core::SpriteRenderer::~SpriteRenderer()
glDeleteBuffers(1, &this->VBO); glDeleteBuffers(1, &this->VBO);
glDeleteBuffers(1, &this->EBO); 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();
// 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) { void Core::SpriteRenderer::DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, GLuint program) {
Spaceship* spaceship = Spaceship::getInstance(); Spaceship* spaceship = Spaceship::getInstance();

View File

@ -10,7 +10,8 @@ namespace Core {
SpriteRenderer(); SpriteRenderer();
~SpriteRenderer(); ~SpriteRenderer();
void DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, GLuint program); 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: private:
unsigned int VAO; unsigned int VAO;

View File

@ -29,6 +29,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="Bullet.h" /> <ClInclude Include="Bullet.h" />
<ClInclude Include="Enemy.h" /> <ClInclude Include="Enemy.h" />
<ClInclude Include="GameEntity.h" />
<ClInclude Include="GameObject.h" /> <ClInclude Include="GameObject.h" />
<ClInclude Include="GameUtils.h" /> <ClInclude Include="GameUtils.h" />
<ClInclude Include="ParticleSystem.h" /> <ClInclude Include="ParticleSystem.h" />
@ -64,6 +65,8 @@
<None Include="shaders\shader_sprite.vert" /> <None Include="shaders\shader_sprite.vert" />
<None Include="shaders\shader_tex.frag" /> <None Include="shaders\shader_tex.frag" />
<None Include="shaders\shader_tex.vert" /> <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.frag" />
<None Include="shaders\test.vert" /> <None Include="shaders\test.vert" />
</ItemGroup> </ItemGroup>

View File

@ -128,6 +128,9 @@
<ClInclude Include="ParticleSystem.h"> <ClInclude Include="ParticleSystem.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="GameEntity.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="shaders\shader_8_sun.vert"> <None Include="shaders\shader_8_sun.vert">
@ -174,5 +177,11 @@
<None Include="particle.vert"> <None Include="particle.vert">
<Filter>Shader Files</Filter> <Filter>Shader Files</Filter>
</None> </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> </ItemGroup>
</Project> </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,13 +63,14 @@ GLuint programSprite;
GLuint programCubemap; GLuint programCubemap;
GLuint programParticle; GLuint programParticle;
GLuint programTex; GLuint programTex;
GLuint programSpriteBar;
std::list<Planet*> planets; std::list<Planet*> planets;
Sun* sun; Sun* sun;
GLuint VAO,VBO; GLuint VAO,VBO;
std::vector<Enemy*> enemies; std::vector<Enemy*> enemies;
std::vector<GameEntity*> gameEntities;
float exposition = 1.f; float exposition = 1.f;
@ -93,7 +94,52 @@ void updateDeltaTime(float time) {
if (deltaTime > 0.1) deltaTime = 0.1; if (deltaTime > 0.1) deltaTime = 0.1;
lastTime = 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));
spriteRenderer->DrawHUDBar(
glm::vec3(0.0f, 1.0f, 0.0f),
glm::scale(healthBarPosition, glm::vec3(26.0f, 4.f, 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(
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)
),
enemy->currentHP / enemy->maxHP,
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, gameEntities);
}
}
}
TextureTuple getRandomPlanetTexture() { TextureTuple getRandomPlanetTexture() {
int textureIndex = rand() % planetTextures.size(); int textureIndex = rand() % planetTextures.size();
TextureTuple selectedTextures = planetTextures[textureIndex]; TextureTuple selectedTextures = planetTextures[textureIndex];
@ -102,7 +148,6 @@ TextureTuple getRandomPlanetTexture() {
return planetTextures[textureIndex]; return planetTextures[textureIndex];
} }
void renderShadowapSun() { void renderShadowapSun() {
float time = glfwGetTime(); float time = glfwGetTime();
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
@ -138,9 +183,8 @@ void renderScene(GLFWwindow* window)
glUseProgram(program); glUseProgram(program);
spaceship->renderBullets(glfwGetTime(), program); std::vector<GameEntity*> gameEntities(enemies.begin(), enemies.end());
spaceship->renderBullets(glfwGetTime(), program, gameEntities);
drawObjectPBR(models::sphereContext, 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::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)),
@ -161,27 +205,34 @@ void renderScene(GLFWwindow* window)
Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 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); 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); //glUseProgram(programSprite);
for (const auto& enemy : enemies) { //for (const auto& enemy : enemies) {
if (enemy->isAlive()) { // if (enemy->isAlive()) {
spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite); // spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite);
} // }
} //}
glUseProgram(program); //glUseProgram(program);
for (const auto& enemy : enemies) { //for (const auto& enemy : enemies) {
if (enemy->isAlive()) { // if (enemy->isAlive()) {
enemy->attack(spaceship->spaceshipPos, glfwGetTime()); // enemy->attack(spaceship->spaceshipPos, glfwGetTime());
enemy->renderBullets(glfwGetTime(), program); // enemy->renderBullets(glfwGetTime(), program, gameEntities);
} // }
} //}
glm::vec3 cameraSide = glm::normalize(glm::cross(spaceship->cameraDir, glm::vec3(0.f, 1.f, 0.f))); 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::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, spaceship->cameraDir));
glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp); glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp);
spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time);
renderHUD();
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);
glUseProgram(0); glUseProgram(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
@ -193,7 +244,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
HEIGHT = height; HEIGHT = height;
} }
void createSuns() { void createSuns() {
createGalaxy(glm::vec3(0.f)); createGalaxy(glm::vec3(0.f));
createGalaxy(glm::vec3(0.f, 50.f, 200.f)); createGalaxy(glm::vec3(0.f, 50.f, 200.f));
@ -226,9 +276,12 @@ void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int
void createEnemies() { void createEnemies() {
enemies.push_back(new Enemy(100, glm::mat4(1.0f), 20, 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, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 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, 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::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);
} }
@ -262,8 +315,12 @@ void init(GLFWwindow* window)
programTest = gameUtils->shaderLoader->CreateProgram("shaders/test.vert", "shaders/test.frag"); programTest = gameUtils->shaderLoader->CreateProgram("shaders/test.vert", "shaders/test.frag");
programSun = gameUtils->shaderLoader->CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.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"); programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
programTex = gameUtils->shaderLoader->CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.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");
programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag");
programParticle = gameUtils->shaderLoader->CreateProgram("shaders/particle.vert", "shaders/particle.frag"); programParticle = gameUtils->shaderLoader->CreateProgram("shaders/particle.vert", "shaders/particle.frag");

View File

@ -21,19 +21,15 @@
#include "../Planet.h" #include "../Planet.h"
#include "../GameObject.h" #include "../GameObject.h"
#include "../GameUtils.h" #include "../GameUtils.h"
<<<<<<< HEAD
#include "../SpriteRenderer.h" #include "../SpriteRenderer.h"
#include "../Enemy.h" #include "../Enemy.h"
=======
#include "../ParticleSystem.h" #include "../ParticleSystem.h"
#include "Camera.h" #include "Camera.h"
>>>>>>> 2162820 (fire from ship)
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
int WIDTH = 500, HEIGHT = 500; int WIDTH = 500, HEIGHT = 500;
namespace models { namespace models {
Core::RenderContext marbleBustContext; Core::RenderContext marbleBustContext;
Core::RenderContext spaceshipContext; Core::RenderContext spaceshipContext;
@ -42,9 +38,19 @@ namespace models {
} }
namespace texture { namespace texture {
GLuint cubemapTexture; GLuint cubemapTexture;
GLuint spaceshipTexture;
GLuint spriteTexture; GLuint spriteTexture;
GLuint earthTexture;
} }
struct TextureTuple {
GLuint textureID;
GLuint normalMapID;
};
std::vector<TextureTuple> planetTextures;
void createGalaxy(glm::vec3 galaxyPosition); void createGalaxy(glm::vec3 galaxyPosition);
GLuint depthMapFBO; GLuint depthMapFBO;
@ -56,13 +62,15 @@ GLuint programTest;
GLuint programSprite; GLuint programSprite;
GLuint programCubemap; GLuint programCubemap;
GLuint programParticle; GLuint programParticle;
GLuint programTex;
GLuint programSpriteBar;
std::list<Planet*> planets; std::list<Planet*> planets;
Sun* sun; Sun* sun;
GLuint VAO,VBO; GLuint VAO,VBO;
std::vector<Enemy*> enemies; std::vector<Enemy*> enemies;
std::vector<GameEntity*> gameEntities;
float exposition = 1.f; float exposition = 1.f;
@ -86,7 +94,60 @@ void updateDeltaTime(float time) {
if (deltaTime > 0.1) deltaTime = 0.1; if (deltaTime > 0.1) deltaTime = 0.1;
lastTime = time; 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()) {
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)
),
enemy->currentHP / enemy->maxHP,
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, gameEntities);
}
}
}
TextureTuple getRandomPlanetTexture() {
int textureIndex = rand() % planetTextures.size();
TextureTuple selectedTextures = planetTextures[textureIndex];
//GLuint textureID = selectedTextures.textureID;
//GLuint normalMapID = selectedTextures.normalMapID;
return planetTextures[textureIndex];
}
void renderShadowapSun() { void renderShadowapSun() {
float time = glfwGetTime(); float time = glfwGetTime();
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
@ -97,6 +158,7 @@ void renderShadowapSun() {
} }
void renderScene(GLFWwindow* window) void renderScene(GLFWwindow* window)
{ {
glClearColor(0.4f, 0.4f, 0.8f, 1.0f); glClearColor(0.4f, 0.4f, 0.8f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float time = glfwGetTime(); float time = glfwGetTime();
@ -114,25 +176,43 @@ void renderScene(GLFWwindow* window)
sun->draw(); sun->draw();
} }
glUseProgram(programTex);
for (Planet* p : planets) {
p->draw(time, programTex);
}
<<<<<<< HEAD
glUseProgram(program); glUseProgram(program);
for (Planet* p : planets) {
p->draw(time, program);
}
spaceship->renderBullets(glfwGetTime(), program); spaceship->renderBullets(glfwGetTime(), program);
=======
std::vector<GameEntity*> gameEntities(enemies.begin(), enemies.end());
spaceship->renderBullets(glfwGetTime(), program, gameEntities);
>>>>>>> 9586849 (Add collision detection and taking dmg)
drawObjectPBR(models::sphereContext, 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::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); 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->calculateModelMatrix(),
spaceship->color, texture::spaceshipTexture,
spaceship->roughness, spaceship->metallic, program spaceship->roughness, spaceship->metallic, programTex
); );
<<<<<<< HEAD
<<<<<<< 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); glUseProgram(programSprite);
for (const auto& enemy : enemies) { for (const auto& enemy : enemies) {
if (enemy->isAlive()) { if (enemy->isAlive()) {
@ -149,20 +229,19 @@ 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 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::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, spaceship->cameraDir));
glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp); glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp);
spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time);
/*for (auto particleSystem : *particleSystems) { =======
particleSystem->drawParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time); =======
}*/ renderHUD();
>>>>>>> 2162820 (fire from ship) >>>>>>> 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); glUseProgram(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
@ -175,7 +254,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
HEIGHT = height; HEIGHT = height;
} }
void createSuns() { void createSuns() {
createGalaxy(glm::vec3(0.f)); createGalaxy(glm::vec3(0.f));
createGalaxy(glm::vec3(0.f, 50.f, 200.f)); createGalaxy(glm::vec3(0.f, 50.f, 200.f));
@ -197,26 +275,50 @@ 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); 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); gu->getSuns()->push_back(sun);
for (int i = 0; i < numberOfPlanets; i++) { for (int i = 0; i < numberOfPlanets; i++) {
TextureTuple textures = getRandomPlanetTexture();
GLuint texID = textures.textureID;
GLuint norMapID = textures.normalMapID;
float distanceFromSum = (i + 1) * planetsDistance; 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); planets.push_back(planet);
} }
} }
void createEnemies() { void createEnemies() {
enemies.push_back(new Enemy(100, glm::mat4(1.0f), 20, 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, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 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, 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::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);
} }
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) void init(GLFWwindow* window)
{ {
GameUtils* gameUtils = GameUtils::getInstance(); GameUtils* gameUtils = GameUtils::getInstance();
spriteRenderer = new Core::SpriteRenderer(); spriteRenderer = new Core::SpriteRenderer();
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
program = gameUtils->shaderLoader->CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag"); program = gameUtils->shaderLoader->CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag");
@ -224,14 +326,18 @@ void init(GLFWwindow* window)
programSun = gameUtils->shaderLoader->CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.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"); programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
<<<<<<< HEAD <<<<<<< 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"); programParticle = gameUtils->shaderLoader->CreateProgram("shaders/particle.vert", "shaders/particle.frag");
>>>>>>> 2162820 (fire from ship)
loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); 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/sphere.obj", models::sphereContext);
loadModelToContext("./models/cube.obj", models::cubeContext); loadModelToContext("./models/cube.obj", models::cubeContext);
Core::loadModelToContext("./models/sphere.obj", GameUtils::getInstance()->sphereContext); Core::loadModelToContext("./models/sphere.obj", GameUtils::getInstance()->sphereContext);
@ -247,7 +353,10 @@ void init(GLFWwindow* window)
"bkg2_back6.png" "bkg2_back6.png"
}; };
loadPlanetsTextures();
texture::cubemapTexture = Core::LoadCubemap(cubeFaces); 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(); spaceship->createParticles();
createSuns(); createSuns();