From d639db711f9549d6e1c6374777f35c2510772fad Mon Sep 17 00:00:00 2001 From: Vicente Ferrari Smith Date: Tue, 13 Jan 2026 20:18:47 +0100 Subject: [PATCH] custom shader --- assets/test.frag | 27 +++++++++++++++++++++++++++ assets/test.vert | 26 ++++++++++++++++++++++++++ build.zig | 15 +++++++++++++-- src/client/main.zig | 37 ++++++++++++++++++++++++++++--------- 4 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 assets/test.frag create mode 100644 assets/test.vert diff --git a/assets/test.frag b/assets/test.frag new file mode 100644 index 0000000..dafce02 --- /dev/null +++ b/assets/test.frag @@ -0,0 +1,27 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec3 vertexPos; +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform sampler2D texture1; +uniform vec4 colDiffuse; + +uniform float divider = 0.5; + +out vec4 finalColor; + +void main() +{ + // // Texel color fetching from texture sampler + // vec4 texelColor0 = texture(texture0, fragTexCoord); + // vec4 texelColor1 = texture(texture1, fragTexCoord); + + // float x = fract(fragTexCoord.s); + // float final = smoothstep(divider - 0.1, divider + 0.1, x); + + finalColor = vec4(fragTexCoord, 0.0, 1.0); +} diff --git a/assets/test.vert b/assets/test.vert new file mode 100644 index 0000000..d564c69 --- /dev/null +++ b/assets/test.vert @@ -0,0 +1,26 @@ +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; +in vec4 vertexColor; + +// Input uniform values +uniform mat4 mvp; + +// Output vertex attributes (to fragment shader) +out vec2 fragTexCoord; +out vec4 fragColor; + +// NOTE: Add your custom variables here + +void main() +{ + // Send vertex attributes to fragment shader + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + + // Calculate final vertex position + gl_Position = mvp * vec4(vertexPosition, 1.0); +} diff --git a/build.zig b/build.zig index 6ff90cc..cbb7aa5 100644 --- a/build.zig +++ b/build.zig @@ -5,7 +5,7 @@ pub fn build(b: *std.Build) void { const optimize = b.standardOptimizeOption(.{}); const assets = b.addInstallDirectory(.{ - .source_dir = .{ .src_path = .{ .owner = b, .sub_path = "assets" } }, + .source_dir = b.path("assets"), .install_dir = .bin, .install_subdir = "assets", }); @@ -61,7 +61,7 @@ pub fn build(b: *std.Build) void { .imports = &.{ .{ .name = "shared", .module = shared }, }, - }), + }) }); const server = b.addExecutable(.{ @@ -76,6 +76,17 @@ pub fn build(b: *std.Build) void { }), }); + // client.addCSourceFile(.{ .file = b.path("src/kb/kb_text_shape.h"), .language = .c, .flags = &.{"-DKB_TEXT_SHAPE_IMPLEMENTATION"} }); + + client.root_module.addCSourceFile(.{ .file = b.path("src/kb/kb_text_shape.h"), .language = .c, .flags = &.{"-DKB_TEXT_SHAPE_IMPLEMENTATION"} }); + + const kb_text_shape = b.addTranslateC(.{ + .root_source_file = b.path("src/kb/kb_text_shape.h"), + .target = target, + .optimize = optimize, + }); + client.root_module.addImport("kb", kb_text_shape.createModule()); + const zmath = b.dependency("zmath", .{}); client.root_module.addImport("zmath", zmath.module("root")); server.root_module.addImport("zmath", zmath.module("root")); diff --git a/src/client/main.zig b/src/client/main.zig index 4a361bc..91c8d84 100644 --- a/src/client/main.zig +++ b/src/client/main.zig @@ -4,9 +4,8 @@ const znet = @import("znet"); const rl = @import("raylib"); const bufzilla = @import("bufzilla"); -const c = @cImport(@cInclude("../kb/kb_text_shape.h")); - const shared = @import("shared"); +const kb = @import("kb"); const client = @import("client.zig"); const entity = @import("entity.zig"); @@ -40,6 +39,16 @@ pub fn main() !void { rl.initWindow(1280, 720, "zzz"); defer rl.closeWindow(); + // kbts_shape_context *Context = kbts_CreateShapeContext(0, 0); + + _ = kb.kbts_CreateShapeContext(null, null); + + const shader = try rl.loadShader(null, "assets/test.frag"); + + const img = rl.genImageColor(32, 32, .blank); + const tx = try rl.loadTextureFromImage(img); + rl.unloadImage(img); + const host = try znet.Host.init(.{ .addr = null, .peer_limit = 1, @@ -168,7 +177,7 @@ pub fn main() !void { rl.beginDrawing(); - const connected_text = "Connected"; + // const connected_text = "Connected"; //const not_connected_text = "Not Connected"; // switch (peer.state()) { @@ -177,13 +186,23 @@ pub fn main() !void { // } //@divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(connected_text, f.baseSize), 2), 50 - const font_size : i32 = 180; - const text_size = rl.Vector2{.x = @floatFromInt(rl.measureText(connected_text, font_size)), .y = font_size};//rl.measureTextEx(try rl.getFontDefault(), connected_text, font_size, font_size / 10); - const pos = rl.Vector2{.x = 0, .y = 0}; - rl.drawText(connected_text, pos.x, pos.y, font_size, .white); - rl.drawRectangleLines(pos.x, pos.y, @intFromFloat(text_size.x), @intFromFloat(text_size.y), .red); - std.log.info("text size: {}", .{text_size}); + rl.beginShaderMode(shader); + + // const font_size : i32 = 180; + // const text_size = rl.Vector2{.x = @floatFromInt(rl.measureText(connected_text, font_size)), .y = font_size};//rl.measureTextEx(try rl.getFontDefault(), connected_text, font_size, font_size / 10); + // const pos = rl.Vector2{.x = 0, .y = 0}; + // rl.drawText(connected_text, pos.x, pos.y, font_size, .white); + // rl.drawRectangleLines(pos.x, pos.y, @intFromFloat(text_size.x), @intFromFloat(text_size.y), .red); + // rl.drawRectangle(pos.x, pos.y, rl.getScreenWidth(), rl.getScreenHeight(), .white); + rl.drawTexturePro(tx, + rl.Rectangle{.x = 0, .y = 0, .width = 32, .height = 32}, + rl.Rectangle{.x = 100, .y = 100, .width = 500, .height = 500}, + .{.x = 0, .y = 0}, + 0, + .white); + rl.endShaderMode(); + //rl.drawLineV(.{.x = @floatFromInt(@divFloor(rl.getScreenWidth(), 2)), .y = 0}, .{.x = @floatFromInt(@divFloor(rl.getScreenWidth(), 2)), .y = @floatFromInt(rl.getScreenHeight())}, .red); //rl.drawText("Congrats! You created your first window!", rl.getMouseX(), rl.getMouseY(), 20, .white);