Skip to content

Commit 442ce26

Browse files
authored
Merge pull request #34 from Interrupt/fixes/material-pointers
Updating meshes to hold pointers to materials, not the actual material
2 parents 1cb6f7d + 2e45496 commit 442ce26

File tree

12 files changed

+72
-57
lines changed

12 files changed

+72
-57
lines changed

src/examples/frustums.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn on_init() !void {
7676
return;
7777
};
7878

79-
cube_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(1, 1, 1), delve.colors.white, material_cube) catch {
79+
cube_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(1, 1, 1), delve.colors.white, &material_cube) catch {
8080
delve.debug.fatal("Could not create cube mesh!", .{});
8181
return;
8282
};
@@ -127,5 +127,5 @@ pub fn createFrustumMesh() !delve.graphics.mesh.Mesh {
127127

128128
try builder.addFrustum(secondary_camera.getViewFrustum(), delve.math.Mat4.identity, delve.colors.cyan);
129129

130-
return builder.buildMesh(material_frustum);
130+
return builder.buildMesh(&material_frustum);
131131
}

src/examples/lighting.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const mesh_texture_file = "assets/meshes/CesiumMan.png";
3636
var cube1: delve.graphics.mesh.Mesh = undefined;
3737
var cube2: delve.graphics.mesh.Mesh = undefined;
3838

39+
var skinned_mesh_material: delve.platform.graphics.Material = undefined;
40+
var static_mesh_material: delve.platform.graphics.Material = undefined;
41+
3942
// This example shows an example of some simple lighting in a shader
4043

