diff --git a/projekt_grk/src/ex_7_1.hpp b/projekt_grk/src/ex_7_1.hpp index 46c7f72..d691cec 100644 --- a/projekt_grk/src/ex_7_1.hpp +++ b/projekt_grk/src/ex_7_1.hpp @@ -8,6 +8,7 @@ #include "Shader_Loader.h" #include "Render_Utils.h" #include "Texture.h" +//#include "../SpriteRenderer.h" #include "Box.cpp" #include @@ -47,6 +48,8 @@ namespace texture { GLuint grid; + GLuint gameOverSprite; + } @@ -55,6 +58,8 @@ GLuint programSun; GLuint programTex; GLuint programSkyBox; GLuint programSpaceShip; +GLuint programSprite; + Core::Shader_Loader shaderLoader; Core::RenderContext shipContext; @@ -72,6 +77,32 @@ GLuint VAO,VBO; float aspectRatio = 1.f; unsigned int textureID; +bool gameOver = false; + + +struct Planet { + glm::vec3 currentPos; + glm::vec3 modelScale; + float rotationSpeed; // ruch obrotowy + float orbitSpeed; // ruch obiegowy + float orbitRadius; + GLuint texture; + GLuint normalTexture; +}; + +struct Satellite { + glm::vec3 currentPos; + glm::vec3 modelScale; + float rotationSpeed; // ruch obrotowy + float orbitSpeed; // ruch obiegowy + float orbitRadius; + GLuint texture; + GLuint normalTexture; + Planet parentPlanet; +}; + +std::vector planets; +std::vector satellites; glm::mat4 createCameraMatrix() { @@ -191,6 +222,8 @@ void drawSpaceShip(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t } + + void renderPlanet(float time, float orbitRadius, float orbitRotationSpeed, float selfRotationSpeed, float scale, GLuint planetTexture, GLuint normalTexture) { glm::mat4 planetTransform = glm::rotate(glm::mat4(1.0f), time * orbitRotationSpeed, glm::vec3(0, 1, 0)) // orbitowanie dookoła słońca * glm::translate(glm::vec3(orbitRadius, 0, 0)) // translacja na odp. odległość @@ -201,7 +234,8 @@ void renderPlanet(float time, float orbitRadius, float orbitRotationSpeed, float } void renderPlanets() { - float time = glfwGetTime(); + float time = 0; + //float time = glfwGetTime(); float mercuryOrbitRadius = 1.3f; float venusOrbitRadius = 2.0f; @@ -262,8 +296,42 @@ void renderPlanets() { } +void initializePlanets() { + planets.push_back(Planet{ glm::vec3(2.0f, 0, 0), glm::vec3(0.11f), 1 / 59.0f, 1.0f / 2, 2.0f, texture::mercury, texture::mercuryNormal }); + planets.push_back(Planet{ glm::vec3(3.5f, 0, 0), glm::vec3(0.29f), 1 / 243.0f, 1.0f / 5, 3.5f, texture::venus, texture::venusNormal }); + planets.push_back(Planet{ glm::vec3(5.0f, 0, 0), glm::vec3(0.3f), 1.f, 1.0f / 6, 5.0f, texture::earth, texture::earthNormal }); + planets.push_back(Planet{ glm::vec3(7.5f, 0, 0), glm::vec3(0.2f), 1.f, 1.0f / 7, 7.5f, texture::mars, texture::marsNormal }); + planets.push_back(Planet{ glm::vec3(10.f, 0, 0), glm::vec3(1.f), 2.4f, 1.0f / 9, 10.f, texture::jupiter, texture::jupiterNormal }); + planets.push_back(Planet{ glm::vec3(12.5f, 0, 0), glm::vec3(0.7f), 2.4f, 1.0f / 10, 12.5f, texture::saturn, texture::saturnNormal }); + planets.push_back(Planet{ glm::vec3(15.f, 0, 0), glm::vec3(1.f), 1.7f, 1.0f / 15, 15.f, texture::uranus, texture::uranusNormal }); + planets.push_back(Planet{ glm::vec3(20.f, 0, 0), glm::vec3(1.f), 1.2f, 1.0f / 24, 20.f, texture::neptune, texture::neptuneNormal }); + planets.push_back(Planet{ glm::vec3(0, 0, 0), glm::vec3(1.f), 0.f, 0.f, 0.f, texture::sun, texture::sun }); + satellites.push_back(Satellite{ glm::vec3(5.0f, 0, 0), glm::vec3(0.055f), 1.f / 2, 1.0f / 2, 0.8f, texture::moon, texture::asteroidNormal, planets[2] }); +} + +void printPlanetPos() { + int index = 0; + for (auto& planet : planets) { + std::cout << index << " " << glm::to_string(planet.currentPos) << std::endl; + ++index; + } +} + +bool checkCollision(glm::vec3 spaceshipPos, glm::vec3 planetPos, glm::vec3 planetScale) { + + float planetRadius = planetScale.x; + float shipSize = 0.5f; + + float distanceSquared = glm::distance2(spaceshipPos, planetPos); + float distance = glm::length(planetPos - spaceshipPos); + + float minDistance = planetRadius + shipSize; + return distanceSquared <= (planetRadius + shipSize) * (planetRadius + shipSize); +} void renderScene(GLFWwindow* window) { + //float time = 0; + float time = glfwGetTime(); glClearColor(0.0f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -274,8 +342,40 @@ void renderScene(GLFWwindow* window) drawSun(sphereContext, glm::mat4(), texture::sun); //rendering all the planets - renderPlanets(); + //renderPlanets(); + /*glm::mat4 modelMatrix = glm::eulerAngleY(time / planet.rotationSpeed) + * glm::translate(glm::vec3(planet.orbitRadius, 0, 0)) + * glm::eulerAngleY(time) + * glm::scale(planet.modelScale);*/ + + + for (auto& planet : planets) { + + glm::mat4 modelMatrix = glm::rotate(glm::mat4(1.0f), time * planet.orbitSpeed, glm::vec3(0, 1, 0)) // orbitowanie dookoła słońca + * glm::translate(glm::vec3(planet.orbitRadius, 0, 0)) // translacja na odp. odległość + * glm::rotate(glm::mat4(1.0f), time * planet.rotationSpeed, glm::vec3(0, 1, 0)) //obrót planety wokół własnej osi + * glm::scale(glm::vec3(planet.modelScale)); //skalowanie planety + drawObjectTexture(sphereContext, modelMatrix, planet.texture, planet.normalTexture); + planet.currentPos = glm::vec3(modelMatrix * glm::vec4(0, 0, 0, 1.0f)); + + + } + + for (auto& satellite : satellites) { + glm::mat4 modelMatrix = glm::eulerAngleY(time * satellite.parentPlanet.orbitSpeed) + * glm::translate(glm::vec3(satellite.parentPlanet.orbitRadius, 0, 0)) + * glm::eulerAngleY(time * satellite.rotationSpeed) + * glm::translate(glm::vec3(satellite.orbitRadius, 0, 0)) + * glm::scale(glm::vec3(satellite.modelScale)); + drawObjectTexture(sphereContext, modelMatrix, satellite.texture, satellite.normalTexture); + satellite.currentPos = glm::vec3(modelMatrix * glm::vec4(0, 0, 0, 1.0f)); + } + + /*drawObjectTexture(sphereContext, + glm::eulerAngleY(time * planets[2].orbitSpeed) * glm::translate(glm::vec3(planets[2].orbitRadius, 0, 0)) * glm::eulerAngleY(time * 0.5f) * glm::translate(glm::vec3(0.8f, 0, 0)) * glm::scale(glm::vec3(0.055f)), + texture::moon, + texture::asteroidNormal);*/ // obliczanie orientacji statku glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); @@ -393,6 +493,12 @@ void init(GLFWwindow* window) glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); //skybox end + initializePlanets(); + +} + +void gameOverScreen(GLFWwindow* window) { + // jakies game over dla jego } void shutdown(GLFWwindow* window) @@ -427,6 +533,9 @@ void processInput(GLFWwindow* window) spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceshipDir, 0)); if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) spaceshipDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceshipDir, 0)); + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) + spaceshipPos += spaceshipDir * 5*moveSpeed; + cameraPos = spaceshipPos - 1.5 * spaceshipDir + glm::vec3(0, 1, 0) * 0.5f; cameraDir = spaceshipDir; @@ -434,12 +543,49 @@ void processInput(GLFWwindow* window) void renderLoop(GLFWwindow* window) { + + double timeOfLastUpdate = glfwGetTime(); + int loopCount = 0; + int interval = 2500; + while (!glfwWindowShouldClose(window)) { - processInput(window); - renderScene(window); + int index = 0; + + for (auto& planet : planets) { + + if (checkCollision(spaceshipPos, planet.currentPos, planet.modelScale)) { + //placeholder + gameOver = true; + std::cout << "Kolizja statku z planeta " << index << std::endl; + } + ++index; + } + for (auto& satellite : satellites) { + + if (checkCollision(spaceshipPos, satellite.currentPos, satellite.modelScale)) { + //placeholder + std::cout << "Kolizja statku z satelita " << index << std::endl; + } + ++index; + } + + + loopCount++; + + if (loopCount >= interval) { + printPlanetPos(); + std::cout << "Pozycja statku " << glm::to_string(spaceshipPos) << std::endl; + loopCount = 0; + timeOfLastUpdate = glfwGetTime(); + } + + if (gameOver) { + gameOverScreen(window); + } glfwPollEvents(); } } + \ No newline at end of file