-
Notifications
You must be signed in to change notification settings - Fork 11
/
meshes.zig
142 lines (111 loc) · 4.47 KB
/
meshes.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const std = @import("std");
const delve = @import("delve");
const app = delve.app;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
// easy access to some imports
const cam = delve.graphics.camera;
const colors = delve.colors;
const debug = delve.debug;
const graphics = delve.platform.graphics;
const images = delve.images;
const input = delve.platform.input;
const math = delve.math;
const modules = delve.modules;
const mesh = delve.graphics.mesh;
// easy access to some types
const Vec3 = math.Vec3;
const Mat4 = math.Mat4;
const Color = colors.Color;
const emissive_shader_builtin = delve.shaders.default_emissive;
var time: f32 = 0.0;
var camera: cam.Camera = undefined;
var mesh_test: ?mesh.Mesh = null;
var shader: delve.platform.graphics.Shader = undefined;
var material: graphics.Material = undefined;
// This example shows loading and drawing meshes
// Web build note: this does not seem to work when built in --release=fast or --release=small
pub fn main() !void {
// Pick the allocator to use depending on platform
const builtin = @import("builtin");
if (builtin.os.tag == .wasi or builtin.os.tag == .emscripten) {
// Web builds hack: use the C allocator to avoid OOM errors
// See https://github.com/ziglang/zig/issues/19072
try delve.init(std.heap.c_allocator);
} else {
// Using the default allocator will let us detect memory leaks
try delve.init(delve.mem.createDefaultAllocator());
}
try registerModule();
try app.start(app.AppConfig{ .title = "Delve Framework - Mesh Drawing Example" });
}
pub fn registerModule() !void {
const meshExample = modules.Module{
.name = "mesh_example",
.init_fn = on_init,
.tick_fn = on_tick,
.draw_fn = on_draw,
.cleanup_fn = on_cleanup,
};
try modules.registerModule(meshExample);
}
fn on_init() !void {
debug.log("Mesh example module initializing", .{});
graphics.setClearColor(colors.examples_bg_light);
// Make a perspective camera, with a 90 degree FOV
camera = cam.Camera.initThirdPerson(90.0, 0.01, 50.0, 5.0, Vec3.up);
camera.position = Vec3.new(0.0, 0.0, 0.0);
// Load the base color texture for the mesh
const base_texture_file = "assets/meshes/SciFiHelmet_BaseColor_512.png";
var base_img: images.Image = images.loadFile(base_texture_file) catch {
debug.log("Assets: Error loading image asset: {s}", .{base_texture_file});
return;
};
defer base_img.deinit();
const tex_base = graphics.Texture.init(base_img);
// Load the emissive texture for the mesh
const emissive_texture_file = "assets/meshes/SciFiHelmet_Emissive_512.png";
var emissive_img: images.Image = images.loadFile(emissive_texture_file) catch {
debug.log("Assets: Error loading image asset: {s}", .{emissive_texture_file});
return;
};
defer emissive_img.deinit();
const tex_emissive = graphics.Texture.init(emissive_img);
// Make our emissive shader from one that is pre-compiled
shader = try graphics.Shader.initFromBuiltin(.{ .vertex_attributes = mesh.getShaderAttributes() }, emissive_shader_builtin);
// Create a material out of our shader and textures
material = try graphics.Material.init(.{
.shader = shader,
.texture_0 = tex_base,
.texture_1 = tex_emissive,
});
// Load our mesh!
mesh_test = mesh.Mesh.initFromFile(delve.mem.getAllocator(), "assets/meshes/SciFiHelmet.gltf", .{ .material = material });
}
fn on_tick(delta: f32) void {
// There is a built in fly mode, but you can also just set the position / direction
camera.runSimpleCamera(4 * delta, 120 * delta, false);
time += delta * 100;
if (input.isKeyJustPressed(.ESCAPE))
delve.platform.app.exit();
}
fn on_draw() void {
// Exit early if we have no mesh loaded
if (mesh_test == null)
return;
const view_mats = camera.update();
var model = Mat4.translate(Vec3.new(2.0, 0.0, 0.0));
model = model.mul(Mat4.rotate(time * 0.6, Vec3.new(0.0, 1.0, 0.0)));
const sin_val = std.math.sin(time * 0.006) + 0.5;
mesh_test.?.material.state.params.draw_color = Color.new(sin_val, sin_val, sin_val, 1.0);
mesh_test.?.draw(view_mats, model);
model = Mat4.translate(Vec3.new(-2.0, 0.0, 0.0));
mesh_test.?.draw(view_mats, model);
}
fn on_cleanup() !void {
debug.log("Mesh example module cleaning up", .{});
if (mesh_test == null)
return;
mesh_test.?.deinit();
material.deinit();
shader.destroy();
}