From b06b3e7f5ef60bdd1a4d45c59781e596e9ad2fbb Mon Sep 17 00:00:00 2001 From: Pawel Felcyn Date: Wed, 10 Jan 2024 20:56:06 +0100 Subject: [PATCH] move suns to game utils --- grk/project/GameUtils.h | 30 +++++++++++++++++++++++++----- grk/project/Sun.h | 8 ++++++++ grk/project/src/Render_Utils.cpp | 16 ++++++++++------ grk/project/src/ex_9_1.hpp | 14 +++++++++----- 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/grk/project/GameUtils.h b/grk/project/GameUtils.h index 0b384e2..c237638 100644 --- a/grk/project/GameUtils.h +++ b/grk/project/GameUtils.h @@ -1,17 +1,37 @@ +#include +#include "Sun.h" + #pragma once class GameUtils { private: // Private constructor to prevent external instantiation - GameUtils() : aspectRatio(1.f) {} - -public: + GameUtils() : aspectRatio(1.f) + { + this->suns = new std::list(); + } + std::list* suns; float aspectRatio; - static GameUtils& getInstance() + +public: + + float getAspectRatio() { + return aspectRatio; + } + + void setAspectRatio(float value) { + aspectRatio = value; + } + + std::list* getSuns() { + return suns; + } + + static GameUtils* getInstance() { static GameUtils instance; // Jedna i jedyna instancja - return instance; + return &instance; } }; diff --git a/grk/project/Sun.h b/grk/project/Sun.h index 8b163a6..b44c724 100644 --- a/grk/project/Sun.h +++ b/grk/project/Sun.h @@ -37,6 +37,14 @@ public: Core::DrawContext(sphereContext); } + glm::vec3 getPosition() { + return sunPos; + } + + glm::vec3 getColor() { + return sunColor; + } + glm::mat4 getPositionMatrix() override { return positionMatrix; } diff --git a/grk/project/src/Render_Utils.cpp b/grk/project/src/Render_Utils.cpp index b1ea725..4967331 100644 --- a/grk/project/src/Render_Utils.cpp +++ b/grk/project/src/Render_Utils.cpp @@ -1,7 +1,7 @@ #include "Render_Utils.h" #include - +#include #include "glew.h" #include "freeglut.h" #include @@ -135,11 +135,11 @@ glm::mat4 Core::createPerspectiveMatrix() glm::mat4 perspectiveMatrix; float n = 0.05; float f = 200.; - float a1 = glm::min(GameUtils::getInstance().aspectRatio, 1.f); - float a2 = glm::min(1 / GameUtils::getInstance().aspectRatio, 1.f); + float a1 = glm::min(GameUtils::getInstance()->getAspectRatio(), 1.f); + float a2 = glm::min(1 / GameUtils::getInstance()->getAspectRatio(), 1.f); perspectiveMatrix = glm::mat4({ 1,0.,0.,0., - 0.,GameUtils::getInstance().aspectRatio,0.,0., + 0.,GameUtils::getInstance()->getAspectRatio(),0.,0., 0.,0.,(f + n) / (n - f),2 * f * n / (n - f), 0.,0.,-1.,0., }); @@ -176,8 +176,12 @@ void Core::drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, gl /*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);*/ + std::list* suns = GameUtils::getInstance()->getSuns(); + glm::vec3 firstSunPos = suns->front()->getPosition(); + glm::vec3 firstSunColor = suns->front()->getColor(); + + glUniform3f(glGetUniformLocation(program, "lightPos"), firstSunPos.x, firstSunPos.y, firstSunPos.z); + glUniform3f(glGetUniformLocation(program, "lightColor"), firstSunColor.x, firstSunColor.y, firstSunColor.z); 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); diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index 544eb70..5280c9f 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -44,7 +44,6 @@ Core::Shader_Loader shaderLoader; Core::RenderContext shipContext; Core::RenderContext sphereContext; -std::list suns; std::list planets; Sun* sun; GLuint VAO,VBO; @@ -87,8 +86,10 @@ void renderScene(GLFWwindow* window) //space lamp glUseProgram(programSun); - for (Sun* s : suns) { - s->draw(); + + std::list* suns = GameUtils::getInstance()->getSuns(); + for (Sun* sun : *suns) { + sun->draw(); } glUseProgram(program); @@ -116,7 +117,7 @@ void renderScene(GLFWwindow* window) } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { - GameUtils::getInstance().aspectRatio = width / float(height); + GameUtils::getInstance()->setAspectRatio(width / float(height)); glViewport(0, 0, width, height); WIDTH = width; HEIGHT = height; @@ -135,12 +136,15 @@ void loadModelToContext(std::string path, Core::RenderContext& context) } void createSuns() { + GameUtils* gu = GameUtils::getInstance(); sun = new 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); Planet* planet = new Planet(sun, 20.f, 0.25f, 1.f, sphereContext); planets.push_back(planet); Planet* moon = new Planet(planet, 5.f, 1.f, 0.2f, sphereContext); planets.push_back(moon); - suns.push_back(sun); + gu->getSuns()->push_back(sun); + Planet* planet2 = new Planet(sun, 50.f, 0.5f, 1.5f, sphereContext); + planets.push_back(planet2); /*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));