Skip to content

Commit daea0ad

Browse files
author
kevyuu
committed
Implement rotation_mat function on hlsl
1 parent b152755 commit daea0ad

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (C) 2018-2024 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_BUILTIN_HLSL_MATH_LINALG_TRANSFORM_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_MATH_LINALG_TRANSFORM_INCLUDED_
6+
7+
8+
#include <nbl/builtin/hlsl/mpl.hlsl>
9+
#include <nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl>
10+
#include <nbl/builtin/hlsl/concepts.hlsl>
11+
12+
13+
namespace nbl
14+
{
15+
namespace hlsl
16+
{
17+
namespace math
18+
{
19+
namespace linalg
20+
{
21+
22+
/// Builds a rotation 4 * 4 matrix created from an axis vector and an angle.
23+
///
24+
/// @param angle Rotation angle expressed in radians.
25+
/// @param axis Rotation axis, must be normalized.
26+
///
27+
/// @tparam T A floating-point scalar type
28+
template <typename T>
29+
matrix<T, 4, 4> rotation_mat(T angle, vector<T, 3> const& axis)
30+
{
31+
T const a = angle;
32+
T const c = cos(a);
33+
T const s = sin(a);
34+
35+
vector<T, 3> temp((T(1) - c) * axis);
36+
37+
matrix<T, 4, 4> rotation;
38+
rotation[0][0] = c + temp[0] * axis[0];
39+
rotation[0][1] = temp[1] * axis[0] - s * axis[2];
40+
rotation[0][2] = temp[2] * axis[0] + s * axis[1];
41+
42+
rotation[1][0] = temp[0] * axis[1] + s * axis[2];
43+
rotation[1][1] = c + temp[1] * axis[1];
44+
rotation[1][2] = temp[2] * axis[1] - s * axis[0];
45+
46+
rotation[2][0] = temp[0] * axis[2] - s * axis[1];
47+
rotation[2][1] = temp[1] * axis[2] + s * axis[0];
48+
rotation[2][2] = c + temp[2] * axis[2];
49+
50+
return rotation;
51+
}
52+
53+
}
54+
}
55+
}
56+
}
57+
#endif

0 commit comments

Comments
 (0)