diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..55083f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +################################################################################ +# Ten plik .gitignore zostaĹ‚ utworzony automatycznie przez Microsoft(R) Visual Studio. +################################################################################ + +/grk/project/shaders diff --git a/grk/.vs/grk-cw/v16/Browse.VC.db b/grk/.vs/grk-cw/v16/Browse.VC.db index a885c17..f14a9ad 100644 Binary files a/grk/.vs/grk-cw/v16/Browse.VC.db and b/grk/.vs/grk-cw/v16/Browse.VC.db differ diff --git a/grk/project/Debug/Render_Utils.obj b/grk/project/Debug/Render_Utils.obj index 079a325..55c3ff6 100644 Binary files a/grk/project/Debug/Render_Utils.obj and b/grk/project/Debug/Render_Utils.obj differ diff --git a/grk/project/Debug/Texture.obj b/grk/project/Debug/Texture.obj index 3006f39..57f70e5 100644 Binary files a/grk/project/Debug/Texture.obj and b/grk/project/Debug/Texture.obj differ diff --git a/grk/project/Debug/main.obj b/grk/project/Debug/main.obj index b3f5b28..024a7f1 100644 Binary files a/grk/project/Debug/main.obj and b/grk/project/Debug/main.obj differ diff --git a/grk/project/Enemy.h b/grk/project/Enemy.h new file mode 100644 index 0000000..54da607 --- /dev/null +++ b/grk/project/Enemy.h @@ -0,0 +1,81 @@ +#include "glm.hpp" +#include "ext.hpp" +#include "src/Render_Utils.h" +#include "Bullet.h" +#include "Spaceship.h" + +#pragma once +class Enemy +{ +private: + std::list bullets; + float lastShootTime = 0.f; + float shootInterval = 0.3f; + +public: + glm::mat4 modelMatrix; + int hp; + int dmg; + float aggroRange; + + Enemy(int initialHp, glm::mat4 initialModelMatrix, int initialDmg, float initialAggroRange) + : hp(initialHp), modelMatrix(initialModelMatrix), dmg(initialDmg), aggroRange(initialAggroRange) + {} + + + virtual ~Enemy() = default; + + virtual void attack(const glm::vec3& shipPos, float time) { + + if (isInAggroRange(shipPos)) { + requestShoot(time); + } + } + + virtual bool isAlive() const { + if (this->hp < 0) { + return false; + } + return true; + } + + virtual bool isInAggroRange(const glm::vec3& shipPos) const { + float distanceFromShip = glm::length(shipPos - glm::vec3(this->modelMatrix[3])); + if (distanceFromShip > this->aggroRange) { + return false; + } + return true; + } + + void renderBullets(float time, GLuint program) { + for (auto it = bullets.begin(); it != bullets.end();) { + Bullet* bullet = *it; + + if (bullet->shouldBeDestroyed(time, program)) { + delete bullet; + it = bullets.erase(it); + } + else { + it++; + } + } + } +private: + void requestShoot(float time) { + if (canShoot(time)) { + shoot(time); + } + } + + bool canShoot(float time) { + return this->lastShootTime == 0 || time - this->lastShootTime > this->shootInterval; + } + + 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/SpriteRenderer.cpp b/grk/project/SpriteRenderer.cpp new file mode 100644 index 0000000..f27e154 --- /dev/null +++ b/grk/project/SpriteRenderer.cpp @@ -0,0 +1,91 @@ +#include "SpriteRenderer.h" +#include + +Core::SpriteRenderer::SpriteRenderer() +{ + this->initRenderData(); +} + +Core::SpriteRenderer::~SpriteRenderer() +{ + glDeleteVertexArrays(1, &this ->VAO); + glDeleteBuffers(1, &this->VBO); + glDeleteBuffers(1, &this->EBO); +} +void Core::SpriteRenderer::DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, 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)); + Core::SetActiveTexture(spriteTexture, "colorTexture", program, 0); + + glBindVertexArray(this->VAO); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); +} + +void Core::SpriteRenderer::initRenderData() +{ + // Definicja wierzchołków kwadratu + float vertices[] = { + // Postions //colors // texture coordiantes + -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Left down + -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f, // Left up + 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,// Right up + 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,0.0f// Right down + }; + + // Definicja indeksów wierzchołków dla kwadratu + unsigned int indices[] = { + 0, 2, 1, // Upper triangle + 0, 3, 2 // Lower triangle + }; + + glGenVertexArrays(1, &this->VAO); + glGenBuffers(1, &this->VBO); + glGenBuffers(1, &this->EBO); + + glBindVertexArray(this->VAO); + + // Wypełnij bufor wierzchołków + glBindBuffer(GL_ARRAY_BUFFER, this->VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + // Wypełnij bufor indeksów + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + + glEnableVertexAttribArray(1); + // Konfiguracja atrybutu koordynatow kolorow + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); + + glEnableVertexAttribArray(2); + // Konfiguracja atrybutu koordynatow tekstury + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); + + glBindVertexArray(0); +} \ No newline at end of file diff --git a/grk/project/SpriteRenderer.h b/grk/project/SpriteRenderer.h new file mode 100644 index 0000000..b25f0af --- /dev/null +++ b/grk/project/SpriteRenderer.h @@ -0,0 +1,26 @@ +#include "src/Render_Utils.h" +#include "src/Texture.h" +#include "Spaceship.h" + +#pragma once +namespace Core { + class SpriteRenderer + { + public: + SpriteRenderer(); + ~SpriteRenderer(); + void DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, GLuint program); + + private: + + unsigned int VAO; + unsigned int VBO; + unsigned int EBO; + // Initializes and configures the quad's buffer and vertex attributes + void initRenderData(); + + + + }; +} + diff --git a/grk/project/grk-project.vcxproj b/grk/project/grk-project.vcxproj index 16a703f..abb491b 100644 --- a/grk/project/grk-project.vcxproj +++ b/grk/project/grk-project.vcxproj @@ -13,6 +13,7 @@ + @@ -26,10 +27,12 @@ + + @@ -53,6 +56,8 @@ + + diff --git a/grk/project/grk-project.vcxproj.filters b/grk/project/grk-project.vcxproj.filters index c8486a9..8109db6 100644 --- a/grk/project/grk-project.vcxproj.filters +++ b/grk/project/grk-project.vcxproj.filters @@ -57,6 +57,9 @@ Source Files + + Source Files + @@ -113,6 +116,12 @@ Header Files + + Header Files + + + Header Files + @@ -139,5 +148,13 @@ Shader Files + + + + Shader Files + + + Shader Files + \ No newline at end of file diff --git a/grk/project/shaders/shader_9_1.frag b/grk/project/shaders/shader_9_1.frag index e208a8e..39d0d36 100644 --- a/grk/project/shaders/shader_9_1.frag +++ b/grk/project/shaders/shader_9_1.frag @@ -118,6 +118,6 @@ void main() vec3 attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2); ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor,normal,viewDir); - + outColor = vec4(vec3(1.0) - exp(-ilumination*exposition),1); } diff --git a/grk/project/shaders/shader_9_1.frag.orig b/grk/project/shaders/shader_9_1.frag.orig new file mode 100644 index 0000000..b4207a1 --- /dev/null +++ b/grk/project/shaders/shader_9_1.frag.orig @@ -0,0 +1,129 @@ +#version 430 core + +float AMBIENT = 0.03; +float PI = 3.14; + +uniform sampler2D depthMap; + +uniform vec3 cameraPos; + +uniform vec3 color; + +uniform vec3 lightPositions[100]; +uniform vec3 lightColors[100]; + +uniform vec3 spotlightPos; +uniform vec3 spotlightColor; +uniform vec3 spotlightConeDir; +uniform vec3 spotlightPhi; + +uniform float metallic; +uniform float roughness; + +uniform float exposition; + +in vec3 vecNormal; +in vec3 worldPos; + +out vec4 outColor; + + +in vec3 viewDirTS; +in vec3 lightDirTS[4]; +in vec3 spotlightDirTS; +in vec3 sunDirTS; + +in vec3 test; + +float DistributionGGX(vec3 normal, vec3 H, float roughness){ + float a = roughness*roughness; + float a2 = a*a; + float NdotH = max(dot(normal, H), 0.0); + float NdotH2 = NdotH*NdotH; + + float num = a2; + float denom = (NdotH2 * (a2 - 1.0) + 1.0); + denom = PI * denom * denom; + + return num / denom; +} +float GeometrySchlickGGX(float NdotV, float roughness){ + float r = (roughness + 1.0); + float k = (r*r) / 8.0; + + float num = NdotV; + float denom = NdotV * (1.0 - k) + k; + + return num / denom; +} +float GeometrySmith(vec3 normal, vec3 V, vec3 lightDir, float roughness){ + float NdotV = max(dot(normal, V), 0.0); + float NdotL = max(dot(normal, lightDir), 0.0); + float ggx2 = GeometrySchlickGGX(NdotV, roughness); + float ggx1 = GeometrySchlickGGX(NdotL, roughness); + + return ggx1 * ggx2; +} +vec3 fresnelSchlick(float cosTheta, vec3 F0){ + return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); +} + +vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V){ + float diffuse=max(0,dot(normal,lightDir)); + + vec3 F0 = vec3(0.04); + F0 = mix(F0, color, metallic); + + vec3 H = normalize(V + lightDir); + + float NDF = DistributionGGX(normal, H, roughness); + float G = GeometrySmith(normal, V, lightDir, roughness); + vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); + + vec3 kS = F; + vec3 kD = vec3(1.0) - kS; + kD *= 1.0 - metallic; + + vec3 numerator = NDF * G * F; + float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, lightDir), 0.0) + 0.0001; + vec3 specular = numerator / denominator; + + float NdotL = max(dot(normal, lightDir), 0.0); + return (kD * color / PI + specular) * radiance * NdotL; +} + + +void main() +{ + vec3 normal = normalize(vecNormal); + + vec3 viewDir = normalize(cameraPos-worldPos); + + vec3 lightDirs[4]; + vec3 ambient = AMBIENT * color; + vec3 ilumination = ambient; + + for (int i = 0; i < 100; ++i) { + lightDirs[i] = normalize(lightPositions[i] - worldPos); + vec3 attenuatedlightColor = lightColors[i] / pow(length(lightPositions[i] - worldPos), 2); + ilumination = ilumination+PBRLight(lightDirs[i], attenuatedlightColor * 300, normal, viewDir); + } + + + + vec3 spotlightDir= normalize(spotlightPos-worldPos); + + + float angle_atenuation = clamp((dot(-normalize(spotlightPos-worldPos),spotlightConeDir)-0.5)*3,0,1); + vec3 attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2); + ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor,normal,viewDir); + +<<<<<<< HEAD + +======= + //sun + //ilumination=ilumination+PBRLight(sunDir,sunColor,normal,viewDir); + +>>>>>>> c0971c9 (Add enemy class) + outColor = vec4(vec3(1.0) - exp(-ilumination*exposition),1); +} diff --git a/grk/project/shaders/shader_sprite.frag b/grk/project/shaders/shader_sprite.frag new file mode 100644 index 0000000..8cf8107 --- /dev/null +++ b/grk/project/shaders/shader_sprite.frag @@ -0,0 +1,16 @@ +#version 430 core + +uniform sampler2D colorTexture; + +in vec3 worldPos; +in vec2 texCoord; +in vec3 color; + +out vec4 outColor; +void main() +{ + + outColor = texture(colorTexture, texCoord); + outColor *= vec4(color, 1.0); + +} \ No newline at end of file diff --git a/grk/project/shaders/shader_sprite.vert b/grk/project/shaders/shader_sprite.vert new file mode 100644 index 0000000..93a0e7d --- /dev/null +++ b/grk/project/shaders/shader_sprite.vert @@ -0,0 +1,21 @@ +#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; + texCoord.y = 1.0 - texCoord.y; // so that it is turned correctly + color = aColor; +} \ No newline at end of file diff --git a/grk/project/src/Texture.cpp b/grk/project/src/Texture.cpp index 0e1a352..664458d 100644 --- a/grk/project/src/Texture.cpp +++ b/grk/project/src/Texture.cpp @@ -17,7 +17,6 @@ GLuint Core::LoadTexture( const char * filepath ) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - int w, h; unsigned char* image = SOIL_load_image(filepath, &w, &h, 0, SOIL_LOAD_RGBA); diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index 0aec084..3443a46 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -21,6 +21,9 @@ #include "../Planet.h" #include "../GameObject.h" #include "../GameUtils.h" +#include "../SpriteRenderer.h" +#include "../Enemy.h" + const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; @@ -34,6 +37,7 @@ namespace models { } namespace texture { GLuint cubemapTexture; + GLuint spriteTexture; } void createGalaxy(glm::vec3 galaxyPosition); @@ -44,13 +48,16 @@ GLuint depthMap; GLuint program; GLuint programSun; GLuint programTest; -GLuint programTex; +GLuint programSprite; GLuint programCubemap; std::list planets; Sun* sun; GLuint VAO,VBO; +std::vector enemies; + + float exposition = 1.f; glm::vec3 pointlightPos = glm::vec3(0, 2, 0); @@ -61,6 +68,8 @@ float deltaTime = 0.f; Spaceship* spaceship = Spaceship::getInstance(); void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef); +Core::SpriteRenderer* spriteRenderer; + void updateDeltaTime(float time) { if (lastTime < 0) { lastTime = time; @@ -107,8 +116,6 @@ void renderScene(GLFWwindow* window) spaceship->renderBullets(glfwGetTime(), program); - //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); - 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); @@ -119,6 +126,27 @@ void renderScene(GLFWwindow* window) spaceship->roughness, spaceship->metallic, program ); + 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); + } + + } + + + + //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); glfwSwapBuffers(window); } @@ -158,9 +186,18 @@ 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)); + +} + void init(GLFWwindow* window) { GameUtils* gameUtils = GameUtils::getInstance(); + spriteRenderer = new Core::SpriteRenderer(); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glEnable(GL_DEPTH_TEST); @@ -169,12 +206,15 @@ 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"); - + programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); + loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); loadModelToContext("./models/spaceship.obj", models::spaceshipContext); loadModelToContext("./models/sphere.obj", models::sphereContext); loadModelToContext("./models/cube.obj", models::cubeContext); Core::loadModelToContext("./models/sphere.obj", GameUtils::getInstance()->sphereContext); + + texture::spriteTexture = Core::LoadTexture("textures/blinky1.png"); std::vector cubeFaces = { "bkg2_right1.png", @@ -188,11 +228,18 @@ void init(GLFWwindow* window) texture::cubemapTexture = Core::LoadCubemap(cubeFaces); createSuns(); + createEnemies(); } void shutdown(GLFWwindow* window) { GameUtils::getInstance()->shaderLoader->DeleteProgram(program); + delete spriteRenderer; + + // Dealokacja pamiÄ™ci po obiektach Enemy + for (const auto& enemy : enemies) { + delete enemy; + } } //obsluga wejscia diff --git a/grk/project/src/ex_9_1.hpp.orig b/grk/project/src/ex_9_1.hpp.orig index 2dfc9a3..328f38e 100644 --- a/grk/project/src/ex_9_1.hpp.orig +++ b/grk/project/src/ex_9_1.hpp.orig @@ -21,6 +21,9 @@ #include "../Planet.h" #include "../GameObject.h" #include "../GameUtils.h" +#include "../SpriteRenderer.h" +#include "../Enemy.h" + const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; @@ -34,21 +37,27 @@ namespace models { } namespace texture { GLuint cubemapTexture; + GLuint spriteTexture; } +void createGalaxy(glm::vec3 galaxyPosition); + GLuint depthMapFBO; GLuint depthMap; GLuint program; GLuint programSun; GLuint programTest; -GLuint programTex; +GLuint programSprite; GLuint programCubemap; std::list planets; Sun* sun; GLuint VAO,VBO; +std::vector enemies; + + float exposition = 1.f; glm::vec3 pointlightPos = glm::vec3(0, 2, 0); @@ -58,7 +67,12 @@ float lastTime = -1.f; float deltaTime = 0.f; Spaceship* spaceship = Spaceship::getInstance(); +<<<<<<< HEAD +void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef); +======= +Core::SpriteRenderer* spriteRenderer; +>>>>>>> c0971c9 (Add enemy class) void updateDeltaTime(float time) { if (lastTime < 0) { lastTime = time; @@ -105,8 +119,6 @@ void renderScene(GLFWwindow* window) spaceship->renderBullets(glfwGetTime(), program); - //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); - 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); @@ -116,6 +128,26 @@ void renderScene(GLFWwindow* window) spaceship->color, spaceship->roughness, spaceship->metallic, program ); + 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); + } + + } + + + + //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); glfwSwapBuffers(window); @@ -130,60 +162,63 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) void createSuns() { - GameUtils* gu = GameUtils::getInstance(); - sun = new Sun(programSun, models::sphereContext, glm::vec3(0, 2, 0), glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(0.9f, 0.9f, 0.7f) * 5, 1); - Planet* planet = new Planet(sun, 20.f, 0.25f, 1.f, models::sphereContext); - planets.push_back(planet); - Planet* moon = new Planet(planet, 5.f, 1.f, 0.2f, models::sphereContext); - planets.push_back(moon); - gu->getSuns()->push_back(sun); - Planet* planet2 = new Planet(sun, 50.f, 0.5f, 1.5f, models::sphereContext); - planets.push_back(planet2); - /*suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-80, 20, 50), glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(1.0f, 0.8f, 0.2f), 4.0)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(50, 40, -30), glm::vec3(-0.73633f, 0.451106, 0.023226f), glm::vec3(0.9f, 0.5f, 0.1f), 3.5)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(0, -60, 100), glm::vec3(-0.53633f, 0.551106, 0.043226f), glm::vec3(0.8f, 0.2f, 0.2f), 4.5)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(20, 80, -70), glm::vec3(0.33633f, 0.651106, 0.063226f), glm::vec3(0.5f, 0.7f, 0.9f), 3.8)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-60, -20, 30), glm::vec3(-0.23633f, 0.751106, 0.083226f), glm::vec3(0.7f, 0.2f, 0.7f), 4.2)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(70, -50, -80), glm::vec3(-0.03633f, 0.851106, 0.103226f), glm::vec3(0.1f, 0.3f, 0.9f), 3.9)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(10, 30, 60), glm::vec3(0.13633f, -0.951106, -0.123226f), glm::vec3(0.6f, 0.9f, 0.4f), 4.3)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-40, -80, -50), glm::vec3(0.33633f, -0.851106, -0.143226f), glm::vec3(0.7f, 0.5f, 0.2f), 3.7)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(90, 70, 20), glm::vec3(0.53633f, -0.751106, -0.163226f), glm::vec3(0.4f, 0.1f, 0.9f), 4.1)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-30, 10, -40), glm::vec3(0.73633f, -0.651106, -0.183226f), glm::vec3(0.8f, 0.4f, 0.1f), 4.4)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(150, -100, 80), glm::vec3(0.33633f, -0.951106, -0.143226f), glm::vec3(0.2f, 0.8f, 0.5f), 3.9)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-120, 50, -60), glm::vec3(0.73633f, 0.551106, 0.103226f), glm::vec3(0.9f, 0.2f, 0.7f), 3.6)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(0, 120, -150), glm::vec3(-0.23633f, -0.751106, 0.083226f), glm::vec3(0.5f, 0.6f, 0.9f), 3.4)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-180, -50, 120), glm::vec3(0.13633f, 0.351106, -0.003226f), glm::vec3(0.7f, 0.9f, 0.2f), 4.2)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(60, -150, 180), glm::vec3(-0.93633f, -0.451106, -0.023226f), glm::vec3(0.3f, 0.7f, 0.8f), 3.8)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(100, 80, -120), glm::vec3(0.53633f, -0.651106, 0.163226f), glm::vec3(0.8f, 0.1f, 0.4f), 4.5)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-50, -180, 150), glm::vec3(-0.33633f, 0.951106, -0.083226f), glm::vec3(0.4f, 0.8f, 0.6f), 3.5)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(200, 150, -100), glm::vec3(0.03633f, 0.151106, 0.103226f), glm::vec3(0.6f, 0.2f, 0.9f), 3.9)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-150, -100, -50), glm::vec3(0.83633f, -0.251106, -0.123226f), glm::vec3(0.7f, 0.5f, 0.8f), 4.1));*/ + createGalaxy(glm::vec3(0.f)); + createGalaxy(glm::vec3(0.f, 50.f, 200.f)); } + +void createGalaxy(glm::vec3 galaxyPosition) { + float planetsSizes[] = { 1.f, 1.5f, 0.8f, 1.2f, 0.2f }; + createSolarSystem(galaxyPosition + glm::vec3(0, 2, 0), 3, planetsSizes, 5, 15.f, 0.2f); + float planetsSizes2[] = { 0.6f, 1.1f, 0.9f }; + createSolarSystem(galaxyPosition + glm::vec3(150, 5, 0), 2, planetsSizes2, 3, 15.f, 0.2f); + float planetsSizes3[] = { 0.7f, 1.5f, 1.2f, 1.f }; + createSolarSystem(galaxyPosition + glm::vec3(-20, -30, 50), 4, planetsSizes3, 4, 20.f, 0.2f); + float planetSizes4[] = { 1.f, 0.5f }; + createSolarSystem(galaxyPosition + glm::vec3(100, 20, -50), 5, planetsSizes3, 2, 25.f, 0.2f); +} + +void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef) { + GameUtils* gu = GameUtils::getInstance(); + 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++) { + float distanceFromSum = (i + 1) * planetsDistance; + Planet* planet = new Planet(sun, distanceFromSum, i * 0.1f + planetSpeedCoef, planetSizes[i], models::sphereContext); + planets.push_back(planet); + } +} +<<<<<<< HEAD + +======= +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)); + +} +>>>>>>> c0971c9 (Add enemy class) void init(GLFWwindow* window) { GameUtils* gameUtils = GameUtils::getInstance(); + spriteRenderer = new Core::SpriteRenderer(); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glEnable(GL_DEPTH_TEST); -<<<<<<< HEAD - program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag"); - programTest = shaderLoader.CreateProgram("shaders/test.vert", "shaders/test.frag"); - programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); - programCubemap = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); - + + program = gameUtils->shaderLoader->CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.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"); + programCubemap = gameUtils->shaderLoader->CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); + programSprite = gameUtils->shaderLoader->CreateProgram("shaders/shader_sprite.vert", "shaders/shader_sprite.frag"); + loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); loadModelToContext("./models/spaceship.obj", models::spaceshipContext); loadModelToContext("./models/sphere.obj", models::sphereContext); loadModelToContext("./models/cube.obj", models::cubeContext); - - /*std::vector cubeFaces = { - "space_rt.png", - "space_lf.png", - "space_up.png", - "space_dn.png", - "space_bk.png", - "space_ft.png" - };*/ + Core::loadModelToContext("./models/sphere.obj", GameUtils::getInstance()->sphereContext); + + texture::spriteTexture = Core::LoadTexture("textures/blinky1.png"); std::vector cubeFaces = { "bkg2_right1.png", @@ -196,28 +231,19 @@ void init(GLFWwindow* window) texture::cubemapTexture = Core::LoadCubemap(cubeFaces); -======= - program = gameUtils->shaderLoader->CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.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"); - - //loadModelToContext("./models/sphere.obj", sphereContext); - //loadModelToContext("./models/spaceship.obj", spaceship.context); - - - - Core::loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); - Core::loadModelToContext("./models/spaceship.obj", models::spaceshipContext); - Core::loadModelToContext("./models/sphere.obj", models::sphereContext); - Core::loadModelToContext("./models/sphere.obj", GameUtils::getInstance()->sphereContext); ->>>>>>> 62afe67 (add shooting) - createSuns(); + createEnemies(); } void shutdown(GLFWwindow* window) { GameUtils::getInstance()->shaderLoader->DeleteProgram(program); + delete spriteRenderer; + + // Dealokacja pamiÄ™ci po obiektach Enemy + for (const auto& enemy : enemies) { + delete enemy; + } } //obsluga wejscia diff --git a/grk/project/textures/blinky1.png b/grk/project/textures/blinky1.png new file mode 100644 index 0000000..83ca8e6 Binary files /dev/null and b/grk/project/textures/blinky1.png differ