Compare commits

..

5 Commits

Author SHA1 Message Date
Pawel Felcyn
2b390af6d9 move rendering sun to Sun class 2024-01-03 22:06:27 +01:00
Pawel Felcyn
034df6be24 move camera to spaceship 2024-01-03 21:11:13 +01:00
Pawel Felcyn
5c8faba1f8 spaceship light 2024-01-03 20:56:56 +01:00
Pawel Felcyn
5ee26a6c64 create spaceship class 2024-01-03 20:51:29 +01:00
Pawel Felcyn
4bdf8d5e8b seperate class for sun variables 2024-01-03 20:40:52 +01:00
8 changed files with 201 additions and 117 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;
}
};

95
grk/project/Spaceship.h Normal file
View File

@ -0,0 +1,95 @@
#include "glm.hpp"
#include "ext.hpp"
#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);
glm::vec3 spotlightPos = glm::vec3(0, 0, 0);
glm::vec3 spotlightConeDir = glm::vec3(0, 0, 0);
glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9) * 3;
float spotlightPhi = 3.14 / 4;
glm::vec3 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f);
glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f);
glm::mat4 calculateModelMatrix() {
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir));
glm::mat4 specshipCameraRotrationMatrix = glm::mat4({
spaceshipSide.x,spaceshipSide.y,spaceshipSide.z,0,
spaceshipUp.x,spaceshipUp.y,spaceshipUp.z ,0,
-spaceshipDir.x,-spaceshipDir.y,-spaceshipDir.z,0,
0.,0.,0.,1.,
});
return glm::translate(spaceshipPos) * specshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.03f));
}
void processInput(GLFWwindow* window, float deltaTime) {
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f);
float angleSpeed = 0.05f * deltaTime * 60;
float moveSpeed = 0.05f * deltaTime * 60;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
spaceshipPos += spaceshipDir * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
spaceshipPos -= spaceshipDir * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
spaceshipPos += spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
spaceshipPos -= spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
spaceshipPos += spaceshipUp * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
spaceshipPos -= spaceshipUp * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceshipDir, 0));
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
spaceshipDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceshipDir, 0));
cameraPos = spaceshipPos - 0.5 * spaceshipDir + glm::vec3(0, 1, 0) * 0.2f;
cameraDir = spaceshipDir;
spotlightPos = spaceshipPos + 0.2 * spaceshipDir;
spotlightConeDir = spaceshipDir;
}
glm::mat4 createCameraMatrix()
{
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, cameraDir));
glm::mat4 cameraRotrationMatrix = glm::mat4({
cameraSide.x,cameraSide.y,cameraSide.z,0,
cameraUp.x,cameraUp.y,cameraUp.z ,0,
-cameraDir.x,-cameraDir.y,-cameraDir.z,0,
0.,0.,0.,1.,
});
cameraRotrationMatrix = glm::transpose(cameraRotrationMatrix);
glm::mat4 cameraMatrix = cameraRotrationMatrix * glm::translate(-cameraPos);
return cameraMatrix;
}
};

31
grk/project/Sun.h Normal file
View File

@ -0,0 +1,31 @@
#include "glm.hpp"
#include "ext.hpp"
#include "./Spaceship.h"
#pragma once
class Sun
{
public:
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);
}
};

View File

@ -23,6 +23,8 @@
<ClCompile Include="src\Texture.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="GameUtils.h" />
<ClInclude Include="Spaceship.h" />
<ClInclude Include="src\Camera.h" />
<ClInclude Include="src\ex_9_1.hpp" />
<ClInclude Include="src\objload.h" />
@ -35,6 +37,7 @@
<ClInclude Include="src\SOIL\stbi_DDS_aug_c.h" />
<ClInclude Include="src\SOIL\stb_image_aug.h" />
<ClInclude Include="src\Texture.h" />
<ClInclude Include="Sun.h" />
</ItemGroup>
<ItemGroup>
<None Include="shaders\shader_9_1.frag" />

View File

@ -89,6 +89,15 @@
<ClInclude Include="src\ex_9_1.hpp">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="Sun.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Spaceship.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="GameUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="shaders\shader_8_sun.vert">

View File

@ -7,6 +7,7 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#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;
}

View File

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

View File

