Skip to content

Commit edee654

Browse files
committed
Fixed projection and quaternion
1 parent 0e81eff commit edee654

File tree

4 files changed

+52
-49
lines changed

4 files changed

+52
-49
lines changed

include/nbl/builtin/hlsl/math/quaternion/quaternion.hlsl

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,23 @@ struct quaternion
4848

4949
static inline quaternion create(float_t pitch, float_t yaw, float_t roll)
5050
{
51-
float angle;
51+
const float rollDiv2 = roll * 0.5f;
52+
const float sr = sinf(rollDiv2);
53+
const float cr = cosf(rollDiv2);
5254

53-
angle = roll * 0.5f;
54-
const float sr = sinf(angle);
55-
const float cr = cosf(angle);
55+
const float pitchDiv2 = pitch * 0.5f;
56+
const float sp = sinf(pitchDiv2);
57+
const float cp = cosf(pitchDiv2);
5658

57-
angle = pitch * 0.5f;
58-
const float sp = sinf(angle);
59-
const float cp = cos(angle);
60-
61-
angle = yaw * 0.5f;
62-
const float sy = sinf(angle);
63-
const float cy = cosf(angle);
64-
65-
const float cpcy = cp * cy;
66-
const float spcy = sp * cy;
67-
const float cpsy = cp * sy;
68-
const float spsy = sp * sy;
59+
const float yawDiv2 = yaw * 0.5f;
60+
const float sy = sinf(yawDiv2);
61+
const float cy = cosf(yawDiv2);
6962

7063
quaternion<float_t> output;
71-
output.data[3] = cr * cp * cy + sr * sp * sy; // w
7264
output.data[0] = cr * sp * cy + sr * cp * sy; // x
7365
output.data[1] = cr * cp * sy - sr * sp * cy; // y
7466
output.data[2] = sr * cp * cy - cr * sp * sy; // z
67+
output.data[3] = cr * cp * cy + sr * sp * sy; // w
7568

7669
return output;
7770
}

include/nbl/builtin/hlsl/matrix_utils/transformation_matrix_utils.hlsl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ inline matrix<T, 4, 4> getMatrix3x4As4x4(NBL_CONST_REF_ARG(matrix<T, 3, 4>) mat)
6767
return output;
6868
}
6969

