GLSL built-in function
pow()
Returns x raised to the power y. On values between 0.0 and 1.0 it bends a straight ramp into a curve.
Syntax
float pow(float x, float y)
vec2 pow(vec2 x, vec2 y)
vec3 pow(vec3 x, vec3 y)
vec4 pow(vec4 x, vec4 y)Parameters
- x
- The base. Must be greater than or equal to 0.
- y
- The exponent. Above 1.0 pushes values down, below 1.0 pulls them up.
Return value
x to the power y, with the same type as x.
Live example
Edit the code and the preview updates as you type.
How it works
For inputs between 0 and 1, pow never changes the two ends: pow(0.0, k) is 0 and pow(1.0, k) is 1. It only bends the middle:
| exponent | pow(0.5, k) | effect |
|---|---|---|
| 4.0 | 0.0625 | stays low for longer, then shoots up |
| 2.0 | 0.25 | gentle ease in |
| 1.0 | 0.5 | straight line |
| 0.5 | 0.707 | rises fast, then flattens |
The example sweeps the exponent over time and draws the result in green against the straight line y = x in gray.
Typical uses
- Specular highlights:
pow(max(dot(r, v), 0.0), 32.0), where a higher exponent gives a smaller, sharper highlight. - Fresnel rim light:
pow(1.0 - dot(n, v), 5.0)lights up the edges of a sphere. - Shaping gradients and easing:
pow(t, 2.0)for a slow start. - Gamma correction:
pow(color, vec3(1.0 / 2.2)).
Common mistakes
- Passing a negative base.
pow(-0.5, 2.0)is undefined in GLSL and often returns NaN (shown as black), even though the math answer is 0.25. Usex * x, orpow(abs(x), k). pow(0.0, 0.0)or a zero base with a negative exponent, which are also undefined.- Using
pow(x, 2.0)in hot code.x * xis clearer and cheaper.
Related functions
sqrt()
Returns the square root of x. For values between 0.0 and 1.0 it lifts the low end, rising fast and then flattening.
exp2()
Returns 2 raised to the power x. Doubling and halving per step, as in noise octaves and exposure stops.
smoothstep()
Returns 0.0 below edge0, 1.0 above edge1, and a smooth S-shaped curve in between.
Tutorials that use pow()
- Contrast with pow()Apply pow() to reshape a gradient curve.
- Rim LightLearn core lighting terms (diffuse/specular/rim) on a simple sphere.
- Phong Lighting ModelImplement the basic Phong lighting model to simulate ambient, diffuse, and specular highlights on object surfaces.
- Material Lighting CompareCompare Lambert, Phong, Blinn-Phong, and rim-light effects in one scene to understand their visual differences.
- Blinn-PhongLearn core lighting terms (diffuse/specular/rim) on a simple sphere.
- FresnelMake the edges of a sphere glow with the Fresnel term pow(1.0 - dot(n, v), 5.0).