JavaScript EditorFree JavaScript Editor     Ajax Editor 

Main Page
Previous Page
Next Page

3.4. Type Conversions

Explicit type conversions are performed with constructors. For example,

float f = 2.3;
bool b = bool(f);

sets b to true. This is useful for flow-control constructs, like if, which require Boolean values. Boolean constructors convert non-zero numeric values to true and zero numeric values to false.

The OpenGL Shading Language does not provide C-style typecast syntax, which can be ambiguous as to whether a value is converted to a different type or is simply reinterpreted as a different type. In fact, there is no way of reinterpreting a value as a different type in the OpenGL Shading Language. There are no pointers, no type unions, no implicit type changes, and no reinterpret casts. Instead, constructors perform conversions. The arguments to a constructor are converted to the type they are constructing. Hence, the following are allowed:

float f = float(3);  // convert integer 3 to floating-point 3.0
float g = float(b);  // convert Boolean b to floating point
vec4 v = vec4(2);    // set all components of v to 2.0

For conversion from a Boolean, true is converted to 1 or 1.0, and false is converted to a zero.

Previous Page
Next Page

JavaScript EditorAjax Editor     JavaScript Editor