4144
// Web build note: this does not seem to work when built in --release=fast or --release=small
@@ -86,7 +89,7 @@ fn on_init() !void {
8689
const tex_base = graphics.Texture.init(&base_img);
8790

8891
// Create a material out of our shader and textures
89-
const skinned_mesh_material = delve.platform.graphics.Material.init(.{
92+
skinned_mesh_material = delve.platform.graphics.Material.init(.{
9093
.shader = skinned_shader.?,
9194
.texture_0 = tex_base,
9295
.texture_1 = delve.platform.graphics.createSolidTexture(0x00000000),
@@ -99,7 +102,7 @@ fn on_init() !void {
99102
});
100103

101104
// Create a material out of the texture
102-
const static_mesh_material = graphics.Material.init(.{
105+
static_mesh_material = graphics.Material.init(.{
103106
.shader = static_shader.?,
104107
.texture_0 = delve.platform.graphics.createSolidTexture(0xFFFFFFFF),
105108
.texture_1 = delve.platform.graphics.createSolidTexture(0x00000000),
@@ -109,16 +112,16 @@ fn on_init() !void {
109112
});
110113

111114
// Load an animated mesh
112-
const loaded_mesh = skinned_mesh.SkinnedMesh.initFromFile(delve.mem.getAllocator(), mesh_file, .{ .material = skinned_mesh_material });
115+
const loaded_mesh = skinned_mesh.SkinnedMesh.initFromFile(delve.mem.getAllocator(), mesh_file, .{ .material = &skinned_mesh_material });
113116

114117
if (loaded_mesh == null) {
115118
debug.fatal("Could not load skinned mesh!", .{});
116119
return;
117120
}
118121

119122
// make some cubes
120-
cube1 = try delve.graphics.mesh.createCube(math.Vec3.new(0, -1.0, 0), math.Vec3.new(10.0, 0.25, 10.0), delve.colors.white, static_mesh_material);
121-
cube2 = try delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2.0, 1.25, 1.0), delve.colors.white, static_mesh_material);
123+
cube1 = try delve.graphics.mesh.createCube(math.Vec3.new(0, -1.0, 0), math.Vec3.new(10.0, 0.25, 10.0), delve.colors.white, &static_mesh_material);
124+
cube2 = try delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2.0, 1.25, 1.0), delve.colors.white, &static_mesh_material);
122125

123126
animated_mesh = loaded_mesh.?;
124127
animation = try animated_mesh.createAnimation(0, 1.0, true);
@@ -159,13 +162,10 @@ fn on_draw() void {
159162
const point_lights = &[_]delve.platform.graphics.PointLight{ point_light_1, point_light_2, point_light_3 };
160163

161164
// add the lights and camera to the materials
162-
animated_mesh.mesh.material.params.point_lights = @constCast(point_lights);
163-
animated_mesh.mesh.material.params.directional_light = directional_light;
164-
animated_mesh.mesh.material.params.ambient_light = colors.Color.new(0.02, 0.02, 0.05, 1.0);
165-
166-
// copy over the material params to the cube mesh too
167-
cube1.material.params = animated_mesh.mesh.material.params;
168-
cube2.material.params = animated_mesh.mesh.material.params;
165+
static_mesh_material.params.point_lights = @constCast(point_lights);
166+
static_mesh_material.params.directional_light = directional_light;
167+
static_mesh_material.params.ambient_light = colors.Color.new(0.02, 0.02, 0.05, 1.0);
168+
skinned_mesh_material.params = static_mesh_material.params;
169169

170170
animated_mesh.draw(view_mats, model);
171171
cube1.draw(view_mats, Mat4.identity);

src/examples/meshbuilder.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ pub fn on_init() !void {
6161
camera = delve.graphics.camera.Camera.initThirdPerson(90.0, 0.01, 20.0, 5.0, math.Vec3.up);
6262

6363
// make a cube
64-
cube1 = delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2, 3, 1), delve.colors.white, material) catch {
64+
cube1 = delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2, 3, 1), delve.colors.white, &material) catch {
6565
delve.debug.log("Could not create cube!", .{});
6666
return;
6767
};
6868

6969
// and another
70-
cube2 = delve.graphics.mesh.createCube(math.Vec3.new(3, 0, -1), math.Vec3.new(1, 1, 2), delve.colors.green, material) catch {
70+
cube2 = delve.graphics.mesh.createCube(math.Vec3.new(3, 0, -1), math.Vec3.new(1, 1, 2), delve.colors.green, &material) catch {
7171
delve.debug.log("Could not create cube!", .{});
7272
return;
7373
};
7474

7575
// and then a floor
76-
cube3 = delve.graphics.mesh.createCube(math.Vec3.new(0, -2, 0), math.Vec3.new(12, 0.25, 12), delve.colors.red, material) catch {
76+
cube3 = delve.graphics.mesh.createCube(math.Vec3.new(0, -2, 0), math.Vec3.new(12, 0.25, 12), delve.colors.red, &material) catch {
7777
delve.debug.log("Could not create cube!", .{});
7878
return;
7979
};

src/examples/meshes.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const emissive_shader_builtin = delve.shaders.default_emissive;
2424

2525
var time: f32 = 0.0;
2626
var mesh_test: ?mesh.Mesh = null;
27+
var material: graphics.Material = undefined;
2728
var camera: cam.Camera = undefined;
2829

2930
// This example shows loading and drawing meshes
@@ -92,14 +93,14 @@ fn on_init() !void {
9293
}
9394

9495
// Create a material out of our shader and textures
95-
const material = graphics.Material.init(.{
96+
material = graphics.Material.init(.{
9697
.shader = shader.?,
9798
.texture_0 = tex_base,
9899
.texture_1 = tex_emissive,
99100
});
100101

101102
// Load our mesh!
102-
mesh_test = mesh.Mesh.initFromFile(delve.mem.getAllocator(), "assets/meshes/SciFiHelmet.gltf", .{ .material = material });
103+
mesh_test = mesh.Mesh.initFromFile(delve.mem.getAllocator(), "assets/meshes/SciFiHelmet.gltf", .{ .material = &material });
103104
}
104105

105106
fn on_tick(delta: f32) void {

src/examples/passes.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ pub fn on_init() !void {
9393
camera_offscreen = delve.graphics.camera.Camera.initThirdPerson(90.0, 0.01, 200.0, 5.0, math.Vec3.up);
9494

9595
// make a cube
96-
cube1 = delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2, 3, 1), delve.colors.white, material1) catch {
96+
cube1 = delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2, 3, 1), delve.colors.white, &material1) catch {
9797
delve.debug.log("Could not create cube!", .{});
9898
return;
9999
};
100100

101101
// and another
102-
cube2 = delve.graphics.mesh.createCube(math.Vec3.new(3, 0, -1), math.Vec3.new(1, 1, 2), delve.colors.white, material3) catch {
102+
cube2 = delve.graphics.mesh.createCube(math.Vec3.new(3, 0, -1), math.Vec3.new(1, 1, 2), delve.colors.white, &material3) catch {
103103
delve.debug.log("Could not create cube!", .{});
104104
return;
105105
};
106106

107107
// and then a screen
108-
cube3 = delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(20, 12.0, 0.25), delve.colors.white, material2) catch {
108+
cube3 = delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(20, 12.0, 0.25), delve.colors.white, &material2) catch {
109109
delve.debug.log("Could not create cube!", .{});
110110
return;
111111
};

src/examples/quakemap.zig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ const math = delve.math;
99

1010
var camera: delve.graphics.camera.Camera = undefined;
1111
var fallback_material: graphics.Material = undefined;
12+
var fallback_quake_material: delve.utils.quakemap.QuakeMaterial = undefined;
13+
var materials: std.StringHashMap(delve.utils.quakemap.QuakeMaterial) = undefined;
1214

1315
var quake_map: delve.utils.quakemap.QuakeMap = undefined;
1416
var map_meshes: std.ArrayList(delve.graphics.mesh.Mesh) = undefined;
1517
var entity_meshes: std.ArrayList(delve.graphics.mesh.Mesh) = undefined;
18+
1619
var cube_mesh: delve.graphics.mesh.Mesh = undefined;
1720

1821
var map_transform: math.Mat4 = undefined;
@@ -150,14 +153,16 @@ pub fn on_init() !void {
150153
.samplers = &[_]graphics.FilterMode{.NEAREST},
151154
});
152155

156+
fallback_quake_material = .{ .material = fallback_material };
157+
153158
// create our camera
154159
camera = delve.graphics.camera.Camera.initThirdPerson(90.0, 0.01, 512, 16.0, math.Vec3.up);
155160
camera.position.y = 10.0;
156161

157162
// set our player position too
158163
player_pos = camera.position;
159164

160-
var materials = std.StringHashMap(delve.utils.quakemap.QuakeMaterial).init(allocator);
165+
materials = std.StringHashMap(delve.utils.quakemap.QuakeMaterial).init(allocator);
161166
const shader = graphics.Shader.initDefault(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() });
162167

163168
for (quake_map.worldspawn.solids.items) |solid| {
@@ -197,11 +202,11 @@ pub fn on_init() !void {
197202
}
198203

199204
// make meshes out of the quake map, one per material
200-
map_meshes = try quake_map.buildWorldMeshes(allocator, math.Mat4.identity, materials, .{ .material = fallback_material });
201-
entity_meshes = try quake_map.buildEntityMeshes(allocator, math.Mat4.identity, materials, .{ .material = fallback_material });
205+
map_meshes = try quake_map.buildWorldMeshes(allocator, math.Mat4.identity, &materials, &fallback_quake_material);
206+
entity_meshes = try quake_map.buildEntityMeshes(allocator, math.Mat4.identity, &materials, &fallback_quake_material);
202207

203208
// make a bounding box cube
204-
cube_mesh = try delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), bounding_box_size, delve.colors.red, fallback_material);
209+
cube_mesh = try delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), bounding_box_size, delve.colors.red, &fallback_material);
205210

