From f2587fca3a17d6b0f13219c7f66ea4032743e205 Mon Sep 17 00:00:00 2001 From: Lyzev Date: Sat, 8 Jun 2024 21:56:24 +0200 Subject: [PATCH] did take way too long, sry Signed-off-by: Lyzev --- .../dev/lyzev/api/opengl/shader/Shaders.kt | 38 ++++++++----------- .../features/gui/guis/ImGuiScreenFeature.kt | 7 +--- .../shaders/core/Random/Random_FP.glsl | 21 ++++++++++ .../shaders/core/Random/Random_VP.glsl | 14 +++++++ 4 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 src/main/resources/assets/schizoid/shaders/core/Random/Random_FP.glsl create mode 100644 src/main/resources/assets/schizoid/shaders/core/Random/Random_VP.glsl diff --git a/src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt b/src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt index f31aa96d46..aedc0639c2 100644 --- a/src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt +++ b/src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt @@ -195,6 +195,8 @@ object ShaderParticle : ShaderCompute("Particle", 64, 1, 1) { } } +object ShaderRandom : ShaderVertexFragment("Random") + object ShaderMovingAveragesBox : ShaderCompute("MovingAveragesBox", 32, 1, 1) { private val horizontal = Matrix2f(1f, 0f, 0f, 1f) @@ -231,12 +233,17 @@ object ShaderGameOfLife : ShaderCompute("GameOfLife", 32, 1, 1) { private lateinit var after: WrappedFramebuffer private var initTime = System.currentTimeMillis() + var queueGenPixels = true var deltaTime = 1000 / 10 var size = 3 var b = "3" - var s = "23" + var s = "236" override fun draw() { + if (queueGenPixels) { + generateRandomPixels() + queueGenPixels = false + } if (System.currentTimeMillis() - initTime > deltaTime) { initTime = System.currentTimeMillis() after.clear() @@ -274,27 +281,13 @@ object ShaderGameOfLife : ShaderCompute("GameOfLife", 32, 1, 1) { ShaderTint.unbind() } - fun generateRandomPixels() { - val width = before.textureWidth - val height = before.textureHeight - val pixels = MemoryUtil.memAlloc(width * height * 4) - for (i in 0 until width * height) { - if (Math.random() < .7) { - pixels.put(0) - pixels.put(0) - pixels.put(0) - pixels.put(0.toByte()) - } else { - pixels.put(255.toByte()) - pixels.put(255.toByte()) - pixels.put(255.toByte()) - pixels.put(255.toByte()) - } - } - pixels.flip() - before.beginRead() - GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels) - MemoryUtil.memFree(pixels) + private fun generateRandomPixels() { + before.clear() + before.beginWrite(true) + ShaderRandom.bind() + ShaderRandom["Time"] = System.nanoTime() / 1000000000f + drawFullScreen() + ShaderRandom.unbind() } override fun delete() { @@ -307,6 +300,7 @@ object ShaderGameOfLife : ShaderCompute("GameOfLife", 32, 1, 1) { super.init() before = WrappedFramebuffer(size, linear = false) after = WrappedFramebuffer(size, linear = false) + queueGenPixels = true } override fun preprocess(source: String) = processIncludes(source).format(myGroupSizeX, myGroupSizeY, myGroupSizeZ, b.toCharArray().joinToString(", "), s.toCharArray().joinToString(", ")) diff --git a/src/main/kotlin/dev/lyzev/schizoid/feature/features/gui/guis/ImGuiScreenFeature.kt b/src/main/kotlin/dev/lyzev/schizoid/feature/features/gui/guis/ImGuiScreenFeature.kt index 097d1193fb..d46ea99082 100644 --- a/src/main/kotlin/dev/lyzev/schizoid/feature/features/gui/guis/ImGuiScreenFeature.kt +++ b/src/main/kotlin/dev/lyzev/schizoid/feature/features/gui/guis/ImGuiScreenFeature.kt @@ -52,7 +52,6 @@ object ImGuiScreenFeature : ImGuiScreen("Feature Screen"), EventListener { } if (it == "Game of Life") { ShaderGameOfLife.init() - ShaderGameOfLife.generateRandomPixels() } else { ShaderGameOfLife.delete() } @@ -94,12 +93,11 @@ object ImGuiScreenFeature : ImGuiScreen("Feature Screen"), EventListener { ) { ShaderGameOfLife.size = it ShaderGameOfLife.reload() - ShaderGameOfLife.generateRandomPixels() } val gameOfLifeRulestring by text( "Game of Life Rulestring", "The rulestring of the game of life.", - "B3/S23", + "B3/S236", true, Regex("B[0-8]+/S[0-8]+"), hide = ::background neq "Game of Life" @@ -108,7 +106,6 @@ object ImGuiScreenFeature : ImGuiScreen("Feature Screen"), EventListener { ShaderGameOfLife.b = rulestring.substringAfter("B").substringBefore("/") ShaderGameOfLife.s = rulestring.substringAfter("S") ShaderGameOfLife.reload() - ShaderGameOfLife.generateRandomPixels() } private val texturesMario = Array(3) { @@ -144,7 +141,7 @@ object ImGuiScreenFeature : ImGuiScreen("Feature Screen"), EventListener { val configManager = ImGuiRenderableConfigManager() override fun onDisplayed() { - ShaderGameOfLife.generateRandomPixels() + ShaderGameOfLife.queueGenPixels = true } override fun renderInGameBackground(context: DrawContext) = diff --git a/src/main/resources/assets/schizoid/shaders/core/Random/Random_FP.glsl b/src/main/resources/assets/schizoid/shaders/core/Random/Random_FP.glsl new file mode 100644 index 0000000000..74dceae052 --- /dev/null +++ b/src/main/resources/assets/schizoid/shaders/core/Random/Random_FP.glsl @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023. Schizoid + * All rights reserved. + */ + +#version 330 + +in vec2 uv; +out vec4 color; + +uniform float Time; + +#include "Noise.glsl" + +void main() { + if (rand(uv * (4500 + 2500 * rand(vec2(Time)))) < .7) { + color = vec4(0); + } else { + color = vec4(1); + } +} diff --git a/src/main/resources/assets/schizoid/shaders/core/Random/Random_VP.glsl b/src/main/resources/assets/schizoid/shaders/core/Random/Random_VP.glsl new file mode 100644 index 0000000000..4bc4a6b3cd --- /dev/null +++ b/src/main/resources/assets/schizoid/shaders/core/Random/Random_VP.glsl @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2023. Schizoid + * All rights reserved. + */ + +#version 330 + +in vec2 position; +out vec2 uv; + +void main() { + gl_Position = vec4(position, 0, 1); + uv = position * .5 + .5; +}