1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
usingnamespace @import("xrvk.zig");
const glm = @import("glm");
const math = @import("std").math;
pub fn vec2vec(v: xr.Vector3f) glm.Vec3 {
return glm.Vec3.init([_]f32{ v.x, v.y, v.z });
}
pub fn quat2mat(q: xr.Quaternionf) glm.Mat4 {
const x = -q.x;
const y = -q.y;
const z = -q.z;
const w = q.w;
const x2 = q.x * q.x;
const y2 = q.y * q.y;
const z2 = q.z * q.z;
// zig fmt: off
return glm.Mat4.init([_][4]f32{
[_]f32{ 1 - 2 * y2 - 2 * z2, 2 * x * y - 2 * z * w, 2 * x * z + 2 * y * w, 0 },
[_]f32{ 2 * x * y + 2 * z * w, 1 - 2 * x2 - 2 * z2, 2 * y * z - 2 * x * w, 0 },
[_]f32{ 2 * x * z - 2 * y * w, 2 * y * z + 2 * x * w, 1 - 2 * x2 - 2 * y2, 0 },
[_]f32{ 0, 0, 0, 1 },
});
// zig fmt: on
}
pub fn pose2mat(pose: xr.Posef) glm.Mat4 {
return glm.translation(vec2vec(pose.position)).mul(quat2mat(pose.orientation));
}
pub fn pose2matInverse(pose: xr.Posef) glm.Mat4 {
var inverse = pose;
inverse.orientation.x *= -1;
inverse.orientation.y *= -1;
inverse.orientation.z *= -1;
inverse.position.x *= -1;
inverse.position.y *= -1;
inverse.position.z *= -1;
return quat2mat(inverse.orientation).mul(glm.translation(vec2vec(inverse.position)));
}
pub fn projection(fov: xr.Fovf, near: f32, far: f32) glm.Mat4 {
const tanUp = math.tan(fov.angle_up);
const tanDown = math.tan(fov.angle_down);
const tanLeft = math.tan(fov.angle_left);
const tanRight = math.tan(fov.angle_right);
const tanW = tanRight - tanLeft;
const tanH = tanUp - tanDown ;
return glm.Mat4.init([_][4]f32{
[_]f32{ 2 / tanW, 0, 0, 0 },
[_]f32{ 0, 2 / tanH, 0, 0 },
[_]f32{ (tanRight + tanLeft) / tanW, (tanUp + tanDown) / tanH, -far / (far - near), -1 },
[_]f32{ 0, 0, -far * near / (far - near), 0 },
});
}
|