diff --git a/cw 9/shaders/shader_5_1_tex.frag b/cw 9/shaders/shader_5_1_tex.frag new file mode 100644 index 0000000..951c43e --- /dev/null +++ b/cw 9/shaders/shader_5_1_tex.frag @@ -0,0 +1,18 @@ +#version 430 core + +float AMBIENT = 0.1; + +uniform vec3 color; +uniform vec3 lightPos; + +in vec3 vecNormal; +in vec3 worldPos; + +out vec4 outColor; +void main() +{ + vec3 lightDir = normalize(lightPos-worldPos); + vec3 normal = normalize(vecNormal); + float diffuse=max(0,dot(normal,lightDir)); + outColor = vec4(color*min(1,AMBIENT+diffuse), 1.0); +} diff --git a/cw 9/shaders/shader_5_1_tex.vert b/cw 9/shaders/shader_5_1_tex.vert new file mode 100644 index 0000000..1b569f5 --- /dev/null +++ b/cw 9/shaders/shader_5_1_tex.vert @@ -0,0 +1,18 @@ +#version 430 core + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 vertexNormal; +layout(location = 2) in vec2 vertexTexCoord; + +uniform mat4 transformation; +uniform mat4 modelMatrix; + +out vec3 vecNormal; +out vec3 worldPos; + +void main() +{ + worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz; + vecNormal = (modelMatrix* vec4(vertexNormal,0)).xyz; + gl_Position = transformation * vec4(vertexPosition, 1.0); +} diff --git a/cw 9/shaders/shader_8_sun.frag b/cw 9/shaders/shader_8_sun.frag new file mode 100644 index 0000000..1dd40b8 --- /dev/null +++ b/cw 9/shaders/shader_8_sun.frag @@ -0,0 +1,11 @@ +#version 430 core + +uniform vec3 color; +uniform float exposition; + + +out vec4 outColor; +void main() +{ + outColor = vec4(vec3(1.0) - exp(-color*exposition),1); +} diff --git a/cw 9/shaders/shader_8_sun.vert b/cw 9/shaders/shader_8_sun.vert new file mode 100644 index 0000000..57204e2 --- /dev/null +++ b/cw 9/shaders/shader_8_sun.vert @@ -0,0 +1,13 @@ +#version 430 core + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 vertexNormal; +layout(location = 2) in vec2 vertexTexCoord; + +uniform mat4 transformation; + +void main() +{ + gl_Position = transformation * vec4(vertexPosition, 1.0); + //gl_Position = vec4(vertexPosition, 1.0); +} diff --git a/cw 9/shaders/shader_9_1.frag b/cw 9/shaders/shader_9_1.frag new file mode 100644 index 0000000..1dfab18 --- /dev/null +++ b/cw 9/shaders/shader_9_1.frag @@ -0,0 +1,146 @@ +#version 430 core + +float AMBIENT = 0.03; +float PI = 3.14; + +uniform sampler2D depthMap; + +uniform vec3 cameraPos; + +uniform vec3 color; + +uniform vec3 sunDir; +uniform vec3 sunColor; + +uniform vec3 lightPos; +uniform vec3 lightColor; + +uniform vec3 spotlightPos; +uniform vec3 spotlightColor; +uniform vec3 spotlightConeDir; +uniform vec3 spotlightPhi; + +uniform float metallic; +uniform float roughness; + +uniform float exposition; + +in vec3 vecNormal; +in vec3 worldPos; + +out vec4 outColor; + + +in vec3 viewDirTS; +in vec3 lightDirTS; +in vec3 spotlightDirTS; +in vec3 sunDirTS; +in vec4 sunSpacePos; + +in vec3 test; + +float calculateShadow() { + vec4 sunSpacePosU = sunSpacePos / sunSpacePos.w; + vec4 sunSpacePosNormalized = sunSpacePosU * 0.5 + 0.5; + float closestDepth = texture2D(depthMap, worldPos.xy).r; + if (closestDepth > sunSpacePosNormalized.z) { + return 1; + } + return 0; +} + +float DistributionGGX(vec3 normal, vec3 H, float roughness){ + float a = roughness*roughness; + float a2 = a*a; + float NdotH = max(dot(normal, H), 0.0); + float NdotH2 = NdotH*NdotH; + + float num = a2; + float denom = (NdotH2 * (a2 - 1.0) + 1.0); + denom = PI * denom * denom; + + return num / denom; +} +float GeometrySchlickGGX(float NdotV, float roughness){ + float r = (roughness + 1.0); + float k = (r*r) / 8.0; + + float num = NdotV; + float denom = NdotV * (1.0 - k) + k; + + return num / denom; +} +float GeometrySmith(vec3 normal, vec3 V, vec3 lightDir, float roughness){ + float NdotV = max(dot(normal, V), 0.0); + float NdotL = max(dot(normal, lightDir), 0.0); + float ggx2 = GeometrySchlickGGX(NdotV, roughness); + float ggx1 = GeometrySchlickGGX(NdotL, roughness); + + return ggx1 * ggx2; +} +vec3 fresnelSchlick(float cosTheta, vec3 F0){ + return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); +} + +vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V){ + float diffuse=max(0,dot(normal,lightDir)); + + //vec3 V = normalize(cameraPos-worldPos); + vec3 F0 = vec3(0.04); + F0 = mix(F0, color, metallic); + + vec3 H = normalize(V + lightDir); + + // cook-torrance brdf + float NDF = DistributionGGX(normal, H, roughness); + float G = GeometrySmith(normal, V, lightDir, roughness); + vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); + + vec3 kS = F; + vec3 kD = vec3(1.0) - kS; + kD *= 1.0 - metallic; + + vec3 numerator = NDF * G * F; + float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, lightDir), 0.0) + 0.0001; + vec3 specular = numerator / denominator; + + // add to outgoing radiance Lo + float NdotL = max(dot(normal, lightDir), 0.0); + return (kD * color / PI + specular) * radiance * NdotL; +} + + +void main() +{ + //vec3 normal = vec3(0,0,1); + vec3 normal = normalize(vecNormal); + + //vec3 viewDir = normalize(viewDirTS); + vec3 viewDir = normalize(cameraPos-worldPos); + + //vec3 lightDir = normalize(lightDirTS); + vec3 lightDir = normalize(lightPos-worldPos); + + + vec3 ambient = AMBIENT*color; + vec3 attenuatedlightColor = lightColor/pow(length(lightPos-worldPos),2); + vec3 ilumination; + ilumination = ambient+PBRLight(lightDir,attenuatedlightColor,normal,viewDir); + + //flashlight + //vec3 spotlightDir= normalize(spotlightDirTS); + vec3 spotlightDir= normalize(spotlightPos-worldPos); + + + float angle_atenuation = clamp((dot(-normalize(spotlightPos-worldPos),spotlightConeDir)-0.5)*3,0,1); + attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2); + ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor,normal,viewDir); + + //sun + ilumination=ilumination+PBRLight(sunDir, sunColor * calculateShadow(), normal, viewDir); + + + outColor = vec4(vec3(1.0) - exp(-ilumination*exposition),1); + //outColor = vec4(roughness,metallic,0,1); + //outColor = vec4(test; +} diff --git a/cw 9/shaders/shader_9_1.vert b/cw 9/shaders/shader_9_1.vert new file mode 100644 index 0000000..25437fd --- /dev/null +++ b/cw 9/shaders/shader_9_1.vert @@ -0,0 +1,46 @@ +#version 430 core + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 vertexNormal; +layout(location = 2) in vec2 vertexTexCoord; +layout(location = 3) in vec3 vertexTangent; +layout(location = 4) in vec3 vertexBitangent; + +uniform mat4 transformation; +uniform mat4 modelMatrix; +uniform mat4 LightVP; + +out vec3 vecNormal; +out vec3 worldPos; + +uniform vec3 lightPos; +uniform vec3 spotlightPos; +uniform vec3 cameraPos; +uniform vec3 sunDir; + +out vec3 viewDirTS; +out vec3 lightDirTS; +out vec3 spotlightDirTS; +out vec3 sunDirTS; +out vec4 sunSpacePos; + +void main() +{ + worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz; + vecNormal = (modelMatrix* vec4(vertexNormal,0)).xyz; + gl_Position = transformation * vec4(vertexPosition, 1.0); + + vec3 w_tangent = normalize(mat3(modelMatrix)*vertexTangent); + vec3 w_bitangent = normalize(mat3(modelMatrix)*vertexBitangent); + mat3 TBN = transpose(mat3(w_tangent, w_bitangent, vecNormal)); + + vec3 V = normalize(cameraPos-worldPos); + viewDirTS = TBN*V; + vec3 L = normalize(lightPos-worldPos); + lightDirTS = TBN*L; + vec3 SL = normalize(spotlightPos-worldPos); + spotlightDirTS = TBN*SL; + sunDirTS = TBN*sunDir; + + sunSpacePos=LightVP*modelMatrix*vec4(vertexPosition,1); +} diff --git a/cw 9/shaders/shadow_shader.frag b/cw 9/shaders/shadow_shader.frag new file mode 100644 index 0000000..ad0b0dd --- /dev/null +++ b/cw 9/shaders/shadow_shader.frag @@ -0,0 +1,5 @@ +#version 430 core + +void main() +{ +} \ No newline at end of file diff --git a/cw 9/shaders/shadow_shader.vert b/cw 9/shaders/shadow_shader.vert new file mode 100644 index 0000000..c018240 --- /dev/null +++ b/cw 9/shaders/shadow_shader.vert @@ -0,0 +1,13 @@ +#version 430 core + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 vertexNormal; +layout(location = 2) in vec2 vertexTexCoord; + +uniform mat4 viewProjectionMatrix; +uniform mat4 modelMatrix; + +void main() +{ + gl_Position = viewProjectionMatrix * modelMatrix * vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/cw 9/shaders/test.frag b/cw 9/shaders/test.frag new file mode 100644 index 0000000..404c2c6 --- /dev/null +++ b/cw 9/shaders/test.frag @@ -0,0 +1,12 @@ +#version 330 core +out vec4 FragColor; + +in vec2 tc; + +uniform sampler2D depthMap; + +void main() +{ + float depthValue = texture(depthMap, tc).r; + FragColor = vec4(vec3(depthValue+0.5), 1.0); +} \ No newline at end of file diff --git a/cw 9/shaders/test.vert b/cw 9/shaders/test.vert new file mode 100644 index 0000000..f65f59d --- /dev/null +++ b/cw 9/shaders/test.vert @@ -0,0 +1,14 @@ +#version 430 core + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 vertexNormal; +layout(location = 2) in vec2 vertexTexCoord; + + +out vec2 tc; + +void main() +{ + tc = vertexTexCoord; + gl_Position = vec4(vertexPosition*0.9, 1.0); +} diff --git a/cw 9/src/ex_9_1.hpp b/cw 9/src/ex_9_1.hpp new file mode 100644 index 0000000..db2feb7 --- /dev/null +++ b/cw 9/src/ex_9_1.hpp @@ -0,0 +1,431 @@ +#include "glew.h" +#include +#include "glm.hpp" +#include "ext.hpp" +#include +#include + +#include "Shader_Loader.h" +#include "Render_Utils.h" +//#include "Texture.h" + +#include "Box.cpp" +#include +#include +#include +#include + +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; + +GLuint programDepth; + +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) { + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, depthMap); + glm::mat4 lightVP = glm::ortho(-3.f, 3.f, -3.f, 3.f, 1.f, 30.0f) * glm::lookAt(sunPos, sunPos - sunDir, glm::vec3(0, 1, 0)); + glUniformMatrix4fv(glGetUniformLocation(program, "LightVP"), 1, GL_FALSE, (float*)&lightVP); + + 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 drawObjectDepth(Core::RenderContext& context, glm::mat4 modelMatrix, glm::mat4 viewProjection) { + glUniformMatrix4fv(glGetUniformLocation(programDepth, "viewProjectionMatrix"), 1, GL_FALSE, (float*)&viewProjection); + glUniformMatrix4fv(glGetUniformLocation(programDepth, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); + + Core::DrawContext(context); +} + +void renderShadowapSun() { + float time = glfwGetTime(); + //uzupelnij o renderowanie glebokosci do tekstury + + //ustawianie przestrzeni rysowania + glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); + //bindowanie FBO + glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); + //czyszczenie mapy głębokości + glClear(GL_DEPTH_BUFFER_BIT); + //ustawianie programu + glUseProgram(programDepth); + + //viewProjection matrix + glm::mat4 lightVP = glm::ortho(-3.f, 3.f, -3.f, 3.f, 1.f, 30.0f) * glm::lookAt(sunPos, sunPos - sunDir, glm::vec3(0, 1, 0)); + + //draw + drawObjectDepth(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)), + lightVP); + + drawObjectDepth(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)), + lightVP); + + drawObjectDepth(models::bedContext, glm::mat4(), lightVP); + drawObjectDepth(models::chairContext, glm::mat4(), lightVP); + drawObjectDepth(models::deskContext, glm::mat4(), lightVP); + drawObjectDepth(models::doorContext, glm::mat4(), lightVP); + drawObjectDepth(models::drawerContext, glm::mat4(), lightVP); + drawObjectDepth(models::marbleBustContext, glm::mat4(), lightVP); + drawObjectDepth(models::materaceContext, glm::mat4(), lightVP); + drawObjectDepth(models::pencilsContext, glm::mat4(), lightVP); + drawObjectDepth(models::planeContext, glm::mat4(), lightVP); + drawObjectDepth(models::roomContext, glm::mat4(), lightVP); + drawObjectDepth(models::windowContext, glm::mat4(), lightVP); + + 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., + }); + + + drawObjectDepth(shipContext, + glm::translate(spaceshipPos) * specshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi()) * glm::scale(glm::vec3(0.03f)), + lightVP + ); + + + 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()), + // glm::vec3(0.3, 0.3, 0.5) + // ); + drawObjectPBR(shipContext, + glm::translate(spaceshipPos) * specshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi()) * 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 initDepthMap() { + glGenFramebuffers(1, &depthMapFBO); + + glGenTextures(1, &depthMap); + glBindTexture(GL_TEXTURE_2D, depthMap); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, + SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + glBindFramebuffer(GL_FRAMEBUFFER, 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"); + programDepth = shaderLoader.CreateProgram("shaders/shadow_shader.vert", "shaders/shadow_shader.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); + + initDepthMap(); +} + +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(); + } +} +//} \ No newline at end of file