#include "glew.h" #include #include "glm.hpp" #include "ext.hpp" #include #include #include #include #include "Shader_Loader.h" #include "Render_Utils.h" #include "texture.h" #include "Box.cpp" #include #include #include #include #include "../Sun.h" #include "../Spaceship.h" #include "../Planet.h" #include "../GameObject.h" #include "../GameUtils.h" const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; int WIDTH = 500, HEIGHT = 500; namespace models { Core::RenderContext marbleBustContext; Core::RenderContext spaceshipContext; Core::RenderContext sphereContext; Core::RenderContext cubeContext; } namespace texture { GLuint cubemapTexture; } void createGalaxy(glm::vec3 galaxyPosition); GLuint depthMapFBO; GLuint depthMap; GLuint program; GLuint programSun; GLuint programTest; GLuint programTex; GLuint programCubemap; std::list planets; Sun* sun; GLuint VAO,VBO; float exposition = 1.f; glm::vec3 pointlightPos = glm::vec3(0, 2, 0); glm::vec3 pointlightColor = glm::vec3(0.9, 0.6, 0.6); float lastTime = -1.f; float deltaTime = 0.f; Spaceship* spaceship = Spaceship::getInstance(); void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef); void updateDeltaTime(float time) { if (lastTime < 0) { lastTime = time; return; } deltaTime = time - lastTime; if (deltaTime > 0.1) deltaTime = 0.1; lastTime = time; } void renderShadowapSun() { float time = glfwGetTime(); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); //uzupelnij o renderowanie glebokosci do tekstury glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, WIDTH, HEIGHT); } void renderScene(GLFWwindow* window) { glClearColor(0.4f, 0.4f, 0.8f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float time = glfwGetTime(); updateDeltaTime(time); renderShadowapSun(); drawSkybox(models::cubeContext, glm::translate(glm::mat4(), spaceship->cameraPos), texture::cubemapTexture, programCubemap); //space lamp glUseProgram(programSun); std::list* suns = GameUtils::getInstance()->getSuns(); for (Sun* sun : *suns) { sun->draw(); } glUseProgram(program); for (Planet* p : planets) { p->draw(time, program); } 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); drawObjectPBR(models::spaceshipContext, spaceship->calculateModelMatrix(), spaceship->color, spaceship->roughness, spaceship->metallic, program ); glUseProgram(0); glfwSwapBuffers(window); } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { GameUtils::getInstance()->setAspectRatio(width / float(height)); glViewport(0, 0, width, height); WIDTH = width; HEIGHT = height; } void createSuns() { 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); } } void init(GLFWwindow* window) { GameUtils* gameUtils = GameUtils::getInstance(); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glEnable(GL_DEPTH_TEST); 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"); 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); std::vector cubeFaces = { "bkg2_right1.png", "bkg2_left2.png", "bkg2_top3.png", "bkg2_bottom4.png", "bkg2_front5.png", "bkg2_back6.png" }; texture::cubemapTexture = Core::LoadCubemap(cubeFaces); createSuns(); } void shutdown(GLFWwindow* window) { GameUtils::getInstance()->shaderLoader->DeleteProgram(program); } //obsluga wejscia void processInput(GLFWwindow* window) { spaceship->processInput(window, deltaTime, glfwGetTime()); /*if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) exposition -= 0.05; if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) exposition += 0.05;*/ if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) { printf("GameUtils::spaceshipPos = glm::vec3(%ff, %ff, %ff);\n",spaceship->spaceshipPos.x,spaceship->spaceshipPos.y,spaceship->spaceshipPos.z); printf("GameUtils::spaceshipDir = glm::vec3(%ff, %ff, %ff);\n",spaceship->spaceshipDir.x,spaceship->spaceshipDir.y,spaceship->spaceshipDir.z); } //cameraDir = glm::normalize(-cameraPos); } // funkcja jest glowna petla void renderLoop(GLFWwindow* window) { while (!glfwWindowShouldClose(window)) { processInput(window); renderScene(window); glfwPollEvents(); } } //}