Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 879e442ac6 |
@@ -21,15 +21,16 @@ pub fn init(
|
|||||||
return .{ .data = data };
|
return .{ .data = data };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *@This(), shader: Shader) void {
|
pub fn draw(self: *@This(), allocator: std.mem.Allocator, shader: Shader) !void {
|
||||||
var iter = self.data.depthFirstIterator();
|
var iter = self.data.depthFirstIterator();
|
||||||
while (iter.next()) |node| {
|
defer iter.deinit(allocator);
|
||||||
var rotation = node.val.rotation;
|
while (try iter.next(allocator)) |node| {
|
||||||
var current = node;
|
const rotation = node.val.rotation;
|
||||||
while (current.parent) |parent| {
|
// var current = node;
|
||||||
rotation = zm.qmul(rotation, parent.val.rotation);
|
// while (current.parent) |parent| {
|
||||||
current = parent;
|
// rotation = zm.qmul(rotation, parent.val.rotation);
|
||||||
}
|
// current = parent;
|
||||||
|
// }
|
||||||
|
|
||||||
const model_matrix = zm.mul(zm.quatToMat(rotation), node.val.translation);
|
const model_matrix = zm.mul(zm.quatToMat(rotation), node.val.translation);
|
||||||
const normal_matrix = zm.transpose(zm.inverse(model_matrix));
|
const normal_matrix = zm.transpose(zm.inverse(model_matrix));
|
||||||
@@ -39,6 +40,6 @@ pub fn draw(self: *@This(), shader: Shader) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void {
|
pub fn deinit(self: *@This(), allocator: std.mem.Allocator) !void {
|
||||||
self.data.deinit(allocator);
|
try self.data.deinit(allocator);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ pub fn main() !void {
|
|||||||
defer floor.delete();
|
defer floor.delete();
|
||||||
|
|
||||||
var robot = try initRobot(allocator, robot_texture);
|
var robot = try initRobot(allocator, robot_texture);
|
||||||
defer robot.deinit(allocator);
|
defer robot.deinit(allocator) catch {};
|
||||||
|
|
||||||
// uniform data
|
// uniform data
|
||||||
var light_color = zm.f32x4(1, 1, 1, 1);
|
var light_color = zm.f32x4(1, 1, 1, 1);
|
||||||
@@ -163,7 +163,7 @@ pub fn main() !void {
|
|||||||
floor.draw(model_shader);
|
floor.draw(model_shader);
|
||||||
|
|
||||||
// animateRobot(&frame_counter, robot);
|
// animateRobot(&frame_counter, robot);
|
||||||
robot.draw(model_shader);
|
try robot.draw(allocator, model_shader);
|
||||||
},
|
},
|
||||||
.fish => {},
|
.fish => {},
|
||||||
}
|
}
|
||||||
|
|||||||
61
src/tree.zig
61
src/tree.zig
@@ -1,5 +1,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
|
const ArrayList = std.ArrayList;
|
||||||
|
|
||||||
pub fn Tree(comptime T: type) type {
|
pub fn Tree(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
@@ -7,71 +8,90 @@ pub fn Tree(comptime T: type) type {
|
|||||||
|
|
||||||
root: Node,
|
root: Node,
|
||||||
|
|
||||||
const Children = std.ArrayList(Node);
|
const Children = ArrayList(Node);
|
||||||
const Node = struct {
|
const Node = struct {
|
||||||
val: T,
|
val: T,
|
||||||
parent: ?*Node,
|
// index: usize,
|
||||||
index: usize,
|
children: ArrayList(Node),
|
||||||
children: std.ArrayList(Node),
|
|
||||||
|
|
||||||
pub fn init(val: T, parent: ?*Node, index: usize) Node {
|
pub fn init(val: T) Node {
|
||||||
return .{
|
return .{
|
||||||
.val = val,
|
.val = val,
|
||||||
.children = .empty,
|
.children = .empty,
|
||||||
.parent = parent,
|
|
||||||
.index = index,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn append(self: *Node, allocator: Allocator, val: T) !void {
|
pub fn append(self: *Node, allocator: Allocator, val: T) !void {
|
||||||
const child = Node.init(val, self, self.children.items.len);
|
const child = Node.init(val);
|
||||||
try self.children.append(allocator, child);
|
try self.children.append(allocator, child);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(val: T) @This() {
|
pub fn init(val: T) @This() {
|
||||||
return .{ .root = .init(val, null, 0) };
|
return .{ .root = .init(val) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const DepthFirstIterator = struct {
|
pub const DepthFirstIterator = struct {
|
||||||
|
const Frame = struct {
|
||||||
|
parent: ?*Node,
|
||||||
|
index: usize,
|
||||||
|
};
|
||||||
const State = enum {
|
const State = enum {
|
||||||
GoDeeper,
|
GoDeeper,
|
||||||
GoBroader,
|
GoBroader,
|
||||||
};
|
};
|
||||||
tree: *Tree(T),
|
tree: *Tree(T),
|
||||||
current: ?*Node,
|
current: ?*Node,
|
||||||
|
path: ArrayList(Frame),
|
||||||
state: State,
|
state: State,
|
||||||
|
|
||||||
pub fn init(tree: *Tree(T)) DepthFirstIterator {
|
pub fn init(tree: *Tree(T)) DepthFirstIterator {
|
||||||
return DepthFirstIterator{
|
return DepthFirstIterator{
|
||||||
.tree = tree,
|
.tree = tree,
|
||||||
.current = &tree.root,
|
.current = &tree.root,
|
||||||
|
.path = .empty,
|
||||||
.state = State.GoDeeper,
|
.state = State.GoDeeper,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next(it: *DepthFirstIterator) ?*Node {
|
pub fn next(it: *DepthFirstIterator, allocator: Allocator) !?*Node {
|
||||||
// State machine
|
// State machine
|
||||||
while (it.current) |current| {
|
while (it.current) |current| {
|
||||||
switch (it.state) {
|
switch (it.state) {
|
||||||
State.GoDeeper => {
|
State.GoDeeper => {
|
||||||
// Follow child node until deepest possible level
|
// Follow child node until deepest possible level
|
||||||
if (current.children.items.len > 0) {
|
if (current.children.items.len > 0) {
|
||||||
|
try it.path.append(allocator, .{
|
||||||
|
.index = 0,
|
||||||
|
.parent = current,
|
||||||
|
});
|
||||||
it.current = ¤t.children.items[0];
|
it.current = ¤t.children.items[0];
|
||||||
} else {
|
} else {
|
||||||
it.state = State.GoBroader;
|
it.state = State.GoBroader;
|
||||||
|
_ = it.path.pop();
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
State.GoBroader => {
|
State.GoBroader => {
|
||||||
if (current.parent) |parent| {
|
const last_frame = it.path.pop();
|
||||||
if (parent.children.items.len - 1 > (current.index)) {
|
if (last_frame) |frame| {
|
||||||
it.current = &parent.children.items[current.index + 1];
|
if (frame.parent) |parent| {
|
||||||
it.state = .GoDeeper;
|
if (parent.children.items.len > frame.index + 1) {
|
||||||
|
it.current = &parent.children.items[frame.index + 1];
|
||||||
|
} else {
|
||||||
|
it.current = parent;
|
||||||
|
// return it.path.pop().?.parent;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
it.current = current.parent;
|
return null;
|
||||||
return current.parent;
|
|
||||||
}
|
}
|
||||||
|
// if (parent.children.items.len > current.index + 1) {
|
||||||
|
// it.current = &parent.children.items[current.index + 1];
|
||||||
|
// it.state = .GoDeeper;
|
||||||
|
// } else {
|
||||||
|
// it.current = current.parent;
|
||||||
|
// return current.parent;
|
||||||
|
// }
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -84,15 +104,20 @@ pub fn Tree(comptime T: type) type {
|
|||||||
pub fn reset(it: *DepthFirstIterator) void {
|
pub fn reset(it: *DepthFirstIterator) void {
|
||||||
it.current = it.tree.root;
|
it.current = it.tree.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *DepthFirstIterator, allocator: Allocator) void {
|
||||||
|
self.path.deinit(allocator);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn depthFirstIterator(tree: *@This()) DepthFirstIterator {
|
pub fn depthFirstIterator(tree: *@This()) DepthFirstIterator {
|
||||||
return DepthFirstIterator.init(tree);
|
return DepthFirstIterator.init(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *@This(), allocator: Allocator) void {
|
pub fn deinit(self: *@This(), allocator: Allocator) !void {
|
||||||
var iterator = self.depthFirstIterator();
|
var iterator = self.depthFirstIterator();
|
||||||
while (iterator.next()) |node| {
|
defer iterator.deinit(allocator);
|
||||||
|
while (try iterator.next(allocator)) |node| {
|
||||||
node.children.deinit(allocator);
|
node.children.deinit(allocator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user