Use castle for vecmath (doesn't work fully yet)

This commit is contained in:
2026-01-04 17:13:58 +01:00
parent 21c1d2e139
commit fed1e982d2
23 changed files with 424 additions and 1894 deletions

View File

@@ -4,6 +4,7 @@ const std = @import("std");
const math = @import("../math.zig");
const shaders = @import("../shaders.zig");
const vk = @import("vulkan");
const vm = @import("vecmath");
const voxels = @import("../voxels.zig");
const Blocks = @import("Blocks.zig");
@@ -13,9 +14,7 @@ const Game = @import("../Game.zig");
const GenericBuffer = @import("../engine/GenericBuffer.zig").GenericBuffer;
const Materials = @import("Materials.zig");
const Matrix4x4 = math.Matrix4x4;
const ObjectUniformsBuffer = GenericBuffer(void, shaders.ObjectUniforms);
const Vector3 = math.Vector3;
pub const chunk_size = 16;
@@ -25,7 +24,7 @@ var next_chunk_id: std.atomic.Value(u64) = .init(0);
/// To get the block at coordinates (x, y, z) relative to the chunk, read the
/// value at `blocks[z][y][x]`.
blocks: [chunk_size][chunk_size][chunk_size]Blocks.Id,
origin: Vector3,
origin: vm.Vector3,
descriptor_set: vk.DescriptorSet,
object_buffer: ObjectUniformsBuffer,
object_count: u32,
@@ -34,7 +33,7 @@ object_count: u32,
chunk_id: u64,
pub const InitInfo = struct {
origin: Vector3,
origin: vm.Vector3,
descriptor_pool: vk.DescriptorPool,
per_batch_descriptor_set_layout: vk.DescriptorSetLayout,
};
@@ -110,14 +109,18 @@ pub fn refresh(self: *Chunk, engine: *Engine, blocks: *const Blocks, neighbors:
for (row, 0..) |id, ix| {
const fx: f32 = @floatFromInt(ix);
const block: *const Blocks.Definition = &blocks.array.items[id.toInt()];
const origin = Vector3.add(self.origin, .init(fx, fy, fz));
const origin = vm.Vector3.add(self.origin, .init(fx, fy, fz));
inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| {
const side = @field(voxels.Orientation, field.name);
const material = @field(block.walls, field.name).material;
if (material != .empty and self.getOpposite(side, ix, iy, iz, blocks, neighbors) == .empty) {
const matrix = getMatrix(side, origin);
try uniforms.append(temp_allocator, .init(matrix, matrix, material));
try uniforms.append(temp_allocator, .{
.matrixOStoWS = matrix,
.matrixOStoWSNormal = matrix,
.material = material,
});
}
}
}
@@ -209,44 +212,44 @@ fn getOpposite(self: *const Chunk, comptime side: voxels.Orientation, x: usize,
};
}
fn getMatrix(comptime side: voxels.Orientation, origin: Vector3) Matrix4x4 {
fn getMatrix(comptime side: voxels.Orientation, origin: vm.Vector3) vm.Matrix4x4 {
return switch (side) {
// zig fmt: off
.positive_x => .init(
0, 1, 0, 0,
0, 0, 1, 0,
1, 0, 0, 0,
origin.getX() + 1, origin.getY(), origin.getZ(), 1,
origin.x + 1, origin.y, origin.z, 1,
),
.negative_x => .init(
0, -1, 0, 0,
0, 0, 1, 0,
-1, 0, 0, 0,
origin.getX(), origin.getY() + 1, origin.getZ(), 1,
0, -1, 0, 0,
0, 0, 1, 0,
-1, 0, 0, 0,
origin.x, origin.y + 1, origin.z, 1,
),
.positive_y => .init(
-1, 0, 0, 0,
0, 0, 1, 0,
0, 1, 0, 0,
origin.getX() + 1, origin.getY() + 1, origin.getZ(), 1,
0, 0, 1, 0,
0, 1, 0, 0,
origin.x + 1, origin.y + 1, origin.z, 1,
),
.negative_y => .init(
1, 0, 0, 0,
0, 0, 1, 0,
1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
origin.getX(), origin.getY(), origin.getZ(), 1,
origin.x, origin.y, origin.z, 1,
),
.positive_z => .init(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
origin.getX(), origin.getY(), origin.getZ() + 1, 1,
origin.x, origin.y, origin.z + 1, 1,
),
.negative_z => .init(
1, 0, 0, 0,
0, -1, 0, 0,
0, 0, -1, 0,
origin.getX() + 1, origin.getY() + 1, origin.getZ(), 1,
-1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
origin.x + 1, origin.y + 1, origin.z, 1,
),
// zig fmt: on
};

View File

@@ -80,8 +80,8 @@ pub fn init(engine: *Engine, allocator: std.mem.Allocator) !Materials {
.element_offset = Id.empty.toInt(),
.elements = &.{
.{
.base_color = .{ 0, 0, 0 },
.emissive = .{ 1, 0, 1 },
.base_color = .zero,
.emissive = .init(1, 0, 1),
.ior = 1.45,
.metallic = 0,
.normal_scale = 1,
@@ -295,8 +295,8 @@ fn loadMaterial(self: *Materials, engine: *Engine, textures: *Textures, filename
.element_offset = index,
.elements = &.{
.{
.base_color = material_json.baseColor,
.emissive = material_json.emissive,
.base_color = .initArray(material_json.baseColor),
.emissive = .initArray(material_json.emissive),
.ior = material_json.ior,
.metallic = material_json.metallic,
.normal_scale = material_json.normalScale,