git.s-ol.nu openxPriments / 2bbdf50
render using OpenXR transform s-ol 1 year, 4 months ago
4 changed file(s) with 45 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
66 [submodule "lib/DiligentEngine"]
77 path = lib/DiligentEngine
88 url = https://github.com/DiligentGraphics/DiligentEngine.git
9 [submodule "lib/glm"]
10 path = lib/glm
11 url = https://github.com/ziglang-contrib/glm.git
1616 exe.linkSystemLibrary("vulkan");
1717 exe.linkSystemLibrary("openxr_loader");
1818 exe.linkSystemLibrary("GraphicsEngineVk");
19
20 exe.addPackagePath("glm", "lib/glm/glm.zig");
1921
2022 const gen_dg = dggen.DgGenerateStep.init(b, "lib/DiligentEngine", "dg.zig");
2123 gen_dg.addDeps(exe, "Debug");
(New empty file)
00 const std = @import("std");
11 const dg = @import("diligent");
2 const glm = @import("glm");
23 const graphics = @import("graphics.zig");
34 usingnamespace @import("xrvk.zig");
45
78 \\
89 \\void main() {
910 \\ 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)
1314 \\ );
1415 \\ vec3 colors[3] = vec3[3](
1516 \\ vec3(1.0, 0.0, 0.0),
6465 \\}
6566 ;
6667
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
6791 pub const Scene = struct {
6892 viewports: []dg.Viewport,
6993 scissors: []dg.Rect,
78102 swc: dg.ISwapChainVk_Wrapper,
79103
80104 const ViewUBO = packed struct {
81 projection: [16]f32,
82 modelview: [16]f32,
105 projection: glm.Mat4,
106 modelview: glm.Mat4,
83107 };
84108
85109 pub fn init(gfx: *const graphics.Graphics) !Scene {
116140 };
117141
118142 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,
131145 };
132146 }
133147
281295
282296 pub fn render(self: *const Scene, views: []const xr.View) !void {
283297 // 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)));
286307 }
287308 try self.updateBufferSlice(ViewUBO, self.views_ubo, self.views);
288309