opengl - GLSL 宏扩展可以做到这一点吗?

标签 opengl macros glsl shader fragment-shader

考虑以下 GLSL 函数:

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;
}

... 等等。有没有办法 #define 一个可以在编译时生成 n 个自身乘法的 GLSL 宏,当然不使用内置的 GLSL pow() 函数?

最佳答案

GLSL 预处理器“等于”标准 C 预处理器。实际上,您可以使用以下预处理器定义来达到您想要的效果:

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

但请注意,因为连接运算符 ( ## ) 仅从 GLSL 1.30 开始可用。事实上,使用以前的 GLSL 版本这个宏会产生一个编译器错误。

仍然想知道你为什么不使用 pow 函数......

关于opengl - GLSL 宏扩展可以做到这一点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467672/

相关文章:

GLSL double 复数幂函数

c++ - OpenGL 如何区分 VAO 中的绑定(bind)点与 glBindBufferBase 定义的绑定(bind)点?

如果不将 Transpose 设置为 GL_TRUE,OpenGL/GSL 将无法工作

node.js - NodeJS native 鼠标和键盘绑定(bind)

opengl-es - 具有可选属性的 GLSL 着色器

opengl - GL_QUERY_BY_REGION_WAIT 和 GL_QUERY_WAIT 有什么区别

c++ - 如何制作可移植且与编译器无关的 glBufferData?

windows - 我是否需要分别为每个 opengl 上下文加载函数?

macros - 带尾部反斜杠的字符串宏\

macros - 在 Common Lisp 中重命名 lambda