170 lines
4.9 KiB
Zig
170 lines
4.9 KiB
Zig
|
|
//! ----------------------------------------------------
|
||
|
|
//! ----------------------------------------------------
|
||
|
|
|
||
|
|
//
|
||
|
|
// PRIVATE
|
||
|
|
//
|
||
|
|
|
||
|
|
const std = @import("std");
|
||
|
|
|
||
|
|
//
|
||
|
|
// PUBLIC
|
||
|
|
//
|
||
|
|
|
||
|
|
// --- GENERICS ---
|
||
|
|
pub const Vec = @import("vec.zig").Vec;
|
||
|
|
pub const Mat = @import("mat.zig").Mat;
|
||
|
|
pub const Color = @import("color.zig").Color;
|
||
|
|
|
||
|
|
// --- VEC2 ---
|
||
|
|
pub const Vec2 = Vec(i32, 2);
|
||
|
|
pub const Vec2f = Vec(f32, 2);
|
||
|
|
|
||
|
|
// --- VEC3 ---
|
||
|
|
pub const Vec3 = Vec(i32, 3);
|
||
|
|
pub const Vec3f = Vec(f32, 3);
|
||
|
|
|
||
|
|
// --- MAT ---
|
||
|
|
pub const Mat2x2 = Mat(f32, 2, 2);
|
||
|
|
pub const Mat3x3 = Mat(f32, 3, 3);
|
||
|
|
pub const Mat4x4 = Mat(f32, 4, 4);
|
||
|
|
|
||
|
|
//
|
||
|
|
// TESTS
|
||
|
|
//
|
||
|
|
|
||
|
|
test "init" {
|
||
|
|
std.debug.print("\x1b[34m--- init ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const vec2: Vec2f = .as(12);
|
||
|
|
const vec3: Vec3f = .new(.{ 1, 2, 3 });
|
||
|
|
|
||
|
|
std.debug.print("Vec2: {}\n", .{vec2.value});
|
||
|
|
std.debug.print("Vec3: {}\n", .{vec3.value});
|
||
|
|
}
|
||
|
|
|
||
|
|
test "math" {
|
||
|
|
std.debug.print("\x1b[35m--- math ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const a: Vec3f = .new(.{ 1, 2, 3 });
|
||
|
|
const b: Vec3f = .new(.{ 2, 3, 4 });
|
||
|
|
|
||
|
|
std.debug.print("a = {}\n", .{a.value});
|
||
|
|
std.debug.print("b = {}\n\n", .{b.value});
|
||
|
|
|
||
|
|
std.debug.print("a + b = {}\n", .{a.add(b).value});
|
||
|
|
std.debug.print("a - b = {}\n", .{a.sub(b).value});
|
||
|
|
std.debug.print("a * b = {}\n", .{a.mul(b).value});
|
||
|
|
std.debug.print("a / b = {:.1}\n", .{a.div(b).value});
|
||
|
|
}
|
||
|
|
|
||
|
|
test "mat init" {
|
||
|
|
std.debug.print("\x1b[34m--- mat init ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const identity: Mat4x4 = .identity();
|
||
|
|
std.debug.print("identity[0][0] = {d}\n", .{identity.value[0][0]});
|
||
|
|
std.debug.print("identity[3][3] = {d}\n", .{identity.value[3][3]});
|
||
|
|
}
|
||
|
|
|
||
|
|
test "mat mul" {
|
||
|
|
std.debug.print("\x1b[35m--- mat mul ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const t: Mat4x4 = .translate(1, 2, 3);
|
||
|
|
const rz: Mat4x4 = .rotateZ(@as(f32, std.math.pi) / 4.0);
|
||
|
|
const m = t.mul(rz);
|
||
|
|
|
||
|
|
std.debug.print("translate * rotateZ:\n", .{});
|
||
|
|
for (m.value, 0..) |row, i| {
|
||
|
|
std.debug.print("row {d}: {d:.4} {d:.4} {d:.4} {d:.4}\n", .{ i, row[0], row[1], row[2], row[3] });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
test "mat det" {
|
||
|
|
std.debug.print("\x1b[34m--- mat det ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const m: Mat3x3 = .fromRows(.{
|
||
|
|
.{ 1, 2, 3 },
|
||
|
|
.{ 4, 5, 6 },
|
||
|
|
.{ 7, 8, 10 },
|
||
|
|
});
|
||
|
|
std.debug.print("det = {d:.2}\n", .{m.determinant()});
|
||
|
|
}
|
||
|
|
|
||
|
|
test "mat inverse" {
|
||
|
|
std.debug.print("\x1b[35m--- mat inverse ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const m: Mat4x4 = .translate(5, -3, 2);
|
||
|
|
const inv = m.inverse();
|
||
|
|
const back = m.mul(inv);
|
||
|
|
|
||
|
|
std.debug.print("M * inv(M) should be identity:\n", .{});
|
||
|
|
for (back.value, 0..) |row, i| {
|
||
|
|
std.debug.print("row {d}: {d:.4} {d:.4} {d:.4} {d:.4}\n", .{ i, row[0], row[1], row[2], row[3] });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
test "mat perspective" {
|
||
|
|
std.debug.print("\x1b[34m--- mat perspective (vulkan) ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const p: Mat4x4 = .perspective(@as(f32, std.math.pi) / 4.0, 16.0 / 9.0, 0.1, 100.0);
|
||
|
|
std.debug.print("perspective:\n", .{});
|
||
|
|
for (p.value, 0..) |row, i| {
|
||
|
|
std.debug.print("row {d}: {d:.4} {d:.4} {d:.4} {d:.4}\n", .{ i, row[0], row[1], row[2], row[3] });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
test "mat lookAt" {
|
||
|
|
std.debug.print("\x1b[35m--- mat lookAt ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const eye: Vec3f = .new(.{ 0, 0, 5 });
|
||
|
|
const target: Vec3f = .new(.{ 0, 0, 0 });
|
||
|
|
const up: Vec3f = .new(.{ 0, 1, 0 });
|
||
|
|
const view: Mat4x4 = .lookAt(eye, target, up);
|
||
|
|
|
||
|
|
std.debug.print("lookAt:\n", .{});
|
||
|
|
for (view.value, 0..) |row, i| {
|
||
|
|
std.debug.print("row {d}: {d:.4} {d:.4} {d:.4} {d:.4}\n", .{ i, row[0], row[1], row[2], row[3] });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
test "color init" {
|
||
|
|
std.debug.print("\x1b[34m--- color init ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const c: Color = .rgb(1, 0.5, 0);
|
||
|
|
std.debug.print("orange: r={d:.1} g={d:.1} b={d:.1} a={d:.1}\n", .{ c.r, c.g, c.b, c.a });
|
||
|
|
}
|
||
|
|
|
||
|
|
test "color hex" {
|
||
|
|
std.debug.print("\x1b[35m--- color hex ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const c: Color = .hex(0xFF8800);
|
||
|
|
std.debug.print("0xFF8800: r={d:.4} g={d:.4} b={d:.4} a={d:.1}\n", .{ c.r, c.g, c.b, c.a });
|
||
|
|
}
|
||
|
|
|
||
|
|
test "color blend" {
|
||
|
|
std.debug.print("\x1b[34m--- color blend ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const over = Color.red.blend(Color.green);
|
||
|
|
std.debug.print("red over green: r={d:.4} g={d:.4} b={d:.4} a={d:.1}\n", .{ over.r, over.g, over.b, over.a });
|
||
|
|
}
|
||
|
|
|
||
|
|
test "color hsv" {
|
||
|
|
std.debug.print("\x1b[35m--- color hsv ---\n", .{});
|
||
|
|
defer std.debug.print("\x1b[0m\n", .{});
|
||
|
|
|
||
|
|
const c: Color = .fromHSV(120, 1, 1);
|
||
|
|
const hsv = c.toHSV();
|
||
|
|
std.debug.print("fromHSV(120,1,1): r={d:.4} g={d:.4} b={d:.4} -> h={d:.1} s={d:.1} v={d:.1}\n", .{ c.r, c.g, c.b, hsv.h, hsv.s, hsv.v });
|
||
|
|
}
|