diff --git a/raylib b/raylib index 446d6c3..6491362 160000 --- a/raylib +++ b/raylib @@ -1 +1 @@ -Subproject commit 446d6c3a6bbb3a592a84cc835a69c0e6f2f34b29 +Subproject commit 649136267cf6c042752c9a1af0d2abe9eac29b17 diff --git a/resources/shaders/tiling.fs b/resources/shaders/tiling.fs index cf89965..daece31 100644 --- a/resources/shaders/tiling.fs +++ b/resources/shaders/tiling.fs @@ -6,11 +6,12 @@ uniform vec2 textureTiling; in vec2 fragTexCoord; in vec3 fragNormal; in vec2 fragTileTexCoord; -flat in int ambientOcclusionSide1; -flat in int ambientOcclusionSide2; -flat in int ambientOcclusionCorner1; -flat in int ambientOcclusionCorner2; -flat in int ambientOcclusionCorner3; + +flat in uvec4 occlusionSides; +flat in int topLeftObscured; +flat in int topRightObscured; +flat in int bottomLeftObscured; +flat in int bottomRightObscured; flat in int quadHeight; flat in int quadWidth; @@ -32,12 +33,23 @@ void main() ivec2 floorFragTileTexCoord = ivec2(fragTileTexCoord); - if(fragTileTexCoord.x < 1 && (((ambientOcclusionSide1 >> floorFragTileTexCoord.y) & 1) == 1)) outColor *= 0.5 + fakeArcsin(fragTileTexCoord.x); + //if(fragTileTexCoord.x < 1 && (int(occlusionSides.x >> floorFragTileTexCoord.x) & 1) == 1) { + //if(fragTileTexCoord.x < 1 && (occlusionSides.x & uint(0x20000000)) > uint(0)) { + //if(fragTileTexCoord.x < 1 && (occlusionSides.x & uint(0x40000000)) > uint(0)) { + //if(fragTileTexCoord.x < 1 && (occlusionSides.x & uint(0x10000000)) > uint(0)) { + + //if ((fragTileTexCoord.x < 1) && (occlusionSides.x > uint(0))) { - // if(fragTileTexCoord.x < 0.125 && fragTileTexCoord.x < fract(fragTileTexCoord.y) && fragTileTexCoord.x + fract(fragTileTexCoord.y) < 1.0 && ((ambientOcclusionSide1 >> floorFragTileTexCoord.y) & 1) == 1) outColor *= 0.5; - - //if((fragTileTexCoord.x < 0.25 || fragTileTexCoord.x > quadWidth-0.25) && (((ambientOcclusionSide1 >> floorFragTileTexCoord.y) & 1) == 1)) outColor *= 0.5; - //if((fragTileTexCoord.y < 0.25 || fragTileTexCoord.y > quadHeight-0.25) && (((ambientOcclusionSide2 >> floorFragTileTexCoord.y) & 1) == 1)) outColor *= 0.5; + if(fragTileTexCoord.x < 1 && ((occlusionSides.x >> floorFragTileTexCoord.x) & uint(1)) == uint(1)) { + outColor *= 0.5 + fakeArcsin(fragTileTexCoord.x); + } outColor.a = 1; + outColor.r = occlusionSides.x > uint(0) ? 1.0 : 0.0; + //outColor.g = ((occlusionSides.x & uint(0x01000000)) > uint(0)) ? 1.0 : 0.0; + + uint bit = uint(fragTileTexCoord * 32); + outColor.g = (((occlusionSides.x >> bit) & uint(1)) == uint(1)) ? + ((bit % uint(2) == uint(0)) ? 1.0 : 0.8): + ((bit % uint(2) == uint(0)) ? 0.0 : 0.2); } diff --git a/resources/shaders/tiling.vs b/resources/shaders/tiling.vs index 6148ca7..0efd713 100644 --- a/resources/shaders/tiling.vs +++ b/resources/shaders/tiling.vs @@ -4,17 +4,18 @@ in vec3 vertexPosition; in vec2 vertexTexCoord; in vec3 vertexNormal; in vec2 vertexTileTexCoord; -in vec4 vertexMetadata1; +in uvec4 vertexMetadata1; +in uvec4 vertexOcclusionSides; out vec2 fragTexCoord; out vec3 fragNormal; out vec2 fragTileTexCoord; -flat out int ambientOcclusionSide1; -flat out int ambientOcclusionSide2; -flat out int ambientOcclusionCorner1; -flat out int ambientOcclusionCorner2; -flat out int ambientOcclusionCorner3; +flat out uvec4 occlusionSides; +flat out int topLeftObscured; +flat out int topRightObscured; +flat out int bottomLeftObscured; +flat out int bottomRightObscured; flat out int quadHeight; flat out int quadWidth; @@ -24,16 +25,15 @@ void main() { fragTexCoord = vertexTexCoord; fragTileTexCoord = vertexTileTexCoord; fragNormal = vertexNormal; + occlusionSides = vertexOcclusionSides; gl_Position = mvp*vec4(vertexPosition, 1.0); // metadata 1 parsing - ambientOcclusionSide1 = floatBitsToInt(vertexMetadata1.x); - ambientOcclusionSide2 = floatBitsToInt(vertexMetadata1.y); - int metadata1Z = floatBitsToInt(vertexMetadata1.z); - int metadata1W = floatBitsToInt(vertexMetadata1.w); - ambientOcclusionCorner1 = (metadata1Z & 0x1) >> 0; // Take 0th bit. - ambientOcclusionCorner2 = (metadata1Z & 0x2) >> 1; // Take 1st bit. - ambientOcclusionCorner3 = (metadata1Z & 0x4) >> 2; // Take 2nd bit. - quadHeight = (metadata1Z & 0x1f8) >> 3; // Take 3rd-8th bits. - quadWidth = (metadata1Z & 0x7e00) >> 9; // Take 9th-14th bits. + int metadata1W = int(vertexMetadata1.w); + topLeftObscured = (metadata1W & 0x1); // Take 0th bit. + topRightObscured = (metadata1W & 0x2) >> 1; // Take 1st bits. + bottomLeftObscured = (metadata1W & 0x4) >> 2; // Take 2nd bits. + bottomRightObscured = (metadata1W & 0x8) >> 3; // Take 3rd bits. + quadHeight = (metadata1W & 0x3f0) >> 4; // Take 4rd-9th bits. + quadWidth = (metadata1W & 0xfc00) >> 10; // Take 10th-16th bits. } diff --git a/src/main.zig b/src/main.zig index cf6ae68..7d2b697 100644 --- a/src/main.zig +++ b/src/main.zig @@ -66,12 +66,13 @@ pub fn main() !void { const shader = raylib.LoadChunkShader("resources/shaders/tiling.vs", "resources/shaders/tiling.fs"); defer raylib.UnloadShader(shader); - //std.debug.print("shader id: {}\n", .{shader.id}); - //std.debug.print("shader attrib position loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexPosition")}); - //std.debug.print("shader attrib texcoord loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexTexCoord")}); - //std.debug.print("shader attrib normal loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexNormal")}); - //std.debug.print("shader attrib tiletexcoord loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexTileTexCoord")}); - //std.debug.print("shader attrib metadata1 loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexMetadata1")}); + std.debug.print("shader id: {}\n", .{shader.id}); + std.debug.print("shader attrib position loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexPosition")}); + std.debug.print("shader attrib texcoord loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexTexCoord")}); + std.debug.print("shader attrib normal loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexNormal")}); + std.debug.print("shader attrib tiletexcoord loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexTileTexCoord")}); + std.debug.print("shader attrib metadata1 loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexMetadata1")}); + std.debug.print("shader attrib ambientOcclusionSides loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexAmbientOcclusionSides")}); //for (0..32) |i|{ // std.debug.print("shader loc {}: {}\n", .{i, shader.locs[i]}); //} diff --git a/src/world/chunk.zig b/src/world/chunk.zig index 446a765..a638e6b 100644 --- a/src/world/chunk.zig +++ b/src/world/chunk.zig @@ -4,9 +4,14 @@ const raylib = raylib_helper.raylib; const v3 = raylib_helper.v3; const A7r = std.mem.Allocator; const comptimePrint = std.fmt.comptimePrint; + const VERTICES_BLOCK_SIZE = 2 * 3 * 3; const TEXCOORDS_BLOCK_SIZE = 2 * 2 * 3; +const X_DIRECTION = 0; +const Y_DIRECTION = 1; +const Z_DIRECTION = 2; + const RawQuad = struct { tile: u32, top_left: raylib.Vector3, @@ -29,15 +34,16 @@ const RawQuad = struct { // Quad shader metadata. Has to be 128 bytes in size. const Metadata1 = packed struct { - ambient_occlusion_1: u32, - ambient_occlusion_2: u32, - ambient_occlusion_corner1: bool, - ambient_occlusion_corner2: bool, - ambient_occlusion_corner3: bool, + top_left_obscured: bool, + top_right_obscured: bool, + bottom_right_obscured: bool, + bottom_left_obscured: bool, quad_height: u6, quad_width: u6, - unused: u17 = 0, + unused: u16 = 0, unused_2: u32 = 0, + unused_3: u32 = 0, + unused_4: u32 = 0, }; comptime { @@ -79,23 +85,23 @@ pub const Chunk = struct { } // This cyclically permutes the x, y, z coordinates at compile time. Useful when iterating over x, y, and z axis. - inline fn getTileRawShifted(self: Chunk, x: u5, y: u5, z: u5, comptime d: comptime_int) u32 { - if (d % 3 == 0) { + inline fn getTileRawShifted(self: Chunk, x: u5, y: u5, z: u5, comptime dimention: comptime_int) u32 { + if (dimention % 3 == 0) { return self.getTileRaw(x, y, z); - } else if (d % 3 == 1) { + } else if (dimention % 3 == 1) { return self.getTileRaw(y, z, x); - } else if (d % 3 == 2) { + } else if (dimention % 3 == 2) { return self.getTileRaw(z, x, y); } } // Create a raw quad with specified parameters and surface, accounting for dimension and sign. Surface is the block ID. - fn pack_raw_quad( + fn packRawQuad( x: f32, y_start: usize, y_end: usize, z_start: usize, z_end: usize, sign: comptime_int, - d: comptime_int, + dimention: comptime_int, surface: u32, y_minus_obscuring_pattern: u32, y_plus_obscuring_pattern: u32, @@ -110,9 +116,21 @@ pub const Chunk = struct { const yright: f32 = if (sign == 1) ymax else ymin; const zleft: f32 = if (sign == 1) zmin else zmax; const zright: f32 = if (sign == 1) zmax else zmin; + + _ = y_minus_obscuring_pattern; + _ = y_plus_obscuring_pattern; + _ = z_plus_obscuring_pattern; + if(dimention == 0 and z_minus_obscuring_pattern == 0b10000){ + std.debug.print("z_minus_obscuring_pattern {b}\n", .{z_minus_obscuring_pattern}); + std.debug.print("dimension {}\n", .{dimention}); + std.debug.print("x {}\n", .{x}); + std.debug.print("y {}-{}\n", .{y_start, y_end}); + std.debug.print("z {}-{}\n", .{z_start, z_end}); + } + var raw_quad: RawQuad = undefined; - switch (d) { - 0 => { // X direction + switch (dimention) { + X_DIRECTION => { raw_quad = .{ .tile = surface, .top_left = v3.new(x + 0.5 * sign, ymax, zright), @@ -123,17 +141,17 @@ pub const Chunk = struct { .width = zmax - zmin, .height = ymax - ymin, - .top_obscuring_pattern = z_plus_obscuring_pattern, - .left_obscuring_pattern = z_minus_obscuring_pattern, - .right_obscuring_pattern = y_plus_obscuring_pattern, - .bottom_obscuring_pattern = y_minus_obscuring_pattern, + .top_obscuring_pattern = 0, //z_plus_obscuring_pattern, + .left_obscuring_pattern = if(z_minus_obscuring_pattern == 0b10000) 0x80 else 0, + .right_obscuring_pattern = 0, //y_plus_obscuring_pattern, + .bottom_obscuring_pattern = 0, //y_minus_obscuring_pattern, .top_left_obscured = false, .top_right_obscured = false, .bottom_right_obscured = false, .bottom_left_obscured = false, }; }, - 1 => { // Y direction + Y_DIRECTION => { raw_quad = .{ .tile = surface, .bottom_left = v3.new(yleft, zmin, x + 0.5 * sign), @@ -154,7 +172,7 @@ pub const Chunk = struct { .bottom_left_obscured = false, }; }, - 2 => { // Z direction + Z_DIRECTION => { raw_quad = .{ .tile = surface, .top_left = v3.new(zleft, x + 0.5 * sign, ymin), @@ -186,7 +204,7 @@ pub const Chunk = struct { defer raw_quads.deinit(); // Begin scanning the chunk for tile surfaces to make raw quads. - inline for (0..3) |d| { // Iterate over the 3 dimensions, X, Y and Z. + inline for (0..3) |dimention| { // Iterate over the 3 dimensions, X, Y and Z. for (0..32) |raw_x| { const x: u5 = @intCast(raw_x); // Create surface arrays for the +x side of the layer and the -x side. @@ -195,11 +213,11 @@ pub const Chunk = struct { for (0..32) |raw_y| for (0..32) |raw_z| { const y: u5 = @intCast(raw_y); const z: u5 = @intCast(raw_z); - const tile: u32 = chunk.getTileRawShifted(x, y, z, d); + const tile: u32 = chunk.getTileRawShifted(x, y, z, dimention); if (tile == 0) continue; // If air, there is no surface. // If either at the edge of the chunk or the tile is exposed, create a tile surface. - if (x == 31 or chunk.getTileRawShifted(x + 1, y, z, d) == 0) positive_tile_surfaces[y][z] = tile; - if (x == 0 or chunk.getTileRawShifted(x - 1, y, z, d) == 0) negative_tile_surfaces[y][z] = tile; + if (x == 31 or chunk.getTileRawShifted(x + 1, y, z, dimention) == 0) positive_tile_surfaces[y][z] = tile; + if (x == 0 or chunk.getTileRawShifted(x - 1, y, z, dimention) == 0) negative_tile_surfaces[y][z] = tile; }; inline for (.{ -1, 1 }) |sign| { var tile_surfaces = if (sign == 1) positive_tile_surfaces else negative_tile_surfaces; @@ -226,7 +244,7 @@ pub const Chunk = struct { for (y_start..y_end) |raw_y| { const y: u5 = @intCast(raw_y); z_minus_obscuring_pattern <<= 1; - if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_start - 1), d) != 0) z_minus_obscuring_pattern |= 1; + if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_start - 1), dimention) != 0) z_minus_obscuring_pattern |= 1; } } var z_plus_obscuring_pattern: u32 = 0; @@ -234,7 +252,7 @@ pub const Chunk = struct { for (y_start..y_end) |raw_y| { const y: u5 = @intCast(raw_y); z_plus_obscuring_pattern <<= 1; - if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_end), d) != 0) z_plus_obscuring_pattern |= 1; + if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_end), dimention) != 0) z_plus_obscuring_pattern |= 1; } } var y_minus_obscuring_pattern: u32 = 0; @@ -242,7 +260,7 @@ pub const Chunk = struct { for (z_start..z_end) |raw_z| { const z: u5 = @intCast(raw_z); y_minus_obscuring_pattern <<= 1; - if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_start - 1), z, d) != 0) y_minus_obscuring_pattern |= 1; + if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_start - 1), z, dimention) != 0) y_minus_obscuring_pattern |= 1; } } var y_plus_obscuring_pattern: u32 = 0; @@ -250,11 +268,11 @@ pub const Chunk = struct { for (z_start..z_end) |raw_z| { const z: u5 = @intCast(raw_z); y_plus_obscuring_pattern <<= 1; - if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_end), z, d) != 0) y_plus_obscuring_pattern |= 1; + if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_end), z, dimention) != 0) y_plus_obscuring_pattern |= 1; } } // std.debug.print("{}\n", .{z_minus_obscuring_pattern}); - const raw_quad = pack_raw_quad(@floatFromInt(raw_x), y_start, y_end, z_start, z_end, sign, d, surface, y_minus_obscuring_pattern, y_plus_obscuring_pattern, z_minus_obscuring_pattern, z_plus_obscuring_pattern); + const raw_quad = packRawQuad(@floatFromInt(raw_x), y_start, y_end, z_start, z_end, sign, dimention, surface, y_minus_obscuring_pattern, y_plus_obscuring_pattern, z_minus_obscuring_pattern, z_plus_obscuring_pattern); try raw_quads.append(raw_quad); }; } @@ -270,7 +288,8 @@ pub const Chunk = struct { const texcoords: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 2))); const tiletexcoords: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 2))); const normals: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 3))); - const metadata1_packed: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 4))); + const metadata1_packed: [*]u32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 4))); + const occlusion_sides: [*]u32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 4))); for (raw_quads.items, 0..) |raw_quad, i| { if (raw_quad.tile <= 0) continue; // air tile, no texture @@ -307,13 +326,12 @@ pub const Chunk = struct { } // Store metadata into OpenGL buffers. - for (0..3) |j| { + for (0..6) |j| { const metadata1 = Metadata1{ - .ambient_occlusion_1 = raw_quad.left_obscuring_pattern, - .ambient_occlusion_2 = raw_quad.right_obscuring_pattern, - .ambient_occlusion_corner1 = raw_quad.top_left_obscured, - .ambient_occlusion_corner2 = raw_quad.bottom_left_obscured, - .ambient_occlusion_corner3 = raw_quad.top_right_obscured, + .top_left_obscured = raw_quad.top_left_obscured, + .top_right_obscured = raw_quad.top_right_obscured, + .bottom_left_obscured = raw_quad.bottom_left_obscured, + .bottom_right_obscured = raw_quad.bottom_right_obscured, .quad_height = @intFromFloat(raw_quad.height), .quad_width = @intFromFloat(raw_quad.width), }; @@ -322,20 +340,13 @@ pub const Chunk = struct { metadata1_packed[24 * i + 4 * j + k] = @bitCast(@as(f32, metadata1_baked[k])); } } - for (3..6) |j| { - const metadata1 = Metadata1{ - .ambient_occlusion_1 = raw_quad.right_obscuring_pattern, - .ambient_occlusion_2 = raw_quad.bottom_obscuring_pattern, - .ambient_occlusion_corner1 = raw_quad.bottom_right_obscured, - .ambient_occlusion_corner2 = raw_quad.top_right_obscured, - .ambient_occlusion_corner3 = raw_quad.bottom_left_obscured, - .quad_height = @intFromFloat(raw_quad.height), - .quad_width = @intFromFloat(raw_quad.width), - }; - const metadata1_baked: [4]f32 = @bitCast(metadata1); - for (0..4) |k| { - metadata1_packed[24 * i + 4 * j + k] = @bitCast(@as(f32, metadata1_baked[k])); - } + + // Store ambient occlusion sides into OpenGL buffers. + for (0..6) |j| { + occlusion_sides[24 * i + 4 * j + 0] = raw_quad.left_obscuring_pattern; + occlusion_sides[24 * i + 4 * j + 1] = raw_quad.right_obscuring_pattern; + occlusion_sides[24 * i + 4 * j + 2] = raw_quad.top_obscuring_pattern; + occlusion_sides[24 * i + 4 * j + 3] = raw_quad.bottom_obscuring_pattern; } } @@ -349,6 +360,7 @@ pub const Chunk = struct { .tiletexcoords = tiletexcoords, .normals = normals, .metadata1 = metadata1_packed, + .occlusion_sides = occlusion_sides, .vaoId = 0, .vboId = null,