start rewriting mesh code in zig

This commit is contained in:
catangent 2024-12-16 23:19:26 +00:00
parent 5a76cdcd13
commit 6f1eb16a30
14 changed files with 47357 additions and 40 deletions

View file

@ -1,11 +1,11 @@
const std = @import("std");
const rh = @import("../util/raylib_helper.zig");
const raylib = rh.raylib;
const v3 = rh.v3;
const raylib_helper = @import("../lib_helpers/raylib_helper.zig");
const raylib = raylib_helper.raylib;
const v3 = raylib_helper.v3;
const A7r = std.mem.Allocator;
const RawQuad = struct {
tile: i32,
tile: u32,
top_left: raylib.Vector3,
top_right: raylib.Vector3,
bottom_right: raylib.Vector3,
@ -16,13 +16,13 @@ const RawQuad = struct {
};
pub const Chunk = struct {
tiles: []i32,
tiles: []u32,
a7r: A7r,
pub fn init(a7r: A7r) !Chunk {
const self = Chunk{
.a7r = a7r,
.tiles = try a7r.alloc(i32, 32 * 32 * 32),
.tiles = try a7r.alloc(u32, 32 * 32 * 32),
};
@memset(self.tiles, 0);
return self;
@ -32,19 +32,19 @@ pub const Chunk = struct {
self.a7r.free(self.tiles);
}
pub fn getTile(self: Chunk, x: u5, y: u5, z: u5) i32 {
pub fn getTile(self: Chunk, x: u5, y: u5, z: u5) u32 {
return self.tiles[@as(u15, x) << 10 | @as(u15, y) << 5 | @as(u15, z)];
}
pub fn setTile(self: Chunk, x: u5, y: u5, z: u5, tile: i32) void {
pub fn setTile(self: Chunk, x: u5, y: u5, z: u5, tile: u32) void {
self.tiles[@as(u15, x) << 10 | @as(u15, y) << 5 | @as(u15, z)] = tile;
}
fn getTileRaw(self: Chunk, x: u5, y: u5, z: u5) i32 {
fn getTileRaw(self: Chunk, x: u5, y: u5, z: u5) u32 {
return self.tiles[@as(u15, x) << 10 | @as(u15, y) << 5 | @as(u15, z)];
}
inline fn getTileRawShifted(self: Chunk, x: u5, y: u5, z: u5, comptime d: comptime_int) i32 {
inline fn getTileRawShifted(self: Chunk, x: u5, y: u5, z: u5, comptime d: comptime_int) u32 {
if (d % 3 == 0) {
return self.getTileRaw(x, y, z);
} else if (d % 3 == 1) {
@ -61,12 +61,12 @@ pub const Chunk = struct {
inline for (0..3) |d| {
for (0..32) |raw_x| {
const x: u5 = @intCast(raw_x);
var positive_tile_surfaces: [32][32]i32 = .{.{0} ** 32} ** 32;
var negative_tile_surfaces: [32][32]i32 = .{.{0} ** 32} ** 32;
var positive_tile_surfaces: [32][32]u32 = .{.{0} ** 32} ** 32;
var negative_tile_surfaces: [32][32]u32 = .{.{0} ** 32} ** 32;
for (0..32) |raw_y| for (0..32) |raw_z| {
const y: u5 = @intCast(raw_y);
const z: u5 = @intCast(raw_z);
const tile: i32 = chunk.getTileRawShifted(x, y, z, d);
const tile: u32 = chunk.getTileRawShifted(x, y, z, d);
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;
@ -154,7 +154,7 @@ pub const Chunk = struct {
for (raw_quads.items, 0..) |raw_quad, i| {
if (raw_quad.tile <= 0) continue;
const tile = @as(u32, @intCast(raw_quad.tile));
const tile = raw_quad.tile;
for (0..6) |j| {
normals[18 * i + 3 * j + 0] = raw_quad.normal.x;
@ -224,13 +224,7 @@ pub const Chunk = struct {
.texcoords = texcoords,
.texcoords2 = texcoords2,
.normals = normals,
.tangents = null,
.colors = null,
.indices = null,
.animVertices = null,
.animNormals = null,
.boneIds = null,
.boneWeights = null,
.vaoId = 0,
.vboId = null,
};