Skip to content

Commit 4a283ba

Browse files
committed
quaternion struct, renamed spherical tri/rect shapes
1 parent cf6760e commit 4a283ba

File tree

7 files changed

+117
-25
lines changed

7 files changed

+117
-25
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (C) 2018-2023 - 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_QUATERNIONS_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_MATH_QUATERNIONS_INCLUDED_
6+
7+
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
8+
#include "nbl/builtin/hlsl/tgmath.hlsl"
9+
10+
namespace nbl
11+
{
12+
namespace hlsl
13+
{
14+
namespace math
15+
{
16+
17+
template <typename T>
18+
struct quaternion_t
19+
{
20+
using this_t = quaternion_t<T>;
21+
using scalar_type = T;
22+
using data_type = vector<T, 4>;
23+
using vector3_type = vector<T, 3>;
24+
using matrix_type = matrix<T, 3, 3>;
25+
26+
static this_t createFromTruncated(const vector3_type first3Components)
27+
{
28+
this_t retval;
29+
retval.data.xyz = first3Components;
30+
retval.data.w = hlsl::sqrt(scalar_type(1.0) - hlsl::dot(first3Components, first3Components));
31+
return retval;
32+
}
33+
34+
static this_t lerp(const this_t start, const this_t end, const scalar_type fraction, const scalar_type totalPseudoAngle)
35+
{
36+
using AsUint = typename unsigned_integer_of_size<sizeof(scalar_type)>::type;
37+
const AsUint negationMask = hlsl::bit_cast<AsUint>(totalPseudoAngle) & AsUint(0x80000000u);
38+
const data_type adjEnd = hlsl::bit_cast<scalar_type>(hlsl::bit_cast<AsUint>(end.data) ^ negationMask);
39+
40+
this_t retval;
41+
retval.data = hlsl::mix(start.data, adjEnd, fraction);
42+
return retval;
43+
}
44+
45+
static this_t lerp(const this_t start, const this_t end, const scalar_type fraction)
46+
{
47+
return lerp(start, end, fraction, hlsl::dot(start.data, end.data));
48+
}
49+
50+
static scalar_type __adj_interpolant(const scalar_type angle, const scalar_type fraction, const scalar_type interpolantPrecalcTerm2, const scalar_type interpolantPrecalcTerm3)
51+
{
52+
const scalar_type A = scalar_type(1.0904) + angle * (scalar_type(-3.2452) + angle * (scalar_type(3.55645) - angle * scalar_type(1.43519)));
53+
const scalar_type B = scalar_type(0.848013) + angle * (scalar_type(-1.06021) + angle * scalar_type(0.215638));
54+
const scalar_type k = A * interpolantPrecalcTerm2 + B;
55+
return fraction + interpolantPrecalcTerm3 * k;
56+
}
57+
58+
static this_t flerp(const this_t start, const this_t end, const scalar_type fraction)
59+
{
60+
const scalar_type pseudoAngle = hlsl::dot(start.data,end.data);
61+
const scalar_type interpolantPrecalcTerm = fraction - scalar_type(0.5);
62+
const scalar_type interpolantPrecalcTerm3 = fraction * interpolantPrecalcTerm * (fraction - scalar_type(1.0));
63+
const scalar_type adjFrac = __adj_interpolant(hlsl::abs(pseudoAngle),fraction,interpolantPrecalcTerm*interpolantPrecalcTerm,interpolantPrecalcTerm3);
64+
65+
this_t retval = lerp(start,end,adjFrac,pseudoAngle);
66+
retval.data = hlsl::normalize(retval.data);
67+
return retval;
68+
}
69+
70+
matrix_type constructMatrix()
71+
{
72+
matrix_type mat;
73+
mat[0] = data.yzx * data.ywz + data.zxy * data.zyw * vector3_type( 1.0, 1.0,-1.0);
74+
mat[1] = data.yzx * data.xzw + data.zxy * data.wxz * vector3_type(-1.0, 1.0, 1.0);
75+
mat[2] = data.yzx * data.wyx + data.zxy * data.xwy * vector3_type( 1.0,-1.0, 1.0);
76+
mat[0][0] = scalar_type(0.5) - mat[0][0];
77+
mat[1][1] = scalar_type(0.5) - mat[1][1];
78+
mat[2][2] = scalar_type(0.5) - mat[2][2];
79+
mat *= scalar_type(2.0);
80+
return hlsl::transpose(mat); // TODO: double check transpose?
81+
}
82+
83+
static vector3_type slerp_delta(const vector3_type start, const vector3_type preScaledWaypoint, scalar_type cosAngleFromStart)
84+
{
85+
vector3_type planeNormal = hlsl::cross(start,preScaledWaypoint);
86+
87+
cosAngleFromStart *= scalar_type(0.5);
88+
const scalar_type sinAngle = hlsl::sqrt(scalar_type(0.5) - cosAngleFromStart);
89+
const scalar_type cosAngle = hlsl::sqrt(scalar_type(0.5) + cosAngleFromStart);
90+
91+
planeNormal *= sinAngle;
92+
const vector3_type precompPart = hlsl::cross(planeNormal, start) * scalar_type(2.0);
93+
94+
return precompPart * cosAngle + hlsl::cross(planeNormal, precompPart);
95+
}
96+
97+
data_type data;
98+
};
99+
100+
}
101+
}
102+
}
103+
104+
#endif

