move suns to game utils

This commit is contained in:
Pawel Felcyn 2024-01-10 20:56:06 +01:00
parent f9c93f9d5b
commit b06b3e7f5e
4 changed files with 52 additions and 16 deletions

View File

@ -1,17 +1,37 @@
#include <list>
#include "Sun.h"
#pragma once #pragma once
class GameUtils class GameUtils
{ {
private: private:
// Private constructor to prevent external instantiation // Private constructor to prevent external instantiation
GameUtils() : aspectRatio(1.f) {} GameUtils() : aspectRatio(1.f)
{
public: this->suns = new std::list<Sun*>();
}
std::list<Sun*>* suns;
float aspectRatio; float aspectRatio;
static GameUtils& getInstance()
public:
float getAspectRatio() {
return aspectRatio;
}
void setAspectRatio(float value) {
aspectRatio = value;
}
std::list<Sun*>* getSuns() {
return suns;
}
static GameUtils* getInstance()
{ {
static GameUtils instance; // Jedna i jedyna instancja static GameUtils instance; // Jedna i jedyna instancja
return instance; return &instance;
} }
}; };

View File

@ -37,6 +37,14 @@ public:
Core::DrawContext(sphereContext); Core::DrawContext(sphereContext);
} }
glm::vec3 getPosition() {
return sunPos;
}
glm::vec3 getColor() {
return sunColor;
}
glm::mat4 getPositionMatrix() override { glm::mat4 getPositionMatrix() override {
return positionMatrix; return positionMatrix;
} }

View File

@ -1,7 +1,7 @@
#include "Render_Utils.h" #include "Render_Utils.h"
#include <algorithm> #include <algorithm>
#include <list>
#include "glew.h" #include "glew.h"
#include "freeglut.h" #include "freeglut.h"
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
@ -135,11 +135,11 @@ glm::mat4 Core::createPerspectiveMatrix()
glm::mat4 perspectiveMatrix; glm::mat4 perspectiveMatrix;
float n = 0.05; float n = 0.05;
float f = 200.; float f = 200.;
float a1 = glm::min(GameUtils::getInstance().aspectRatio, 1.f); float a1 = glm::min(GameUtils::getInstance()->getAspectRatio(), 1.f);
float a2 = glm::min(1 / GameUtils::getInstance().aspectRatio, 1.f); float a2 = glm::min(1 / GameUtils::getInstance()->getAspectRatio(), 1.f);
perspectiveMatrix = glm::mat4({ perspectiveMatrix = glm::mat4({
1,0.,0.,0., 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.,(f + n) / (n - f),2 * f * n / (n - f),
0.,0.,-1.,0., 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, "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, "sunColor"), sun.sunColor.x, sun.sunColor.y, sun.sunColor.z);*/
/* glUniform3f(glGetUniformLocation(program, "lightPos"), sun.sunPos.x, sun.sunPos.y, sun.sunPos.z); std::list<Sun*>* suns = GameUtils::getInstance()->getSuns();
glUniform3f(glGetUniformLocation(program, "lightColor"), sun.sunColor.x, sun.sunColor.y, sun.sunColor.z);*/ 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, "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, "spotlightPos"), Spaceship::getInstance().spotlightPos.x, Spaceship::getInstance().spotlightPos.y, Spaceship::getInstance().spotlightPos.z);

View File

@ -44,7 +44,6 @@ Core::Shader_Loader shaderLoader;
Core::RenderContext shipContext; Core::RenderContext shipContext;
Core::RenderContext sphereContext; Core::RenderContext sphereContext;
std::list<Sun*> suns;
std::list<Planet*> planets; std::list<Planet*> planets;
Sun* sun; Sun* sun;
GLuint VAO,VBO; GLuint VAO,VBO;
@ -87,8 +86,10 @@ void renderScene(GLFWwindow* window)
//space lamp //space lamp
glUseProgram(programSun); glUseProgram(programSun);
for (Sun* s : suns) {
s->draw(); std::list<Sun*>* suns = GameUtils::getInstance()->getSuns();
for (Sun* sun : *suns) {
sun->draw();
} }
glUseProgram(program); glUseProgram(program);
@ -116,7 +117,7 @@ void renderScene(GLFWwindow* window)
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) 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); glViewport(0, 0, width, height);
WIDTH = width; WIDTH = width;
HEIGHT = height; HEIGHT = height;
@ -135,12 +136,15 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
} }
void createSuns() { 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); 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); Planet* planet = new Planet(sun, 20.f, 0.25f, 1.f, sphereContext);
planets.push_back(planet); planets.push_back(planet);
Planet* moon = new Planet(planet, 5.f, 1.f, 0.2f, sphereContext); Planet* moon = new Planet(planet, 5.f, 1.f, 0.2f, sphereContext);
planets.push_back(moon); 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(-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(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(0, -60, 100), glm::vec3(-0.53633f, 0.551106, 0.043226f), glm::vec3(0.8f, 0.2f, 0.2f), 4.5));