grafika_komputerowa/grk/project/src/ex_9_1.hpp

258 lines
9.4 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-03 22:06:27 +01:00
#include "../GameUtils.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
GLuint VAO,VBO;
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;
}
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic) {
2024-01-03 22:06:27 +01:00
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * Spaceship::getInstance().createCameraMatrix();
2024-01-02 20:54:56 +01:00
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);
2024-01-03 22:06:27 +01:00
glUniform3f(glGetUniformLocation(program, "cameraPos"), Spaceship::getInstance().cameraPos.x, Spaceship::getInstance().cameraPos.y, Spaceship::getInstance().cameraPos.z);
2024-01-02 20:54:56 +01:00
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 22:06:27 +01:00
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);
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);
2024-01-03 22:06:27 +01:00
sun.draw();
2024-01-03 20:59:48 +01:00
2024-01-02 20:54:56 +01:00
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 22:06:27 +01:00
Spaceship::getInstance().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)
{
2024-01-03 22:06:27 +01:00
GameUtils::getInstance().aspectRatio = width / float(height);
2024-01-02 20:54:56 +01:00
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);
2024-01-03 22:06:27 +01:00
loadModelToContext("./models/GameUtils::spaceship.obj", models::spaceshipContext);
2024-01-02 20:54:56 +01:00
loadModelToContext("./models/sphere.obj", models::sphereContext);
loadModelToContext("./models/window.obj", models::windowContext);
loadModelToContext("./models/test.obj", models::testContext);
2024-01-03 22:06:27 +01:00
sun = Sun(programSun, models::sphereContext);
2024-01-02 20:54:56 +01:00
}
void shutdown(GLFWwindow* window)
{
shaderLoader.DeleteProgram(program);
}
//obsluga wejscia
void processInput(GLFWwindow* window)
{
2024-01-03 22:06:27 +01:00
Spaceship::getInstance().processInput(window, deltaTime);
2024-01-03 20:51:29 +01:00
2024-01-03 22:06:27 +01:00
/*if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
2024-01-02 20:54:56 +01:00
exposition -= 0.05;
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
2024-01-03 22:06:27 +01:00
exposition += 0.05;*/
2024-01-02 20:54:56 +01:00
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) {
2024-01-03 22:06:27 +01:00
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);
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();
}
}
//}