我有一些关于OpenGL着色器的抽象概念,我希望可以这样使用它们:
WITH_SHADER(shader_name) {
// here will be gl commands
}
它应该自动将着色器从当前的gl上下文中绑定/解绑。
在这个复合语句之前绑定,之后解绑。
我能在C++中以某种方式构造这个宏吗?
最接近的东西可能是RAII。
创建一个名为 WithShader
的类,它封装了您的着色器:
使用示例:
{
with_shader ws(shader_name)
// use your shader
}
// binding and unbinding occured automatically, thats RAII.
注意:
RAII一般不是简单的,要注意复制和赋值构造函数。
WITH_SHADER(blah blah)
的方法是简单地声明Shader blahshader_usage;
。 - Cheers and hth. - Alf