this shit is finally working

This commit is contained in:
catangent 2025-07-03 14:11:35 +01:00
parent f1dcc0f1b5
commit b781938559
5 changed files with 107 additions and 82 deletions

View file

@ -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,