From cdce9652332acf685ed015d47191c978fbf880dc Mon Sep 17 00:00:00 2001 From: sasankasa Date: Sat, 3 Feb 2024 14:34:57 +0100 Subject: [PATCH] =?UTF-8?q?pr=C3=B3by=20ruszania=20chmurami=20-=20usuni?= =?UTF-8?q?=C4=99cie=20linii=20przy=20po=C5=82=C4=85czeniu=20chmur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- grk/cw 6/shaders/shader_noise.frag | 198 +++++------------------------ grk/cw 6/shaders/shader_pbr.frag | 41 +++--- grk/cw 6/shaders/shader_pbr.vert | 5 + grk/cw 6/src/Planet.hpp | 56 ++------ 4 files changed, 71 insertions(+), 229 deletions(-) diff --git a/grk/cw 6/shaders/shader_noise.frag b/grk/cw 6/shaders/shader_noise.frag index 2454642..1febbcb 100644 --- a/grk/cw 6/shaders/shader_noise.frag +++ b/grk/cw 6/shaders/shader_noise.frag @@ -1,164 +1,5 @@ #version 430 core -/*float PI = 3.14159f; - -uniform sampler2D colorTexture; - -uniform float exposition; - -in vec3 vecNormal; -in vec3 worldPos; -in vec2 vtc; - -out vec4 outColor; - -in vec3 viewDirTS; -in vec3 lightDirTS; -in vec3 sunDirTS; - -uniform float u_time; -float pi = 3.14159; -// 2D Random -float random (in vec2 st) { - return fract(sin(dot(st.xy, - vec2(12.9898,78.233))) - * 43758.5453123); -} - -// 2D Noise based on Morgan McGuire @morgan3d -// https://www.shadertoy.com/view/4dS3Wd -float noise (in vec2 st) { - vec2 i = floor(st); - vec2 f = fract(st); - - // Four corners in 2D of a tile - float a = random(i); - float b = random(i + vec2(1.0, 0.0)); - float c = random(i + vec2(0.0, 1.0)); - float d = random(i + vec2(1.0, 1.0)); - - vec2 randA = vec2(a *2.0 *pi); - vec2 randB = vec2(b *2.0 *pi); - vec2 randC = vec2(c *2.0 *pi); - vec2 randD = vec2(d *2.0 *pi); - - vec2 offsetA = a-st; - vec2 offsetB = b-st; - vec2 offsetC = c-st; - vec2 offsetD = d-st; - - offsetA = offsetA*randA; - offsetB = offsetB*randB; - offsetC = offsetC*randC; - offsetD = offsetD*randD; - - float resA = smoothstep(offsetA.x,offsetA.y, f.y); - float resB = smoothstep(offsetB.x,offsetB.y, f.y); - float resC = smoothstep(offsetC.x,offsetC.y, f.y); - float resD = smoothstep(offsetD.x,offsetD.y, f.y); - - vec2 u = smoothstep(0.,1.,f); - - - float ab= mix(a, b, u.x); - float cd = mix(c, d, u.x); - return mix(ab,cd,u.y); - -} - -void main() -{ - - vec2 st = vtc; - vec2 pos = vec2(st*3.776); - pos.x -= u_time * 0.07; - // Use the noise function - float n = 1.836 * 0.676*noise(pos*2.576); - n = smoothstep(-0.096, 1.176, n); - - outColor = vec4(vec3(n), 1); -} -*/ -/* -out vec4 outColor; -uniform float u_time; - -in vec2 vtc; - -float random (in vec2 _st) { - return fract(sin(dot(_st.xy, - vec2(12.9898,78.233)))* - 43758.5453123); -} - -// Based on Morgan McGuire @morgan3d -// https://www.shadertoy.com/view/4dS3Wd -float noise (in vec2 _st) { - vec2 i = floor(_st); - vec2 f = fract(_st); - - // Four corners in 2D of a tile - float a = random(i); - float b = random(i + vec2(1.0, 0.0)); - float c = random(i + vec2(0.0, 1.0)); - float d = random(i + vec2(1.0, 1.0)); - - vec2 u = f * f * (3.0 - 2.0 * f); - - return mix(a, b, u.x) + - (c - a)* u.y * (1.0 - u.x) + - (d - b) * u.x * u.y; -} - -#define NUM_OCTAVES 5 - -float fbm ( in vec2 _st) { - float v = 0.0; - float a = 0.5; - vec2 shift = vec2(100.0); - // Rotate to reduce axial bias - mat2 rot = mat2(cos(0.5), sin(0.5), - -sin(0.5), cos(0.50)); - for (int i = 0; i < NUM_OCTAVES; ++i) { - v += a * noise(_st); - _st = rot * _st * 3.0 + shift; - a *= 0.6; - } - return v; -} - -void main() { - vec2 st = vtc; - // st += st * abs(sin(u_time*0.1)*3.0); - vec3 color = vec3(0.0); - - vec2 q = vec2(0.); - q.x = fbm( st + 0.00*u_time); - q.y = fbm( st + vec2(1.0)); - - vec2 r = vec2(0.); - r.x = fbm( st + 1.0*q + vec2(1.7,9.2)+ 0.15*u_time ); - r.y = fbm( st + 1.0*q + vec2(8.3,2.8)+ 0.126*u_time); - - float f = fbm(st+r); - - color = mix(vec3(0.101961,0.619608,0.666667), - vec3(0.666667,0.666667,0.498039), - clamp((f*f)*4.0,0.0,1.0)); - - color = mix(color, - vec3(0.995,0.077,0.220), - clamp(length(q),0.0,1.0)); - - color = mix(color, - vec3(1.000,0.925,0.132), - clamp(length(r.x),0.0,1.0)); - - outColor = vec4((f*f*f+.9*f*f+.9*f)*color,1.); -} - -*/ -/* uniform float u_time; out vec4 outColor; in vec2 vtc; @@ -208,17 +49,40 @@ float fbm (in vec2 st) { } void main() { + +/* vec2 st = vtc; - //vec2 st = vtc.xy/worldPos.xy; - //st += st * abs(sin(u_time*0.1)*10.0); - //st.x *= worldPos.x/worldPos.y; + float cloudIntensity = 15.0; + + + + // Efekt lustrzanego odbicia + vec2 mirroredSt = vec2(1.0 - st.x, st.y); + mirroredSt.x += u_time * 0.07; + vec3 mirroredColor = vec3(0.0); + mirroredColor += fbm(mirroredSt * cloudIntensity); + + st.x -= u_time * 0.07; + // Efekt oryginalny + vec3 color = vec3(0.0); + color += fbm(st * cloudIntensity); + + // Dodaj gradientowe mieszanie między oryginałem a lustrzanym odbiciem + float gradient = smoothstep(0.45, 0.55, st.x); + vec3 finalColor = mix(color, mirroredColor, gradient); + + outColor = vec4(finalColor, 1.0); + */ + + vec2 st = vtc; + st.x -= u_time * 0.07; vec3 color = vec3(0.0); - color += fbm(st*3.0); + color += fbm(st*10.0); - vec3 mixedColor = mix(vec3(0.4), color, color.r); + //vec3 mixedColor = mix(vec3(0.4), color, color.r); //color = mix(vec3(1.0), fbm(st*3.0), 0.1); - outColor = vec4(mixedColor,1.0); - -}*/ \ No newline at end of file + outColor = vec4(color,1.0); + +} \ No newline at end of file diff --git a/grk/cw 6/shaders/shader_pbr.frag b/grk/cw 6/shaders/shader_pbr.frag index ff946fe..715a5d2 100644 --- a/grk/cw 6/shaders/shader_pbr.frag +++ b/grk/cw 6/shaders/shader_pbr.frag @@ -24,6 +24,7 @@ uniform float u_time; in vec3 vecNormal; in vec3 worldPos; in vec2 vtc; +in vec2 vtcNoise; out vec4 outColor; @@ -113,8 +114,6 @@ float random (in vec2 st) { 43758.5453123); } -// Based on Morgan McGuire @morgan3d -// https://www.shadertoy.com/view/4dS3Wd float noise (in vec2 st) { vec2 i = floor(st); vec2 f = fract(st); @@ -148,22 +147,30 @@ float fbm (in vec2 st) { return value; } -vec4 noiseColor() { - vec2 st = vtc; +vec4 noiseColor(float time) { float cloudIntensity = 15.0; - // Efekt lustrzanego odbicia + vec2 st = vtc.xy; vec2 mirroredSt = vec2(1.0 - st.x, st.y); - vec3 mirroredColor = vec3(0.0); - mirroredColor += fbm(mirroredSt * cloudIntensity); - // Efekt oryginalny - vec3 color = vec3(0.0); - color += fbm(st * cloudIntensity); + // Adjust animation to be boundary-aware + float timeOffset = time * 0.07; + st.x -= timeOffset; + mirroredSt.x += timeOffset; // Inverse direction for mirrored effect - // Dodaj gradientowe mieszanie między oryginałem a lustrzanym odbiciem - float gradient = smoothstep(0.45, 0.55, st.x); - vec3 finalColor = mix(color, mirroredColor, gradient); + // Ensure wrapping or clamping based on your texture coordinates' expectations + st.x = fract(st.x); + mirroredSt.x = fract(mirroredSt.x); + + // Calculate cloud patterns + vec3 color = vec3(fbm(st * cloudIntensity)); + vec3 mirroredColor = vec3(fbm(mirroredSt * cloudIntensity)); + + // Dynamic gradient based on X coordinate, ensuring smooth transition + float blend = smoothstep(0.45, 0.55, st.x); // Adjust these values as needed + + // Blend based on the dynamic gradient + vec3 finalColor = mix(color, mirroredColor, blend); vec4 noiseColor = vec4(finalColor, 1.0); @@ -196,9 +203,11 @@ void main() { textureColor = vec4(vec3(1.0) - exp(-illumination * exposition), 1); - vec4 noiseColor = noiseColor(); + vec4 noiseColor = noiseColor(u_time); - vec3 mixedColor = mix(textureColor.rgb, noiseColor.rgb, noiseColor.r); + vec3 mixedColor = mix(textureColor.rgb, noiseColor.rgb, u_time); - outColor = vec4(mixedColor * min(1, AMBIENT + diffuse), 1.0); + outColor = vec4(mixedColor, 1.0); + + //outColor = vec4(mixedColor * min(1, AMBIENT + diffuse), 1.0); } diff --git a/grk/cw 6/shaders/shader_pbr.vert b/grk/cw 6/shaders/shader_pbr.vert index ae1608d..d0b05bc 100644 --- a/grk/cw 6/shaders/shader_pbr.vert +++ b/grk/cw 6/shaders/shader_pbr.vert @@ -9,9 +9,13 @@ layout(location = 4) in vec3 vertexBitangent; uniform mat4 transformation; uniform mat4 modelMatrix; +uniform mat4 noiseTransformation; +uniform mat4 noiseMatrix; + out vec3 vecNormal; out vec3 worldPos; out vec2 vtc; +out vec2 vtcNoise; uniform vec3 lightPos; uniform vec3 cameraPos; @@ -28,6 +32,7 @@ void main() gl_Position = transformation * vec4(vertexPosition, 1.0); vtc = vec2(vertexTexCoord.x, 1.0 - vertexTexCoord.y); + vtcNoise = vec2(vertexTexCoord.x, 1.0 - vertexTexCoord.y); vec3 w_tangent = normalize(mat3(modelMatrix) * vertexTangent); vec3 w_bitangent = normalize(mat3(modelMatrix) * vertexBitangent); diff --git a/grk/cw 6/src/Planet.hpp b/grk/cw 6/src/Planet.hpp index fefc815..2d698ca 100644 --- a/grk/cw 6/src/Planet.hpp +++ b/grk/cw 6/src/Planet.hpp @@ -14,49 +14,6 @@ #include #include -/* -namespace texturePlanets { - GLuint blank; - GLuint earth; - GLuint planet; - GLuint mercury; - GLuint venus; - GLuint mars; - GLuint alpine; - GLuint ceres; - GLuint eris; - GLuint haumea; - GLuint icy; - GLuint jupiter; - GLuint makemake; - GLuint martian; - GLuint neptune; - GLuint saturn; - GLuint savannah; - GLuint swamp; - GLuint tropical; - GLuint uranus; - GLuint venusian; - GLuint volcanic; -} - -namespace textureSuns { - GLuint sun1; - GLuint sun2; - GLuint sun3; - GLuint sun4; -} - -namespace textureMoons { - GLuint moon1; - GLuint moon2; - GLuint moon3; -} - -namespace textureMaterials { - GLuint clouds; -} -*/ GLuint programDepth; GLuint programTex; GLuint programPbr; @@ -232,13 +189,16 @@ void drawSkyBox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint text glUseProgram(0); } -void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, float roughness, float metallic, float time) { +void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::mat4 noiseMatrix, GLuint texture, float roughness, float metallic, float time) { glUseProgram(programPbr); Core::SetActiveTexture(texture, "colorTexture", programPbr, 0); glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix(); glm::mat4 transformation = viewProjectionMatrix * modelMatrix; + glm::mat4 noiseTransformation = viewProjectionMatrix * noiseMatrix; glUniformMatrix4fv(glGetUniformLocation(programPbr, "transformation"), 1, GL_FALSE, (float*)&transformation); + glUniformMatrix4fv(glGetUniformLocation(programPbr, "noiseTransformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(programPbr, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); + glUniformMatrix4fv(glGetUniformLocation(programPbr, "noiseMatrix"), 1, GL_FALSE, (float*)&noiseMatrix); glUniform1f(glGetUniformLocation(programPbr, "exposition"), lightPower); @@ -283,10 +243,13 @@ void renderScene(GLFWwindow* window) { glm::mat4 planetRotate = glm::rotate(time * planetRot, glm::vec3(0, 1, 0)); glm::mat4 planetTranslate = glm::translate(planetPos); + glm::mat4 planetMatrix = planetTranslate * planetRotate * planetScale; + glm::mat4 noiseMatrix = planetTranslate * planetScale; + glm::mat4 cloudScale = glm::scale(glm::vec3(planetSize * 1.05f)); //drawPlanet(sphereContext, planetTranslate * planetRotate * planetScale, planetTex); - drawObjectPBR(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal, time); + drawObjectPBR(sphereContext, planetMatrix, noiseMatrix, planetTex, planetRough, planetMetal, time); //drawObjectNoise(sphereContext, planetTranslate * planetRotate * cloudScale, time); //rysowanie sĹ‚oĹ„ca @@ -336,7 +299,7 @@ void init(GLFWwindow* window) { programPbr = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_pbr.frag"); programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.frag"); programSkyBox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); - //programNoise = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_noise.frag"); + programNoise = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_noise.frag"); loadModelToContext("./models/sphere.obj", sphereContext); loadModelToContext("./models/cube.obj", cubeContext); @@ -353,6 +316,7 @@ void shutdown(GLFWwindow* window) { shaderLoader.DeleteProgram(programPbr); shaderLoader.DeleteProgram(programSun); shaderLoader.DeleteProgram(programSkyBox); + shaderLoader.DeleteProgram(programNoise); } //obsluga wejscia