Loading...
Searching...
No Matches
shading_frame.h
Go to the documentation of this file.
1#ifndef _BBM_SHADINGFRAME_H_
2#define _BBM_SHADINGFRAME_H_
3
4#include "core/mat.h"
5#include "core/vec.h"
6
7/************************************************************************/
8/*! \file shading_frame.h
9 \brief Shading frame transformations
10*************************************************************************/
11
12namespace bbm {
13
14 /**********************************************************************/
15 /*! \brief Construct a local shading frame to global frame transformation
16 given a normal direction
17
18 \param normal = local shading frame normal direction
19 \returns shading frame transformation matrix from local to global frame.
20
21 Create a shading frame transformation from the local to the global shading
22 frame determined by the surface normal. The tangent vector is randomly
23 selected.
24 ***********************************************************************/
25 template<typename T>
27 {
28 using value_t = std::decay_t<T>;
29
30 // normal == Z axis
31 vec3d<value_t> X, Y, Z = bbm::normalize(normal);
32
33 // Implements: Duff et al. "Building an Orthonormal Basis, Revisited", Journal of Computer Graphics, Vol. 6, No. 1, 2017
34 value_t sign = bbm::sign(vec::z(Z));
35 const value_t a = -1.0 / (sign + vec::z(Z));
36 const value_t b = vec::x(Z) * vec::y(Z) * a;
37
38 X = vec3d<value_t>(1.0 + sign * vec::x(Z) * vec::x(Z) * a,
39 sign * b,
40 -sign * vec::x(Z));
41
42 Y = vec3d<value_t>(b,
43 sign + vec::y(Z) * vec::y(Z) * a,
44 -vec::y(Z));
45
46 // done.
47 return mat3d<value_t>(X, Y, Z);
48 }
49
50
51 /**********************************************************************/
52 /*! \brief Construct a global to local shading frame transformation
53 **********************************************************************/
54 template<typename T>
55 inline auto toLocalShadingFrame(const vec3d<T>& normal)
56 {
57 return transpose(toGlobalShadingFrame(normal));
58 }
59
60} // end bbm namespace
61
62#endif /* _BBM_SHADINGFRAME_H_ */
Defines additional helper methods for vectors.
constexpr decltype(auto) y(bbm::vec3d< T > &v)
Definition: vec.h:23
constexpr decltype(auto) z(bbm::vec3d< T > &v)
Definition: vec.h:26
constexpr decltype(auto) x(bbm::vec3d< T > &v)
Definition: vec.h:20
Definition: aggregatebsdf.h:29
constexpr M transpose(const M &m)
Bring 'transpose' in the bbm namespace.
Definition: mat.h:170
bbm::detail::mat< vec3d< T >, 3 > mat3d
3D matrix
Definition: mat.h:178
mat3d< std::decay_t< T > > toGlobalShadingFrame(const vec3d< T > &normal)
Construct a local shading frame to global frame transformation given a normal direction.
Definition: shading_frame.h:26
auto toLocalShadingFrame(const vec3d< T > &normal)
Construct a global to local shading frame transformation.
Definition: shading_frame.h:55