我试图根据变量的值调用许多函数之一.变量在运行时设置,因此CPU上的代码不起作用.由于存在大量可能性,因此使用if/switch语句会很慢.[可能不正确]
我正在寻找一种方法来将函数存储在数组中,方法是使用函数指针,或者将实际的方程式(例如texcoord.x*2)存储在数组中.
示例伪代码:
in vec2 texcoord;
out vec4 color;
int number; //Assigned a number between 0 and 300 during runtime
float func1(void) {
return texcoord.x + texcoord.y;
}
float func2(void) {
return texcoord.x*2;
}
...
float func299(void) {
return texcoord.y - 7;
}
void main(void) {
number = <something calculated during runtime>;
float output = func<number>(); // <--------------
color = vec4(output, output, output, 1);
}
Run Code Online (Sandbox Code Playgroud)