70+
template<typename T>
71+
inline matrix<T, 3, 4> extractSub3x4From4x4Matrix(NBL_CONST_REF_ARG(matrix<T, 4, 4>) mat)
72+
{
73+
matrix<T, 3, 4> output;
74+
for (int i = 0; i < 3; ++i)
75+
output[i] = mat[i];
76+
77+
return output;
78+
}
79+
7080
template<typename T, int N>
7181
inline matrix<T, 3, 3> getSub3x3(NBL_CONST_REF_ARG(matrix<T, N, 4>) mat)
7282
{
@@ -135,7 +145,7 @@ inline bool getSub3x3InverseTranspose(NBL_CONST_REF_ARG(matrix<T, N, 4>) matIn,
135145
// TODO: use portable_float when merged
136146
//! multiplies matrices a and b, 3x4 matrices are treated as 4x4 matrices with 4th row set to (0, 0, 0 ,1)
137147
template<typename T>
138-
inline matrix<T, 3, 4> concatenateBFollowedByA(NBL_CONST_REF_ARG(matrix<T, 3, 4>) a, NBL_CONST_REF_ARG(const matrix<T, 3, 4>) b)
148+
inline matrix<T, 3, 4> concatenateBFollowedByA(NBL_CONST_REF_ARG(matrix<T, 3, 4>) a, NBL_CONST_REF_ARG(matrix<T, 3, 4>) b)
139149
{
140150
// TODO
141151
// static_assert(N == 3 || N == 4);

include/nbl/builtin/hlsl/projection/projection.hlsl

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,69 @@ namespace nbl
88
namespace hlsl
99
{
1010
// TODO: use glm instead for c++
11-
template<typename T>
12-
inline matrix<T, 4, 4> buildProjectionMatrixPerspectiveFovRH(float fieldOfViewRadians, float aspectRatio, float zNear, float zFar)
11+
template<typename FloatingPoint>
12+
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixPerspectiveFovRH(FloatingPoint fieldOfViewRadians, FloatingPoint aspectRatio, FloatingPoint zNear, FloatingPoint zFar)
1313
{
14-
const float h = core::reciprocal<float>(tanf(fieldOfViewRadians * 0.5f));
14+
const FloatingPoint h = core::reciprocal<FloatingPoint>(tan(fieldOfViewRadians * 0.5f));
1515
_NBL_DEBUG_BREAK_IF(aspectRatio == 0.f); //division by zero
1616
const float w = h / aspectRatio;
1717

1818
_NBL_DEBUG_BREAK_IF(zNear == zFar); //division by zero
1919

20-
matrix<T, 4, 4> m;
21-
m[0] = vector<T, 4>(w, 0.f, 0.f, 0.f);
22-
m[1] = vector<T, 4>(0.f, -h, 0.f, 0.f);
23-
m[2] = vector<T, 4>(0.f, 0.f, -zFar / (zFar - zNear), -zNear * zFar / (zFar - zNear));
24-
m[3] = vector<T, 4>(0.f, 0.f, -1.f, 0.f);
20+
matrix<FloatingPoint, 4, 4> m;
21+
m[0] = vector<FloatingPoint, 4>(w, 0.f, 0.f, 0.f);
22+
m[1] = vector<FloatingPoint, 4>(0.f, -h, 0.f, 0.f);
23+
m[2] = vector<FloatingPoint, 4>(0.f, 0.f, -zFar / (zFar - zNear), -zNear * zFar / (zFar - zNear));
24+
m[3] = vector<FloatingPoint, 4>(0.f, 0.f, -1.f, 0.f);
2525

2626
return m;
2727
}
28-
template<typename T>
29-
inline matrix<T, 4, 4> buildProjectionMatrixPerspectiveFovLH(float fieldOfViewRadians, float aspectRatio, float zNear, float zFar)
28+
template<typename FloatingPoint>
29+
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixPerspectiveFovLH(FloatingPoint fieldOfViewRadians, FloatingPoint aspectRatio, FloatingPoint zNear, FloatingPoint zFar)
3030
{
31-
const float h = core::reciprocal<float>(tanf(fieldOfViewRadians * 0.5f));
31+
const FloatingPoint h = core::reciprocal<FloatingPoint>(tan(fieldOfViewRadians * 0.5f));
3232
_NBL_DEBUG_BREAK_IF(aspectRatio == 0.f); //division by zero
3333
const float w = h / aspectRatio;
3434

3535
_NBL_DEBUG_BREAK_IF(zNear == zFar); //division by zero
3636

37-
matrix<T, 4, 4> m;
38-
m[0] = vector<T, 4>(w, 0.f, 0.f, 0.f);
39-
m[1] = vector<T, 4>(0.f, -h, 0.f, 0.f);
40-
m[2] = vector<T, 4>(0.f, 0.f, zFar / (zFar - zNear), -zNear * zFar / (zFar - zNear));
41-
m[3] = vector<T, 4>(0.f, 0.f, 1.f, 0.f);
37+
matrix<FloatingPoint, 4, 4> m;
38+
m[0] = vector<FloatingPoint, 4>(w, 0.f, 0.f, 0.f);
39+
m[1] = vector<FloatingPoint, 4>(0.f, -h, 0.f, 0.f);
40+
m[2] = vector<FloatingPoint, 4>(0.f, 0.f, zFar / (zFar - zNear), -zNear * zFar / (zFar - zNear));
41+
m[3] = vector<FloatingPoint, 4>(0.f, 0.f, 1.f, 0.f);
4242

4343
return m;
4444
}
4545

46-
template<typename T>
47-
inline matrix<T, 4, 4> buildProjectionMatrixOrthoRH(float widthOfViewVolume, float heightOfViewVolume, float zNear, float zFar)
46+
template<typename FloatingPoint>
47+
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixOrthoRH(FloatingPoint widthOfViewVolume, FloatingPoint heightOfViewVolume, FloatingPoint zNear, FloatingPoint zFar)
4848
{
4949
_NBL_DEBUG_BREAK_IF(widthOfViewVolume == 0.f); //division by zero
5050
_NBL_DEBUG_BREAK_IF(heightOfViewVolume == 0.f); //division by zero
5151
_NBL_DEBUG_BREAK_IF(zNear == zFar); //division by zero
5252

53-
matrix<T, 4, 4> m;
54-
m[0] = vector<T, 4>(2.f / widthOfViewVolume, 0.f, 0.f, 0.f);
55-
m[1] = vector<T, 4>(0.f, -2.f / heightOfViewVolume, 0.f, 0.f);
56-
m[2] = vector<T, 4>(0.f, 0.f, -1.f / (zFar - zNear), -zNear / (zFar - zNear));
57-
m[3] = vector<T, 4>(0.f, 0.f, 0.f, 1.f);
53+
matrix<FloatingPoint, 4, 4> m;
54+
m[0] = vector<FloatingPoint, 4>(2.f / widthOfViewVolume, 0.f, 0.f, 0.f);
55+
m[1] = vector<FloatingPoint, 4>(0.f, -2.f / heightOfViewVolume, 0.f, 0.f);
56+
m[2] = vector<FloatingPoint, 4>(0.f, 0.f, -1.f / (zFar - zNear), -zNear / (zFar - zNear));
57+
m[3] = vector<FloatingPoint, 4>(0.f, 0.f, 0.f, 1.f);
5858

5959
return m;
6060
}
6161

62-
template<typename T>
63-
inline matrix<T, 4, 4> buildProjectionMatrixOrthoLH(float widthOfViewVolume, float heightOfViewVolume, float zNear, float zFar)
62+
template<typename FloatingPoint>
63+
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixOrthoLH(FloatingPoint widthOfViewVolume, FloatingPoint heightOfViewVolume, FloatingPoint zNear, FloatingPoint zFar)
6464
{
6565
_NBL_DEBUG_BREAK_IF(widthOfViewVolume == 0.f); //division by zero
6666
_NBL_DEBUG_BREAK_IF(heightOfViewVolume == 0.f); //division by zero
6767
_NBL_DEBUG_BREAK_IF(zNear == zFar); //division by zero
6868

69-
matrix<T, 4, 4> m;
70-
m[0] = vector<T, 4>(2.f / widthOfViewVolume, 0.f, 0.f, 0.f);
71-
m[1] = vector<T, 4>(0.f, -2.f / heightOfViewVolume, 0.f, 0.f);
72-
m[2] = vector<T, 4>(0.f, 0.f, 1.f / (zFar - zNear), -zNear / (zFar - zNear));
73-
m[3] = vector<T, 4>(0.f, 0.f, 0.f, 1.f);
69+
matrix<FloatingPoint, 4, 4> m;
70+
m[0] = vector<FloatingPoint, 4>(2.f / widthOfViewVolume, 0.f, 0.f, 0.f);
71+
m[1] = vector<FloatingPoint, 4>(0.f, -2.f / heightOfViewVolume, 0.f, 0.f);
72+
m[2] = vector<FloatingPoint, 4>(0.f, 0.f, 1.f / (zFar - zNear), -zNear / (zFar - zNear));
73+
m[3] = vector<FloatingPoint, 4>(0.f, 0.f, 0.f, 1.f);
7474

7575
return m;
7676
}

0 commit comments

Comments
 (0)