52 lines
1.4 KiB
Zig
52 lines
1.4 KiB
Zig
const Transform = @This();
|
|
|
|
const math = @import("math.zig");
|
|
|
|
base: math.Mat4x4,
|
|
translation: math.Mat4x4,
|
|
scale: math.Mat4x4,
|
|
|
|
pub fn init() Transform {
|
|
return .{
|
|
.base = .id,
|
|
.translation = .id,
|
|
.scale = .id,
|
|
};
|
|
}
|
|
|
|
const TransformType = enum {
|
|
orthographic,
|
|
translation,
|
|
scale,
|
|
};
|
|
|
|
pub fn set(transform: *Transform, tag: TransformType, transformation: math.Mat4x4) void {
|
|
switch (tag) {
|
|
.orthographic => transform.base = transformation,
|
|
.translation => transform.translation = transformation,
|
|
.scale => transform.scale = transformation,
|
|
}
|
|
}
|
|
|
|
pub fn add(transform: *Transform, tag: TransformType, transformation: math.Mat4x4) void {
|
|
switch (tag) {
|
|
.orthographic => @compileError("you shouldn't add to orthographic transformation, use `.set` instead!"),
|
|
.translation => transform.translation.mult(transformation),
|
|
.scale => transform.scale.mult(transformation),
|
|
}
|
|
}
|
|
|
|
pub fn get(transform: *Transform) math.Mat4x4 {
|
|
const width: f32 = 2.0 / transform.base.data[0][0];
|
|
const height: f32 = 2.0 / transform.base.data[1][1];
|
|
|
|
var result = transform.base;
|
|
result.mult(math.Mat4x4.translation(math.Vec2 { width/2.0, height/2.0 }));
|
|
result.mult(transform.scale);
|
|
result.mult(math.Mat4x4.translation(math.Vec2 { -width/2.0, -height/2.0 }));
|
|
|
|
result.mult(transform.translation);
|
|
|
|
return result;
|
|
}
|