Skip to content

Commit

Permalink
camera
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Oct 5, 2024
1 parent b27d30f commit d0377e2
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/zig-out/bin/gltf.exe",
"program": "${workspaceFolder}/zig-out/bin/camera.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/zig-out/web",
Expand Down
4 changes: 4 additions & 0 deletions deps/sokol_samples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ pub const samples = [_]Sample{
.name = "simple_texture",
.root_source_file = "tutorials/simple_texture/main.zig",
},
.{
.name = "camera",
.root_source_file = "tutorials/camera/main.zig",
},
//
.{
.name = "glb",
Expand Down
5 changes: 2 additions & 3 deletions deps/sokol_samples/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
},
// camera etc
.rowmath = .{
.url = "git+https://github.com/ousttrue/rowmath.git#62ef788d5643f0530bed28b19b390a5a5ca3f72f",
.hash = "122085faa14bfe71cc4c324a16a4a156f6cbd05ec3c6ea9e668bb569d2ee1f24b729",
.url = "git+https://github.com/ousttrue/rowmath.git#0bfae3e40cfea967346bd8a4d5b2745af8e2b79b",
.hash = "1220d127336ca0674f352559883de2420a529f13dcb7fbe55b653364452768c950a4",
},
// assets
.@"gltf-test-models" = .{
.url = "git+https://github.com/ousttrue/gltf-test-models.git#55328b2d063141993373ab8c770c72d7cfda260f",
.hash = "1220aa78e130eb4c584dc21b408dcd26e8cfd30043e0f673ba6d229a0bfff592c54e",
Expand Down
147 changes: 82 additions & 65 deletions deps/sokol_samples/sample_framework/Scene.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pip: sg.Pipeline = undefined,
gltf: ?std.json.Parsed(zigltf.Gltf) = null,
white_texture: Mesh.Texture = undefined,
animations: []Animation = &.{},
currentAnimation: ?usize = null,
current_animation: ?usize = null,
node_matrices: []Mat4 = &.{},

pub fn init(self: *@This(), allocator: std.mem.Allocator) void {
self.allocator = allocator;
Expand Down Expand Up @@ -135,8 +136,7 @@ pub fn load(
return error.IndexNotScalar;
}
switch (index_accessor.componentType) {
5121 => {
// byte
.byte => {
const indices = try gltf_buffer.getAccessorBytes(
u8,
indices_accessor_index,
Expand All @@ -151,8 +151,7 @@ pub fn load(
);
}
},
5123 => {
// ushort
.ushort => {
const indices = try gltf_buffer.getAccessorBytes(
u16,
indices_accessor_index,
Expand All @@ -164,8 +163,7 @@ pub fn load(
);
}
},
5125 => {
// uint
.uint => {
const indices = try gltf_buffer.getAccessorBytes(
u32,
indices_accessor_index,
Expand Down Expand Up @@ -302,75 +300,73 @@ pub fn load(
}

self.animations = try animations.toOwnedSlice();
self.currentAnimation = 0;
self.current_animation = 0;
}

var node_matrices = std.ArrayList(Mat4).init(self.allocator);
defer node_matrices.deinit();
try node_matrices.resize(gltf.nodes.len);
self.node_matrices = try node_matrices.toOwnedSlice();
_ = self.update(0);
}

pub fn update(self: @This(), time: f32) ?f32 {
pub fn update(self: *@This(), time: f32) ?f32 {
const gltf = self.gltf orelse {
return null;
};

const current = self.currentAnimation orelse {
return null;
};

if (current >= self.animations.len) {
return null;
}

const animation = &self.animations[current];
const looptime = animation.loopTime(time);
for (animation.curves) |curve| {
switch (curve.target) {
.translation => |values| {
const t = values.sample(looptime);
gltf.value.nodes[curve.node_index].translation = .{
t.x,
t.y,
t.z,
};
},
.rotation => |values| {
const r = values.sample(looptime);
gltf.value.nodes[curve.node_index].rotation = .{
r.x,
r.y,
r.z,
r.w,
};
},
.scale => |values| {
_ = values;
unreachable;
},
.weights => |values| {
_ = values;
unreachable;
},
var _looptime: ?f32 = null;
if (self.current_animation) |current| {
if (current < self.animations.len) {
const animation = &self.animations[current];
const looptime = animation.loopTime(time);
_looptime = looptime;
for (animation.curves) |curve| {
switch (curve.target) {
.translation => |values| {
const t = values.sample(looptime);
gltf.value.nodes[curve.node_index].translation = .{
t.x,
t.y,
t.z,
};
},
.rotation => |values| {
const r = values.sample(looptime);
gltf.value.nodes[curve.node_index].rotation = .{
r.x,
r.y,
r.z,
r.w,
};
},
.scale => |values| {
_ = values;
unreachable;
},
.weights => |values| {
_ = values;
unreachable;
},
}
}
}
}
return looptime;
}

pub fn draw(self: *@This(), camera: Camera) void {
if (self.gltf) |json| {
sg.applyPipeline(self.pip);
for (json.value.scenes[0].nodes) |root_node_index| {
self.draw_node(
json.value,
camera.viewProjectionMatrix(),
root_node_index,
Mat4.identity,
);
}
for (gltf.value.scenes[0].nodes) |root_node_index| {
self.update_node_matrix(
gltf.value,
root_node_index,
Mat4.identity,
);
}

return _looptime;
}

fn draw_node(
fn update_node_matrix(
self: *@This(),
gltf: zigltf.Gltf,
vp: Mat4,
node_index: u32,
parent_matrix: Mat4,
) void {
Expand Down Expand Up @@ -406,13 +402,34 @@ fn draw_node(
break :block Mat4.trs(.{ .t = t, .r = r, .s = s });
};
const model_matrix = local_matrix.mul(parent_matrix);
self.node_matrices[node_index] = model_matrix;

if (node.mesh) |mesh_index| {
self.draw_mesh(mesh_index, vp, model_matrix);
for (node.children) |child_node_index| {
self.update_node_matrix(gltf, child_node_index, model_matrix);
}
}

for (node.children) |child_node_index| {
self.draw_node(gltf, vp, child_node_index, model_matrix);
pub fn draw(self: *@This(), camera: Camera) void {
if (self.gltf) |json| {
sg.applyPipeline(self.pip);
const vp = camera.viewProjectionMatrix();
for (0..json.value.nodes.len) |i| {
self.draw_node(json.value, vp, i);
}
}
}

fn draw_node(
self: *@This(),
gltf: zigltf.Gltf,
vp: Mat4,
node_index: usize,
) void {
const node = gltf.nodes[node_index];

const model_matrix = self.node_matrices[node_index];
if (node.mesh) |mesh_index| {
self.draw_mesh(mesh_index, vp, model_matrix);
}
}

Expand Down
Loading

0 comments on commit d0377e2

Please sign in to comment.