206211
// set a bg color
207212
delve.platform.graphics.setClearColor(delve.colors.examples_bg_light);

src/examples/rays.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ pub fn on_init() !void {
7676
camera.position = delve.math.Vec3.new(0, 30, 32);
7777
camera.pitch_angle = -50.0;
7878

79-
cube_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(1, 1, 1), delve.colors.white, material_cube) catch {
79+
cube_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(1, 1, 1), delve.colors.white, &material_cube) catch {
8080
delve.debug.fatal("Could not create cube mesh!", .{});
8181
return;
8282
};
8383

84-
hit_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(0.5, 0.5, 0.5), delve.colors.white, material_cube) catch {
84+
hit_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(0.5, 0.5, 0.5), delve.colors.white, &material_cube) catch {
8585
delve.debug.fatal("Could not create cube mesh!", .{});
8686
return;
8787
};
8888

89-
ray_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(50, 0, 0), delve.math.Vec3.new(100, 0.1, 0.1), delve.colors.red, material_cube) catch {
89+
ray_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(50, 0, 0), delve.math.Vec3.new(100, 0.1, 0.1), delve.colors.red, &material_cube) catch {
9090
delve.debug.fatal("Could not create cube mesh!", .{});
9191
return;
9292
};

src/examples/skinned-meshes.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const Color = colors.Color;
2222

