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
class GameUtils
{
private:
// Private constructor to prevent external instantiation
GameUtils() : aspectRatio(1.f) {}
public:
GameUtils() : aspectRatio(1.f)
{
this->suns = new std::list<Sun*>();
}
std::list<Sun*>* suns;
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
return instance;
return &instance;
}
};

View File

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

View File

@ -1,7 +1,7 @@
#include "Render_Utils.h"
#include <algorithm>
#include <list>
#include "glew.h"
#include "freeglut.h"
#include <assimp/Importer.hpp>
@ -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<Sun*>* 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);

View File

@ -44,7 +44,6 @@ Core::Shader_Loader shaderLoader;
Core::RenderContext shipContext;
Core::RenderContext sphereContext;
std::list<Sun*> suns;
std::list<Planet*> 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<Sun*>* 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));