Blender V5.3
gpu_shader_math_quaternion.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9struct Quaternion {
10 float x, y, z, w;
11
13 {
14 return {1, 0, 0, 0};
15 }
16
18 {
19 return Quaternion{.x = v.x, .y = v.y, .z = v.z, .w = v.w};
20 }
21
23 {
24 return float4(this->x, this->y, this->z, this->w);
25 }
26};
27
28/* -------------------------------------------------------------------- */
31
33{
34 Quaternion result;
35 result.x = a.x * b.x - a.y * b.y - a.z * b.z - a.w * b.w;
36 result.y = a.x * b.y + a.y * b.x + a.z * b.w - a.w * b.z;
37 result.z = a.x * b.z - a.y * b.w + a.z * b.x + a.w * b.y;
38 result.w = a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x;
39 return result;
40}
41
43{
44 return {q.x, -q.y, -q.z, -q.w};
45}
46
48{
49 const Quaternion v_quat = {0.0f, v.x, v.y, v.z};
52 return float3(result.y, result.z, result.w);
53}
54
56{
57 float3x3 result;
58
59 result[0][0] = 1.0f - 2.0f * (q.y * q.y + q.z * q.z);
60 result[0][1] = 2.0f * (q.x * q.y + q.w * q.z);
61 result[0][2] = 2.0f * (q.x * q.z - q.w * q.y);
62
63 result[1][0] = 2.0f * (q.x * q.y - q.w * q.z);
64 result[1][1] = 1.0f - 2.0f * (q.x * q.x + q.z * q.z);
65 result[1][2] = 2.0f * (q.y * q.z + q.w * q.x);
66
67 result[2][0] = 2.0f * (q.x * q.z + q.w * q.y);
68 result[2][1] = 2.0f * (q.y * q.z - q.w * q.x);
69 result[2][2] = 1.0f - 2.0f * (q.x * q.x + q.y * q.y);
70
71 return result;
72}
73
float3 transform_point_by_quaternion(Quaternion q, float3 v)
Quaternion quaternion_conjugate(Quaternion q)
float3x3 quaternion_to_float3x3(Quaternion q)
Quaternion math_quaternion_multiply(Quaternion a, Quaternion b)
static Quaternion identity()
static Quaternion from_float4(float4 v)