Graphics Reference
In-Depth Information
Table B-5
Geometric Functions
Syntax
Description
float,
length
(genType
x
)
Returns the length of vector
x,
2
2
X
[0]
+
X
[1] …
+
float,
distance
(genType
p0,
genType
p1
)
Returns the distance between
p0
and
p1
, i.e.,
length
(
p0
-
p1
)
.
float,
dot
(genType
x,
genType
y
)
Returns the dot product of
x
and
y
, i.e.,
x
[0]
* y
[0] +
x
[l]
* y
[l]
+ ...
vec3
cross
(vec3
x
, vec3
y
)
Returns the cross product of
x
and
y
, i.e.,
result
[0] =
x
[1] *
y
[2] -
y
[1] *
x
[2]
result
[1] =
x
[2] *
y
[0] -
y
[2] *
x
[0]
result
[2] =
x
[0] *
y
[1] -
y
[0] *
x
[1]
genType
normalize
(genType
x
)
Returns a vector in the same direction as
x
but with a length of 1.
Returns
x
/
length
(
x
).
genType
faceforward
(genType
N
, genType
I
, genType
N
ref
)
If
dot
(
N
ref
,
I
) < 0, return
N
; otherwise,
return -
N
.
genType
relect
(genType
I
,
genType
N
)
For the incident vector
I
and surface
orientation
N
, returns the reflection direction:
I
- 2 *
dot
(
N,
I
) *
N
N
must already be normalized to achieve the
desired result.
genType
refract
(genType
I
,
genType
N
,
float,
eta
)
For the incident vector
I
, surface normal
N
,
and ratio of indices of refraction
eta,
return the
refraction vector. The result is computed by
k = 1.0 -
eta
*
eta
*
(1.0 -
dot
(
N
,
I
)
*
dot
(
N
,
I
))
if (k < 0.0)
// genType is float, vec2,
// vec3, or vec4
return genType(0.0)
else
return
eta
*
I
- (
eta
*
dot
(
N
,
I
) +
sqrt
(k)) *
N
Input parameters for the incident vector
I
and the surface normal
N
must already be
normalized to get the desired results.