JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
Previous Page
Next Page

5.1. Angle and Trigonometry Functions

Trigonometry functions can be used within either vertex shaders or fragment shaders. Function parameters specified as angle are assumed to be in units of radians. In no case do any of these functions result in a divide-by-zero error. If the divisor of a ratio is 0, results are undefined.

These functions all operate component-wise (see Table 5.1). The description column specifies the operation on each component.

Table 5.1. Angle and trigonometry functions

Syntax

Description

float radians (float degrees)
vec2 radians (vec2 degrees)
vec3 radians (vec3 degrees)
vec4 radians (vec4 degrees)

Converts degrees to radians and returns the result, i.e., result = p/180 · degrees.

float degrees (float radians)
vec2 degrees (vec2 radians)
vec3 degrees (vec3 radians)
vec4 degrees (vec4 radians)

Converts radians to degrees and returns the result, i.e., result = 180/p · radians.

float sin (float radians)
vec2 sin (vec2 radians)
vec3 sin (vec3 radians)
vec4 sin (vec4 radians)

The standard trigonometric sine function.
The values returned by this function range from [-1,1].

float cos (float radians)
vec2 cos (vec2 radians)
vec3 cos (vec3 radians)
vec4 cos (vec4 radians)

The standard trigonometric cosine function.
The values returned by this function range from [-1,1].

float tan (float radians)
vec2 tan (vec2 radians)
vec3 tan (vec3 radians)
vec4 tan (vec4 radians)

The standard trigonometric tangent function.

float asin (float x)
vec2 asin (vec2 x)
vec3 asin (vec3 x)
vec4 asin (vec4 x)

Arc sine. Returns an angle whose sine is x.
The range of values returned by this function is [-p/2, p/2]. Results are undefined if |x| > 1.

float acos (float x)
vec2 acos (vec2 x)
vec3 acos (vec3 x)
vec4 acos (vec4 x)

Arc cosine. Returns an angle whose cosine is x.
The range of values returned by this function is [0, p]. Results are undefined if |x| > 1.

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

Arc tangent. Returns an angle whose tangent is y/x. The signs of x and y determine what quadrant the angle is in. The range of values returned by this function is [-p, p]. Results are undefined if x and y are both 0.

float atan (float y_over_x)
vec2 atan (vec2 y_over_x)
vec3 atan (vec3 y_over_x)
vec4 atan (vec4 y_over_x)

Arc tangent. Returns an angle whose tangent is y_over_x. The range of values returned by this function is [-p/2, p/2].


In addition to their usefulness as trigonometric functions, sin and cos can be used in a variety of ways as the basis for a smoothly varying function with no cusps or discontinuities (see Figure 5.1). Such a function can be used to model waves on the surface of an object, to change periodically between two materials, to introduce a rocking motion to an object, or to achieve many other effects.

Figure 5.1. The sin and cos functions



Previous Page
Next Page




JavaScript EditorAjax Editor     JavaScript Editor