From aa865376f6737393a7f089dda8e1254520af30ba Mon Sep 17 00:00:00 2001 From: realtradam Date: Tue, 19 Apr 2022 04:16:52 -0400 Subject: cleaned up some code --- shaders/pixalizer.fs | 43 ++++++++++++++++++++++++++ shaders/test.fs | 42 ------------------------- src/main.zig | 86 +++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 104 insertions(+), 67 deletions(-) create mode 100644 shaders/pixalizer.fs delete mode 100644 shaders/test.fs diff --git a/shaders/pixalizer.fs b/shaders/pixalizer.fs new file mode 100644 index 0000000..a215a73 --- /dev/null +++ b/shaders/pixalizer.fs @@ -0,0 +1,43 @@ +#version 330 + +#define PI 3.1415926538 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// Output fragment color +out vec4 finalColor; + +// Imported variables from code +uniform float renderWidth; +uniform float renderHeight; +uniform float grid_size; + +// Variable set up +vec2 flipped = vec2(fragTexCoord.x, 1 - fragTexCoord.y); // flipping +vec2 textureResolution = vec2(renderWidth, renderHeight); +//vec2 onePixel = vec2(1.0, 1.0) / textureResolution; // distance of a single pixel in texels +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() +{ + //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 + finalColor = texelColor*colDiffuse; +} diff --git a/shaders/test.fs b/shaders/test.fs deleted file mode 100644 index cba5bfb..0000000 --- a/shaders/test.fs +++ /dev/null @@ -1,42 +0,0 @@ -#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; -uniform vec4 colDiffuse; - -// Output fragment color -out vec4 finalColor; - -// NOTE: Add here your custom variables -uniform float renderWidth; -uniform float renderHeight; -vec2 textureResolution = vec2(renderWidth, renderHeight); -uniform float grid_size; -//vec2 onePixel = vec2(1.0, 1.0) / 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() -{ - //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 - - finalColor = texelColor*colDiffuse; -} diff --git a/src/main.zig b/src/main.zig index ce49e14..5f3d7e0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -6,23 +6,58 @@ const ray = @import("raylib.zig"); const print = std.debug.print; const fs = std.fs; +const fmt = std.fmt; const screenWidth: u32 = 64 * 10; const screenHeight: u32 = (32 * 10) + 24 + 60; const RndGen = std.rand.DefaultPrng; var Rnd = RndGen.init(0); var chip8_screen: ray.RenderTexture = undefined; -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_gridsizeLocation: c_int = undefined; -var shader_gridsize: f32 = 1.0; +var shader_pixalizer: ShaderPixalizer = undefined; var slider: f32 = 100; const slider_max: f32 = 150; const slider_min: f32 = 50; -var swirl_center = [_]u32{ screenWidth / 2, screenHeight / 2 }; +const ShaderPixalizer = struct { + shader: ray.Shader = undefined, + grid_size: f32 = 1.0, + grid_size_location: c_int = undefined, + texture_width: f32 = 64.0, + texture_width_location: c_int = undefined, + texture_height: f32 = 32.0, + texture_height_location: c_int = undefined, + + pub fn new(shader_val: ray.Shader) ShaderPixalizer { + const temp_grid_size_loc = ray.GetShaderLocation(shader_val, "grid_size"); + const temp_texture_width_loc = ray.GetShaderLocation(shader_val, "renderWidth"); + const temp_texture_height_loc = ray.GetShaderLocation(shader_val, "renderHeight"); + var result = ShaderPixalizer{ + .shader = shader_val, + .grid_size_location = temp_grid_size_loc, + .texture_width_location = temp_texture_width_loc, + .texture_height_location = temp_texture_height_loc, + }; + //var result.texture_width: f32 = 64.0 * 3.0; + //var temp_shader_texture_height: f32 = 32.0; + + ray.SetShaderValue(result.shader, result.grid_size_location, &result.grid_size, ray.SHADER_UNIFORM_FLOAT); + ray.SetShaderValue(result.shader, result.texture_width_location, &result.texture_width, ray.SHADER_UNIFORM_FLOAT); + ray.SetShaderValue(result.shader, result.texture_height_location, &result.texture_height, ray.SHADER_UNIFORM_FLOAT); + return result; + } + + pub fn set_grid_size(self: *ShaderPixalizer, new_grid_size: f32) f32 { + self.*.grid_size = new_grid_size; + ray.SetShaderValue(self.*.shader, self.*.grid_size_location, &self.*.grid_size, ray.SHADER_UNIFORM_FLOAT); + return self.*.grid_size; + } + + pub fn set_render_width(self: *ShaderPixalizer, new_grid_size: f32) f32 { + self.*.grid_size = new_grid_size; + ray.SetShaderValue(self.*.shader, self.*.grid_size_location, &self.*.grid_size, ray.SHADER_UNIFORM_FLOAT); + return self.*.grid_size; + } +}; const Chip8 = struct { opcode: u16 = 0, @@ -167,14 +202,19 @@ 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); + _ = shader_pixalizer.set_grid_size(slider / 100.0); 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); + var slider_buf: [4]u8 = undefined; + const slider_str = fmt.bufPrint(&slider_buf, "{d:02.2}", .{slider / 100.0}) catch |err| { + print("{}\n", .{err}); + return; + }; + + slider = ray.GuiSlider(ray.Rectangle{ .x = 70, .y = 32 * 10 + 24 + 10, .width = 64 * 8, .height = 30 }, "Pixel Size", @ptrCast([*c]const u8, slider_str), slider, 50, 150); if (delay == execute_delay) { chip8.emulate_cycles(1); @@ -194,27 +234,23 @@ 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, 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"); - 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); - //ray.SetShaderValue(shader, swirl_center_loc, swirlCenter, ray.SHADER_UNIFORM_VEC2); + shader_pixalizer = ShaderPixalizer.new(ray.LoadShader(0, "shaders/pixalizer.fs")); + //renderPoint_renderHeight = ray.GetShaderLocation(shader, "renderHeight"); + //renderPoint_renderWidth = ray.GetShaderLocation(shader, "renderWidth"); + + //var renderVar_renderWidth: f32 = @intToFloat(f32, chip8_screen.texture.width); + //var renderVar_renderHeight: f32 = @intToFloat(f32, chip8_screen.texture.height); //const shaderwidth: c_ = @intToFloat(c_float, chip8_screen.texture.width //ray.SetShaderValue(shader, renderPoint_renderWidth, &chip8_screen.texture.width, ray.SHADER_UNIFORM_FLOAT); - ray.SetShaderValue(shader, renderPoint_renderWidth, &renderVar_renderWidth, ray.SHADER_UNIFORM_FLOAT); - ray.SetShaderValue(shader, renderPoint_renderHeight, &renderVar_renderHeight, ray.SHADER_UNIFORM_FLOAT); - ray.SetShaderValue(shader, shader_gridsizeLocation, &shader_gridsize, ray.SHADER_UNIFORM_FLOAT); + //ray.SetShaderValue(shader, renderPoint_renderWidth, &renderVar_renderWidth, ray.SHADER_UNIFORM_FLOAT); + //ray.SetShaderValue(shader, renderPoint_renderHeight, &renderVar_renderHeight, ray.SHADER_UNIFORM_FLOAT); + //ray.SetShaderValue(shader, shader_gridsizeLocation, &shader_gridsize, ray.SHADER_UNIFORM_FLOAT); //ray.SetShaderValue(shader, renderPoint_renderHeight, chip8_screen.texture.height, ray.SHADER_UNIFORM_FLOAT); ray.SetTargetFPS(60); } fn close_graphics() void { - ray.UnloadShader(shader); + ray.UnloadShader(shader_pixalizer.shader); ray.CloseWindow(); } @@ -233,7 +269,7 @@ fn update_screen(screen: *ray.RenderTexture, pixels: [2048]bool) void { ray.DrawRectangleRec(ray.Rectangle{ .x = @intToFloat(f32, @mod(index, 64)), .y = @intToFloat(f32, (index / 64)), .width = 1, .height = 1 }, ray.Color{ .r = on, .g = on, .b = on, .a = 255 }); } ray.EndTextureMode(); - ray.BeginShaderMode(shader); + ray.BeginShaderMode(shader_pixalizer.shader); ray.DrawTexturePro( screen.texture, ray.Rectangle{ .x = 0, .y = 0, .width = @intToFloat(f32, screen.texture.width), .height = @intToFloat(f32, screen.texture.height) }, -- cgit v1.2.3