2024-09-09 18:05:14 +01:00
|
|
|
const std = @import("std");
|
2024-12-16 23:19:26 +00:00
|
|
|
const raylib_helper = @import("../lib_helpers/raylib_helper.zig");
|
|
|
|
|
const raylib = raylib_helper.raylib;
|
|
|
|
|
const v3 = raylib_helper.v3;
|
2024-09-09 18:05:14 +01:00
|
|
|
const A7r = std.mem.Allocator;
|
2024-12-23 18:45:07 +00:00
|
|
|
const comptimePrint = std.fmt.comptimePrint;
|
2024-09-09 18:05:14 +01:00
|
|
|
|
2024-09-12 22:51:16 +01:00
|
|
|
const RawQuad = struct {
|
2024-12-16 23:19:26 +00:00
|
|
|
tile: u32,
|
2024-09-12 22:51:16 +01:00
|
|
|
top_left: raylib.Vector3,
|
|
|
|
|
top_right: raylib.Vector3,
|
|
|
|
|
bottom_right: raylib.Vector3,
|
|
|
|
|
bottom_left: raylib.Vector3,
|
|
|
|
|
normal: raylib.Vector3,
|
2024-09-15 21:25:30 +01:00
|
|
|
width: f32,
|
|
|
|
|
height: f32,
|
2024-09-12 22:51:16 +01:00
|
|
|
};
|
|
|
|
|
|
2024-12-23 18:45:07 +00:00
|
|
|
const Metadata1 = packed struct {
|
|
|
|
|
ambient_occlusion_1: u32,
|
|
|
|
|
ambient_occlusion_2: u32,
|
|
|
|
|
ambient_occlusion_corner1: bool,
|
|
|
|
|
ambient_occlusion_corner2: bool,
|
|
|
|
|
ambient_occlusion_corner3: bool,
|
|
|
|
|
quad_height: u6,
|
|
|
|
|
quad_width: u6,
|
|
|
|
|
unused: u17 = 0,
|
|
|
|
|
unused_2: u32 = 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
comptime {
|
|
|
|
|
if (@bitSizeOf(Metadata1) != 128) {
|
|
|
|
|
@compileError(comptimePrint("Metadata 1 has wrong size. Expected 128 bits, found {}", .{@bitSizeOf(Metadata1)}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 18:05:14 +01:00
|
|
|
pub const Chunk = struct {
|
2024-12-16 23:19:26 +00:00
|
|
|
tiles: []u32,
|
2024-09-09 18:05:14 +01:00
|
|
|
a7r: A7r,
|
|
|
|
|
|
|
|
|
|
pub fn init(a7r: A7r) !Chunk {
|
2024-09-12 22:51:16 +01:00
|
|
|
const self = Chunk{
|
2024-09-09 18:05:14 +01:00
|
|
|
.a7r = a7r,
|
2024-12-16 23:19:26 +00:00
|
|
|
.tiles = try a7r.alloc(u32, 32 * 32 * 32),
|
2024-09-09 18:05:14 +01:00
|
|
|
};
|
2024-09-12 22:51:16 +01:00
|
|
|
@memset(self.tiles, 0);
|
|
|
|
|
return self;
|
2024-09-09 18:05:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn deinit(self: Chunk) void {
|
|
|
|
|
self.a7r.free(self.tiles);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 23:19:26 +00:00
|
|
|
pub fn getTile(self: Chunk, x: u5, y: u5, z: u5) u32 {
|
2024-09-09 18:05:14 +01:00
|
|
|
return self.tiles[@as(u15, x) << 10 | @as(u15, y) << 5 | @as(u15, z)];
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 23:19:26 +00:00
|
|
|
pub fn setTile(self: Chunk, x: u5, y: u5, z: u5, tile: u32) void {
|
2024-09-09 18:05:14 +01:00
|
|
|
self.tiles[@as(u15, x) << 10 | @as(u15, y) << 5 | @as(u15, z)] = tile;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 23:19:26 +00:00
|
|
|
fn getTileRaw(self: Chunk, x: u5, y: u5, z: u5) u32 {
|
2024-09-15 21:25:30 +01:00
|
|
|
return self.tiles[@as(u15, x) << 10 | @as(u15, y) << 5 | @as(u15, z)];
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 23:19:26 +00:00
|
|
|
inline fn getTileRawShifted(self: Chunk, x: u5, y: u5, z: u5, comptime d: comptime_int) u32 {
|
2024-09-15 21:25:30 +01:00
|
|
|
if (d % 3 == 0) {
|
|
|
|
|
return self.getTileRaw(x, y, z);
|
|
|
|
|
} else if (d % 3 == 1) {
|
|
|
|
|
return self.getTileRaw(y, z, x);
|
|
|
|
|
} else if (d % 3 == 2) {
|
|
|
|
|
return self.getTileRaw(z, x, y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 21:33:32 +01:00
|
|
|
pub fn createMesh(chunk: Chunk, tile_rows: u32, tile_columns: u32) !raylib.Mesh {
|
2024-09-12 22:51:16 +01:00
|
|
|
var raw_quads = try std.ArrayList(RawQuad).initCapacity(chunk.a7r, 4096);
|
|
|
|
|
defer raw_quads.deinit();
|
2024-09-09 18:05:14 +01:00
|
|
|
|
2024-09-15 21:25:30 +01:00
|
|
|
inline for (0..3) |d| {
|
|
|
|
|
for (0..32) |raw_x| {
|
2024-09-12 22:51:16 +01:00
|
|
|
const x: u5 = @intCast(raw_x);
|
2024-12-16 23:19:26 +00:00
|
|
|
var positive_tile_surfaces: [32][32]u32 = .{.{0} ** 32} ** 32;
|
|
|
|
|
var negative_tile_surfaces: [32][32]u32 = .{.{0} ** 32} ** 32;
|
2024-09-15 21:25:30 +01:00
|
|
|
for (0..32) |raw_y| for (0..32) |raw_z| {
|
|
|
|
|
const y: u5 = @intCast(raw_y);
|
|
|
|
|
const z: u5 = @intCast(raw_z);
|
2024-12-16 23:19:26 +00:00
|
|
|
const tile: u32 = chunk.getTileRawShifted(x, y, z, d);
|
2024-09-15 21:25:30 +01:00
|
|
|
if (tile == 0) continue;
|
|
|
|
|
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;
|
2024-09-12 22:51:16 +01:00
|
|
|
};
|
2024-09-15 21:25:30 +01:00
|
|
|
const xf: f32 = @floatFromInt(raw_x);
|
|
|
|
|
inline for (.{ -1, 1 }) |sign| {
|
|
|
|
|
var tile_surfaces = if (sign == 1) positive_tile_surfaces else negative_tile_surfaces;
|
|
|
|
|
for (0..32) |y| for (0..32) |z| {
|
|
|
|
|
const surface = tile_surfaces[y][z];
|
|
|
|
|
if (surface == 0) continue;
|
|
|
|
|
var y2 = y + 1;
|
2024-09-19 21:33:32 +01:00
|
|
|
var z2 = z + 1;
|
2024-09-15 21:25:30 +01:00
|
|
|
while (y2 <= 31 and tile_surfaces[y2][z] == surface) : (y2 += 1) {
|
|
|
|
|
tile_surfaces[y2][z] = 0;
|
|
|
|
|
}
|
|
|
|
|
zloop: while (z2 <= 31) : (z2 += 1) {
|
|
|
|
|
for (y..y2) |ytmp| if (tile_surfaces[ytmp][z2] != surface) break :zloop;
|
|
|
|
|
for (y..y2) |ytmp| tile_surfaces[ytmp][z2] = 0;
|
|
|
|
|
}
|
|
|
|
|
y2 -= 1;
|
|
|
|
|
z2 -= 1;
|
|
|
|
|
tile_surfaces[y][z] = 0;
|
|
|
|
|
const ymin: f32 = @as(f32, @floatFromInt(y)) - 0.5;
|
|
|
|
|
const ymax: f32 = @as(f32, @floatFromInt(y2)) + 0.5;
|
|
|
|
|
const zmin: f32 = @as(f32, @floatFromInt(z)) - 0.5;
|
|
|
|
|
const zmax: f32 = @as(f32, @floatFromInt(z2)) + 0.5;
|
|
|
|
|
const yleft: f32 = if (sign == 1) ymin else ymax;
|
|
|
|
|
const yright: f32 = if (sign == 1) ymax else ymin;
|
2024-09-20 10:03:22 +01:00
|
|
|
const zleft: f32 = if (sign == 1) zmin else zmax;
|
|
|
|
|
const zright: f32 = if (sign == 1) zmax else zmin;
|
|
|
|
|
var raw_quad: RawQuad = undefined;
|
|
|
|
|
switch (d) {
|
|
|
|
|
0 => {
|
|
|
|
|
raw_quad = .{
|
|
|
|
|
.tile = surface,
|
|
|
|
|
.top_left = v3.new(xf + 0.5 * sign, ymax, zright),
|
|
|
|
|
.top_right = v3.new(xf + 0.5 * sign, ymax, zleft),
|
|
|
|
|
.bottom_left = v3.new(xf + 0.5 * sign, ymin, zright),
|
|
|
|
|
.bottom_right = v3.new(xf + 0.5 * sign, ymin, zleft),
|
|
|
|
|
.normal = v3.new(sign, 0, 0),
|
|
|
|
|
.width = zmax - zmin,
|
|
|
|
|
.height = ymax - ymin,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
1 => {
|
|
|
|
|
raw_quad = .{
|
|
|
|
|
.tile = surface,
|
|
|
|
|
.bottom_left = v3.new(yleft, zmin, xf + 0.5 * sign),
|
|
|
|
|
.top_left = v3.new(yleft, zmax, xf + 0.5 * sign),
|
|
|
|
|
.bottom_right = v3.new(yright, zmin, xf + 0.5 * sign),
|
|
|
|
|
.top_right = v3.new(yright, zmax, xf + 0.5 * sign),
|
|
|
|
|
.normal = v3.new(0, 0, sign),
|
|
|
|
|
.height = zmax - zmin,
|
|
|
|
|
.width = ymax - ymin,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
2 => {
|
|
|
|
|
raw_quad = .{
|
|
|
|
|
.tile = surface,
|
|
|
|
|
.top_left = v3.new(zleft, xf + 0.5 * sign, ymin),
|
|
|
|
|
.top_right = v3.new(zright, xf + 0.5 * sign, ymin),
|
|
|
|
|
.bottom_left = v3.new(zleft, xf + 0.5 * sign, ymax),
|
|
|
|
|
.bottom_right = v3.new(zright, xf + 0.5 * sign, ymax),
|
|
|
|
|
.normal = v3.new(0, sign, 0),
|
|
|
|
|
.width = zmax - zmin,
|
|
|
|
|
.height = ymax - ymin,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
else => unreachable,
|
|
|
|
|
}
|
2024-09-15 21:25:30 +01:00
|
|
|
try raw_quads.append(raw_quad);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-12 22:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const triangle_count: c_int = @intCast(raw_quads.items.len * 2);
|
|
|
|
|
|
|
|
|
|
const arr_size: c_uint = @as(c_uint, @intCast(triangle_count)) * 3 * @sizeOf(f32);
|
2024-09-09 18:05:14 +01:00
|
|
|
|
|
|
|
|
const vertices: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 3)));
|
|
|
|
|
const texcoords: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 2)));
|
2024-09-19 21:33:32 +01:00
|
|
|
const texcoords2: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 2)));
|
2024-09-09 18:05:14 +01:00
|
|
|
const normals: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 3)));
|
2024-12-23 18:45:07 +00:00
|
|
|
const metadata1_packed: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 4)));
|
2024-09-09 18:05:14 +01:00
|
|
|
|
2024-09-12 22:51:16 +01:00
|
|
|
for (raw_quads.items, 0..) |raw_quad, i| {
|
2024-09-19 21:33:32 +01:00
|
|
|
if (raw_quad.tile <= 0) continue;
|
2024-12-16 23:19:26 +00:00
|
|
|
const tile = raw_quad.tile;
|
2024-09-19 21:33:32 +01:00
|
|
|
|
2024-09-12 22:51:16 +01:00
|
|
|
for (0..6) |j| {
|
|
|
|
|
normals[18 * i + 3 * j + 0] = raw_quad.normal.x;
|
|
|
|
|
normals[18 * i + 3 * j + 1] = raw_quad.normal.y;
|
|
|
|
|
normals[18 * i + 3 * j + 2] = raw_quad.normal.z;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 21:33:32 +01:00
|
|
|
const left_uv = @as(f32, @floatFromInt(tile % tile_columns)) / @as(f32, @floatFromInt(tile_columns));
|
|
|
|
|
const right_uv = @as(f32, @floatFromInt(tile % tile_columns + 1)) / @as(f32, @floatFromInt(tile_columns));
|
|
|
|
|
const top_uv = @as(f32, @floatFromInt(tile / tile_columns)) / @as(f32, @floatFromInt(tile_rows));
|
|
|
|
|
const bottom_uv = @as(f32, @floatFromInt(tile / tile_columns + 1)) / @as(f32, @floatFromInt(tile_rows));
|
|
|
|
|
|
2024-09-12 22:51:16 +01:00
|
|
|
vertices[18 * i + 0] = raw_quad.top_left.x;
|
|
|
|
|
vertices[18 * i + 1] = raw_quad.top_left.y;
|
|
|
|
|
vertices[18 * i + 2] = raw_quad.top_left.z;
|
2024-09-19 21:33:32 +01:00
|
|
|
texcoords[12 * i + 0] = left_uv;
|
|
|
|
|
texcoords[12 * i + 1] = top_uv;
|
|
|
|
|
texcoords2[12 * i + 0] = 0.0;
|
|
|
|
|
texcoords2[12 * i + 1] = 0.0;
|
2024-09-12 22:51:16 +01:00
|
|
|
|
2024-09-20 10:03:22 +01:00
|
|
|
vertices[18 * i + 3] = raw_quad.bottom_left.x;
|
|
|
|
|
vertices[18 * i + 4] = raw_quad.bottom_left.y;
|
|
|
|
|
vertices[18 * i + 5] = raw_quad.bottom_left.z;
|
|
|
|
|
texcoords[12 * i + 2] = left_uv;
|
|
|
|
|
texcoords[12 * i + 3] = bottom_uv;
|
|
|
|
|
texcoords2[12 * i + 2] = 0.0;
|
|
|
|
|
texcoords2[12 * i + 3] = raw_quad.height;
|
|
|
|
|
|
|
|
|
|
vertices[18 * i + 6] = raw_quad.top_right.x;
|
|
|
|
|
vertices[18 * i + 7] = raw_quad.top_right.y;
|
|
|
|
|
vertices[18 * i + 8] = raw_quad.top_right.z;
|
|
|
|
|
texcoords[12 * i + 4] = right_uv;
|
|
|
|
|
texcoords[12 * i + 5] = top_uv;
|
|
|
|
|
texcoords2[12 * i + 4] = raw_quad.width;
|
|
|
|
|
texcoords2[12 * i + 5] = 0.0;
|
2024-09-12 22:51:16 +01:00
|
|
|
|
|
|
|
|
vertices[18 * i + 9] = raw_quad.bottom_right.x;
|
|
|
|
|
vertices[18 * i + 10] = raw_quad.bottom_right.y;
|
|
|
|
|
vertices[18 * i + 11] = raw_quad.bottom_right.z;
|
2024-09-19 21:33:32 +01:00
|
|
|
texcoords[12 * i + 6] = right_uv;
|
|
|
|
|
texcoords[12 * i + 7] = bottom_uv;
|
|
|
|
|
texcoords2[12 * i + 6] = raw_quad.width;
|
|
|
|
|
texcoords2[12 * i + 7] = raw_quad.height;
|
2024-09-12 22:51:16 +01:00
|
|
|
|
2024-09-20 10:03:22 +01:00
|
|
|
vertices[18 * i + 12] = raw_quad.top_right.x;
|
|
|
|
|
vertices[18 * i + 13] = raw_quad.top_right.y;
|
|
|
|
|
vertices[18 * i + 14] = raw_quad.top_right.z;
|
|
|
|
|
texcoords[12 * i + 8] = right_uv;
|
|
|
|
|
texcoords[12 * i + 9] = top_uv;
|
|
|
|
|
texcoords2[12 * i + 8] = raw_quad.width;
|
|
|
|
|
texcoords2[12 * i + 9] = 0.0;
|
|
|
|
|
|
|
|
|
|
vertices[18 * i + 15] = raw_quad.bottom_left.x;
|
|
|
|
|
vertices[18 * i + 16] = raw_quad.bottom_left.y;
|
|
|
|
|
vertices[18 * i + 17] = raw_quad.bottom_left.z;
|
|
|
|
|
texcoords[12 * i + 10] = left_uv;
|
|
|
|
|
texcoords[12 * i + 11] = bottom_uv;
|
|
|
|
|
texcoords2[12 * i + 10] = 0.0;
|
|
|
|
|
texcoords2[12 * i + 11] = raw_quad.height;
|
2024-12-23 18:45:07 +00:00
|
|
|
|
|
|
|
|
for (0..6) |j| {
|
|
|
|
|
const metadata1 = Metadata1{
|
|
|
|
|
.ambient_occlusion_1 = 0xffffffff,
|
|
|
|
|
.ambient_occlusion_2 = 0xffffffff,
|
|
|
|
|
.ambient_occlusion_corner1 = true,
|
|
|
|
|
.ambient_occlusion_corner2 = true,
|
|
|
|
|
.ambient_occlusion_corner3 = true,
|
|
|
|
|
.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]));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-12 22:51:16 +01:00
|
|
|
}
|
2024-09-09 18:05:14 +01:00
|
|
|
|
|
|
|
|
var mesh = raylib.Mesh{
|
|
|
|
|
.triangleCount = triangle_count,
|
|
|
|
|
.vertexCount = triangle_count * 3,
|
|
|
|
|
|
|
|
|
|
.vertices = vertices,
|
|
|
|
|
.texcoords = texcoords,
|
2024-09-19 21:33:32 +01:00
|
|
|
.texcoords2 = texcoords2,
|
2024-09-09 18:05:14 +01:00
|
|
|
.normals = normals,
|
2024-12-23 18:45:07 +00:00
|
|
|
.tangents = metadata1_packed,
|
2024-12-16 23:19:26 +00:00
|
|
|
|
2024-09-09 18:05:14 +01:00
|
|
|
.vaoId = 0,
|
|
|
|
|
.vboId = null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
raylib.UploadMesh(@ptrCast(&mesh), false);
|
|
|
|
|
|
|
|
|
|
return mesh;
|
|
|
|
|
}
|
|
|
|
|
};
|