From 2b390af6d9fb2ed57d5cf93c8545e6deb433b200 Mon Sep 17 00:00:00 2001 From: Pawel Felcyn Date: Wed, 3 Jan 2024 22:06:27 +0100 Subject: [PATCH] move rendering sun to Sun class --- grk/project/GameUtils.h | 17 ++++++ grk/project/Spaceship.h | 15 ++++++ grk/project/Sun.h | 22 +++++++- grk/project/grk-project.vcxproj | 1 + grk/project/grk-project.vcxproj.filters | 3 ++ grk/project/src/Render_Utils.cpp | 22 ++++++++ grk/project/src/Render_Utils.h | 2 + grk/project/src/ex_9_1.hpp | 70 ++++++------------------- 8 files changed, 98 insertions(+), 54 deletions(-) create mode 100644 grk/project/GameUtils.h diff --git a/grk/project/GameUtils.h b/grk/project/GameUtils.h new file mode 100644 index 0000000..0b384e2 --- /dev/null +++ b/grk/project/GameUtils.h @@ -0,0 +1,17 @@ +#pragma once +class GameUtils +{ +private: + + // Private constructor to prevent external instantiation + GameUtils() : aspectRatio(1.f) {} + +public: + float aspectRatio; + + static GameUtils& getInstance() + { + static GameUtils instance; // Jedna i jedyna instancja + return instance; + } +}; diff --git a/grk/project/Spaceship.h b/grk/project/Spaceship.h index f7e84d7..fa423a7 100644 --- a/grk/project/Spaceship.h +++ b/grk/project/Spaceship.h @@ -4,7 +4,22 @@ #pragma once class Spaceship { +private: + // Prywatny konstruktor, aby zapobiec zewnętrznemu tworzeniu instancji + Spaceship() {} + + // Prywatny destruktor, aby zapobiec przypadkowemu usuwaniu instancji + ~Spaceship() {} + + public: + + static Spaceship& getInstance() + { + static Spaceship instance; // Jedna i jedyna instancja + return instance; + } + glm::vec3 spaceshipPos = glm::vec3(0.065808f, 1.250000f, -2.189549f); glm::vec3 spaceshipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f); diff --git a/grk/project/Sun.h b/grk/project/Sun.h index d9c69ed..230d696 100644 --- a/grk/project/Sun.h +++ b/grk/project/Sun.h @@ -1,11 +1,31 @@ #include "glm.hpp" #include "ext.hpp" +#include "./Spaceship.h" #pragma once class Sun { public: - glm::vec3 sunPos = glm::vec3(-4.740971f, 2.149999f, 0.369280f); + glm::vec3 sunPos = glm::vec3(0, 2, 0); glm::vec3 sunDir = glm::vec3(-0.93633f, 0.351106, 0.003226f); glm::vec3 sunColor = glm::vec3(0.9f, 0.9f, 0.7f) * 5; + GLuint program; + Core::RenderContext sphereContext; + + Sun(GLuint program, Core::RenderContext sphereContext) { + this->program = program; + this->sphereContext = sphereContext; + } + + Sun(){} + + void draw() { + glUseProgram(program); + glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * Spaceship::getInstance().createCameraMatrix(); + glm::mat4 transformation = viewProjectionMatrix * glm::translate(sunPos) * glm::scale(glm::vec3(0.1)); + glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation); + glUniform3f(glGetUniformLocation(program, "color"), sunColor.x / 2, sunColor.y / 2, sunColor.z / 2); + glUniform1f(glGetUniformLocation(program, "exposition"), 1.f); + Core::DrawContext(sphereContext); + } }; \ No newline at end of file diff --git a/grk/project/grk-project.vcxproj b/grk/project/grk-project.vcxproj index 9e42e81..02d17c8 100644 --- a/grk/project/grk-project.vcxproj +++ b/grk/project/grk-project.vcxproj @@ -23,6 +23,7 @@ + diff --git a/grk/project/grk-project.vcxproj.filters b/grk/project/grk-project.vcxproj.filters index 8b06a13..fe8c10b 100644 --- a/grk/project/grk-project.vcxproj.filters +++ b/grk/project/grk-project.vcxproj.filters @@ -95,6 +95,9 @@ Header Files + + Header Files + diff --git a/grk/project/src/Render_Utils.cpp b/grk/project/src/Render_Utils.cpp index 18f71f9..698e226 100644 --- a/grk/project/src/Render_Utils.cpp +++ b/grk/project/src/Render_Utils.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "../GameUtils.h" @@ -126,3 +127,24 @@ void Core::DrawContext(Core::RenderContext& context) ); glBindVertexArray(0); } + +glm::mat4 Core::createPerspectiveMatrix() +{ + + glm::mat4 perspectiveMatrix; + float n = 0.05; + float f = 20.; + float a1 = glm::min(GameUtils::getInstance().aspectRatio, 1.f); + float a2 = glm::min(1 / GameUtils::getInstance().aspectRatio, 1.f); + perspectiveMatrix = glm::mat4({ + 1,0.,0.,0., + 0.,GameUtils::getInstance().aspectRatio,0.,0., + 0.,0.,(f + n) / (n - f),2 * f * n / (n - f), + 0.,0.,-1.,0., + }); + + + perspectiveMatrix = glm::transpose(perspectiveMatrix); + + return perspectiveMatrix; +} \ No newline at end of file diff --git a/grk/project/src/Render_Utils.h b/grk/project/src/Render_Utils.h index 052008d..1271898 100644 --- a/grk/project/src/Render_Utils.h +++ b/grk/project/src/Render_Utils.h @@ -69,4 +69,6 @@ namespace Core void DrawVertexArray(const VertexData & data); void DrawContext(RenderContext& context); + + glm::mat4 createPerspectiveMatrix(); } \ No newline at end of file diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index da17178..7b04378 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -16,6 +16,7 @@ #include #include "../Sun.h" #include "../Spaceship.h" +#include "../GameUtils.h" const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; @@ -52,12 +53,8 @@ Core::RenderContext shipContext; Core::RenderContext sphereContext; Sun sun; - -Spaceship spaceship; GLuint VAO,VBO; -float aspectRatio = 1.f; - float exposition = 1.f; glm::vec3 pointlightPos = glm::vec3(0, 2, 0); @@ -77,30 +74,9 @@ void updateDeltaTime(float time) { lastTime = time; } -glm::mat4 createPerspectiveMatrix() -{ - - glm::mat4 perspectiveMatrix; - float n = 0.05; - float f = 20.; - float a1 = glm::min(aspectRatio, 1.f); - float a2 = glm::min(1 / aspectRatio, 1.f); - perspectiveMatrix = glm::mat4({ - 1,0.,0.,0., - 0.,aspectRatio,0.,0., - 0.,0.,(f+n) / (n - f),2*f * n / (n - f), - 0.,0.,-1.,0., - }); - - - perspectiveMatrix=glm::transpose(perspectiveMatrix); - - return perspectiveMatrix; -} - void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic) { - glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * spaceship.createCameraMatrix(); + glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * Spaceship::getInstance().createCameraMatrix(); glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); @@ -112,7 +88,7 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z); - glUniform3f(glGetUniformLocation(program, "cameraPos"), spaceship.cameraPos.x, spaceship.cameraPos.y, spaceship.cameraPos.z); + glUniform3f(glGetUniformLocation(program, "cameraPos"), Spaceship::getInstance().cameraPos.x, Spaceship::getInstance().cameraPos.y, Spaceship::getInstance().cameraPos.z); 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); @@ -120,10 +96,10 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec glUniform3f(glGetUniformLocation(program, "lightPos"), pointlightPos.x, pointlightPos.y, pointlightPos.z); glUniform3f(glGetUniformLocation(program, "lightColor"), pointlightColor.x, pointlightColor.y, pointlightColor.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); + glUniform3f(glGetUniformLocation(program, "spotlightConeDir"), Spaceship::getInstance().spotlightConeDir.x, Spaceship::getInstance().spotlightConeDir.y, Spaceship::getInstance().spotlightConeDir.z); + glUniform3f(glGetUniformLocation(program, "spotlightPos"), Spaceship::getInstance().spotlightPos.x, Spaceship::getInstance().spotlightPos.y, Spaceship::getInstance().spotlightPos.z); + glUniform3f(glGetUniformLocation(program, "spotlightColor"), Spaceship::getInstance().spotlightColor.x, Spaceship::getInstance().spotlightColor.y, Spaceship::getInstance().spotlightColor.z); + glUniform1f(glGetUniformLocation(program, "spotlightPhi"), Spaceship::getInstance().spotlightPhi); Core::DrawContext(context); } @@ -152,19 +128,7 @@ void renderScene(GLFWwindow* window) //space lamp glUseProgram(programSun); - glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * spaceship.createCameraMatrix(); - glm::mat4 transformation = viewProjectionMatrix * glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)); - glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation); - glUniform3f(glGetUniformLocation(programSun, "color"), sun.sunColor.x / 2, sun.sunColor.y / 2, sun.sunColor.z / 2); - glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition); - Core::DrawContext(sphereContext); - - glm::mat4 viewProjectionMatrix2 = createPerspectiveMatrix() * spaceship.createCameraMatrix(); - glm::mat4 transformation2 = viewProjectionMatrix2 * glm::translate(pointlightPos + glm::vec3(1, 1, 1)) * glm::scale(glm::vec3(0.1)); - glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation2); - glUniform3f(glGetUniformLocation(programSun, "color"), sun.sunColor.x / 2, sun.sunColor.y / 2, sun.sunColor.z / 2); - glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition); - Core::DrawContext(sphereContext); + sun.draw(); glUseProgram(program); @@ -191,7 +155,7 @@ void renderScene(GLFWwindow* window) // glm::vec3(0.3, 0.3, 0.5) // ); drawObjectPBR(shipContext, - spaceship.calculateModelMatrix(), + Spaceship::getInstance().calculateModelMatrix(), glm::vec3(0.3, 0.3, 0.5), 0.2,1.0 ); @@ -208,7 +172,7 @@ void renderScene(GLFWwindow* window) } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { - aspectRatio = width / float(height); + GameUtils::getInstance().aspectRatio = width / float(height); glViewport(0, 0, width, height); WIDTH = width; HEIGHT = height; @@ -249,11 +213,12 @@ void init(GLFWwindow* window) loadModelToContext("./models/pencils.obj", models::pencilsContext); loadModelToContext("./models/plane.obj", models::planeContext); loadModelToContext("./models/room.obj", models::roomContext); - loadModelToContext("./models/spaceship.obj", models::spaceshipContext); + loadModelToContext("./models/GameUtils::spaceship.obj", models::spaceshipContext); loadModelToContext("./models/sphere.obj", models::sphereContext); loadModelToContext("./models/window.obj", models::windowContext); loadModelToContext("./models/test.obj", models::testContext); + sun = Sun(programSun, models::sphereContext); } void shutdown(GLFWwindow* window) @@ -264,17 +229,16 @@ void shutdown(GLFWwindow* window) //obsluga wejscia void processInput(GLFWwindow* window) { - spaceship.processInput(window, deltaTime); - + Spaceship::getInstance().processInput(window, deltaTime); - if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) + /*if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) exposition -= 0.05; if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) - exposition += 0.05; + exposition += 0.05;*/ if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) { - printf("spaceshipPos = glm::vec3(%ff, %ff, %ff);\n", spaceship.spaceshipPos.x, spaceship.spaceshipPos.y, spaceship.spaceshipPos.z); - printf("spaceshipDir = glm::vec3(%ff, %ff, %ff);\n", spaceship.spaceshipDir.x, spaceship.spaceshipDir.y, spaceship.spaceshipDir.z); + printf("GameUtils::spaceshipPos = glm::vec3(%ff, %ff, %ff);\n", Spaceship::getInstance().spaceshipPos.x, Spaceship::getInstance().spaceshipPos.y, Spaceship::getInstance().spaceshipPos.z); + printf("GameUtils::spaceshipDir = glm::vec3(%ff, %ff, %ff);\n", Spaceship::getInstance().spaceshipDir.x, Spaceship::getInstance().spaceshipDir.y, Spaceship::getInstance().spaceshipDir.z); } //cameraDir = glm::normalize(-cameraPos);