R2 Core Shaders

Functions
R2Normals.h File Reference

Functions for transforming normal vectors. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Functions
vec2 | R2_normalsCompress (const vec3 n)
vec3 | R2_normalsDecompress (const vec2 n)
vec3 | R2_normalsBitangent (const vec3 n, const vec4 t)
vec3 | R2_normalsUnpack (const sampler2D map, const vec2 uv)
vec3 | R2_normalsTransform (const vec3 m, const vec3 t, const vec3 b, const vec3 n)
vec3 | R2_normalsBumpLocal (const sampler2D t_normal, const vec3 n, const vec3 t, const vec3 b, const vec2 uv)
vec3 | R2_normalsBump (const sampler2D t_normal, const mat3x3 m_normal, const vec3 n, const vec3 t, const vec3 b, const vec2 uv)

## Detailed Description

Functions for transforming normal vectors.

Definition in file R2Normals.h.

## Function Documentation

## § R2_normalsBitangent()

vec3 R2_normalsBitangent | ( | const vec3 | n,
|  | const vec4 | t
| ) |  |

Compute a bitangent vector given a normal n and tangent vector t, assuming a sign [-1.0, 1.0] stored in t.w.

Parameters

  n | A normal vector
  t | A tangent vector

Returns
  A bitangent vector

Definition at line 58 of file R2Normals.h.

61 {
62 vec3 p = cross (n, t.xyz);
63 return p * t.w;
64 }

## § 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
| ) |  |

Given a texture consisting of normal vectors t_normal, an object-to-eye-space matrix m_normal, object-space normal n, object-space tangent t, object-space bitangent b, and texture coordinates uv, unpack and transform a vector from the texture, and transform it to eye-space.

Parameters

  t_normal | A normal map texture
  m_normal | A object-to-eye-space matrix
  n | A normal vector
  t | A tangent vector
  b | A bitangent vector
  uv | A set of UV coordinates

Definition at line 144 of file R2Normals.h.

151 {
152 vec3 nl = R2_normalsBumpLocal (t_normal, n, t, b, uv);
153 return normalize (m_normal * nl);
154 }
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_normalsBumpLocal()

vec3 R2_normalsBumpLocal | ( | const sampler2D | t_normal,
|  | const vec3 | n,
|  | const vec3 | t,
|  | const vec3 | b,
|  | const vec2 | uv
| ) |  |

Given a texture consisting of normal vectors t_normal, object-space normal n, object-space tangent t, object-space bitangent b, and texture coordinates uv, unpack and transform a vector from the texture, resulting in an object-space peturbed normal.

Parameters

  t_normal | A normal map texture
  n | A normal vector
  t | A tangent vector
  b | A bitangent vector
  uv | A set of UV coordinates

Definition at line 115 of file R2Normals.h.

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 }
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_normalsCompress()

vec2 R2_normalsCompress | ( | const vec3 | n | ) |

Compress the (normalized) vector n into two elements using a spheremap transform (Lambert Azimuthal Equal-Area).

Parameters

  n | A normal vector

Returns
  A compressed normal vector

Definition at line 17 of file R2Normals.h.

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 }

## § R2_normalsDecompress()

vec3 R2_normalsDecompress | ( | const vec2 | n | ) |

Decompress the given vector, assuming it was encoded with a spheremap transform (Lambert Azimuthal Equal-Area).

Parameters

  n | The compressed normal vector

Returns
  A decompressed normal vector

Definition at line 34 of file R2Normals.h.

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 }

## § R2_normalsTransform()

vec3 R2_normalsTransform | ( | const vec3 | m,
|  | const vec3 | t,
|  | const vec3 | b,
|  | const vec3 | n
| ) |  |

Transform the tangent-space vector m into the coordinate system given by the orthonormal vectors {t, b, n}.

Parameters

  m | A tangent-space vector
  t | A tangent vector
  b | A bitangent vector
  n | A normal vector

Definition at line 92 of file R2Normals.h.

97 {
98 mat3x3 mat = mat3x3 (t, b, n);
99 return normalize (mat * m);
100 }

## § R2_normalsUnpack()

vec3 R2_normalsUnpack | ( | const sampler2D | map,
|  | const vec2 | uv
| ) |  |

Unpack a tangent-space normal vector from the texture map.

Parameters

  map | A normal map texture
  uv | The UV coordinates

Definition at line 74 of file R2Normals.h.

77 {
78 vec3 rgb = texture (map, uv).xyz;
79 return (rgb * 2.0) + (-1.0);
80 }