include/nbl/builtin/hlsl/sampling/spherical_rectangle.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
99
#include <nbl/builtin/hlsl/limits.hlsl>
1010
#include <nbl/builtin/hlsl/math/functions.hlsl>
11-
#include <nbl/builtin/hlsl/shapes/triangle.hlsl>
11+
#include <nbl/builtin/hlsl/shapes/spherical_triangle.hlsl>
1212

1313
namespace nbl
1414
{

include/nbl/builtin/hlsl/sampling/spherical_triangle.hlsl

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
99
#include <nbl/builtin/hlsl/limits.hlsl>
1010
#include <nbl/builtin/hlsl/math/functions.hlsl>
11-
#include <nbl/builtin/hlsl/shapes/triangle.hlsl>
11+
#include <nbl/builtin/hlsl/math/quaternions.hlsl>
12+
#include <nbl/builtin/hlsl/shapes/spherical_triangle.hlsl>
1213

1314
namespace nbl
1415
{
@@ -31,20 +32,6 @@ struct SphericalTriangle
3132
return retval;
3233
}
3334

34-
vector3_type slerp_delta(NBL_CONST_REF_ARG(vector3_type) start, NBL_CONST_REF_ARG(vector3_type) preScaledWaypoint, scalar_type cosAngleFromStart)
35-
{
36-
vector3_type planeNormal = nbl::hlsl::cross(start,preScaledWaypoint);
37-
38-
cosAngleFromStart *= 0.5;
39-
const scalar_type sinAngle = nbl::hlsl::sqrt(0.5 - cosAngleFromStart);
40-
const scalar_type cosAngle = nbl::hlsl::sqrt(0.5 + cosAngleFromStart);
41-
42-
planeNormal *= sinAngle;
43-
const vector3_type precompPart = nbl::hlsl::cross(planeNormal, start) * 2.0;
44-
45-
return precompPart * cosAngle + nbl::hlsl::cross(planeNormal, precompPart);
46-
}
47-
4835
// WARNING: can and will return NAN if one or three of the triangle edges are near zero length
4936
vector3_type generate(scalar_type solidAngle, NBL_CONST_REF_ARG(vector3_type) cos_vertices, NBL_CONST_REF_ARG(vector3_type) sin_vertices, scalar_type cos_a, scalar_type cos_c, scalar_type csc_b, scalar_type csc_c, NBL_CONST_REF_ARG(vector2_type) u)
5037
{
@@ -64,7 +51,7 @@ struct SphericalTriangle
6451
{
6552
const scalar_type cosAngleAlongAC = ((v_ * q - u_ * p) * cos_vertices[0] - v_) / ((v_ * p + u_ * q) * sin_vertices[0]);
6653
if (nbl::hlsl::abs(cosAngleAlongAC) < 1.f)
67-
C_s += slerp_delta(tri.vertex0, tri.vertex2 * csc_b, cosAngleAlongAC);
54+
C_s += math::quaternion_t<scalar_type>::slerp_delta(tri.vertex0, tri.vertex2 * csc_b, cosAngleAlongAC);
6855
}
6956

7057
vector3_type retval = tri.vertex1;
@@ -74,7 +61,7 @@ struct SphericalTriangle
7461
{
7562
const scalar_type cosAngleAlongBC_s = nbl::hlsl::clamp(1.0 + cosBC_s * u.y - u.y, -1.f, 1.f);
7663
if (nbl::hlsl::abs(cosAngleAlongBC_s) < 1.f)
77-
retval += slerp_delta(tri.vertex1, C_s * csc_b_s, cosAngleAlongBC_s);
64+
retval += math::quaternion_t<scalar_type>::slerp_delta(tri.vertex1, C_s * csc_b_s, cosAngleAlongBC_s);
7865
}
7966
return retval;
8067
}

include/nbl/builtin/hlsl/shapes/rectangle.hlsl renamed to include/nbl/builtin/hlsl/shapes/spherical_rectangle.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
44

5-
#ifndef _NBL_BUILTIN_HLSL_SHAPES_RECTANGLE_INCLUDED_
6-
#define _NBL_BUILTIN_HLSL_SHAPES_RECTANGLE_INCLUDED_
5+
#ifndef _NBL_BUILTIN_HLSL_SHAPES_SPHERICAL_RECTANGLE_INCLUDED_
6+
#define _NBL_BUILTIN_HLSL_SHAPES_SPHERICAL_RECTANGLE_INCLUDED_
77

88
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
99
#include <nbl/builtin/hlsl/numbers.hlsl>

include/nbl/builtin/hlsl/shapes/triangle.hlsl renamed to include/nbl/builtin/hlsl/shapes/spherical_triangle.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
44

5-
#ifndef _NBL_BUILTIN_HLSL_SHAPES_TRIANGLE_INCLUDED_
6-
#define _NBL_BUILTIN_HLSL_SHAPES_TRIANGLE_INCLUDED_
5+
#ifndef _NBL_BUILTIN_HLSL_SHAPES_SPHERICAL_TRIANGLE_INCLUDED_
6+
#define _NBL_BUILTIN_HLSL_SHAPES_SPHERICAL_TRIANGLE_INCLUDED_
77

88
#include <nbl/builtin/hlsl/tgmath.hlsl>
99
#include <nbl/builtin/hlsl/cpp_compat.hlsl>

src/nbl/asset/utils/CSmoothNormalGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "CSmoothNormalGenerator.h"
66

77
#include "nbl/core/declarations.h"
8-
#include "nbl/builtin/hlsl/shapes/triangle.hlsl"
8+
#include "nbl/builtin/hlsl/shapes/spherical_triangle.hlsl"
99

1010
#include <algorithm>
1111

src/nbl/builtin/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/geometry.hlsl")
225225
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/intutil.hlsl")
226226
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/polar.hlsl")
227227
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/angle_adding.hlsl")
228+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/quaternions.hlsl")
228229
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/equations/quadratic.hlsl")
229230
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/equations/cubic.hlsl")
230231
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/equations/quartic.hlsl")
@@ -248,8 +249,8 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/circle.hlsl")
248249
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/ellipse.hlsl")
249250
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/line.hlsl")
250251
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/beziers.hlsl")
251-
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/triangle.hlsl")
252-
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/rectangle.hlsl")
252+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/spherical_triangle.hlsl")
253+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/spherical_rectangle.hlsl")
253254
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/aabb.hlsl")
254255
#sampling
255256
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/linear.hlsl")

0 commit comments

Comments
 (0)