2323
const shader_builtin = delve.shaders.default_skinned;
2424

25+
var material: delve.platform.graphics.Material = undefined;
2526
var mesh_test: skinned_mesh.SkinnedMesh = undefined;
2627
var animation: skinned_mesh.PlayingAnimation = undefined;
2728

@@ -90,7 +91,7 @@ fn on_init() !void {
9091
const tex_base = graphics.Texture.init(&base_img);
9192

9293
// Create a material out of our shader and textures
93-
const material = delve.platform.graphics.Material.init(.{
94+
material = delve.platform.graphics.Material.init(.{
9495
.shader = shader.?,
9596
.texture_0 = tex_base,
9697
.texture_1 = delve.platform.graphics.createSolidTexture(0x00000000),
@@ -100,7 +101,7 @@ fn on_init() !void {
100101
});
101102

102103
// Load our mesh!
103-
const loaded = skinned_mesh.SkinnedMesh.initFromFile(delve.mem.getAllocator(), mesh_file, .{ .material = material });
104+
const loaded = skinned_mesh.SkinnedMesh.initFromFile(delve.mem.getAllocator(), mesh_file, .{ .material = &material });
104105

105106
if (loaded == null) {
106107
debug.fatal("Could not load skinned mesh!", .{});

src/framework/graphics/mesh.zig

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const FSParams = graphics.FSDefaultUniforms;
2424
const vertex_layout = getVertexLayout();
2525

2626
pub const MeshConfig = struct {
27-
material: ?graphics.Material = null,
27+
material: ?*graphics.Material = null,
2828
};
2929

3030
pub fn init() !void {
@@ -39,7 +39,7 @@ pub fn deinit() void {
3939
/// These can be created on the fly, using a MeshBuilder, or loaded from GLTF files.
4040
pub const Mesh = struct {
4141
bindings: graphics.Bindings = undefined,
42-
material: graphics.Material = undefined,
42+
material: *graphics.Material = undefined,
4343
bounds: boundingbox.BoundingBox = undefined,
4444

4545
has_skin: bool = false,
@@ -107,10 +107,12 @@ pub const Mesh = struct {
107107
}
108108
}
109109

110-
var material: graphics.Material = undefined;
110+
var material: *graphics.Material = undefined;
111111
if (cfg.material == null) {
112-
const tex = graphics.createDebugTexture();
113-
material = graphics.Material.init(.{ .texture_0 = tex });
112+
// Todo: Use a backup material!
113+
debug.log("No material in mesh config!", .{});
114+
// const tex = graphics.createDebugTexture();
115+
// material = graphics.Material.init(.{ .texture_0 = tex });
114116
} else {
115117
material = cfg.material.?;
116118
}
@@ -158,7 +160,7 @@ pub const Mesh = struct {
158160

159161
/// Draw this mesh
160162
pub fn draw(self: *Mesh, cam_matrices: CameraMatrices, model_matrix: math.Mat4) void {
161-
graphics.drawWithMaterial(&self.bindings, &self.material, cam_matrices, model_matrix);
163+
graphics.drawWithMaterial(&self.bindings, self.material, cam_matrices, model_matrix);
162164
}
163165

164166
/// Draw this mesh, using the specified material instead of the set one
@@ -168,12 +170,12 @@ pub const Mesh = struct {
168170
};
169171

170172
/// Create a mesh out of some vertex data
171-
pub fn createMesh(vertices: []PackedVertex, indices: []u32, normals: [][3]f32, tangents: [][4]f32, material: graphics.Material) Mesh {
173+
pub fn createMesh(vertices: []PackedVertex, indices: []u32, normals: [][3]f32, tangents: [][4]f32, material: *graphics.Material) Mesh {
172174
// create a mesh with the default vertex layout
173175
return createMeshWithLayout(vertices, indices, normals, tangents, material, vertex_layout);
174176
}
175177

176-
pub fn createSkinnedMesh(vertices: []PackedVertex, indices: []u32, normals: [][3]f32, tangents: [][4]f32, joints: [][4]f32, weights: [][4]f32, material: graphics.Material, data: *zmesh.io.zcgltf.Data) Mesh {
178+
pub fn createSkinnedMesh(vertices: []PackedVertex, indices: []u32, normals: [][3]f32, tangents: [][4]f32, joints: [][4]f32, weights: [][4]f32, material: *graphics.Material, data: *zmesh.io.zcgltf.Data) Mesh {
177179
// create a mesh with the default vertex layout
178180
// debug.log("Creating skinned mesh: {d} indices, {d} normals, {d}tangents, {d} joints, {d} weights", .{ indices.len, normals.len, tangents.len, joints.len, weights.len });
179181

@@ -199,7 +201,7 @@ pub fn createSkinnedMesh(vertices: []PackedVertex, indices: []u32, normals: [][3
199201
}
200202

201203
/// Create a mesh out of some vertex data with a given vertex layout
202-
pub fn createMeshWithLayout(vertices: []PackedVertex, indices: []u32, normals: [][3]f32, tangents: [][4]f32, material: graphics.Material, layout: graphics.VertexLayout) Mesh {
204+
pub fn createMeshWithLayout(vertices: []PackedVertex, indices: []u32, normals: [][3]f32, tangents: [][4]f32, material: *graphics.Material, layout: graphics.VertexLayout) Mesh {
203205
// debug.log("Creating mesh: {d} indices", .{indices.len});
204206

205207
var bindings = graphics.Bindings.init(.{
@@ -219,7 +221,7 @@ pub fn createMeshWithLayout(vertices: []PackedVertex, indices: []u32, normals: [
219221
}
220222

221223
/// Creates a cube using a mesh builder
222-
pub fn createCube(pos: Vec3, size: Vec3, color: Color, material: graphics.Material) !Mesh {
224+
pub fn createCube(pos: Vec3, size: Vec3, color: Color, material: *graphics.Material) !Mesh {
223225
var builder = MeshBuilder.init(mem.getAllocator());
224226
defer builder.deinit();
225227

@@ -466,7 +468,7 @@ pub const MeshBuilder = struct {
466468
}
467469

468470
/// Bakes a mesh out of the mesh builder from the current state
469-
pub fn buildMesh(self: *const MeshBuilder, material: graphics.Material) Mesh {
471+
pub fn buildMesh(self: *const MeshBuilder, material: *graphics.Material) Mesh {
470472
const layout = getVertexLayout();
471473
return createMeshWithLayout(self.vertices.items, self.indices.items, self.normals.items, self.tangents.items, material, layout);
472474
}

src/framework/graphics/skinned-mesh.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub const SkinnedMesh = struct {
222222
self.applySkeletonTransforms();
223223

224224
self.mesh.material.params.joints = &self.joint_locations;
225-
graphics.drawWithMaterial(&self.mesh.bindings, &self.mesh.material, cam_matrices, model_matrix);
225+
graphics.drawWithMaterial(&self.mesh.bindings, self.mesh.material, cam_matrices, model_matrix);
226226
}
227227

228228
/// Draw this mesh, using the specified material instead of the set one

0 commit comments

Comments
 (0)