R2 Core Shaders

R2Normals.h
Go to the documentation of this file.
1 #ifndef R2_NORMALS_H
2 #define R2_NORMALS_H
3
4 /// \file R2Normals.h
5 /// \brief Functions for transforming normal vectors.
6 ///
7
8 ///
9 /// Compress the (normalized) vector \a n into two elements
10 /// using a spheremap transform (Lambert Azimuthal Equal-Area).
11 ///
12 /// @param n A normal vector
13 /// @return A compressed normal vector
14 ///
15
16 vec2
17 R2_normalsCompress(
18 const vec3 n)
19 {
20 float p = sqrt ((n.z * 8.0) + 8.0);
21 float x = (n.x / p) + 0.5;
22 float y = (n.y / p) + 0.5;
23 return vec2 (x, y);
24 }
25
26 ///
27 /// Decompress the given vector, assuming it was encoded with
28 /// a spheremap transform (Lambert Azimuthal Equal-Area).
29 ///
30 /// @param n The compressed normal vector
31 /// @return A decompressed normal vector
32
33 vec3
34 R2_normalsDecompress(
35 const vec2 n)
36 {
37 vec2 fn = vec2 (
38 (n.x * 4.0) - 2.0,
39 (n.y * 4.0) - 2.0
40 );
41 float f = dot (fn, fn);
42 float g = sqrt (1.0 - (f / 4.0));
43 float x = fn.x * g;
44 float y = fn.y * g;
45 float z = 1.0 - (f / 2.0);
46 return vec3 (x, y, z);
47 }
48
49 ///
50 /// Compute a bitangent vector given a normal \a n and tangent vector \a t,
51 /// assuming a sign [-1.0, 1.0] stored in \a t.w.
52 ///
53 /// @param n A normal vector
54 /// @param t A tangent vector
55 /// @return A bitangent vector
56
57 vec3
58 R2_normalsBitangent(
59 const vec3 n,
60 const vec4 t)
61 {
62 vec3 p = cross (n, t.xyz);
63 return p * t.w;
64 }
65
66 ///
67 /// Unpack a tangent-space normal vector from the texture \a map.
68 ///
69 /// @param map A normal map texture
70 /// @param uv The UV coordinates
71 ///
72
73 vec3
74 R2_normalsUnpack(
75 const sampler2D map,
76 const vec2 uv)
77 {
78 vec3 rgb = texture (map, uv).xyz;
79 return (rgb * 2.0) + (-1.0);
80 }
81
82 ///
83 /// Transform the tangent-space vector \a m into the coordinate
84 /// system given by the orthonormal vectors {t, b, n}.
85 ///
86 /// @param m A tangent-space vector
87 /// @param t A tangent vector
88 /// @param b A bitangent vector
89 /// @param n A normal vector
90
91 vec3
92 R2_normalsTransform(
93 const vec3 m,
94 const vec3 t,
95 const vec3 b,
96 const vec3 n)
97 {
98 mat3x3 mat = mat3x3 (t, b, n);
99 return normalize (mat * m);
100 }
101
102 ///
103 /// Given a texture consisting of normal vectors \a t_normal, object-space
104 /// normal \a n, object-space tangent \a t, object-space bitangent \a b,
105 /// and texture coordinates \a uv, unpack and transform a
106 /// vector from the texture, resulting in an object-space peturbed normal.
107 ///
108 /// @param t_normal A normal map texture
109 /// @param n A normal vector
110 /// @param t A tangent vector
111 /// @param b A bitangent vector
112 /// @param uv A set of UV coordinates
113
114 vec3
115 R2_normalsBumpLocal(
116 const sampler2D t_normal,
117 const vec3 n,
118 const vec3 t,
119 const vec3 b,
120 const vec2 uv)
121 {
122 vec3 m = R2_normalsUnpack (t_normal, uv);
123 vec3 nn = normalize (n);
124 vec3 nt = normalize (t);
125 vec3 nb = normalize (b);
126 return R2_normalsTransform (m, nt, nb, nn);
127 }
128
129 ///
130 /// Given a texture consisting of normal vectors \a t_normal,
131 /// an object-to-eye-space matrix \a m_normal, object-space
132 /// normal \a n, object-space tangent \a t, object-space bitangent \a b,
133 /// and texture coordinates \a uv, unpack and transform a
134 /// vector from the texture, and transform it to eye-space.
135 ///
136 /// @param t_normal A normal map texture
137 /// @param m_normal A object-to-eye-space matrix
138 /// @param n A normal vector
139 /// @param t A tangent vector
140 /// @param b A bitangent vector
141 /// @param uv A set of UV coordinates
142
143 vec3
144 R2_normalsBump(
145 const sampler2D t_normal,
146 const mat3x3 m_normal,
147 const vec3 n,
148 const vec3 t,
149 const vec3 b,
150 const vec2 uv)
151 {
152 vec3 nl = R2_normalsBumpLocal (t_normal, n, t, b, uv);
153 return normalize (m_normal * nl);
154 }
155
156 #endif // R2_NORMALS_H
R2_normalsBumpLocal
vec3 R2_normalsBumpLocal(const sampler2D t_normal, const vec3 n, const vec3 t, const vec3 b, const vec2 uv)
Definition: R2Normals.h:115
R2_normalsBitangent
vec3 R2_normalsBitangent(const vec3 n, const vec4 t)
Definition: R2Normals.h:58
R2_normalsTransform
vec3 R2_normalsTransform(const vec3 m, const vec3 t, const vec3 b, const vec3 n)
Definition: R2Normals.h:92
R2_normalsUnpack
vec3 R2_normalsUnpack(const sampler2D map, const vec2 uv)
Definition: R2Normals.h:74
R2_normalsDecompress
vec3 R2_normalsDecompress(const vec2 n)
Definition: R2Normals.h:34
R2_normalsCompress
vec2 R2_normalsCompress(const vec3 n)
Definition: R2Normals.h:17
R2_normalsBump
vec3 R2_normalsBump(const sampler2D t_normal, const mat3x3 m_normal, const vec3 n, const vec3 t, const vec3 b, const vec2 uv)
Definition: R2Normals.h:144