fix matrix issue and compile errors relating to table (one remains)

This commit is contained in:
Vivianne 2022-07-26 03:00:39 -07:00
parent 742f966e14
commit 73a91195b6
3 changed files with 53 additions and 63 deletions

View file

@ -3,48 +3,9 @@ const vec = @import("vec.zig");
const Vec3f = vec.Vec3f;
const Vec4f = vec.Vec4f;
pub const Mat3f = Mat(3, Vec3f);
pub const Mat4f = packed struct {
const Self = @This();
pub const zero = Mat(4, Vec4f).zero;
pub const id = Mat(4, Vec4f).id;
pub usingnamespace Mat(4, Vec4f);
pub inline fn lookAt(eye: Vec4f, at: Vec4f, up: Vec4f) Self {
// right-handed (ew)
const f = at.sub(eye).normalize3();
const s = f.cross3(up).normalize3();
const u = s.cross3(f).normalize3();
const tx = -s.dot3(eye);
const ty = -u.dot3(eye);
const tz = f.dot3(eye);
return Self{
.c0 = .{ .x = s.x, .y = u.x, .z = -f.x, .w = 0 },
.c1 = .{ .x = s.y, .y = u.y, .z = -f.y, .w = 0 },
.c2 = .{ .x = s.z, .y = u.z, .z = -f.z, .w = 0 },
.c3 = .{ .x = tx, .y = ty, .z = tz, .w = 1 },
};
}
pub inline fn glPerspective(fov_y: f32, aspect: f32, z_near: f32, z_far: f32) Self {
const t = std.math.tan(fov_y * 0.5);
var m = zero;
m.c0.x = 1.0 / (aspect * t);
m.c1.y = 1.0 / t;
m.c2.z = -1.0 * (z_far * z_near) / (z_far - z_near);
m.c3.z = -2.0 * (z_far * z_near) / (z_far - z_near);
return m;
}
pub inline fn vkPerspective(fov_y: f32, aspect: f32, z_near: f32, z_far: f32) Self {
var m = glPerspective(fov_y, aspect, z_near, z_far);
m.c1.y *= -1.0;
return m;
}
};
// Intentionally use Vec4f to hopefully improve vectorization.
pub const Mat3f = Mat(3, Vec4f);
pub const Mat4f = Mat(4, Vec4f);
pub fn Mat(comptime nelem: comptime_int, comptime Vec: type) type {
return packed struct {
@ -80,5 +41,41 @@ pub fn Mat(comptime nelem: comptime_int, comptime Vec: type) type {
.c3 = if (nelem > 3) a.mulCol(b.c3) else {},
};
}
// Special methods specifically for Mat4f.
pub usingnamespace if (nelem == 4 and Vec == Vec4f) struct {
pub inline fn lookAt(eye: Vec4f, at: Vec4f, up: Vec4f) Self {
// right-handed (ew)
const f = at.sub(eye).normalize3();
const s = f.cross3(up).normalize3();
const u = s.cross3(f).normalize3();
const tx = -s.dot3(eye);
const ty = -u.dot3(eye);
const tz = f.dot3(eye);
return Self{
.c0 = .{ .x = s.x, .y = u.x, .z = -f.x, .w = 0 },
.c1 = .{ .x = s.y, .y = u.y, .z = -f.y, .w = 0 },
.c2 = .{ .x = s.z, .y = u.z, .z = -f.z, .w = 0 },
.c3 = .{ .x = tx, .y = ty, .z = tz, .w = 1 },
};
}
pub inline fn glPerspective(fov_y: f32, aspect: f32, z_near: f32, z_far: f32) Self {
const t = std.math.tan(fov_y * 0.5);
var m = Mat(4, Vec4f).zero;
m.c0.x = 1.0 / (aspect * t);
m.c1.y = 1.0 / t;
m.c2.z = -1.0 * (z_far * z_near) / (z_far - z_near);
m.c3.z = -2.0 * (z_far * z_near) / (z_far - z_near);
return m;
}
pub inline fn vkPerspective(fov_y: f32, aspect: f32, z_near: f32, z_far: f32) Self {
var m = glPerspective(fov_y, aspect, z_near, z_far);
m.c1.y *= -1.0;
return m;
}
} else struct {};
};
}

View file

@ -4,7 +4,7 @@ const UUID = @import("../common/uuid.zig").UUID;
const Table = @import("../containers/table.zig").Table;
const Vec2i = @import("../math/vec.zig").Vec2i;
const TTable = Table(UUID, Texture.Id);
const TTable = Table(UUID, Texture);
var s_table: TTable = undefined;
pub fn init() void {
@ -22,6 +22,8 @@ pub fn getTable() *const TTable {
pub const Texture = packed struct {
const Self = @This();
pub const Id = TTable.Id;
const PalRow = enum {
white,
brown,
@ -48,18 +50,13 @@ pub const Texture = packed struct {
format: vk.Format,
slot: Id,
pub fn get(id: Id) *const Self {
pub fn get(id: Id) ?*const Self {
return s_table.get(id);
}
pub fn isCurrent(id: Id) bool {
return s_table.exists(id);
}
pub const Id = packed struct {
version: u8,
index: u24,
};
};
pub const DiskTexture = packed struct {
@ -67,8 +64,4 @@ pub const DiskTexture = packed struct {
format: vk.Format,
name: []u8,
size: Vec2i,
pub const Id = packed struct {
id: UUID,
};
};

View file

@ -427,19 +427,19 @@ const OpaquePass = struct {
const ents = Entities.get();
for (ents.meshes.items) |mesh, i| {
const albedo = Texture.get(ents.materials.items[i].albedo);
const rome = Texture.get(ents.materials.items[i].rome);
const normal = Texture.get(ents.materials.items[i].normal);
const mAlbedo = Texture.get(ents.materials.items[i].albedo);
const mRome = Texture.get(ents.materials.items[i].rome);
const mNormal = Texture.get(ents.materials.items[i].normal);
const pc = PushConstants{
.kLocalToWorld = ents.matrices[i],
.kIMc0 = ents.invMatrices[i].c0,
.kIMc1 = ents.invMatrices[i].c1,
.kIMc2 = ents.invMatrices[i].c2,
.kLocalToWorld = ents.matrices.items[i],
.kIMc0 = ents.inv_matrices.items[i].c0,
.kIMc1 = ents.inv_matrices.items[i].c1,
.kIMc2 = ents.inv_matrices.items[i].c2,
.kTexInds = .{
.x = if (albedo) albedo.slot.index else 0,
.y = if (rome) rome.slot.index else 0,
.z = if (normal) normal.slot.index else 0,
.x = if (mAlbedo) |albedo| albedo.slot.index else 0,
.y = if (mRome) |rome| rome.slot.index else 0,
.z = if (mNormal) |normal| normal.slot.index else 0,
.w = 0,
},
};