From 0bc2be66b391aa9a8a78ee987df4c0b56434235c Mon Sep 17 00:00:00 2001 From: DaniTheSkunk <> Date: Wed, 14 Dec 2022 10:41:31 +0000 Subject: [PATCH] playing around with colour threshold filter --- com/danitheskunk/skunkworks/Test3D.java | 4 +- .../backends/gl/FilterColorThreshold.java | 55 +++++++++++++++++++ .../skunkworks/backends/gl/Window.java | 10 +++- 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 com/danitheskunk/skunkworks/backends/gl/FilterColorThreshold.java diff --git a/com/danitheskunk/skunkworks/Test3D.java b/com/danitheskunk/skunkworks/Test3D.java index becddc2..0a2087e 100644 --- a/com/danitheskunk/skunkworks/Test3D.java +++ b/com/danitheskunk/skunkworks/Test3D.java @@ -18,7 +18,7 @@ public class Test3D extends BaseGame { public Test3D() { super(new Vec2i(1920, 1080), "Skunkworks 3d test"); - window.setRenderSize(new Vec2i(1920/8, 1080/8)); + window.setRenderSize(new Vec2i(1920, 1080)); System.out.printf("assimp version: %d.%d.%d\n", Assimp.aiGetVersionMajor(), Assimp.aiGetVersionMinor(), @@ -48,7 +48,7 @@ public class Test3D extends BaseGame { @Override protected void render3D(IRenderContext3D rc) { - rc.clear(Color.GRAY); + rc.clear(Color.BLACK); rc.renderModel(model); } diff --git a/com/danitheskunk/skunkworks/backends/gl/FilterColorThreshold.java b/com/danitheskunk/skunkworks/backends/gl/FilterColorThreshold.java new file mode 100644 index 0000000..b5e2dd7 --- /dev/null +++ b/com/danitheskunk/skunkworks/backends/gl/FilterColorThreshold.java @@ -0,0 +1,55 @@ +package com.danitheskunk.skunkworks.backends.gl; + +public class FilterColorThreshold extends Filter { + private static final String fragmentSource = """ + #version 450 + layout(location = 1) in vec2 texCoord; + layout(binding = 0) uniform sampler2D tex; + out vec4 color; + void main() { + vec4 col = texture(tex, texCoord); + /* + if(col.r > 0.5) + col.r = 1.0; + else + col.r = 0.0; + if(col.g > 0.5) + col.g = 1.0; + else + col.g = 0.0; + if(col.b > 0.5) + col.b = 1.0; + else + col.b = 0.0; + */ + if(col.r > 0.5 && col.g < 0.5 && col.b < 0.5) { + col.r = 1.0; + col.g = 0.0; + col.b = 0.0; + } else if(col.r + col.g + col.b > 2.5) { + col.r = 1.0; + col.g = 1.0; + col.b = 1.0; + } else { + col.r = 0.0; + col.g = 0.0; + col.b = 0.0; + } + if(col.a > 0.0) { + color = col; + } + } + """; + private static final String vertexSource = """ + #version 450 + layout(location = 0) in vec2 pos; + layout(location = 1) out vec2 out_texCoord; + void main() { + gl_Position = vec4(pos, 0.0f, 1.0f); + out_texCoord = vec2(pos.x * 0.5 + 0.5, pos.y * 0.5 + 0.5); + } + """; + public FilterColorThreshold(Framebuffer input) { + super(input, new Program(vertexSource, fragmentSource), false); + } +} diff --git a/com/danitheskunk/skunkworks/backends/gl/Window.java b/com/danitheskunk/skunkworks/backends/gl/Window.java index 5461747..0be1a21 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Window.java +++ b/com/danitheskunk/skunkworks/backends/gl/Window.java @@ -24,6 +24,7 @@ public class Window extends BaseWindow { private final Pipeline2D pipeline2D; private final Pipeline3D pipeline3D; private final Map scalers; + private final Filter filter; private final TextureAtlas textureAtlas; private final long window; private boolean shouldClose; @@ -66,10 +67,12 @@ public class Window extends BaseWindow { pipeline2D = new Pipeline2D(framebuffer, textureAtlas); pipeline3D = new Pipeline3D(framebuffer, textureAtlas); + filter = new FilterColorThreshold(framebuffer); + var out = filter.output; scalers = new HashMap<>(); - scalers.put("stretch", new ScalerStretch(framebuffer)); - scalers.put("aspect", new ScalerAspect(framebuffer)); - scalers.put("integer", new ScalerInteger(framebuffer)); + scalers.put("stretch", new ScalerStretch(out)); + scalers.put("aspect", new ScalerAspect(out)); + scalers.put("integer", new ScalerInteger(out)); scaler = "aspect"; mouse = new Mouse(window, scalers.get(scaler)); @@ -145,6 +148,7 @@ public class Window extends BaseWindow { @Override public void runScaler() { + filter.apply(); scalers.get(scaler).apply(); scalers.get(scaler).getOutput().copyToScreen(); }