From 27eb43b140cdafc5da3f500746527794b24bee72 Mon Sep 17 00:00:00 2001 From: sasankasa Date: Mon, 29 Jan 2024 19:41:22 +0100 Subject: [PATCH] szum kolorkowy --- grk/cw 6/shaders/shader_noise.frag | 146 ++++++++++++++++++++++++++++- grk/cw 6/src/Planet.hpp | 9 +- 2 files changed, 151 insertions(+), 4 deletions(-) diff --git a/grk/cw 6/shaders/shader_noise.frag b/grk/cw 6/shaders/shader_noise.frag index 8ec9a97..32fa2c2 100644 --- a/grk/cw 6/shaders/shader_noise.frag +++ b/grk/cw 6/shaders/shader_noise.frag @@ -1,6 +1,6 @@ #version 430 core -float PI = 3.14159f; +/*float PI = 3.14159f; uniform sampler2D colorTexture; @@ -71,10 +71,152 @@ 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; +in vec3 worldPos; + +vec2 u_resolution = vec2(1024, 1024); + +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 OCTAVES 6 +float fbm (in vec2 st) { + // Initial values + float value = 0.0; + float amplitude = .5; + float frequency = 0.; + // + // Loop of octaves + for (int i = 0; i < OCTAVES; i++) { + value += amplitude * noise(st); + st *= 2.; + amplitude *= .5; + } + return value; +} + +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; + + vec3 color = vec3(0.0); + color += fbm(st*3.0); + + 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 diff --git a/grk/cw 6/src/Planet.hpp b/grk/cw 6/src/Planet.hpp index ed7ed58..10b126f 100644 --- a/grk/cw 6/src/Planet.hpp +++ b/grk/cw 6/src/Planet.hpp @@ -257,12 +257,15 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t glUseProgram(0); } -void drawObjectNoise(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, float roughness, float metallic) { +void drawObjectNoise(Core::RenderContext& context, glm::mat4 modelMatrix, float time) { + + glUseProgram(programNoise); glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix(); glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(programNoise, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(programNoise, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); + glUniform1f(glGetUniformLocation(programNoise, "u_time"), time); Core::DrawContext(context); glUseProgram(0); @@ -279,9 +282,11 @@ void renderScene(GLFWwindow* window) { glm::mat4 planetRotate = glm::rotate(time * planetRot, glm::vec3(0, 1, 0)); glm::mat4 planetTranslate = glm::translate(planetPos); + glm::mat4 cloudScale = glm::scale(glm::vec3(planetSize * 1.05f)); + //drawPlanet(sphereContext, planetTranslate * planetRotate * planetScale, planetTex); drawObjectPBR(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal); - drawObjectNoise(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal); + drawObjectNoise(sphereContext, planetTranslate * planetRotate * cloudScale, time); //rysowanie słońca glm::mat4 sunScale = glm::scale(glm::vec3(sunSize));