#include "glew.h" #include #include "glm.hpp" #include "ext.hpp" #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 "../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; } GLuint depthMapFBO; GLuint depthMap; GLuint program; GLuint programSun; GLuint programTest; GLuint programTex; Core::Shader_Loader shaderLoader; std::list suns; 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 updateDeltaTime(float time) { if (lastTime < 0) { lastTime = time; return; } deltaTime = time - lastTime; if (deltaTime > 0.1) deltaTime = 0.1; lastTime = time; } void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic) { glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() *spaceship.createCameraMatrix(); glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); glUniform1f(glGetUniformLocation(program, "exposition"), exposition); glUniform1f(glGetUniformLocation(program, "roughness"), roughness); glUniform1f(glGetUniformLocation(program, "metallic"), metallic); glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z); glUniform3f(glGetUniformLocation(program, "cameraPos"),spaceship.cameraPos.x,spaceship.cameraPos.y,spaceship.cameraPos.z); const int NUM_LIGHTS = 23; glm::vec3 lightsPositions[NUM_LIGHTS]; glm::vec3 lightsColors[NUM_LIGHTS]; glm::vec3 lightsDirections[NUM_LIGHTS]; /*glUniform3f(glGetUniformLocation(program, "sunDir"), sun.sunDir.x, sun.sunDir.y, sun.sunDir.z); glUniform3f(glGetUniformLocation(program, "sunColor"), sun.sunColor.x, sun.sunColor.y, sun.sunColor.z);*/ glUniform3f(glGetUniformLocation(program, "lightPos"), sun.sunPos.x, sun.sunPos.y, sun.sunPos.z); glUniform3f(glGetUniformLocation(program, "lightColor"), sun.sunColor.x, sun.sunColor.y, sun.sunColor.z); glUniform3f(glGetUniformLocation(program, "spotlightConeDir"),spaceship.spotlightConeDir.x,spaceship.spotlightConeDir.y,spaceship.spotlightConeDir.z); glUniform3f(glGetUniformLocation(program, "spotlightPos"),spaceship.spotlightPos.x,spaceship.spotlightPos.y,spaceship.spotlightPos.z); glUniform3f(glGetUniformLocation(program, "spotlightColor"),spaceship.spotlightColor.x,spaceship.spotlightColor.y,spaceship.spotlightColor.z); glUniform1f(glGetUniformLocation(program, "spotlightPhi"),spaceship.spotlightPhi); Core::DrawContext(context); } 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(); //space lamp glUseProgram(programSun); for (Sun& s : suns) { s.draw(); } glUseProgram(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::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0); 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); drawObjectPBR(models::marbleBustContext, glm::mat4(), glm::vec3(1.f, 1.f, 1.f), 0.5f, 1.0f); drawObjectPBR(models::spaceshipContext, spaceship.calculateModelMatrix(), spaceship.color, spaceship.roughness, spaceship.metallic); glUseProgram(0); glfwSwapBuffers(window); } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { GameUtils::getInstance().aspectRatio = width / float(height); glViewport(0, 0, width, height); WIDTH = width; HEIGHT = height; } void loadModelToContext(std::string path, Core::RenderContext& context) { Assimp::Importer import; const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace); if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl; return; } context.initFromAssimpMesh(scene->mMeshes[0]); } void createSuns() { sun = 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); suns.push_back(sun); 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)); } void init(GLFWwindow* window) { glfwSetFramebufferSizeCallback(window, framebuffer_size_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"); programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); //loadModelToContext("./models/sphere.obj", sphereContext); //loadModelToContext("./models/spaceship.obj", spaceship.context); loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); loadModelToContext("./models/spaceship.obj", models::spaceshipContext); loadModelToContext("./models/sphere.obj", models::sphereContext); createSuns(); } void shutdown(GLFWwindow* window) { shaderLoader.DeleteProgram(program); } //obsluga wejscia void processInput(GLFWwindow* window) { Spaceship::getInstance().processInput(window, deltaTime); /*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(); } } //}