JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
Previous Page
Next Page

5.4. Geometric Functions

Except for ftransform, geometric functions can be used within either vertex shaders or fragment shaders. These functions operate on vectors as vectors, not in a component-wise fashion (see Table 5.4).

Table 5.4. Geometric functions

Syntax

Description

float length (float x)
float length (vec2 x)
float length (vec3 x)
float length (vec4 x)

Returns the length of vector x, i.e.,
sqrt(x[0] * x[0] + x[1] * x[1] + . . .).

float distance (float p0, float p1)
float distance (vec2 p0, vec2 p1)
float distance (vec3 p0, vec3 p1)
float distance (vec4 p0, vec4 p1)

Returns the distance between p0 and p1,
i.e., length(p0 - p1).

float dot (float x, float y)
float dot (vec2 x, vec2 y)
float dot (vec3 x, vec3 y)
float dot (vec4 x, vec4 y)

Returns the dot product of x and y, i.e.,
result = x[0] * y[0] + x[1] * y[1] + . . ..

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]

float normalize (float x)
vec2 normalize (vec2 x)
vec3 normalize (vec3 x)
vec4 normalize (vec4 x)

Returns a vector in the same direction as x but with a length of 1.

vec4 ftransform()

For vertex shaders only. This function ensures that the incoming vertex position is transformed in a way that produces exactly the same result as would be produced by OpenGL's fixed functionality transform. This function typically computes the value for gl_Position.

float faceforward (float N, float I, float Nref)
vec2 faceforward (vec2 N, vec2 I, vec2 Nref)
vec3 faceforward (vec3 N, vec3 I, vec3 Nref)
vec4 faceforward (vec4 N, vec4 I, vec4 Nref)

If dot (Nref, I) < 0.0, return N; otherwise, return -N.

float reflect (float I, float N)
vec2 reflect (vec2 I, vec2 N)
vec3 reflect (vec3 I, vec3 N)
vec4 reflect (vec4 I, vec4 N)

For the incident vector I and surface orientation N, returns the reflection direction:
result = I 2.0 * dot (N, I) * N
N
must already be normalized to achieve the desired result. I need not be normalized.

float refract (float I, float N, float eta)
vec2 refract (vec2 I, vec2 N, float eta)
vec3 refract (vec3 I, vec3 N, float eta)
vec4 refract (vec4 I, vec4 N, float eta)

For the incident vector I and surface normal N and the ratio of indices of refraction eta, returns the refraction vector. The returned result is computed as

k = 1.0 - eta * eta *
      (1.0 - dot (N, I) * dot (N, I))
if (k < 0.0)
    result = 0.0;
    // result type is float or vec2/3/4
 else
   result = eta * I-
              (eta * dot (N, I) * sqrt (k)) * N


The input parameters for the incident vector I and surface normal N must already be normalized to achieve the desired result.


The float version of the distance function may not seem terribly useful (it's the same as the absolute value of the difference), but the vector forms compute the Euclidean distance between two points. Similarly, the float version of normalize always returns 1, and the float version of length always returns the absolute value of the input argument as the result. These scalar forms are useful in that the data type of the argument can be changed without the need to change the code that calls the built-in function.

You can use the ftransform function to transform the incoming vertex position:

gl_Position = ftransform()

It transforms the value of gl_Vertex by the current modelview-projection matrix to produce a value for gl_Position that is identical to what would have been computed by the fixed functionality pipeline. This function should be used, for example, when an application is rendering the same geometry in separate passes, where one pass uses the fixed functionality path to render and another pass uses the programmable processors.


Previous Page
Next Page




JavaScript EditorAjax Editor     JavaScript Editor