diff --git a/cw 9/src/Laser.cpp b/cw 9/src/Laser.cpp index ac758ec..5df79b0 100644 --- a/cw 9/src/Laser.cpp +++ b/cw 9/src/Laser.cpp @@ -5,16 +5,18 @@ #include #include #include +#include "Render_Utils.h" Laser::Laser() { } -Laser::Laser(glm::vec3 dirOfLaser, glm::vec3 posOfLaser, float speedOfLaser) { - laserDir = dirOfLaser; - laserPos = posOfLaser; - laserSpeed = speedOfLaser; +Laser::Laser(glm::vec3 dirOfLaser, glm::vec3 posOfLaser, float speedOfLaser, bool lasFly) { + this->laserDir = dirOfLaser; + this->laserPos = posOfLaser; + this->laserSpeed = speedOfLaser; + this->laserFly = lasFly; } void Laser::setLaserDir(glm::vec3 dirOfLaser) { @@ -48,3 +50,19 @@ void Laser::setTimeSinceShootLaser(float tSinceShootLaser) { float Laser::getTimeSinceShootLaser() { return timeSinceShootLaser; } + +void Laser::setLaserContext(Core::RenderContext lasContext) { + this->laserContext = lasContext; +} + +Core::RenderContext Laser::getLaserContext() { + return this->laserContext; +} + +void Laser::setLaserFly(bool lasFly) { + this->laserFly = lasFly; +} + +bool Laser::isLaserFly() { + return this->laserFly; +} diff --git a/cw 9/src/Laser.h b/cw 9/src/Laser.h index 4de2a17..d605d0a 100644 --- a/cw 9/src/Laser.h +++ b/cw 9/src/Laser.h @@ -5,21 +5,23 @@ #include #include #include +#include "Render_Utils.h" class Laser { private: + Core::RenderContext laserContext; glm::vec3 laserDir; glm::vec3 laserPos; float timeSinceShootLaser; - bool isLaserFire; float laserSpeed; + bool laserFly; public: Laser(); - Laser(glm::vec3 dirOfLaser, glm::vec3 posOfLaser, float speedOfLaser); + Laser(glm::vec3 dirOfLaser, glm::vec3 posOfLaser, float speedOfLaser, bool lasFly); void setLaserDir(glm::vec3 dirOfLaser); @@ -34,5 +36,11 @@ public: void setTimeSinceShootLaser(float tSinceShootLaser); float getTimeSinceShootLaser(); + void setLaserContext(Core::RenderContext lasContext); + Core::RenderContext getLaserContext(); + + void setLaserFly(bool lasFly); + bool isLaserFly(); + }; diff --git a/cw 9/src/SpaceShip.cpp b/cw 9/src/SpaceShip.cpp index 01832a2..2df3925 100644 --- a/cw 9/src/SpaceShip.cpp +++ b/cw 9/src/SpaceShip.cpp @@ -5,14 +5,25 @@ #include #include #include +#include +#include "Laser.h" SpaceShip::SpaceShip() { } -SpaceShip::SpaceShip(bool attackDec) { - attackDecision = attackDec; +SpaceShip::SpaceShip(int numberOfLasers) { + maxNumberOfLasers = numberOfLasers; + numberOfLeftLasers = maxNumberOfLasers; + gun.reserve(maxNumberOfLasers); + + this->spaceShipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f); + this->spaceShipPos = glm::vec3(0.065808f, 1.250000f, -2.189549f); + + for (int i = 0; i < maxNumberOfLasers; i++) { + gun.push_back(Laser(this->spaceShipDir, this->spaceShipPos, 0.01, false)); + } } @@ -41,3 +52,45 @@ void SpaceShip::setSpaceShipPos(glm::vec3 spShipPos) { glm::vec3 SpaceShip::getSpaceShipPos() { return spaceShipPos; } + +void SpaceShip::setMaxNumberOfLasers(int numberOfLasers) { + this->maxNumberOfLasers = numberOfLasers; +} + +int SpaceShip::getMaxNumberOfLasers() { + return this->maxNumberOfLasers; +} + +void SpaceShip::setNumberOfLeftLasers(int numberOfLasers) { + this->numberOfLeftLasers = numberOfLasers; +} + +int SpaceShip::getNumberOfLeftLasers() { + return this->numberOfLeftLasers; +} + +void SpaceShip::setGun(std::vector g) { + this->gun = g; +} + +std::vector SpaceShip::getGun() { + return this->gun; +} + + +void SpaceShip::laserPreparationBeforeShoot(float time) { + + if (this->numberOfLeftLasers > 0) { + this->numberOfLeftLasers -= 1; + } + else if (this->numberOfLeftLasers == 0) { + this->numberOfLeftLasers = this->maxNumberOfLasers; + this->numberOfLeftLasers -= 1; + } + //this->numberOfLeftLasers -= 1; + + this->gun[this->numberOfLeftLasers].setTimeSinceShootLaser(time); + this->gun[this->numberOfLeftLasers].setLaserDir(this->spaceShipDir); + this->gun[this->numberOfLeftLasers].setLaserPos(this->spaceShipPos); + this->gun[this->numberOfLeftLasers].setLaserSpeed(0.01); +} diff --git a/cw 9/src/SpaceShip.h b/cw 9/src/SpaceShip.h index 066f7e3..954606d 100644 --- a/cw 9/src/SpaceShip.h +++ b/cw 9/src/SpaceShip.h @@ -5,20 +5,28 @@ #include #include #include +#include +#include +#include "Laser.h" class SpaceShip { private: bool attackDecision = false; - glm::vec3 spaceShipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f); - glm::vec3 spaceShipPos = glm::vec3(0.065808f, 1.250000f, -2.189549f); + glm::vec3 spaceShipDir; + glm::vec3 spaceShipPos; + + std::vector gun; + int maxNumberOfLasers; + int numberOfLeftLasers; public: SpaceShip(); - SpaceShip(bool attackDec); + SpaceShip(int numberOfLasers); + //getters and setters void setAttackDecision(bool attackDec); bool getAttackDecision(); @@ -27,5 +35,18 @@ public: void setSpaceShipPos(glm::vec3 spShipPos); glm::vec3 getSpaceShipPos(); + + void setGun(std::vector g); + std::vector getGun(); + + void setMaxNumberOfLasers(int numberOfLasers); + int getMaxNumberOfLasers(); + + void setNumberOfLeftLasers(int numberOfLasers); + int getNumberOfLeftLasers(); + + + //methods + void laserPreparationBeforeShoot(float time); }; diff --git a/cw 9/src/ex_9_1.hpp b/cw 9/src/ex_9_1.hpp index fec9f0b..eedb449 100644 --- a/cw 9/src/ex_9_1.hpp +++ b/cw 9/src/ex_9_1.hpp @@ -39,7 +39,27 @@ namespace models { Core::RenderContext testContext; - Core::RenderContext laserContext; + Core::RenderContext laserContext1; + Core::RenderContext laserContext2; + Core::RenderContext laserContext3; + Core::RenderContext laserContext4; + Core::RenderContext laserContext5; + Core::RenderContext laserContext6; + Core::RenderContext laserContext7; + Core::RenderContext laserContext8; + Core::RenderContext laserContext9; + Core::RenderContext laserContext10; + Core::RenderContext laserContext11; + Core::RenderContext laserContext12; + Core::RenderContext laserContext13; + Core::RenderContext laserContext14; + Core::RenderContext laserContext15; + Core::RenderContext laserContext16; + Core::RenderContext laserContext17; + Core::RenderContext laserContext18; + Core::RenderContext laserContext19; + Core::RenderContext laserContext20; + } GLuint depthMapFBO; @@ -59,12 +79,12 @@ Core::RenderContext sphereContext; glm::vec3 sunPos = glm::vec3(-4.740971f, 2.149999f, 0.369280f); glm::vec3 sunDir = glm::vec3(-0.93633f, 0.351106, 0.003226f); -glm::vec3 sunColor = glm::vec3(0.9f, 0.9f, 0.7f)*5; +glm::vec3 sunColor = glm::vec3(0.9f, 0.9f, 0.7f) * 5; glm::vec3 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f); glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f); -GLuint VAO,VBO; +GLuint VAO, VBO; float aspectRatio = 1.f; @@ -75,33 +95,46 @@ glm::vec3 pointlightColor = glm::vec3(0.9, 0.6, 0.6); glm::vec3 spotlightPos = glm::vec3(0, 0, 0); glm::vec3 spotlightConeDir = glm::vec3(0, 0, 0); -glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9)*3; +glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9) * 3; float spotlightPhi = 3.14 / 4; bool drawLaserShot = false; +double startCursorXPos; +double startCursorYPos; + + float lastTime = -1.f; float deltaTime = 0.f; -Laser laser = Laser(); -SpaceShip spaceShip = SpaceShip(); +Laser laser1 = Laser(); +Laser laser2 = Laser(); +SpaceShip spaceShip = SpaceShip(20); + +std::vector lasersContexts{ models::laserContext1, models::laserContext2, models::laserContext3, models::laserContext4, models::laserContext5, + models::laserContext6, models::laserContext7, models::laserContext8, models::laserContext9, models::laserContext10, + models::laserContext11, models::laserContext12, models::laserContext13, models::laserContext14, models::laserContext15, + models::laserContext16, models::laserContext17, models::laserContext18, models::laserContext19, models::laserContext20 }; + + +std::vector lasers; void updateDeltaTime(float time) { if (lastTime < 0) { lastTime = time; return; } - + deltaTime = time - lastTime; if (deltaTime > 0.1) deltaTime = 0.1; lastTime = time; } glm::mat4 createCameraMatrix() { - glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f))); - glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide,cameraDir)); + glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f))); + glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, cameraDir)); glm::mat4 cameraRotrationMatrix = glm::mat4({ cameraSide.x,cameraSide.y,cameraSide.z,0, cameraUp.x,cameraUp.y,cameraUp.z ,0, @@ -116,7 +149,7 @@ glm::mat4 createCameraMatrix() glm::mat4 createPerspectiveMatrix() { - + glm::mat4 perspectiveMatrix; float n = 0.05; float f = 20.; @@ -125,12 +158,12 @@ glm::mat4 createPerspectiveMatrix() perspectiveMatrix = glm::mat4({ 1,0.,0.,0., 0.,aspectRatio,0.,0., - 0.,0.,(f+n) / (n - f),2*f * n / (n - f), + 0.,0.,(f + n) / (n - f),2 * f * n / (n - f), 0.,0.,-1.,0., }); - - perspectiveMatrix=glm::transpose(perspectiveMatrix); + + perspectiveMatrix = glm::transpose(perspectiveMatrix); return perspectiveMatrix; } @@ -166,7 +199,7 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec } -void drawLaser() { +void drawLaser(Laser laser, Core::RenderContext laserContext, float dyst) { float time = glfwGetTime(); @@ -180,16 +213,17 @@ void drawLaser() { }); float t = time - laser.getTimeSinceShootLaser(); - + glm::vec3 newLaserPos = laser.getLaserPos() + laser.getLaserDir() * (laser.getLaserSpeed() * (t + sin(0.2 * t))); laser.setLaserPos(newLaserPos); - drawObjectPBR(models::laserContext, - glm::translate(laser.getLaserPos()) * laserCameraRotrationMatrix * glm::eulerAngleY(glm::pi()) * glm::scale(glm::vec3(0.03f)), + drawObjectPBR(laserContext, + glm::translate(glm::vec3(laser.getLaserPos().x + dyst, laser.getLaserPos().y, laser.getLaserPos().z)) * laserCameraRotrationMatrix * glm::eulerAngleY(glm::pi()) * glm::scale(glm::vec3(0.03f)), glm::vec3(1.0f, 0.f, 0.f), 0.f, 0.f); } + void renderShadowapSun() { float time = glfwGetTime(); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); @@ -239,9 +273,9 @@ void renderScene(GLFWwindow* window) drawObjectPBR(models::pencilsContext, glm::mat4(), glm::vec3(0.10039f, 0.018356f, 0.001935f), 0.1f, 0.0f); drawObjectPBR(models::planeContext, glm::mat4(), glm::vec3(0.402978f, 0.120509f, 0.057729f), 0.2f, 0.0f); drawObjectPBR(models::roomContext, glm::mat4(), glm::vec3(0.9f, 0.9f, 0.9f), 0.8f, 0.0f); - + drawObjectPBR(models::windowContext, glm::mat4(), glm::vec3(0.402978f, 0.120509f, 0.057729f), 0.2f, 0.0f); - + glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceShip.getSpaceShipDir(), glm::vec3(0.f, 1.f, 0.f))); glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceShip.getSpaceShipDir())); glm::mat4 specshipCameraRotrationMatrix = glm::mat4({ @@ -259,13 +293,31 @@ void renderScene(GLFWwindow* window) drawObjectPBR(shipContext, glm::translate(spaceShip.getSpaceShipPos()) * specshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi()) * glm::scale(glm::vec3(0.03f)), glm::vec3(0.3, 0.3, 0.5), - 0.2,1.0 + 0.2, 1.0 ); spotlightPos = spaceShip.getSpaceShipPos() + 0.2 * spaceShip.getSpaceShipDir(); spotlightConeDir = spaceShip.getSpaceShipDir(); + //draw lasers + for (int i = 0; i < lasers.size(); i++) { + drawLaser(lasers[i], lasersContexts[i], 0.1); + drawLaser(lasers[i], lasersContexts[i], -0.1); + + /*if (i % 2 == 0) { + drawLaser(lasers[i], lasersContexts[i], 0.1); + drawLaser(lasers[i], lasersContexts[i], -0.1); + + + }*/ + /*else { + drawLaser(lasers[i], lasersContexts[i], 0.1); + + }*/ + + } + //test depth buffer //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -274,10 +326,6 @@ void renderScene(GLFWwindow* window) //glBindTexture(GL_TEXTURE_2D, depthMap); //Core::DrawContext(models::testContext); - if (drawLaserShot) { - drawLaser(); - } - glUseProgram(0); glfwSwapBuffers(window); } @@ -301,10 +349,71 @@ void loadModelToContext(std::string path, Core::RenderContext& context) context.initFromAssimpMesh(scene->mMeshes[0]); } +void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) +{ + std::cout << "yStart: " << startCursorYPos << "\n"; + + float angleSpeed = 0.05f * deltaTime * 60; + + if (ypos < startCursorYPos) { + startCursorYPos = ypos; + spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleX(angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0))); + //startCursorYPos = ypos; + std::cout << "yNew: " << ypos << "\n"; + } + else if (ypos > startCursorYPos) { + startCursorYPos = ypos; + spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleX(-angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0))); + //startCursorYPos = ypos; + std::cout << "yNew: " << ypos << "\n"; + } + + + //spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleZ(angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0))); + + +} + + +void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) +{ + if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { + drawLaserShot = true; + + spaceShip.setAttackDecision(true); + /*spaceShip.laserPreparationBeforeShoot(glfwGetTime());*/ + + + for (int i = 0; i < lasers.size(); i++) { + if (!lasers[i].isLaserFly() && spaceShip.getAttackDecision()) { + lasers[i].setTimeSinceShootLaser(glfwGetTime()); + lasers[i].setLaserDir(spaceShip.getSpaceShipDir()); + lasers[i].setLaserPos(spaceShip.getSpaceShipPos()); + lasers[i].setLaserSpeed(1); + lasers[i].setLaserFly(true); + spaceShip.setAttackDecision(false); + + + std::cout << "i = " << i << " " << "laser fly = " << lasers[i].isLaserFly() << "\n"; + } + } + } + +} + + void init(GLFWwindow* window) { glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + glfwGetCursorPos(window, &startCursorXPos, &startCursorYPos); + + glfwSetMouseButtonCallback(window, mouse_button_callback); + //glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + glfwSetCursorPosCallback(window, cursor_position_callback); + glEnable(GL_DEPTH_TEST); program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag"); programTest = shaderLoader.CreateProgram("shaders/test.vert", "shaders/test.frag"); @@ -331,7 +440,23 @@ void init(GLFWwindow* window) loadModelToContext("./models/window.obj", models::windowContext); loadModelToContext("./models/test.obj", models::testContext); - loadModelToContext("./models/newlaser2.obj", models::laserContext); + //loadModelToContext("./models/newlaser2.obj", models::laserContext); + + //lasersContexts.reserve(spaceShip.getMaxNumberOfLasers()); + + for (int i = 0; i < spaceShip.getMaxNumberOfLasers(); i++) { + //lasersContexts.push_back(models::laserContext); + loadModelToContext("./models/newlaser2.obj", lasersContexts[i]); + + lasers.push_back(Laser(spaceShip.getSpaceShipDir(), spaceShip.getSpaceShipPos(), 1, false)); + + } + + + /*for (int i = 0; i < spaceShip.getMaxNumberOfLasers(); i++) { + + loadModelToContext("./models/newlaser2.obj", spaceShip.getGun()[i].getLaserContext()); + }*/ } @@ -340,10 +465,15 @@ void shutdown(GLFWwindow* window) shaderLoader.DeleteProgram(program); } + + //obsluga wejscia void processInput(GLFWwindow* window) { - glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceShip.getSpaceShipDir(), glm::vec3(0.f,1.f,0.f))); + + //glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceShip.getSpaceShipDir(), glm::vec3(0.f, 1.f, 0.f))); glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f); float angleSpeed = 0.05f * deltaTime * 60; float moveSpeed = 0.05f * deltaTime * 60; @@ -380,11 +510,11 @@ void processInput(GLFWwindow* window) } if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { //spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0)); - + spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0))); - + if (!spaceShip.getAttackDecision()) { - laser.setLaserDir(spaceShip.getSpaceShipDir()); + //laser.setLaserDir(spaceShip.getSpaceShipDir()); } } if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { @@ -393,7 +523,7 @@ void processInput(GLFWwindow* window) spaceShip.setSpaceShipDir(glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceShip.getSpaceShipDir(), 0))); if (!spaceShip.getAttackDecision()) { - laser.setLaserDir(spaceShip.getSpaceShipDir()); + //laser.setLaserDir(spaceShip.getSpaceShipDir()); } } @@ -412,15 +542,48 @@ void processInput(GLFWwindow* window) if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { drawLaserShot = true; - - spaceShip.setAttackDecision(true); - laser.setTimeSinceShootLaser(glfwGetTime()); - laser.setLaserDir(spaceShip.getSpaceShipDir()); - laser.setLaserPos(spaceShip.getSpaceShipPos()); - laser.setLaserSpeed(0.001); + spaceShip.setAttackDecision(true); + /*spaceShip.laserPreparationBeforeShoot(glfwGetTime()); + + + if (spaceShip.getNumberOfLeftLasers() > 0) { + spaceShip.setNumberOfLeftLasers(spaceShip.getNumberOfLeftLasers() - 1); + } + else if (spaceShip.getNumberOfLeftLasers() == 0) { + spaceShip.setNumberOfLeftLasers(spaceShip.getMaxNumberOfLasers()); + spaceShip.setNumberOfLeftLasers(spaceShip.getNumberOfLeftLasers() - 1); + } + + lasers[spaceShip.getNumberOfLeftLasers()].setTimeSinceShootLaser(glfwGetTime()); + lasers[spaceShip.getNumberOfLeftLasers()].setLaserDir(spaceShip.getSpaceShipDir()); + lasers[spaceShip.getNumberOfLeftLasers()].setLaserPos(spaceShip.getSpaceShipPos()); + lasers[spaceShip.getNumberOfLeftLasers()].setLaserSpeed(0.01);*/ + + + for (int i = 0; i < lasers.size(); i++) { + if (!lasers[i].isLaserFly() && spaceShip.getAttackDecision()) { + lasers[i].setTimeSinceShootLaser(glfwGetTime()); + lasers[i].setLaserDir(spaceShip.getSpaceShipDir()); + lasers[i].setLaserPos(spaceShip.getSpaceShipPos()); + lasers[i].setLaserSpeed(1); + lasers[i].setLaserFly(true); + spaceShip.setAttackDecision(false); + + + std::cout << "i = " << i << " " << "laser fly = " << lasers[i].isLaserFly() << "\n"; + } + } + + + //laser.setTimeSinceShootLaser(glfwGetTime()); + //laser.setLaserDir(spaceShip.getSpaceShipDir()); + //laser.setLaserPos(spaceShip.getSpaceShipPos()); + //laser.setLaserSpeed(0.01); } + //if(glfwGetKey(window, GLFW_MOUSE)) + //cameraDir = glm::normalize(-cameraPos); }