如何在C++项目中组合着色器源?
How do you compose your shader source in a C++ project?
在Javascript中使用WebGL,我可以像这样编写一个片段着色器:
protected getShader(textureFunc: string, opline: string): string {
return `
float getTexel() {
return getColorAsFloat(${textureFunc}(A, TexCoords));
}
void main() {
setFragColor(${opline});
}
`;
}
在这里,我使用一堆直接粘贴在代码中的动态参数构建我的源代码。如何在C++项目中做到这一点?我正在寻找构建动态字符串而不是sprintf()
或std::cout <<
的新方法。OpenGL程序员是否使用库或技术来促进这种组合?
C++在语言中缺少字符串插值功能。类似的C++代码是:
std::string getShader(const std::string &textureFunc, const std::string &opline) const {
return R"(
float getTexel() {
return getColorAsFloat()" + textureFunc + R"((A, TexCoords));
}
void main() {
setFragColor()" + opline + R"();
}
)";
}
或者,您可以使用一些类似snprintf
格式库:
std::string getShader(const char *textureFunc, const char *opline) const {
return Sn::sprintf(R"(
float getTexel() {
return getColorAsFloat(%1$s(A, TexCoords));
}
void main() {
setFragColor(%2$s);
}
)", textureFunc, opline);
}
这里我使用我自己的printf
包装器,它返回一个std::string
,带有POSIX位置参数语法。但是您可以找到适合该工作的任何其他格式库。
C++,核心语言,因此对着色器一无所知。您几乎总是会使用库。各种可用的库不同。
我在做着色器时使用的库是SFML,它有一个方便的sf::Shader类。其他库将有其他类。或者原始OpenGL将采用原始字符串作为着色器源(通常通过库访问(。
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 混合组合和继承的C++问题
- 我需要将多个函数组合为一个函数
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 通过组合不同的类型来创建唯一的id
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 模板元编程:如何将参数包组合成新的参数包
- 检查向量是否具有所有可能的字符组合
- 如何在加密++中将两个源组合成新的源
- 根中的组合
- 更改 C++ 中的组合分类变量
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- 组合字符串不适用于 libCurl,C++
- 测试两个类型列表中的所有组合
- 将多个 for 循环组合成单个迭代器
- 如何从组合指数中找到仓位
- 防止组合框被关闭
- 如何解决可能的唯一组合问题