| #version 450 | |
| #extension GL_ARB_separate_shader_objects : enable | |
| struct Pose { | |
| vec4 quaternion; | |
| vec3 position; | |
| uint reserved; | |
| }; | |
| layout(push_constant) uniform PushConstants { | |
| uint latch_index; | |
| }; | |
| layout (binding = 0) uniform VPMatrices { | |
| mat4 left_view_transform; | |
| mat4 left_projection_matrix; | |
| mat4 right_view_transform; | |
| mat4 right_projection_matrix; | |
| }; | |
| layout (binding = 1) buffer PoseBuffer { | |
| Pose poses[]; | |
| }; | |
| layout (binding = 2) buffer OutputBuffer { | |
| Pose latched_pose; | |
| mat4 left_vp_matrix; | |
| mat4 right_vp_matrix; | |
| }; | |
| // interpreted from GLM's mat3_cast | |
| mat3 quaternion_to_mat3(vec4 q) | |
| { | |
| mat3 result; | |
| float qxx = q.x * q.x; | |
| float qyy = q.y * q.y; | |
| float qzz = q.z * q.z; | |
| float qxz = q.x * q.z; | |
| float qxy = q.x * q.y; | |
| float qyz = q.y * q.z; | |
| float qwx = q.w * q.x; | |
| float qwy = q.w * q.y; | |
| float qwz = q.w * q.z; | |
| result[0][0] = float(1) - float(2) * (qyy + qzz); | |
| result[0][1] = float(2) * (qxy + qwz); | |
| result[0][2] = float(2) * (qxz - qwy); | |
| result[1][0] = float(2) * (qxy - qwz); | |
| result[1][1] = float(1) - float(2) * (qxx + qzz); | |
| result[1][2] = float(2) * (qyz + qwx); | |
| result[2][0] = float(2) * (qxz + qwy); | |
| result[2][1] = float(2) * (qyz - qwx); | |
| result[2][2] = float(1) - float(2) * (qxx + qyy); | |
| return result; | |
| } | |
| mat4 translate(vec3 t){ | |
| return mat4( | |
| vec4(1.0, 0.0, 0.0, 0.0), | |
| vec4(0.0, 1.0, 0.0, 0.0), | |
| vec4(0.0, 0.0, 1.0, 0.0), | |
| vec4(t.x, t.y, t.z, 1.0) | |
| ); | |
| } | |
| void main() { | |
| latched_pose = poses[latch_index]; | |
| left_vp_matrix = left_projection_matrix * | |
| mat4(quaternion_to_mat3(latched_pose.quaternion)) * | |
| translate(latched_pose.position) * left_view_transform; | |
| right_vp_matrix = right_projection_matrix * | |
| mat4(quaternion_to_mat3(latched_pose.quaternion)) * | |
| translate(latched_pose.position) * right_view_transform; | |
| } |