@ -14,6 +14,9 @@
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
#include "../Sun.h"
#include "../Spaceship.h"
#include "../GameUtils.h"
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
@ -49,32 +52,14 @@ Core::Shader_Loader shaderLoader;
Core::RenderContext shipContext;
Core::RenderContext sphereContext;
glm::vec3 sunPos = glm::vec3(-4.740971f, 2.149999f, 0.369280f);
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 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f);
glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f);
glm::vec3 spaceshipPos = glm::vec3(0.065808f, 1.250000f, -2.189549f);
glm::vec3 spaceshipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f);
Sun sun;
GLuint VAO,VBO;
float aspectRatio = 1.f;
float exposition = 1.f;
glm::vec3 pointlightPos = glm::vec3(0, 2, 0);
glm::vec3 pointlightColor = glm::vec3(0.9, 0.6, 0.6);
glm::vec3 spotlightPos = glm::vec3(0, 0, 0);
glm::vec3 spotlightConeDir = glm::vec3(0, 0, 0);
glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9)*3;
float spotlightPhi = 3.14 / 4;
float lastTime = -1.f;
float deltaTime = 0.f;
@ -88,46 +73,10 @@ void updateDeltaTime(float time) {
if (deltaTime > 0.1) deltaTime = 0.1;
lastTime = time;
}
glm::mat4 createCameraMatrix()
{
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f)));
glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide,cameraDir));
glm::mat4 cameraRotrationMatrix = glm::mat4({
cameraSide.x,cameraSide.y,cameraSide.z,0,
cameraUp.x,cameraUp.y,cameraUp.z ,0,
-cameraDir.x,-cameraDir.y,-cameraDir.z,0,
0.,0.,0.,1.,
});
cameraRotrationMatrix = glm::transpose(cameraRotrationMatrix);
glm::mat4 cameraMatrix = cameraRotrationMatrix * glm::translate(-cameraPos);
return cameraMatrix;
}
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() * 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);
@ -139,18 +88,18 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), Spaceship::getInstance().cameraPos.x, Spaceship::getInstance().cameraPos.y, Spaceship::getInstance().cameraPos.z);
glUniform3f(glGetUniformLocation(program, "sunDir"), sunDir.x, sunDir.y, sunDir.z);
glUniform3f(glGetUniformLocation(program, "sunColor"), sunColor.x, sunColor.y, sunColor.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, "lightPos"), pointlightPos.x, pointlightPos.y, pointlightPos.z);
glUniform3f(glGetUniformLocation(program, "lightColor"), pointlightColor.x, pointlightColor.y, pointlightColor.z);
glUniform3f(glGetUniformLocation(program, "spotlightConeDir"), spotlightConeDir.x, spotlightConeDir.y, spotlightConeDir.z);
glUniform3f(glGetUniformLocation(program, "spotlightPos"), spotlightPos.x, spotlightPos.y, spotlightPos.z);
glUniform3f(glGetUniformLocation(program, "spotlightColor"), spotlightColor.x, spotlightColor.y, spotlightColor.z);
glUniform1f(glGetUniformLocation(program, "spotlightPhi"), 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);
}
@ -179,12 +128,7 @@ void renderScene(GLFWwindow* window)
//space lamp
glUseProgram(programSun);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * 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"), sunColor.x / 2, sunColor.y / 2, sunColor.z / 2);
glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition);
Core::DrawContext(sphereContext);
sun.draw();
glUseProgram(program);
@ -206,31 +150,16 @@ void renderScene(GLFWwindow* window)
drawObjectPBR(models::roomContext, glm::mat4(), glm::vec3(0.9f, 0.9f, 0.9f), 0.8f, 0.0f);
drawObjectPBR(models::windowContext, glm::mat4(), glm::vec3(0.402978f, 0.120509f, 0.057729f), 0.2f, 0.0f);
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir));
glm::mat4 specshipCameraRotrationMatrix = glm::mat4({
spaceshipSide.x,spaceshipSide.y,spaceshipSide.z,0,
spaceshipUp.x,spaceshipUp.y,spaceshipUp.z ,0,
-spaceshipDir.x,-spaceshipDir.y,-spaceshipDir.z,0,
0.,0.,0.,1.,
});
//drawObjectColor(shipContext,
// glm::translate(cameraPos + 1.5 * cameraDir + cameraUp * -0.5f) * inveseCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()),
// glm::vec3(0.3, 0.3, 0.5)
// );
drawObjectPBR(shipContext,
glm::translate(spaceshipPos) * specshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.03f)),
Spaceship::getInstance().calculateModelMatrix(),
glm::vec3(0.3, 0.3, 0.5),
0.2,1.0
);
spotlightPos = spaceshipPos + 0.2 * spaceshipDir;
spotlightConeDir = spaceshipDir;
//test depth buffer
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glUseProgram(programTest);
@ -243,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;
@ -284,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)
@ -299,41 +229,16 @@ void shutdown(GLFWwindow* window)
//obsluga wejscia
void processInput(GLFWwindow* window)
{
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f,1.f,0.f)));
glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f);
float angleSpeed = 0.05f * deltaTime * 60;
float moveSpeed = 0.05f * deltaTime * 60;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
spaceshipPos += spaceshipDir * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
spaceshipPos -= spaceshipDir * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
spaceshipPos += spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
spaceshipPos -= spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
spaceshipPos += spaceshipUp * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
spaceshipPos -= spaceshipUp * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceshipDir, 0));
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
spaceshipDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceshipDir, 0));
Spaceship::getInstance().processInput(window, deltaTime);
cameraPos = spaceshipPos - 0.5 * spaceshipDir + glm::vec3(0, 1, 0) * 0.2f;
cameraDir = spaceshipDir;
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", spaceshipPos.x, spaceshipPos.y, spaceshipPos.z);
printf("spaceshipDir = glm::vec3(%ff, %ff, %ff);\n", spaceshipDir.x, spaceshipDir.y, 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);