return faceforward, reflect and refract functions

This commit is contained in:
BlackMATov
2021-02-24 03:40:22 +07:00
parent e80f49133b
commit e95b9cf898
7 changed files with 67 additions and 1 deletions

View File

@@ -14,7 +14,7 @@ jobs:
- { os: "windows-2016", vs: "Visual Studio 2017", arch: "x64" } - { os: "windows-2016", vs: "Visual Studio 2017", arch: "x64" }
- { os: "windows-2019", vs: "Visual Studio 2019", arch: "x86" } - { os: "windows-2019", vs: "Visual Studio 2019", arch: "x86" }
- { os: "windows-2019", vs: "Visual Studio 2019", arch: "x64" } - { os: "windows-2019", vs: "Visual Studio 2019", arch: "x64" }
name: "${{matrix.config.vs}}" name: "${{matrix.config.vs}} ${{matrix.config.arch}}"
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Build && Test - name: Build && Test

View File

@@ -1325,6 +1325,15 @@ constexpr T distance2(T x, T y) noexcept;
template < floating_point T > template < floating_point T >
T normalize(T x) noexcept; T normalize(T x) noexcept;
template < floating_point T >
constexpr T faceforward(T n, T i, T nref) noexcept;
template < floating_point T >
constexpr T reflect(T i, T n) noexcept;
template < floating_point T >
T refract(T i, T n, T eta) noexcept;
``` ```
#### Vector #### Vector
@@ -1359,6 +1368,15 @@ constexpr vec<T, 3> cross(const vec<T, 3>& xs, const vec<T, 3>& ys);
template < typename T, size_t Size > template < typename T, size_t Size >
vec<T, Size> normalize(const vec<T, Size>& xs); vec<T, Size> normalize(const vec<T, Size>& xs);
template < typename T, size_t Size >
constexpr vec<T, Size> faceforward(const vec<T, Size>& n, const vec<T, Size>& i, const vec<T, Size>& nref);
template < typename T, size_t Size >
constexpr vec<T, Size> reflect(const vec<T, Size>& i, const vec<T, Size>& n);
template < typename T, size_t Size >
vec<T, Size> refract(const vec<T, Size>& i, const vec<T, Size>& n, T eta);
``` ```
#### Quaternion #### Quaternion

View File

@@ -358,6 +358,26 @@ namespace vmath_hpp
normalize(T x) noexcept { normalize(T x) noexcept {
return x * rlength(x); return x * rlength(x);
} }
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_floating_point_v<T>, T>
constexpr faceforward(T n, T i, T nref) noexcept {
return dot(nref, i) < T{0} ? n : -n;
}
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_floating_point_v<T>, T>
constexpr reflect(T i, T n) noexcept {
return i - T{2} * dot(n, i) * n;
}
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_floating_point_v<T>, T>
refract(T i, T n, T eta) noexcept {
const T d = dot(n, i);
const T k = T{1} - sqr(eta) * (T{1} - sqr(d));
return k < T{0} ? T{0} : (eta * i - (eta * d + sqrt(k)) * n);
}
} }
// //

View File

@@ -834,6 +834,23 @@ namespace vmath_hpp
[[nodiscard]] vec<T, Size> normalize(const vec<T, Size>& xs) { [[nodiscard]] vec<T, Size> normalize(const vec<T, Size>& xs) {
return xs * rlength(xs); return xs * rlength(xs);
} }
template < typename T, std::size_t Size >
[[nodiscard]] constexpr vec<T, Size> faceforward(const vec<T, Size>& n, const vec<T, Size>& i, const vec<T, Size>& nref) {
return dot(nref, i) < T{0} ? n : -n;
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr vec<T, Size> reflect(const vec<T, Size>& i, const vec<T, Size>& n) {
return i - T{2} * dot(n, i) * n;
}
template < typename T, std::size_t Size >
[[nodiscard]] vec<T, Size> refract(const vec<T, Size>& i, const vec<T, Size>& n, T eta) {
const T d = dot(n, i);
const T k = T{1} - sqr(eta) * (T{1} - sqr(d));
return k < T{0} ? vec<T, Size>{T{0}} : (eta * i - (eta * d + sqrt(k)) * n);
}
} }
// //

View File

@@ -267,6 +267,9 @@ namespace vmath_hpp
template fix<float> cross(const fix2f&, const fix2f&); template fix<float> cross(const fix2f&, const fix2f&);
template fix3f cross(const fix3f&, const fix3f&); template fix3f cross(const fix3f&, const fix3f&);
template fix2f normalize(const fix2f&); template fix2f normalize(const fix2f&);
template fix3f faceforward(const fix3f&, const fix3f&, const fix3f&);
template fix3f reflect(const fix3f&, const fix3f&);
template fix3f refract(const fix3f&, const fix3f&, fix<float>);
} }
namespace vmath_hpp namespace vmath_hpp

View File

@@ -130,6 +130,10 @@ TEST_CASE("vmath/fun") {
STATIC_CHECK(dot(2.f, 5.f) == uapprox(10.f)); STATIC_CHECK(dot(2.f, 5.f) == uapprox(10.f));
CHECK(normalize(0.5f) == uapprox(1.f)); CHECK(normalize(0.5f) == uapprox(1.f));
STATIC_CHECK(faceforward(1.f, 2.f, 3.f) == uapprox(-1.f));
STATIC_CHECK(reflect(1.f, 2.f) == uapprox(-7.f));
CHECK(refract(1.f, 2.f, 1.f) == uapprox(-7.f));
} }
SUBCASE("Relational Functions") { SUBCASE("Relational Functions") {

View File

@@ -276,6 +276,10 @@ TEST_CASE("vmath/vec_fun") {
STATIC_CHECK(cross(int2(1,0),int2(0,1)) == 1); STATIC_CHECK(cross(int2(1,0),int2(0,1)) == 1);
STATIC_CHECK(cross(int3(1,0,0),int3(0,1,0)) == int3(0,0,1)); STATIC_CHECK(cross(int3(1,0,0),int3(0,1,0)) == int3(0,0,1));
CHECK(normalize(float2(0.5f,0.f)).x == uapprox(1.f)); CHECK(normalize(float2(0.5f,0.f)).x == uapprox(1.f));
STATIC_CHECK(faceforward(float2(1.f), float2(2.f), float2(3.f)).x == uapprox(-1.f));
STATIC_CHECK(reflect(float2(1.f), float2(2.f)).x == uapprox(-15.f));
CHECK(refract(float2(1.f), float2(2.f), 1.f).x == uapprox(-15.f));
} }
SUBCASE("Relational Functions") { SUBCASE("Relational Functions") {