diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.zig | 85 | ||||
| -rw-r--r-- | src/matrix.zig | 11 | 
2 files changed, 54 insertions, 42 deletions
| diff --git a/src/main.zig b/src/main.zig index 89ce7d8..ffaf130 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,7 @@  const std = @import("std");  const matrix = @import("matrix.zig");  const sdl = @import("sdl.zig"); +const ArenaAllocator = std.heap.ArenaAllocator;  const Cube = struct {      points: [8]Point, @@ -57,10 +58,19 @@ pub fn center_point(point: Point, offset: f32) Point {      };  } -pub fn rotate_cube(cube: Cube, alpha: f32, beta: f32) Cube { +pub fn rotate_cube(arena: *ArenaAllocator, cube: Cube, alpha: f32, beta: f32) Cube {      var rotated_cube = Cube{ .points = undefined }; +    for (cube.points) |point, i| { +        rotated_cube.points[i] = rotate_point(arena, point, alpha, beta); +    } + +    return rotated_cube; +} + +pub fn rotate_point(arena: *ArenaAllocator, point: Point, alpha: f32, beta: f32) Point {      var a = matrix.new( +        arena,          3,          3,          &[_]f32{ @@ -71,6 +81,7 @@ pub fn rotate_cube(cube: Cube, alpha: f32, beta: f32) Cube {      );      var b = matrix.new( +        arena,          3,          3,          &[_]f32{ @@ -80,45 +91,43 @@ pub fn rotate_cube(cube: Cube, alpha: f32, beta: f32) Cube {          },      ); -    for (cube.points) |point, i| { -        var m = matrix.multiply( -            matrix.multiply(a, b), -            matrix.new(3, 1, &[3]f32{ point.x, point.y, point.z }), -        ).x.items; -        rotated_cube.points[i].x = m[0]; -        rotated_cube.points[i].y = m[1]; -        rotated_cube.points[i].z = m[2]; -    } +    var m = matrix.multiply( +        arena, +        matrix.multiply(arena, a, b), +        matrix.new(arena, 3, 1, &[3]f32{ point.x, point.y, point.z }), +    ); -    return rotated_cube; +    return Point{ +        .x = m.x.items[0], +        .y = m.x.items[1], +        .z = m.x.items[2], +    };  } -pub fn project_cube(cube: Cube) Cube { +pub fn project_cube(arena: *ArenaAllocator, cube: Cube) Cube {      var projected_cube = Cube{ .points = undefined };      for (cube.points) |point, i| { -        projected_cube.points[i] = project_point(point); +        projected_cube.points[i] = project_point(arena, point);      }      return projected_cube;  } -pub fn project_point(p: Point) Point { +pub fn project_point(arena: *ArenaAllocator, p: Point) Point {      var cx = [_]f32{          1, 0, 0,          0, 1, 0,          0, 0, 0,      }; -    var c = matrix.new(3, 3, &cx); -    var m = matrix.multiply(c, matrix.new(3, 1, &[3]f32{ p.x, p.y, p.z })).x.items; +    var c = matrix.new(arena, 3, 3, &cx); +    var m = matrix.multiply(arena, c, matrix.new(arena, 3, 1, &[3]f32{ p.x, p.y, p.z })); -    var projected_point = Point{ -        .x = m[0], -        .y = m[1], -        .z = m[2], +    return Point{ +        .x = m.x.items[0], +        .y = m.x.items[1], +        .z = m.x.items[2],      }; - -    return projected_point;  }  pub fn scale_cube(cube: Cube, scale: f32) Cube { @@ -131,21 +140,21 @@ pub fn scale_cube(cube: Cube, scale: f32) Cube {      return scaled_cube;  } -pub fn draw_axis(instance: sdl.instance) void { +pub fn draw_axis(arena: *ArenaAllocator, instance: sdl.instance) void {      var o = center_point( -        project_point(Point{ .x = 00, .y = 00, .z = 00 }), +        project_point(arena, Point{ .x = 00, .y = 00, .z = 00 }),          250,      );      var x = center_point( -        project_point(Point{ .x = 50, .y = 00, .z = 00 }), +        project_point(arena, Point{ .x = 50, .y = 00, .z = 00 }),          250,      );      var y = center_point( -        project_point(Point{ .x = 00, .y = 50, .z = 00 }), +        project_point(arena, Point{ .x = 00, .y = 50, .z = 00 }),          250,      );      var z = center_point( -        project_point(Point{ .x = 00, .y = 00, .z = 50 }), +        project_point(arena, Point{ .x = 00, .y = 00, .z = 50 }),          250,      ); @@ -178,6 +187,7 @@ pub fn main() anyerror!void {      var beta: f32 = 0;      var alpha: f32 = 0;      while (true) { +        var arena = ArenaAllocator.init(std.heap.page_allocator);          var event = sdl.get_event();          if (event.mode == sdl.modes.quit) {              break; @@ -191,37 +201,38 @@ pub fn main() anyerror!void {          sdl.draw_color(instance, 0, 0, 0, 0);          sdl.clear(instance); -        var cube = rotate_cube(unit_cube, alpha, beta); +        draw_axis(&arena, instance); + +        var cube = rotate_cube(&arena, unit_cube, alpha, beta);          cube = scale_cube(cube, 50); -        cube = project_cube(cube); +        cube = project_cube(&arena, cube);          cube = center_cube(cube, 250);          draw_cube(instance, cube); -        cube = rotate_cube(unit_cube, alpha + 90, beta + 90); +        cube = rotate_cube(&arena, unit_cube, alpha + 90, beta + 90);          cube = scale_cube(cube, 25); -        cube = project_cube(cube); +        cube = project_cube(&arena, cube);          cube = center_cube(cube, 250);          draw_cube(instance, cube);          cube = shift_cube(unit_cube, 5, 0, 0); -        cube = rotate_cube(cube, 2 * alpha, 2 * beta); +        cube = rotate_cube(&arena, cube, 2 * alpha, 2 * beta);          cube = scale_cube(cube, 25); -        cube = project_cube(cube); +        cube = project_cube(&arena, cube);          cube = center_cube(cube, 250);          draw_cube(instance, cube);          cube = shift_cube(unit_cube, -5, 0, 0); -        cube = rotate_cube(cube, 2 * alpha, 2 * beta); +        cube = rotate_cube(&arena, cube, 2 * alpha, 2 * beta);          cube = scale_cube(cube, 25); -        cube = project_cube(cube); +        cube = project_cube(&arena, cube);          cube = center_cube(cube, 250);          draw_cube(instance, cube); -        draw_axis(instance); -          beta += 0.01;          alpha += 0.01; +        arena.deinit();          sdl.present(instance);          sdl.delay(10);      } diff --git a/src/matrix.zig b/src/matrix.zig index eaf9685..e566bc4 100644 --- a/src/matrix.zig +++ b/src/matrix.zig @@ -1,5 +1,6 @@  const std = @import("std");  const ArrayList = std.ArrayList; +const ArenaAllocator = std.heap.ArenaAllocator;  const matrix = struct {      h: usize, @@ -7,11 +8,11 @@ const matrix = struct {      x: ArrayList(f32),  }; -pub fn new(h: usize, w: usize, x: []f32) matrix { +pub fn new(arena: *ArenaAllocator, h: usize, w: usize, x: []f32) matrix {      var m = matrix{          .h = h,          .w = w, -        .x = ArrayList(f32).init(std.heap.page_allocator), +        .x = ArrayList(f32).init(&arena.allocator),      };      var i: usize = 0; @@ -37,8 +38,8 @@ pub fn dot(a: []f32, b: []f32, len: usize, step: usize) f32 {      return x;  } -pub fn multiply(a: matrix, b: matrix) matrix { -    var x = ArrayList(f32).init(std.heap.page_allocator); +pub fn multiply(arena: *ArenaAllocator, a: matrix, b: matrix) matrix { +    var x = ArrayList(f32).init(&arena.allocator);      var i: usize = 0;      var k: usize = 0; @@ -50,5 +51,5 @@ pub fn multiply(a: matrix, b: matrix) matrix {          k += a.w;      } -    return new(a.h, b.w, x.items); +    return new(arena, a.h, b.w, x.items);  } | 
