Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/foundations/math/geometry/Plane.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ pub fn closestPointToPoint(self: Plane, p: vector.vec4) vector.vec4 {
return vector.sub(p, q);
}

pub fn reflectPointAcross(self: Plane, p: vector.vec4) vector.vec4 {
const point_on_plane = vector.mul(
vector.dotProduct(self.parameterized, p),
vector.vec3ToVec4Vector(self.normal),
);
return vector.sub(p, vector.mul(2.0, point_on_plane));
}

pub fn debug(self: Plane) void {
std.debug.print("plane: ({d}, {d}, {d}| {d})\n", .{
self.parameterized[0],
Expand Down
6 changes: 6 additions & 0 deletions src/foundations/object/object.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub const object_type = enum {
norender,
triangle,
quad,
cube,
Expand All @@ -8,9 +9,11 @@ pub const object_type = enum {
parallelepiped,
cylinder,
cone,
instanced_triangle,
};

pub const object = union(object_type) {
norender: NoRender,
triangle: Triangle,
quad: Quad,
cube: Cube,
Expand All @@ -20,8 +23,10 @@ pub const object = union(object_type) {
parallelepiped: Parallelepiped,
cylinder: Cylinder,
cone: Cone,
instanced_triangle: InstancedTriangle,
};

pub const NoRender = @import("object_no_render/ObjectNoRender.zig");
pub const Triangle = @import("object_triangle/ObjectTriangle.zig");
pub const Quad = @import("object_quad/ObjectQuad.zig");
pub const Cube = @import("object_cube/ObjectCube.zig");
Expand All @@ -31,3 +36,4 @@ pub const Strip = @import("object_strip/ObjectStrip.zig");
pub const Parallelepiped = @import("object_parallelepiped/ObjectParallelepiped.zig");
pub const Cylinder = @import("object_cylinder/ObjectCylinder.zig");
pub const Cone = @import("object_cone/ObjectCone.zig");
pub const InstancedTriangle = @import("object_instanced_triangle/ObjectInstancedTriangle.zig");
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
mesh: rhi.mesh,
vertex_data_size: usize,
instance_data_stride: usize,
attribute_data: [3]rhi.attributeData,

const InstancedTriangle = @This();

pub fn init(
program: u32,
instance_data: []rhi.instanceData,
) InstancedTriangle {
var attribute_data: [3]rhi.attributeData = undefined;

const p0: math.vector.vec3 = .{ 0, 0, 0 };
const p1: math.vector.vec3 = .{ 0, 0, 1 };
const p2: math.vector.vec3 = .{ 1, 0, 0 };

const triangle = math.geometry.Triangle.init(p0, p1, p2);
attribute_data[0] = .{
.position = triangle.p0,
.normals = triangle.normal,
};
attribute_data[1] = .{
.position = triangle.p1,
.normals = triangle.normal,
};
attribute_data[2] = .{
.position = triangle.p2,
.normals = triangle.normal,
};

const indices: [3]u32 = .{ 0, 1, 2 };
const vao_buf = rhi.attachInstancedBuffer(attribute_data[0..], instance_data);
const ebo = rhi.initEBO(@ptrCast(indices[0..]), vao_buf.vao);
return .{
.mesh = .{
.program = program,
.vao = vao_buf.vao,
.buffer = vao_buf.buffer,
.instance_type = .{
.instanced = .{
.index_count = indices.len,
.instances_count = instance_data.len,
.ebo = ebo,
.primitive = c.GL_TRIANGLES,
.format = c.GL_UNSIGNED_INT,
},
},
},
.vertex_data_size = vao_buf.vertex_data_size,
.instance_data_stride = vao_buf.instance_data_stride,
.attribute_data = attribute_data,
};
}

pub fn updateInstanceAt(self: InstancedTriangle, index: usize, instance_data: rhi.instanceData) void {
rhi.updateInstanceData(self.mesh.buffer, self.vertex_data_size, self.instance_data_stride, index, instance_data);
}

const c = @import("../../c.zig").c;
const rhi = @import("../../rhi/rhi.zig");
const math = @import("../../math/math.zig");
3 changes: 3 additions & 0 deletions src/foundations/object/object_no_render/ObjectNoRender.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mesh: rhi.mesh = .{ .instance_type = .{ .norender = {} } },

const rhi = @import("../../rhi/rhi.zig");
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mesh: rhi.mesh,
vertex_data_size: usize,
instance_data_stride: usize,
attribute_data: [num_vertices]rhi.attributeData,
indices: [num_indices]u32,

const Parallelepied = @This();

Expand Down Expand Up @@ -42,6 +43,7 @@ pub fn init(
.vertex_data_size = vao_buf.vertex_data_size,
.instance_data_stride = vao_buf.instance_data_stride,
.attribute_data = d.data,
.indices = d.indices,
};
}
pub fn updateInstanceAt(self: Parallelepied, index: usize, instance_data: rhi.instanceData) void {
Expand Down
17 changes: 17 additions & 0 deletions src/foundations/object/object_triangle/ObjectTriangle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,43 @@ pub const default_colors: [3][4]f32 = .{
.{ 1, 0, 0, 1 },
};

pub const default_normals: [3][3]f32 = .{
.{ 0, -1, 0 },
.{ 0, -1, 0 },
.{ 0, -1, 0 },
};

pub fn init(
vertex_shader: []const u8,
frag_shader: []const u8,
positions: [3][3]f32,
colors: [3][4]f32,
normals: [3][3]f32,
) Triangle {
const program = rhi.createProgram();
rhi.attachShaders(program, vertex_shader, frag_shader);
return initWithProgram(program, positions, colors, normals);
}

pub fn initWithProgram(
program: u32,
positions: [3][3]f32,
colors: [3][4]f32,
normals: [3][3]f32,
) Triangle {
var data: [3]rhi.attributeData = undefined;
var i: usize = 0;
while (i < data.len) : (i += 1) {
data[i] = .{
.position = positions[i],
.color = colors[i],
.normals = normals[i],
};
}
const vao_buf = rhi.attachBuffer(data[0..]);
return .{
.mesh = .{
.cull = false,
.program = program,
.vao = vao_buf.vao,
.buffer = vao_buf.buffer,
Expand Down
8 changes: 5 additions & 3 deletions src/foundations/rhi/mesh.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
program: u32,
vao: u32,
buffer: u32,
program: u32 = 0,
vao: u32 = 0,
buffer: u32 = 0,
instance_type: mesh_instance,
wire_mesh: bool = false,
blend: bool = false,
Expand All @@ -12,12 +12,14 @@ pub const mesh_type = enum(usize) {
array,
element,
instanced,
norender,
};

pub const mesh_instance = union(mesh_type) {
array: array,
element: element,
instanced: instanced,
norender: void,
};

pub const array = struct {
Expand Down
21 changes: 20 additions & 1 deletion src/foundations/rhi/rhi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ pub fn deleteMesh(m: mesh) void {
if (m.buffer != 0) c.glDeleteBuffers(1, @ptrCast(&m.buffer));
}

pub fn deleteMeshVao(m: mesh) void {
c.glDeleteVertexArrays(1, @ptrCast(&m.vao));
switch (m.instance_type) {
.element => |e| c.glDeleteBuffers(1, e.ebo),
else => {},
}
if (m.buffer != 0) c.glDeleteBuffers(1, @ptrCast(&m.buffer));
}

pub fn drawObjects(objects: []const object.object) void {
var i: usize = 0;
while (i < objects.len) : (i += 1) {
Expand Down Expand Up @@ -338,6 +347,7 @@ pub fn drawMesh(m: mesh) void {
.array => |a| drawArrays(m.program, m.vao, a.count),
.element => |e| drawElements(m, e),
.instanced => |i| drawInstances(m, i),
.norender => {},
}
if (!m.cull) {
c.glEnable(c.GL_CULL_FACE);
Expand All @@ -353,7 +363,7 @@ pub fn drawMesh(m: mesh) void {
}
}

pub fn deleteObjects(objects: []object.object) void {
pub fn deleteObjects(objects: []const object.object) void {
var i: usize = 0;
while (i < objects.len) : (i += 1) {
switch (objects[i]) {
Expand All @@ -362,6 +372,15 @@ pub fn deleteObjects(objects: []object.object) void {
}
}

pub fn deleteObjectVaos(objects: []const object.object) void {
var i: usize = 0;
while (i < objects.len) : (i += 1) {
switch (objects[i]) {
inline else => |o| deleteMeshVao(o.mesh),
}
}
}

const std = @import("std");
const c = @import("../c.zig").c;
const ui = @import("../ui/ui.zig");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn init(allocator: std.mem.Allocator, cfg: *config) *LinearColorSpace {
frag_shader,
triangle_positions,
triangle_colors,
object.Triangle.default_normals,
),
};
p.objects[0] = triangle1;
Expand Down Expand Up @@ -77,6 +78,7 @@ pub fn init(allocator: std.mem.Allocator, cfg: *config) *LinearColorSpace {
frag_shader,
triangle_positions,
triangle_colors,
object.Triangle.default_normals,
),
};
triangle2.triangle.mesh.linear_colorspace = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn addVector(self: *MathVectorArithmetic) void {
frag_shader,
triangle_positions,
triangle_colors,
object.Triangle.default_normals,
),
};
self.num_objects += 1;
Expand Down
Loading