fix literally fucking everything
This commit is contained in:
parent
0b7eb66703
commit
109d666eb8
9 changed files with 235 additions and 75 deletions
|
|
@ -21,6 +21,8 @@ const RawQuad = struct {
|
|||
normal: raylib.Vector3,
|
||||
width: f32,
|
||||
height: f32,
|
||||
flip_x: bool,
|
||||
flip_y: bool,
|
||||
|
||||
top_obscuring_pattern: u32,
|
||||
left_obscuring_pattern: u32,
|
||||
|
|
@ -85,12 +87,12 @@ 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 dimention: comptime_int) u32 {
|
||||
if (dimention % 3 == 0) {
|
||||
inline fn getTileRawShifted(self: Chunk, x: u5, y: u5, z: u5, comptime dimension: comptime_int) u32 {
|
||||
if (dimension % 3 == 0) {
|
||||
return self.getTileRaw(x, y, z);
|
||||
} else if (dimention % 3 == 1) {
|
||||
} else if (dimension % 3 == 1) {
|
||||
return self.getTileRaw(y, z, x);
|
||||
} else if (dimention % 3 == 2) {
|
||||
} else if (dimension % 3 == 2) {
|
||||
return self.getTileRaw(z, x, y);
|
||||
}
|
||||
}
|
||||
|
|
@ -101,12 +103,16 @@ pub const Chunk = struct {
|
|||
y_start: usize, y_end: usize,
|
||||
z_start: usize, z_end: usize,
|
||||
sign: comptime_int,
|
||||
dimention: comptime_int,
|
||||
dimension: comptime_int,
|
||||
surface: u32,
|
||||
y_minus_obscuring_pattern: u32,
|
||||
y_plus_obscuring_pattern: u32,
|
||||
z_minus_obscuring_pattern: u32,
|
||||
z_plus_obscuring_pattern: u32,
|
||||
y_minus_z_minus_obscured: bool,
|
||||
y_minus_z_plus_obscured: bool,
|
||||
y_plus_z_plus_obscured: bool,
|
||||
y_plus_z_minus_obscured: bool,
|
||||
) RawQuad {
|
||||
const ymin: f32 = @as(f32, @floatFromInt(y_start)) - 0.5;
|
||||
const ymax: f32 = @as(f32, @floatFromInt(y_end)) - 0.5;
|
||||
|
|
@ -117,16 +123,8 @@ pub const Chunk = struct {
|
|||
const zleft: f32 = if (sign == 1) zmin else zmax;
|
||||
const zright: f32 = if (sign == 1) zmax else zmin;
|
||||
|
||||
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 (dimention) {
|
||||
switch (dimension) {
|
||||
X_DIRECTION => {
|
||||
raw_quad = .{
|
||||
.tile = surface,
|
||||
|
|
@ -137,15 +135,17 @@ pub const Chunk = struct {
|
|||
.normal = v3.new(sign, 0, 0),
|
||||
.width = zmax - zmin,
|
||||
.height = ymax - ymin,
|
||||
.flip_x = sign == -1,
|
||||
.flip_y = false,
|
||||
|
||||
.top_obscuring_pattern = z_plus_obscuring_pattern,
|
||||
.top_obscuring_pattern = y_plus_obscuring_pattern,
|
||||
.left_obscuring_pattern = z_minus_obscuring_pattern,
|
||||
.right_obscuring_pattern = y_plus_obscuring_pattern,
|
||||
.right_obscuring_pattern = z_plus_obscuring_pattern,
|
||||
.bottom_obscuring_pattern = y_minus_obscuring_pattern,
|
||||
.top_left_obscured = false,
|
||||
.top_right_obscured = false,
|
||||
.bottom_right_obscured = false,
|
||||
.bottom_left_obscured = false,
|
||||
.top_left_obscured = y_plus_z_minus_obscured,
|
||||
.top_right_obscured = y_plus_z_plus_obscured,
|
||||
.bottom_right_obscured = y_minus_z_plus_obscured,
|
||||
.bottom_left_obscured = y_minus_z_minus_obscured,
|
||||
};
|
||||
},
|
||||
Y_DIRECTION => {
|
||||
|
|
@ -158,15 +158,17 @@ pub const Chunk = struct {
|
|||
.normal = v3.new(0, 0, sign),
|
||||
.height = zmax - zmin,
|
||||
.width = ymax - ymin,
|
||||
.flip_x = sign == 1,
|
||||
.flip_y = false,
|
||||
|
||||
.top_obscuring_pattern = 0,
|
||||
.left_obscuring_pattern = 0,
|
||||
.right_obscuring_pattern = 0,
|
||||
.bottom_obscuring_pattern = 0,
|
||||
.top_left_obscured = false,
|
||||
.top_right_obscured = false,
|
||||
.bottom_right_obscured = false,
|
||||
.bottom_left_obscured = false,
|
||||
.top_obscuring_pattern = z_plus_obscuring_pattern,
|
||||
.left_obscuring_pattern = y_minus_obscuring_pattern,
|
||||
.right_obscuring_pattern = y_plus_obscuring_pattern,
|
||||
.bottom_obscuring_pattern = z_minus_obscuring_pattern,
|
||||
.top_left_obscured = y_minus_z_plus_obscured,
|
||||
.top_right_obscured = y_plus_z_plus_obscured,
|
||||
.bottom_right_obscured = y_plus_z_minus_obscured,
|
||||
.bottom_left_obscured = y_minus_z_minus_obscured,
|
||||
};
|
||||
},
|
||||
Z_DIRECTION => {
|
||||
|
|
@ -179,15 +181,17 @@ pub const Chunk = struct {
|
|||
.normal = v3.new(0, sign, 0),
|
||||
.width = zmax - zmin,
|
||||
.height = ymax - ymin,
|
||||
.flip_x = sign == 1,
|
||||
.flip_y = true,
|
||||
|
||||
.top_obscuring_pattern = 0,
|
||||
.left_obscuring_pattern = 0,
|
||||
.right_obscuring_pattern = 0,
|
||||
.bottom_obscuring_pattern = 0,
|
||||
.top_left_obscured = false,
|
||||
.top_right_obscured = false,
|
||||
.bottom_right_obscured = false,
|
||||
.bottom_left_obscured = false,
|
||||
.top_obscuring_pattern = y_plus_obscuring_pattern,
|
||||
.left_obscuring_pattern = z_minus_obscuring_pattern,
|
||||
.right_obscuring_pattern = z_plus_obscuring_pattern,
|
||||
.bottom_obscuring_pattern = y_minus_obscuring_pattern,
|
||||
.top_left_obscured = y_plus_z_minus_obscured,
|
||||
.top_right_obscured = y_plus_z_plus_obscured,
|
||||
.bottom_right_obscured = y_minus_z_plus_obscured,
|
||||
.bottom_left_obscured = y_minus_z_minus_obscured,
|
||||
};
|
||||
},
|
||||
else => unreachable,
|
||||
|
|
@ -201,7 +205,7 @@ pub const Chunk = struct {
|
|||
defer raw_quads.deinit();
|
||||
|
||||
// Begin scanning the chunk for tile surfaces to make raw quads.
|
||||
inline for (0..3) |dimention| { // Iterate over the 3 dimensions, X, Y and Z.
|
||||
inline for (0..3) |dimension| { // 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.
|
||||
|
|
@ -210,11 +214,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, dimention);
|
||||
const tile: u32 = chunk.getTileRawShifted(x, y, z, dimension);
|
||||
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, 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;
|
||||
if (x == 31 or chunk.getTileRawShifted(x + 1, y, z, dimension) == 0) positive_tile_surfaces[y][z] = tile;
|
||||
if (x == 0 or chunk.getTileRawShifted(x - 1, y, z, dimension) == 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;
|
||||
|
|
@ -241,7 +245,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), dimention) != 0) z_minus_obscuring_pattern |= 1;
|
||||
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_start - 1), dimension) != 0) z_minus_obscuring_pattern |= 1;
|
||||
}
|
||||
}
|
||||
var z_plus_obscuring_pattern: u32 = 0;
|
||||
|
|
@ -249,7 +253,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), dimention) != 0) z_plus_obscuring_pattern |= 1;
|
||||
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_end), dimension) != 0) z_plus_obscuring_pattern |= 1;
|
||||
}
|
||||
}
|
||||
var y_minus_obscuring_pattern: u32 = 0;
|
||||
|
|
@ -257,7 +261,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, dimention) != 0) y_minus_obscuring_pattern |= 1;
|
||||
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_start - 1), z, dimension) != 0) y_minus_obscuring_pattern |= 1;
|
||||
}
|
||||
}
|
||||
var y_plus_obscuring_pattern: u32 = 0;
|
||||
|
|
@ -265,11 +269,22 @@ 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, dimention) != 0) y_plus_obscuring_pattern |= 1;
|
||||
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_end), z, dimension) != 0) y_plus_obscuring_pattern |= 1;
|
||||
}
|
||||
}
|
||||
// std.debug.print("{}\n", .{z_minus_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);
|
||||
var y_minus_z_minus_obscured = false;
|
||||
var y_minus_z_plus_obscured = false;
|
||||
var y_plus_z_plus_obscured = false;
|
||||
var y_plus_z_minus_obscured = false;
|
||||
if(x != (if (sign == 1) 31 else 0) and y_start != 0 and z_start != 0)
|
||||
y_minus_z_minus_obscured = chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_start-1), @intCast(z_start-1), dimension) != 0;
|
||||
if(x != (if (sign == 1) 31 else 0) and y_start != 0 and z_end != 32)
|
||||
y_minus_z_plus_obscured = chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_start-1), @intCast(z_end), dimension) != 0;
|
||||
if(x != (if (sign == 1) 31 else 0) and y_end != 32 and z_end != 32)
|
||||
y_plus_z_plus_obscured = chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_end), @intCast(z_end), dimension) != 0;
|
||||
if(x != (if (sign == 1) 31 else 0) and y_end != 32 and z_start != 0)
|
||||
y_plus_z_minus_obscured = chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_end), @intCast(z_start-1), dimension) != 0;
|
||||
const raw_quad = packRawQuad(@floatFromInt(raw_x), y_start, y_end, z_start, z_end, sign, dimension, surface, y_minus_obscuring_pattern, y_plus_obscuring_pattern, z_minus_obscuring_pattern, z_plus_obscuring_pattern, y_minus_z_minus_obscured, y_minus_z_plus_obscured, y_plus_z_plus_obscured, y_plus_z_minus_obscured);
|
||||
try raw_quads.append(raw_quad);
|
||||
};
|
||||
}
|
||||
|
|
@ -309,8 +324,10 @@ pub const Chunk = struct {
|
|||
const vertex_corners = .{ raw_quad.top_left, raw_quad.bottom_left, raw_quad.top_right, raw_quad.bottom_right, raw_quad.top_right, raw_quad.bottom_left };
|
||||
const texcoords_x = .{ left_uv, left_uv, right_uv, right_uv, right_uv, left_uv };
|
||||
const texcoords_y = .{ top_uv, bottom_uv } ** 3;
|
||||
const tiletexcoords_x = .{ 0.0, 0.0, raw_quad.width, raw_quad.width, raw_quad.width, 0.0 };
|
||||
const tiletexcoords_y = .{ 0.0, raw_quad.height } ** 3;
|
||||
const tiletexcoords_x = if(raw_quad.flip_x)
|
||||
.{raw_quad.width, raw_quad.width, 0.0, 0.0, 0.0, raw_quad.width} else
|
||||
.{ 0.0, 0.0, raw_quad.width, raw_quad.width, raw_quad.width, 0.0 };
|
||||
const tiletexcoords_y = if(raw_quad.flip_y) .{ raw_quad.height, 0.0 } ** 3 else .{ 0.0, raw_quad.height } ** 3;
|
||||
|
||||
inline for (0..6) |corner_id| {
|
||||
vertices[VERTICES_BLOCK_SIZE * i + corner_id * 3 + 0] = vertex_corners[corner_id].x;
|
||||
|
|
@ -332,9 +349,9 @@ pub const Chunk = struct {
|
|||
.quad_height = @intFromFloat(raw_quad.height),
|
||||
.quad_width = @intFromFloat(raw_quad.width),
|
||||
};
|
||||
const metadata1_baked: [4]f32 = @bitCast(metadata1);
|
||||
const metadata1_baked: [4]u32 = @bitCast(metadata1);
|
||||
for (0..4) |k| {
|
||||
metadata1_packed[24 * i + 4 * j + k] = @bitCast(@as(f32, metadata1_baked[k]));
|
||||
metadata1_packed[24 * i + 4 * j + k] = metadata1_baked[k];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue