diff options
author | alaric <alaric@netmythos.org> | 2024-04-12 14:02:06 -0700 |
---|---|---|
committer | alaric <alaric@netmythos.org> | 2024-04-12 14:02:06 -0700 |
commit | 9af697af684a15aa22a04221e214e26b220c33b0 (patch) | |
tree | ba09c0b81298657a8d05e6dfd83adae1790640b5 | |
parent | 07e97f107706098712d23b433da34ad4a0dcea88 (diff) | |
download | engine-9af697af684a15aa22a04221e214e26b220c33b0.tar.gz engine-9af697af684a15aa22a04221e214e26b220c33b0.zip |
Add view to basic shader
-rw-r--r-- | src/matrix.zig | 19 | ||||
-rw-r--r-- | src/shaders/vertex_solid2d.glsl | 3 | ||||
-rw-r--r-- | src/webgl.zig | 1 |
3 files changed, 20 insertions, 3 deletions
diff --git a/src/matrix.zig b/src/matrix.zig index 8ce1dfc..49e9b31 100644 --- a/src/matrix.zig +++ b/src/matrix.zig @@ -24,6 +24,22 @@ pub fn Matrix(comptime T: type, row_count: usize, col_count: usize) type { data: [cols]ColT, + pub fn transform(self: Self, point: ColT) ColT { + const other_mat: Matrix(T, 3, 1) = .{ .data = @bitCast(point) }; + const r = self.multiply(other_mat); + return r.col(0); + } + + pub fn multScalar(self: Self, val: T) Self { + var result = self; + for (&result.data) |*c| { + for (c) |*r| { + r.* = r.* * val; + } + } + return result; + } + pub fn multiply(self: Self, other: anytype) MatMultResult(Self, @TypeOf(other)) { const OtherT = @TypeOf(other); comptime assert(Self.cols == OtherT.rows); @@ -112,8 +128,7 @@ pub fn Matrix(comptime T: type, row_count: usize, col_count: usize) type { } pub fn col(self: Self, ci: usize) ColT { - const start = ci * rows; - return self.data[start .. start + cols]; + return self.data[ci]; } pub fn equals(self: Self, other: Self) bool { diff --git a/src/shaders/vertex_solid2d.glsl b/src/shaders/vertex_solid2d.glsl index 874784a..0ff6142 100644 --- a/src/shaders/vertex_solid2d.glsl +++ b/src/shaders/vertex_solid2d.glsl @@ -1,6 +1,7 @@ #version 300 es uniform mat3 projection; +uniform mat3 view; uniform mat3 model; uniform vec3 color; @@ -9,6 +10,6 @@ in vec2 pos; out vec4 a_col; void main() { - gl_Position = vec4((projection * model * vec3(pos, 1)).xy, 0, 1); + gl_Position = vec4((projection * view * model * vec3(pos, 1)).xy, 0, 1); a_col = vec4(color, 1); } diff --git a/src/webgl.zig b/src/webgl.zig index abd9df4..c4b7af8 100644 --- a/src/webgl.zig +++ b/src/webgl.zig @@ -237,6 +237,7 @@ pub const Shaders = struct { pub const General2DSolidProgram = Program( &.{ .{ .identifier = "projection", .shader_name = "projection", .kind = .mat3 }, + .{ .identifier = "view", .shader_name = "view", .kind = .mat3 }, .{ .identifier = "model", .shader_name = "model", .kind = .mat3 }, .{ .identifier = "color", .shader_name = "color", .kind = .vec3 }, }, |