From ee0c5a43c122991f1ae51a4c0870d1d6675f3533 Mon Sep 17 00:00:00 2001 From: kdx Date: Mon, 22 May 2023 00:43:59 +0200 Subject: random dithering --- src/fragment.glsl | 21 ++++++++++++--------- src/main.c | 3 ++- src/vertex.glsl | 3 +++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/fragment.glsl b/src/fragment.glsl index edeccab..e233466 100644 --- a/src/fragment.glsl +++ b/src/fragment.glsl @@ -2,23 +2,26 @@ out vec4 FragColor; in vec2 texCoord; +in float randOffset; uniform sampler2D texture0; uniform sampler2D texture1; +/* thank you internet i guess + * note: this has terrible distribution, not sure if i care */ +float +rand(vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))+randOffset) * 43758.5453); +} + void main() { vec4 col = mix(texture(texture0, texCoord), texture(texture1, texCoord), 0.2); - col.r = (col.r < 0.5) ? 0.0 : 1.0; - col.g = (col.g < 0.5) ? 0.0 : 1.0; - col.b = (col.b < 0.5) ? 0.0 : 1.0; - if (col.r == 1.0 && col.g == 1.0) - col.rgb = vec3(1.0, 1.0, 0.0); - else if (col.r == 1.0) - col.rgb = vec3(0.0, 1.0, 1.0); + float gray = 0.2989 * col.r + 0.5870 * col.g + 0.1140 * col.b; + if (gray + rand(texCoord) - 0.5 < 0.5) + FragColor = vec4(vec3(0.0), 1.0); else - col.rgb = vec3(1.0, 0.0, 1.0); - FragColor = col; + FragColor = vec4(1.0); } diff --git a/src/main.c b/src/main.c index e8f14d7..69a2d8e 100644 --- a/src/main.c +++ b/src/main.c @@ -136,12 +136,13 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, GL_LINEAR); while (!glfwWindowShouldClose(window)) { - glClearColor(1.0f, 0.0f, 1.0f, 1.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader_use(&shader); glUniform1i(shader_uniform_location(&shader, "texture0"), 0); glUniform1i(shader_uniform_location(&shader, "texture1"), 1); + glUniform1f(shader_uniform_location(&shader, "randOffset"), rand() / (float)rand()); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture.id); glActiveTexture(GL_TEXTURE1); diff --git a/src/vertex.glsl b/src/vertex.glsl index 0a6f24e..9083749 100644 --- a/src/vertex.glsl +++ b/src/vertex.glsl @@ -1,8 +1,10 @@ #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; +layout (location = 2) in float randOffset; out vec2 texCoord; +out float fragRandOffset; uniform mat4 model; uniform mat4 view; @@ -14,4 +16,5 @@ main() { gl_Position = projection * view * model * transform * vec4(aPos, 1.0); texCoord = aTexCoord; + fragRandOffset = randOffset; } -- cgit v1.2.3