From 4cfbf59ab53ede8eb39a8317c504e7cee889a827 Mon Sep 17 00:00:00 2001 From: realtradam Date: Mon, 18 Apr 2022 02:28:17 -0400 Subject: rewrote shader to be more flexible --- shaders/test.fs | 31 ++++++++++++++----------------- src/main.zig | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/shaders/test.fs b/shaders/test.fs index 16cf7c3..cba5bfb 100644 --- a/shaders/test.fs +++ b/shaders/test.fs @@ -1,9 +1,11 @@ #version 330 +#define PI 3.1415926538 // Input vertex attributes (from vertex shader) in vec2 fragTexCoord; in vec4 fragColor; +vec2 flipped = vec2(fragTexCoord.x, 1 - fragTexCoord.y); // Input uniform values uniform sampler2D texture0; @@ -18,26 +20,21 @@ uniform float renderHeight; vec2 textureResolution = vec2(renderWidth, renderHeight); uniform float grid_size; //vec2 onePixel = vec2(1.0, 1.0) / textureResolution; -vec2 pixelCoor = fragTexCoord * textureResolution; +vec2 pixelCoor = flipped * textureResolution; + +float gridify(float coordinate, float spacing) { + return clamp(floor(((-cos( coordinate * 2 * PI)) / 2) + spacing), 0.0, 1.0); +} + +float is_pixel(vec2 coordinate, float spacing) { + return gridify(coordinate.x, spacing) * gridify(coordinate.y, spacing); +} void main() { - vec4 texelColor = vec4(0.0, 0.0, 0.0, 0.0); - // Texel color fetching from texture sampler - if ((mod(floor(pixelCoor.x), grid_size + 1.0) == grid_size) || (mod(floor(pixelCoor.y), grid_size + 1.0) == grid_size)) - { - texelColor = vec4(0.0, 0.0, 0.0, 1.0); - } - else { - pixelCoor.y = -pixelCoor.y; - pixelCoor = pixelCoor / (grid_size + 1.0); - texelColor = texture(texture0, (pixelCoor /textureResolution)); - } -//if ((pix % 3) == 2) -//black pixel -//else -//get color of floor(pix / 3) -//end + //float is_pixel = gridify(pixelCoor.x, 1.0) * gridify(pixelCoor.y, 1.0); + vec4 texelColor = texture(texture0, flipped); + texelColor.rbg *= is_pixel(pixelCoor, grid_size); // NOTE: Implement here your fragment shader code diff --git a/src/main.zig b/src/main.zig index 63e98d6..ce49e14 100644 --- a/src/main.zig +++ b/src/main.zig @@ -8,7 +8,7 @@ const print = std.debug.print; const fs = std.fs; const screenWidth: u32 = 64 * 10; -const screenHeight: u32 = (32 * 10) + 24; +const screenHeight: u32 = (32 * 10) + 24 + 60; const RndGen = std.rand.DefaultPrng; var Rnd = RndGen.init(0); var chip8_screen: ray.RenderTexture = undefined; @@ -16,7 +16,11 @@ var shader: ray.Shader = undefined; var swirl_center_loc: c_int = undefined; var renderPoint_renderWidth: c_int = undefined; var renderPoint_renderHeight: c_int = undefined; -var shader_gridsize: f32 = 2.0; +var shader_gridsizeLocation: c_int = undefined; +var shader_gridsize: f32 = 1.0; +var slider: f32 = 100; +const slider_max: f32 = 150; +const slider_min: f32 = 50; var swirl_center = [_]u32{ screenWidth / 2, screenHeight / 2 }; @@ -163,11 +167,15 @@ pub fn main() !void { chip8.load_game("roms/IBM_Logo.ch8"); while (!ray.WindowShouldClose() and !exitWindow) { + shader_gridsize = slider / 100.0; + ray.SetShaderValue(shader, shader_gridsizeLocation, &shader_gridsize, ray.SHADER_UNIFORM_FLOAT); ray.BeginDrawing(); defer ray.EndDrawing(); exitWindow = ray.GuiWindowBox(ray.Rectangle{ .x = 0, .y = 0, .height = screenHeight, .width = screenWidth }, "CHIP-8"); + slider = ray.GuiSlider(ray.Rectangle{ .x = 70, .y = 32 * 10 + 24 + 10, .width = 64 * 8, .height = 30 }, "Pixel Size", "", slider, 50, 150); + if (delay == execute_delay) { chip8.emulate_cycles(1); delay = 0; @@ -185,13 +193,13 @@ fn get_coords(x: u32, y: u32) u32 { fn setup_graphics() void { ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); - chip8_screen = ray.LoadRenderTexture(64 * @floatToInt(c_int, shader_gridsize + 1), 32 * @floatToInt(c_int, shader_gridsize + 1)); + chip8_screen = ray.LoadRenderTexture(64, 32); //shader = ray.LoadShader(0, "shaders/glsl330/swirl.fs"); shader = ray.LoadShader(0, "shaders/test.fs"); //swirl_center_loc = ray.GetShaderLocation(shader, "center"); renderPoint_renderHeight = ray.GetShaderLocation(shader, "renderHeight"); renderPoint_renderWidth = ray.GetShaderLocation(shader, "renderWidth"); - var shader_gridsizeLocation = ray.GetShaderLocation(shader, "grid_size"); + shader_gridsizeLocation = ray.GetShaderLocation(shader, "grid_size"); var renderVar_renderWidth: f32 = @intToFloat(f32, chip8_screen.texture.width); var renderVar_renderHeight: f32 = @intToFloat(f32, chip8_screen.texture.height); -- cgit v1.2.3