Skip to content

Commit d6d2263

Browse files
authored
Merge pull request #2 from btipling/bt/plane_distance
Plane distance scene
2 parents 57c9d34 + a555652 commit d6d2263

File tree

12 files changed

+122
-18
lines changed

12 files changed

+122
-18
lines changed

src/foundations/object/object_cube/object_cube.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ pub const default_positions: [36][3]f32 = .{
1212
.{ 1, -1, 1 },
1313

1414
// z neg
15-
.{ -1, -1, -1 },
16-
.{ -1, 1, -1 },
1715
.{ 1, -1, -1 },
18-
.{ -1, 1, -1 },
1916
.{ 1, 1, -1 },
17+
.{ -1, 1, -1 },
2018
.{ 1, -1, -1 },
19+
.{ -1, 1, -1 },
20+
.{ -1, -1, -1 },
2121

2222
// y pos
23-
.{ -1, 1, -1 },
24-
.{ -1, 1, 1 },
2523
.{ 1, 1, -1 },
26-
.{ -1, 1, 1 },
2724
.{ 1, 1, 1 },
25+
.{ -1, 1, 1 },
2826
.{ 1, 1, -1 },
27+
.{ -1, 1, 1 },
28+
.{ -1, 1, -1 },
2929

3030
// y neg
3131
.{ -1, -1, -1 },
@@ -44,12 +44,12 @@ pub const default_positions: [36][3]f32 = .{
4444
.{ 1, 1, -1 },
4545

4646
// x neg
47-
.{ -1, -1, -1 },
48-
.{ -1, -1, 1 },
4947
.{ -1, 1, -1 },
50-
.{ -1, -1, 1 },
5148
.{ -1, 1, 1 },
49+
.{ -1, -1, 1 },
5250
.{ -1, 1, -1 },
51+
.{ -1, -1, 1 },
52+
.{ -1, -1, -1 },
5353
};
5454

5555
pub const normals: [36][3]f32 = .{

src/foundations/object/object_parallelepiped/ObjectParallelepiped.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const num_indices: usize = 36; // because normals
1010
pub fn init(
1111
program: u32,
1212
instance_data: []rhi.instanceData,
13+
blend: bool,
1314
) Parallelepied {
1415
var d = data();
1516

@@ -29,6 +30,7 @@ pub fn init(
2930
.format = c.GL_UNSIGNED_INT,
3031
},
3132
},
33+
.blend = blend,
3234
},
3335
.vertex_data_size = vao_buf.vertex_data_size,
3436
.instance_data_stride = vao_buf.instance_data_stride,

src/foundations/physics/camera.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
99
allocator: std.mem.Allocator,
1010
cfg: *config,
1111
camera_matrix: math.matrix = undefined,
12-
camera_pos: math.vector.vec3 = .{ 1, 3.5, 1 },
12+
camera_pos: math.vector.vec3 = undefined,
1313
camera_orientation_pitch: math.rotation.Quat = .{ 1, 0, 0, 0 },
1414
camera_orientation_heading: math.rotation.Quat = .{ 1, 0, 0, 0 },
1515
camera_orientation: math.rotation.Quat = .{ 1, 0, 0, 0 },
@@ -35,7 +35,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
3535
const world_right: math.vector.vec3 = .{ 0, 0, 1 };
3636
const world_forward: math.vector.vec3 = .{ 0, 1, 0 };
3737

38-
pub fn init(allocator: std.mem.Allocator, cfg: *config, scene: T, integrator: IntegratorT) *Self {
38+
pub fn init(allocator: std.mem.Allocator, cfg: *config, scene: T, integrator: IntegratorT, pos: math.vector.vec3) *Self {
3939
const cam = allocator.create(Self) catch @panic("OOM");
4040
const s = @as(f32, @floatFromInt(cfg.width)) / @as(f32, @floatFromInt(cfg.height));
4141
const mvp = math.matrix.transformMatrix(
@@ -45,6 +45,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
4545

4646
cam.* = .{
4747
.allocator = allocator,
48+
.camera_pos = pos,
4849
.cfg = cfg,
4950
.persp_m = mvp,
5051
.mvp = mvp,

src/foundations/rhi/mesh.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ vao: u32,
33
buffer: u32,
44
instance_type: mesh_instance,
55
wire_mesh: bool = false,
6+
blend: bool = false,
67

78
linear_colorspace: bool = true,
89

src/foundations/rhi/rhi.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub fn init(allocator: std.mem.Allocator) void {
5252
c.glClipControl(c.GL_LOWER_LEFT, c.GL_NEGATIVE_ONE_TO_ONE);
5353
c.glEnable(c.GL_DEPTH_TEST);
5454
c.glFrontFace(c.GL_CCW);
55+
c.glEnable(c.GL_CULL_FACE);
5556
c.glClipControl(c.GL_LOWER_LEFT, c.GL_ZERO_TO_ONE);
5657
rhi = allocator.create(RHI) catch @panic("OOM");
5758
rhi.* = .{ .allocator = allocator };
@@ -326,11 +327,18 @@ pub fn drawMesh(m: mesh) void {
326327
if (m.wire_mesh) {
327328
c.glPolygonMode(c.GL_FRONT_AND_BACK, c.GL_LINE);
328329
}
330+
if (m.blend) {
331+
c.glEnable(c.GL_BLEND);
332+
c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA);
333+
}
329334
switch (m.instance_type) {
330335
.array => |a| drawArrays(m.program, m.vao, a.count),
331336
.element => |e| drawElements(m, e),
332337
.instanced => |i| drawInstances(m, i),
333338
}
339+
if (m.blend) {
340+
c.glDisable(c.GL_BLEND);
341+
}
334342
if (m.linear_colorspace) {
335343
c.glDisable(c.GL_FRAMEBUFFER_SRGB);
336344
}

src/foundations/scenery/grid/grid.zig renamed to src/foundations/scenery/grid/Grid.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub fn renderGrid(self: *Grid) void {
104104
.parallelepiped = object.Parallelepiped.init(
105105
prog,
106106
i_datas[0..],
107+
false,
107108
),
108109
};
109110
self.grid = grid;

src/foundations/scenery/scenery.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub const grid = @import("grid/grid.zig");
1+
pub const Grid = @import("grid/Grid.zig");

src/foundations/scenes/look_at/LookAt.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ui_state: LookAtUI,
22
allocator: std.mem.Allocator,
3-
grid: *scenery.grid = undefined,
3+
grid: *scenery.Grid = undefined,
44
cube: object.object = undefined,
55
camera: object.object = undefined,
66
view_camera: *physics.camera.Camera(*LookAt, physics.Integrator(physics.SmoothDeceleration)),
@@ -28,9 +28,10 @@ pub fn init(allocator: std.mem.Allocator, cfg: *config) *LookAt {
2828
cfg,
2929
lkt,
3030
integrator,
31+
.{ 1, 3.5, 1 },
3132
);
3233
errdefer cam.deinit(allocator);
33-
const grid = scenery.grid.init(allocator);
34+
const grid = scenery.Grid.init(allocator);
3435
errdefer grid.deinit();
3536
const ui_state: LookAtUI = .{};
3637

@@ -41,7 +42,7 @@ pub fn init(allocator: std.mem.Allocator, cfg: *config) *LookAt {
4142
.grid = grid,
4243
};
4344
grid.renderGrid();
44-
cam.addProgram(grid.program(), scenery.grid.mvp_uniform_name);
45+
cam.addProgram(grid.program(), scenery.Grid.mvp_uniform_name);
4546
lkt.renderCube();
4647
lkt.renderCamera();
4748
return lkt;

src/foundations/scenes/plane_distance/PlaneDistance.zig

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
ui_state: PlaneDistanceUI,
22
allocator: std.mem.Allocator,
3-
grid: *scenery.grid = undefined,
3+
grid: *scenery.Grid = undefined,
4+
plane: object.object = undefined,
45
view_camera: *physics.camera.Camera(*PlaneDistance, physics.Integrator(physics.SmoothDeceleration)),
56

67
const PlaneDistance = @This();
78

9+
const grid_vertex_shader: []const u8 = @embedFile("plane_vertex.glsl");
10+
const grid_frag_shader: []const u8 = @embedFile("plane_frag.glsl");
11+
812
pub fn navType() ui.ui_state.scene_nav_info {
913
return .{
1014
.nav_type = .math,
@@ -21,9 +25,10 @@ pub fn init(allocator: std.mem.Allocator, cfg: *config) *PlaneDistance {
2125
cfg,
2226
pd,
2327
integrator,
28+
.{ 0, 0, 0 },
2429
);
2530
errdefer cam.deinit(allocator);
26-
const grid = scenery.grid.init(allocator);
31+
const grid = scenery.Grid.init(allocator);
2732
errdefer grid.deinit();
2833
const ui_state: PlaneDistanceUI = .{};
2934

@@ -34,7 +39,8 @@ pub fn init(allocator: std.mem.Allocator, cfg: *config) *PlaneDistance {
3439
.grid = grid,
3540
};
3641
grid.renderGrid();
37-
cam.addProgram(grid.program(), scenery.grid.mvp_uniform_name);
42+
pd.renderPlane();
43+
cam.addProgram(grid.program(), scenery.Grid.mvp_uniform_name);
3844
return pd;
3945
}
4046

@@ -48,11 +54,44 @@ pub fn deinit(self: *PlaneDistance, allocator: std.mem.Allocator) void {
4854
pub fn draw(self: *PlaneDistance, dt: f64) void {
4955
self.view_camera.update(dt);
5056
self.grid.draw(dt);
57+
const objects: [1]object.object = .{self.plane};
58+
rhi.drawObjects(objects[0..]);
5159
self.ui_state.draw();
5260
}
5361

5462
pub fn updateCamera(_: *PlaneDistance) void {}
5563

64+
pub fn renderPlane(self: *PlaneDistance) void {
65+
const prog = rhi.createProgram();
66+
rhi.attachShaders(prog, grid_vertex_shader, grid_frag_shader);
67+
var i_datas: [1]rhi.instanceData = undefined;
68+
{
69+
const m = math.matrix.identity();
70+
i_datas[0] = .{
71+
.t_column0 = m.columns[0],
72+
.t_column1 = m.columns[1],
73+
.t_column2 = m.columns[2],
74+
.t_column3 = m.columns[3],
75+
.color = .{ 1, 0, 0, 0.1 },
76+
};
77+
}
78+
const plane: object.object = .{
79+
.parallelepiped = object.Parallelepiped.init(
80+
prog,
81+
i_datas[0..],
82+
true,
83+
),
84+
};
85+
{
86+
var m = math.matrix.identity();
87+
m = math.matrix.transformMatrix(m, math.matrix.translate(-100, 300, -200));
88+
m = math.matrix.transformMatrix(m, math.matrix.scale(200.0, 0.01, 400.0));
89+
rhi.setUniformMatrix(prog, "f_plane_transform", m);
90+
}
91+
self.view_camera.addProgram(prog, "f_mvp");
92+
self.plane = plane;
93+
}
94+
5695
const std = @import("std");
5796
const c = @import("../../c.zig").c;
5897
const ui = @import("../../ui/ui.zig");

src/foundations/scenes/plane_distance/PlaneDistanceUI.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
distance: usize = 0,
2+
rotation_angle: f32 = 0,
3+
rotation_axis: [3]f32 = .{ 0, 0, 1 },
24

35
const PlaneDistanceUI = @This();
46

@@ -14,6 +16,15 @@ pub fn draw(self: *PlaneDistanceUI) void {
1416
c.igSetNextWindowSize(size.*, c.ImGuiCond_FirstUseEver);
1517
_ = c.igBegin("Distance to Plane", null, 0);
1618
c.igText(@ptrCast(txt));
19+
20+
if (c.igTreeNode_Str("plane normal")) {
21+
_ = c.igSliderFloat("angle", &self.rotation_angle, 0.01, std.math.pi * 2, "%.3f", c.ImGuiSliderFlags_None);
22+
c.igText("axis of rotation");
23+
_ = c.igSliderFloat("x", &self.rotation_axis[0], -1, 1, "%.3f", c.ImGuiSliderFlags_None);
24+
_ = c.igSliderFloat("y", &self.rotation_axis[1], -1, 1, "%.3f", c.ImGuiSliderFlags_None);
25+
_ = c.igSliderFloat("z", &self.rotation_axis[2], -1, 1, "%.3f", c.ImGuiSliderFlags_None);
26+
c.igTreePop();
27+
}
1728
c.igEnd();
1829
}
1930

0 commit comments

Comments
 (0)