0 | 0 |
const std = @import("std");
|
1 | 1 |
const dg = @import("diligent");
|
|
2 |
const glm = @import("glm");
|
2 | 3 |
const graphics = @import("graphics.zig");
|
3 | 4 |
usingnamespace @import("xrvk.zig");
|
4 | 5 |
|
|
7 | 8 |
\\
|
8 | 9 |
\\void main() {
|
9 | 10 |
\\ vec4 positions[3] = vec4[3](
|
10 | |
\\ vec4(-0.5, -0.5, 0.0, 1.0),
|
11 | |
\\ vec4(0.0, 0.5, 0.0, 1.0),
|
12 | |
\\ vec4(0.5, -0.5, 0.0, 1.0)
|
|
11 |
\\ vec4(-0.5, 1, -2.0, 1.0),
|
|
12 |
\\ vec4( 0.0, 2, -2.0, 1.0),
|
|
13 |
\\ vec4( 0.5, 1, -2.0, 1.0)
|
13 | 14 |
\\ );
|
14 | 15 |
\\ vec3 colors[3] = vec3[3](
|
15 | 16 |
\\ vec3(1.0, 0.0, 0.0),
|
|
64 | 65 |
\\}
|
65 | 66 |
;
|
66 | 67 |
|
|
68 |
fn vec2vec(v: xr.Vector3f) glm.Vec3 {
|
|
69 |
return glm.Vec3.init([_]f32{ v.x, v.y, v.z });
|
|
70 |
}
|
|
71 |
|
|
72 |
fn quat2mat(q: xr.Quaternionf) glm.Mat4 {
|
|
73 |
const x = q.x;
|
|
74 |
const y = q.y;
|
|
75 |
const z = q.z;
|
|
76 |
const w = q.w;
|
|
77 |
const x2 = q.x * q.x;
|
|
78 |
const y2 = q.y * q.y;
|
|
79 |
const z2 = q.z * q.z;
|
|
80 |
|
|
81 |
// zig fmt: off
|
|
82 |
return glm.Mat4.init([_][4]f32{
|
|
83 |
[_]f32{ 1 - 2 * y2 - 2 * z2, 2 * x * y - 2 * z * w, 2 * x * z + 2 * y * w, 0 },
|
|
84 |
[_]f32{ 2 * x * y + 2 * z * w, 1 - 2 * x2 - 2 * z2, 2 * y * z - 2 * x * w, 0 },
|
|
85 |
[_]f32{ 2 * x * z - 2 * y * w, 2 * y * z + 2 * x * w, 1 - 2 * x2 - 2 * y2, 0 },
|
|
86 |
[_]f32{ 0, 0, 0, 1 },
|
|
87 |
});
|
|
88 |
// zig fmt: on
|
|
89 |
}
|
|
90 |
|
67 | 91 |
pub const Scene = struct {
|
68 | 92 |
viewports: []dg.Viewport,
|
69 | 93 |
scissors: []dg.Rect,
|
|
78 | 102 |
swc: dg.ISwapChainVk_Wrapper,
|
79 | 103 |
|
80 | 104 |
const ViewUBO = packed struct {
|
81 | |
projection: [16]f32,
|
82 | |
modelview: [16]f32,
|
|
105 |
projection: glm.Mat4,
|
|
106 |
modelview: glm.Mat4,
|
83 | 107 |
};
|
84 | 108 |
|
85 | 109 |
pub fn init(gfx: *const graphics.Graphics) !Scene {
|
|
116 | 140 |
};
|
117 | 141 |
|
118 | 142 |
self.views[i] = .{
|
119 | |
.projection = [_]f32{
|
120 | |
1, 0, 0, 0,
|
121 | |
0, 1, 0, 0,
|
122 | |
0, 0, 1, 0,
|
123 | |
0, 0, 0, 1,
|
124 | |
},
|
125 | |
.modelview = [_]f32{
|
126 | |
1, 0, 0, 0,
|
127 | |
0, 1, 0, 0,
|
128 | |
0, 0, 1, 0,
|
129 | |
0, 0, 0, 1,
|
130 | |
},
|
|
143 |
.projection = glm.Mat4.IDENTITY,
|
|
144 |
.modelview = glm.Mat4.IDENTITY,
|
131 | 145 |
};
|
132 | 146 |
}
|
133 | 147 |
|
|
281 | 295 |
|
282 | 296 |
pub fn render(self: *const Scene, views: []const xr.View) !void {
|
283 | 297 |
// update uniform buffer
|
284 | |
for (views) |view, i| {
|
285 | |
self.views[i].modelview[3] = view.pose.position.x / 3.0;
|
|
298 |
for (self.views) |*view, i| {
|
|
299 |
const fov = views[i].fov;
|
|
300 |
const viewport = self.viewports[i];
|
|
301 |
const fovY = fov.angle_up - fov.angle_down;
|
|
302 |
const aspect = viewport.Width / viewport.Height;
|
|
303 |
view.projection = glm.perspective(fovY, aspect, 0.1, 100);
|
|
304 |
|
|
305 |
const pose = views[i].pose;
|
|
306 |
view.modelview = quat2mat(pose.orientation).mul(glm.translation(vec2vec(pose.position).mulScalar(-1)));
|
286 | 307 |
}
|
287 | 308 |
try self.updateBufferSlice(ViewUBO, self.views_ubo, self.views);
|
288 | 309 |
|