Can GLSL macro expansion do this?

fragment-shader, glsl, macros, opengl, shader

Solution

GLSL preprocessor "equals" to the standard C preprocessor. Indeed, you can reach what you want with the following preprocessor definition:

#define POW(a, b) Pow ## b ## (a)

But pay attention, since the concatenation operator (`##`) is available only starting from GLSL 1.30. Indeed using previous GLSL versions this macro will generate a compiler error.

Still wondering why you don't use the `pow` function...

Problem

Consider the following GLSL functions: ``` float Pow3 (const in float f) { return f * f * f; } float Pow4 (const in float f) { return f * f * f * f; } float Pow5 (const in float f) { return f * f * f * f * f; } ``` ... and so on. Is there a way to #define a GLSL macro that can generate n multiplications-of-f-by-itself at compile time, not using the built-in GLSL pow() function of course?

Original source

Related problems