move rendering sun to Sun class

This commit is contained in:
Pawel Felcyn 2024-01-03 22:06:27 +01:00
parent 034df6be24
commit 2b390af6d9
8 changed files with 98 additions and 54 deletions

17
grk/project/GameUtils.h Normal file
View File

@ -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;
}
};

View File

@ -4,7 +4,22 @@
#pragma once #pragma once
class Spaceship class Spaceship
{ {
private:
// Prywatny konstruktor, aby zapobiec zewnętrznemu tworzeniu instancji
Spaceship() {}
// Prywatny destruktor, aby zapobiec przypadkowemu usuwaniu instancji
~Spaceship() {}
public: 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 spaceshipPos = glm::vec3(0.065808f, 1.250000f, -2.189549f);
glm::vec3 spaceshipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f); glm::vec3 spaceshipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f);

View File

@ -1,11 +1,31 @@
#include "glm.hpp" #include "glm.hpp"
#include "ext.hpp" #include "ext.hpp"
#include "./Spaceship.h"
#pragma once #pragma once
class Sun class Sun
{ {
public: 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 sunDir = glm::vec3(-0.93633f, 0.351106, 0.003226f);
glm::vec3 sunColor = glm::vec3(0.9f, 0.9f, 0.7f) * 5; 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);
}
}; };

View File

@ -23,6 +23,7 @@
<ClCompile Include="src\Texture.cpp" /> <ClCompile Include="src\Texture.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="GameUtils.h" />
<ClInclude Include="Spaceship.h" /> <ClInclude Include="Spaceship.h" />
<ClInclude Include="src\Camera.h" /> <ClInclude Include="src\Camera.h" />
<ClInclude Include="src\ex_9_1.hpp" /> <ClInclude Include="src\ex_9_1.hpp" />

View File

@ -95,6 +95,9 @@
<ClInclude Include="Spaceship.h"> <ClInclude Include="Spaceship.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="GameUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="shaders\shader_8_sun.vert"> <None Include="shaders\shader_8_sun.vert">

View File

@ -7,6 +7,7 @@
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
#include <assimp/scene.h> #include <assimp/scene.h>
#include <assimp/postprocess.h> #include <assimp/postprocess.h>
#include "../GameUtils.h"
@ -126,3 +127,24 @@ void Core::DrawContext(Core::RenderContext& context)
); );
glBindVertexArray(0); 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;
}

View File

@ -69,4 +69,6 @@ namespace Core
void DrawVertexArray(const VertexData & data); void DrawVertexArray(const VertexData & data);
void DrawContext(RenderContext& context); void DrawContext(RenderContext& context);
glm::mat4 createPerspectiveMatrix();
} }

View File

@ -16,6 +16,7 @@
#include <string> #include <string>
#include "../Sun.h" #include "../Sun.h"
#include "../Spaceship.h" #include "../Spaceship.h"
#include "../GameUtils.h"
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
@ -52,12 +53,8 @@ Core::RenderContext shipContext;
Core::RenderContext sphereContext; Core::RenderContext sphereContext;
Sun sun; Sun sun;
Spaceship spaceship;
GLuint VAO,VBO; GLuint VAO,VBO;
float aspectRatio = 1.f;
float exposition = 1.f; float exposition = 1.f;
glm::vec3 pointlightPos = glm::vec3(0, 2, 0); glm::vec3 pointlightPos = glm::vec3(0, 2, 0);
@ -77,30 +74,9 @@ void updateDeltaTime(float time) {
lastTime = 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) { 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; glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); 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, "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, "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);
@ -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, "lightPos"), pointlightPos.x, pointlightPos.y, pointlightPos.z);
glUniform3f(glGetUniformLocation(program, "lightColor"), pointlightColor.x, pointlightColor.y, pointlightColor.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, "spotlightConeDir"), Spaceship::getInstance().spotlightConeDir.x, Spaceship::getInstance().spotlightConeDir.y, Spaceship::getInstance().spotlightConeDir.z);
glUniform3f(glGetUniformLocation(program, "spotlightPos"), spaceship.spotlightPos.x, spaceship.spotlightPos.y, spaceship.spotlightPos.z); glUniform3f(glGetUniformLocation(program, "spotlightPos"), Spaceship::getInstance().spotlightPos.x, Spaceship::getInstance().spotlightPos.y, Spaceship::getInstance().spotlightPos.z);
glUniform3f(glGetUniformLocation(program, "spotlightColor"), spaceship.spotlightColor.x, spaceship.spotlightColor.y, spaceship.spotlightColor.z); glUniform3f(glGetUniformLocation(program, "spotlightColor"), Spaceship::getInstance().spotlightColor.x, Spaceship::getInstance().spotlightColor.y, Spaceship::getInstance().spotlightColor.z);
glUniform1f(glGetUniformLocation(program, "spotlightPhi"), spaceship.spotlightPhi); glUniform1f(glGetUniformLocation(program, "spotlightPhi"), Spaceship::getInstance().spotlightPhi);
Core::DrawContext(context); Core::DrawContext(context);
} }
@ -152,19 +128,7 @@ void renderScene(GLFWwindow* window)
//space lamp //space lamp
glUseProgram(programSun); glUseProgram(programSun);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * spaceship.createCameraMatrix(); sun.draw();
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);
glUseProgram(program); glUseProgram(program);
@ -191,7 +155,7 @@ void renderScene(GLFWwindow* window)
// glm::vec3(0.3, 0.3, 0.5) // glm::vec3(0.3, 0.3, 0.5)
// ); // );
drawObjectPBR(shipContext, drawObjectPBR(shipContext,
spaceship.calculateModelMatrix(), Spaceship::getInstance().calculateModelMatrix(),
glm::vec3(0.3, 0.3, 0.5), glm::vec3(0.3, 0.3, 0.5),
0.2,1.0 0.2,1.0
); );
@ -208,7 +172,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)
{ {
aspectRatio = width / float(height); GameUtils::getInstance().aspectRatio = width / float(height);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
WIDTH = width; WIDTH = width;
HEIGHT = height; HEIGHT = height;
@ -249,11 +213,12 @@ void init(GLFWwindow* window)
loadModelToContext("./models/pencils.obj", models::pencilsContext); loadModelToContext("./models/pencils.obj", models::pencilsContext);
loadModelToContext("./models/plane.obj", models::planeContext); loadModelToContext("./models/plane.obj", models::planeContext);
loadModelToContext("./models/room.obj", models::roomContext); 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/sphere.obj", models::sphereContext);
loadModelToContext("./models/window.obj", models::windowContext); loadModelToContext("./models/window.obj", models::windowContext);
loadModelToContext("./models/test.obj", models::testContext); loadModelToContext("./models/test.obj", models::testContext);
sun = Sun(programSun, models::sphereContext);
} }
void shutdown(GLFWwindow* window) void shutdown(GLFWwindow* window)
@ -264,17 +229,16 @@ void shutdown(GLFWwindow* window)
//obsluga wejscia //obsluga wejscia
void processInput(GLFWwindow* window) 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; exposition -= 0.05;
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
exposition += 0.05; exposition += 0.05;*/
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) { 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("GameUtils::spaceshipPos = glm::vec3(%ff, %ff, %ff);\n", Spaceship::getInstance().spaceshipPos.x, Spaceship::getInstance().spaceshipPos.y, Spaceship::getInstance().spaceshipPos.z);
printf("spaceshipDir = glm::vec3(%ff, %ff, %ff);\n", spaceship.spaceshipDir.x, spaceship.spaceshipDir.y, spaceship.spaceshipDir.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); //cameraDir = glm::normalize(-cameraPos);