From deec0c23005dfd51d53e36723861282abb1fc0fd Mon Sep 17 00:00:00 2001 From: Thyme1 Date: Sat, 8 Jan 2022 15:55:06 +0100 Subject: [PATCH] add fog on objects --- grafika_projekt/shaders/shader_tex.frag | 18 +++++++++++++++++- grafika_projekt/src/main.cpp | 5 ++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/grafika_projekt/shaders/shader_tex.frag b/grafika_projekt/shaders/shader_tex.frag index 5ec1fe9..6df6b6c 100644 --- a/grafika_projekt/shaders/shader_tex.frag +++ b/grafika_projekt/shaders/shader_tex.frag @@ -10,8 +10,24 @@ in vec3 fragPos; in vec3 interpNormal; in vec2 interpTexCoord; +float near = 0.02f; +float far = 100.0f; + +float linearizeDepth(float depth) +{ + return (2.0 * near * far) / (far + near - (depth * 2.0 - 1.0) * (far - near)); +} + +float logisticDepth(float depth, float steepness, float offset) +{ + float zVal = linearizeDepth(depth); + return (1 / (1 + exp(-steepness * (zVal - offset)))); +} + + void main() { + float depth = logisticDepth(gl_FragCoord.z, 0.1f, 3.0f); vec3 lightDir = normalize(lightPos-fragPos); vec3 V = normalize(cameraPos-fragPos); vec2 modifiedTexCoord = vec2(interpTexCoord.x, 1.0 - interpTexCoord.y); // Poprawka dla tekstur Ziemi, ktore bez tego wyswietlaja sie 'do gory nogami' @@ -21,6 +37,6 @@ void main() vec3 R = reflect(-normalize(lightDir),normal); float ambient = 0.2; float specular = pow(max(0,dot(R,V)),1000); - gl_FragColor = vec4(color*(ambient + (1-ambient)*diffuse)+vec3(1)*specular*0.2, 1.0); + gl_FragColor = vec4(color*(ambient + (1-ambient)*diffuse)+vec3(1)*specular*0.2, 1.0) * (1.0f - depth) + vec4(depth * vec3(0.0f, 0.109f, 0.447f), 1.0f);; } diff --git a/grafika_projekt/src/main.cpp b/grafika_projekt/src/main.cpp index df07d1f..fa7bfdf 100644 --- a/grafika_projekt/src/main.cpp +++ b/grafika_projekt/src/main.cpp @@ -258,9 +258,6 @@ void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::ve glUseProgram(program); - glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z); - glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z); - glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(program, "modelViewProjectionMatrix"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); @@ -293,7 +290,9 @@ void renderScene() cameraMatrix = createCameraMatrix(); perspectiveMatrix = Core::createPerspectiveMatrix(); + glClearColor(0.219f, 0.407f, 0.658f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + float time = glutGet(GLUT_ELAPSED_TIME) / 1000.f; glUseProgram(skyboxProgram); glUniform1i(glGetUniformLocation(skyboxProgram, "skybox"), 0);