353 lines
13 KiB
C++
353 lines
13 KiB
C++
|
#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>
|
|||
|
|
|||
|
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;
|
|||
|
|
|||
|
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);
|
|||
|
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;
|
|||
|
|
|||
|
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);
|
|||
|
|
|||
|
glUniform3f(glGetUniformLocation(program, "sunDir"), sunDir.x, sunDir.y, sunDir.z);
|
|||
|
glUniform3f(glGetUniformLocation(program, "sunColor"), sunColor.x, sunColor.y, 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);
|
|||
|
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);
|
|||
|
glUniform3f(glGetUniformLocation(programSun, "color"), sunColor.x / 2, sunColor.y / 2, sunColor.z / 2);
|
|||
|
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);
|
|||
|
|
|||
|
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)),
|
|||
|
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);
|
|||
|
//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)
|
|||
|
{
|
|||
|
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;
|
|||
|
|
|||
|
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) {
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
//cameraDir = glm::normalize(-cameraPos);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// funkcja jest glowna petla
|
|||
|
void renderLoop(GLFWwindow* window) {
|
|||
|
while (!glfwWindowShouldClose(window))
|
|||
|
{
|
|||
|
processInput(window);
|
|||
|
|
|||
|
renderScene(window);
|
|||
|
glfwPollEvents();
|
|||
|
}
|
|||
|
}
|
|||
|
//}
|