Skip to content

Instantly share code, notes, and snippets.

@tonis2
Last active January 24, 2021 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonis2/0a4657fcc1c2b965e43f54a339f8af0d to your computer and use it in GitHub Desktop.
Save tonis2/0a4657fcc1c2b965e43f54a339f8af0d to your computer and use it in GitHub Desktop.
const std = @import("std");
const print = std.debug.print;
pub const Vec3 = packed struct {
x: u32,
y: u32,
z: u32,
pub fn new(x: u32, y: u32, z: u32) Vec3 {
return Vec3{
.x = x,
.y = y,
.z = z,
};
}
};
pub const Vertex = struct {
position: Vec3, color: [4]u16
};
pub const MeshData = struct {
indices: []const u16, vertices: []const Vertex
};
pub fn render() MeshData {
return MeshData{
.vertices = &[_]Vertex{
Vertex{ .position = Vec3.new(100, 100, 0), .color = [4]u16{ 1.0, 1.0, 1.0, 1.0 } },
Vertex{ .position = Vec3.new(400, 100, 0), .color = [4]u16{ 1.0, 1.0, 1.0, 1.0 } },
Vertex{ .position = Vec3.new(400, 100, 0), .color = [4]u16{ 1.0, 1.0, 1.0, 1.0 } },
Vertex{ .position = Vec3.new(100, 100, 0), .color = [4]u16{ 1.0, 1.0, 1.0, 1.0 } },
},
.indices = &[_]u16{ 0, 1, 2, 2, 3, 0 },
};
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = &gpa.allocator;
var data_working = MeshData{
.vertices = &[_]Vertex{
Vertex{ .position = Vec3.new(100, 100, 0), .color = [4]u16{ 1.0, 1.0, 1.0, 1.0 } },
Vertex{ .position = Vec3.new(400, 100, 0), .color = [4]u16{ 1.0, 1.0, 1.0, 1.0 } },
Vertex{ .position = Vec3.new(400, 100, 0), .color = [4]u16{ 1.0, 1.0, 1.0, 1.0 } },
Vertex{ .position = Vec3.new(100, 100, 0), .color = [4]u16{ 1.0, 1.0, 1.0, 1.0 } },
},
.indices = &[_]u16{ 0, 1, 2, 2, 3, 0 },
};
var data_broken = render();
var save = std.ArrayList(Vertex).init(allocator);
for (result.vertices) |vert| {
print("vert {} \n", .{vert.position.x});
try save.append(vert);
}
// When using data_working prints
// vert 100
// vert 400
// vert 400
// vert 100
// When using data_broken prints
// vert 100
// vert 400
// vert 405411416
// vert 32533
defer {
save.deinit();
_ = gpa.deinit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment