What Is the Difference Between radians and degrees in GLSL?
In the OpenGL Shading Language (GLSL), the radians() and
degrees() built-in functions serve as conversion utilities
between two standard units of angular measurement. While
radians() converts an angle given in degrees into radians,
degrees() performs the exact inverse operation by
converting radians back into degrees. Understanding this difference is
essential in shader programming because all native GLSL trigonometric
functions—such as sin(), cos(), and
tan()—strictly operate in radians, whereas developer
inputs, uniform variables, and 3D modeling transformations are
frequently specified in degrees.
Mathematical Behavior and Formulas
The core distinction between the two functions lies in the mathematical conversion factor applied to the input argument:
radians(degrees_value): Multiplies the input value by \(\frac{\pi}{180.0}\) (approximately \(0.0174532925\)).degrees(radians_value): Multiplies the input value by \(\frac{180.0}{\pi}\) (approximately \(57.2957795\)).
Both functions handle floating-point values and execute element-wise across vector types.
Vector Support and Overloads
GLSL defines both radians() and degrees()
for scalar floats as well as multi-component floating-point vectors:
float radians(float degrees)/float degrees(float radians)vec2 radians(vec2 degrees)/vec2 degrees(vec2 radians)vec3 radians(vec3 degrees)/vec3 degrees(vec3 radians)vec4 radians(vec4 degrees)/vec4 degrees(vec4 radians)
When passed a vector, the conversion is applied independently to every component (\(x\), \(y\), \(z\), and \(w\)), eliminating the need for manual looping or per-component calculations.
Practical Shader Example
A common use case involves passing rotation angles in degrees from the CPU application via uniform variables, then converting those values to radians inside the vertex or fragment shader prior to constructing rotation matrices or evaluating trigonometric curves:
uniform float u_rotationInDegrees; // e.g., 45.0
uniform vec3 u_eulerRotation; // e.g., vec3(0.0, 90.0, 180.0)
void main() {
// Convert single float from degrees to radians
float angleRad = radians(u_rotationInDegrees);
float cosAngle = cos(angleRad);
float sinAngle = sin(angleRad);
// Convert full vector of angles at once
vec3 rotationInRadians = radians(u_eulerRotation);
// Perform inverse calculation if needed
vec3 backToDegrees = degrees(rotationInRadians);
}Side-by-Side Comparison
| Property | radians() |
degrees() |
|---|---|---|
| Input Unit | Degrees (\(0^\circ \text{ to } 360^\circ\)) | Radians (\(0 \text{ to } 2\pi\)) |
| Output Unit | Radians (\(0 \text{ to } 2\pi\)) | Degrees (\(0^\circ \text{ to } 360^\circ\)) |
| Multiplier | \(\frac{\pi}{180.0}\) | \(\frac{180.0}{\pi}\) |
| Primary Use Case | Prepping human-readable degree values for GLSL sin(),
cos(), tan() |
Exporting computed angular data back into standard \(0^\circ\text{–}360^\circ\) formats |
| Supported Types | float, vec2, vec3,
vec4 |
float, vec2, vec3,
vec4 |
Utilizing these built-in functions ensures hardware-optimized calculation routines and maintains clear, readable shader code when handling coordinate transformations and rotational calculations.