grafika_komputerowa/grk/project/src/ex_9_1.hpp

308 lines
11 KiB
C++
Raw Normal View History

2024-01-02 20:54:56 +01:00
#include "glew.h"
#include <GLFW/glfw3.h>
#include "glm.hpp"
#include "ext.hpp"
#include <iostream>
#include <cmath>
#include "Shader_Loader.h"
#include "Render_Utils.h"
//#include "Texture.h"
#include "Box.cpp"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
2024-01-03 20:40:52 +01:00
#include "../Sun.h"
2024-01-03 20:51:29 +01:00
#include "../Spaceship.h"
2024-01-02 20:54:56 +01:00
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
int WIDTH = 500, HEIGHT = 500;
namespace models {
Core::RenderContext bedContext;
Core::RenderContext chairContext;
Core::RenderContext deskContext;
Core::RenderContext doorContext;
Core::RenderContext drawerContext;
Core::RenderContext marbleBustContext;
Core::RenderContext materaceContext;
Core::RenderContext pencilsContext;
Core::RenderContext planeContext;
Core::RenderContext roomContext;
Core::RenderContext spaceshipContext;
Core::RenderContext sphereContext;
Core::RenderContext windowContext;
Core::RenderContext testContext;
}
GLuint depthMapFBO;
GLuint depthMap;
GLuint program;
GLuint programSun;
GLuint programTest;
GLuint programTex;
Core::Shader_Loader shaderLoader;
Core::RenderContext shipContext;
Core::RenderContext sphereContext;
2024-01-03 20:40:52 +01:00
Sun sun;
2024-01-02 20:54:56 +01:00
glm::vec3 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f);
glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f);
2024-01-03 20:51:29 +01:00
Spaceship spaceship;
2024-01-02 20:54:56 +01:00
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);
float lastTime = -1.f;
float deltaTime = 0.f;
void updateDeltaTime(float time) {
if (lastTime < 0) {
lastTime = time;
return;
}
deltaTime = time - lastTime;
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 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform1f(glGetUniformLocation(program, "exposition"), exposition);
glUniform1f(glGetUniformLocation(program, "roughness"), roughness);
glUniform1f(glGetUniformLocation(program, "metallic"), metallic);
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
2024-01-03 20:40:52 +01:00
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);
2024-01-02 20:54:56 +01:00
glUniform3f(glGetUniformLocation(program, "lightPos"), pointlightPos.x, pointlightPos.y, pointlightPos.z);
glUniform3f(glGetUniformLocation(program, "lightColor"), pointlightColor.x, pointlightColor.y, pointlightColor.z);
2024-01-03 20:56:56 +01:00
glUniform3f(glGetUniformLocation(program, "spotlightConeDir"), spaceship.spotlightConeDir.x, spaceship.spotlightConeDir.y, spaceship.spotlightConeDir.z);
glUniform3f(glGetUniformLocation(program, "spotlightPos"), spaceship.spotlightPos.x, spaceship.spotlightPos.y, spaceship.spotlightPos.z);
glUniform3f(glGetUniformLocation(program, "spotlightColor"), spaceship.spotlightColor.x, spaceship.spotlightColor.y, spaceship.spotlightColor.z);
glUniform1f(glGetUniformLocation(program, "spotlightPhi"), spaceship.spotlightPhi);
2024-01-02 20:54:56 +01:00
Core::DrawContext(context);
}
void renderShadowapSun() {
float time = glfwGetTime();
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
//uzupelnij o renderowanie glebokosci do tekstury
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT);
}
void renderScene(GLFWwindow* window)
{
glClearColor(0.4f, 0.4f, 0.8f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float time = glfwGetTime();
updateDeltaTime(time);
renderShadowapSun();
//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);
2024-01-03 20:40:52 +01:00
glUniform3f(glGetUniformLocation(programSun, "color"), sun.sunColor.x / 2, sun.sunColor.y / 2, sun.sunColor.z / 2);
2024-01-02 20:54:56 +01:00
glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition);
Core::DrawContext(sphereContext);
glUseProgram(program);
drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0);
drawObjectPBR(sphereContext,
glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::translate(glm::vec3(1.f, 0, 0)) * glm::scale(glm::vec3(0.1f)),
glm::vec3(0.5, 0.5, 0.5), 0.7, 0.0);
drawObjectPBR(models::bedContext, glm::mat4(), glm::vec3(0.03f, 0.03f, 0.03f), 0.2f, 0.0f);
drawObjectPBR(models::chairContext, glm::mat4(), glm::vec3(0.195239f, 0.37728f, 0.8f), 0.4f, 0.0f);
drawObjectPBR(models::deskContext, glm::mat4(), glm::vec3(0.428691f, 0.08022f, 0.036889f), 0.2f, 0.0f);
drawObjectPBR(models::doorContext, glm::mat4(), glm::vec3(0.402978f, 0.120509f, 0.057729f), 0.2f, 0.0f);
drawObjectPBR(models::drawerContext, glm::mat4(), glm::vec3(0.428691f, 0.08022f, 0.036889f), 0.2f, 0.0f);
drawObjectPBR(models::marbleBustContext, glm::mat4(), glm::vec3(1.f, 1.f, 1.f), 0.5f, 1.0f);
drawObjectPBR(models::materaceContext, glm::mat4(), glm::vec3(0.9f, 0.9f, 0.9f), 0.8f, 0.0f);
drawObjectPBR(models::pencilsContext, glm::mat4(), glm::vec3(0.10039f, 0.018356f, 0.001935f), 0.1f, 0.0f);
drawObjectPBR(models::planeContext, glm::mat4(), glm::vec3(0.402978f, 0.120509f, 0.057729f), 0.2f, 0.0f);
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);
//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,
2024-01-03 20:51:29 +01:00
spaceship.calculateModelMatrix(),
2024-01-02 20:54:56 +01:00
glm::vec3(0.3, 0.3, 0.5),
0.2,1.0
);
//test depth buffer
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glUseProgram(programTest);
//glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, depthMap);
//Core::DrawContext(models::testContext);
glUseProgram(0);
glfwSwapBuffers(window);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
aspectRatio = width / float(height);
glViewport(0, 0, width, height);
WIDTH = width;
HEIGHT = height;
}
void loadModelToContext(std::string path, Core::RenderContext& context)
{
Assimp::Importer import;
const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl;
return;
}
context.initFromAssimpMesh(scene->mMeshes[0]);
}
void init(GLFWwindow* window)
{
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag");
programTest = shaderLoader.CreateProgram("shaders/test.vert", "shaders/test.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag");
loadModelToContext("./models/sphere.obj", sphereContext);
loadModelToContext("./models/spaceship.obj", shipContext);
loadModelToContext("./models/bed.obj", models::bedContext);
loadModelToContext("./models/chair.obj", models::chairContext);
loadModelToContext("./models/desk.obj", models::deskContext);
loadModelToContext("./models/door.obj", models::doorContext);
loadModelToContext("./models/drawer.obj", models::drawerContext);
loadModelToContext("./models/marbleBust.obj", models::marbleBustContext);
loadModelToContext("./models/materace.obj", models::materaceContext);
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/sphere.obj", models::sphereContext);
loadModelToContext("./models/window.obj", models::windowContext);
loadModelToContext("./models/test.obj", models::testContext);
}
void shutdown(GLFWwindow* window)
{
shaderLoader.DeleteProgram(program);
}
//obsluga wejscia
void processInput(GLFWwindow* window)
{
2024-01-03 20:51:29 +01:00
spaceship.processInput(window, deltaTime);
cameraPos = spaceship.spaceshipPos - 0.5 * spaceship.spaceshipDir + glm::vec3(0, 1, 0) * 0.2f;
cameraDir = spaceship.spaceshipDir;
2024-01-02 20:54:56 +01:00
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
exposition -= 0.05;
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
exposition += 0.05;
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) {
2024-01-03 20:51:29 +01:00
printf("spaceshipPos = glm::vec3(%ff, %ff, %ff);\n", spaceship.spaceshipPos.x, spaceship.spaceshipPos.y, spaceship.spaceshipPos.z);
printf("spaceshipDir = glm::vec3(%ff, %ff, %ff);\n", spaceship.spaceshipDir.x, spaceship.spaceshipDir.y, spaceship.spaceshipDir.z);
2024-01-02 20:54:56 +01:00
}
//cameraDir = glm::normalize(-cameraPos);
}
// funkcja jest glowna petla
void renderLoop(GLFWwindow* window) {
while (!glfwWindowShouldClose(window))
{
processInput(window);
renderScene(window);
glfwPollEvents();
}
}
//}