不使用任何 Apple API 加载 GLSL 着色器
使用 C/C++ 加载 GLSL 着色器而不使用 Objective-C 或任何 Apple API 的好方法是什么?
我目前正在使用以下方法,该方法来自iPhone 3D编程一书,但它说不建议用于生产代码:
Simple.vsh
const char* SimpleVertexShader = STRINGIFY
(
// Shader code...
);
RenderingEngine.cpp
#define STRINGIFY(A) #A
#include "Simple.vsh"
// ...
glShaderSource( shaderHandle, 1, &SimpleVertexShader, 0 );
What is a good way to load a GLSL shader using C/C++ without using Objective-C or any Apple APIs?
I am currently using the following method, which is from the iPhone 3D Programming book, but it says that it is not recommended for production code:
Simple.vsh
const char* SimpleVertexShader = STRINGIFY
(
// Shader code...
);
RenderingEngine.cpp
#define STRINGIFY(A) #A
#include "Simple.vsh"
// ...
glShaderSource( shaderHandle, 1, &SimpleVertexShader, 0 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想从应用程序包中的文件加载着色器,您可以使用
NSBundle
对象(在 Objective-C 中)或使用 CoreFoundationCFBundle
获取文件路径对象(纯 C 语言)。无论哪种方式,您都在使用 Apple 特定的 API。使用
CFBundle
而不是NSBundle
唯一得到的就是更多的样板代码。如果您不想使用任何 Apple API,那么您的选择是将着色器嵌入为文字字符串,或者连接到 Internet 上的服务器并下载它们(使用 Unix 套接字 API)。
您真正需要做的是定义一个接口,您的 RenderingEngine 通过该接口获取其着色器的源代码,并在您将 RenderingEngine 移植到的每个平台上使用适当的特定于平台的 API 来实现该接口。界面可以非常简单,如下所示:
RenderingEngineShaderSourceInterface.h
然后创建 RenderingEngineShaderSource_Windows.cpp 、 RenderingEngineShaderSource_iOS.m 、 RenderingEngineShaderSource_Linux.cpp 等每个都使用适合该平台的 API 来实现 RenderingEngine_shaderSourceForName。
If you want to load your shaders from files in your app bundle, you can get the file paths using the
NSBundle
object (in Objective-C), or using the CoreFoundationCFBundle
object (in pure C).Either way, you are using Apple-specific APIs. The only thing you're getting by using
CFBundle
instead ofNSBundle
is more boilerplate code.If you don't want to use any Apple APIs, then your options are to embed your shaders as literal strings, or connect to a server on the Internet and download them (using the Unix socket API).
What you really need to do is define an interface by which your RenderingEngine gets the source code for its shaders, and implement that interface using the appropriate platform-specific API on each platform to which your port the RenderingEngine. The interface can be something super simple like this:
RenderingEngineShaderSourceInterface.h
Then you create
RenderingEngineShaderSource_Windows.cpp
,RenderingEngineShaderSource_iOS.m
,RenderingEngineShaderSource_Linux.cpp
, etc. Each one implementsRenderingEngine_shaderSourceForName
using the appropriate API for that platform.我使用两种方法之一。如果它是一个短着色器,我可能只是把它放在代码中:
或者,如果它更长或要在其他地方重复使用,我会将它放入资源中的文本文件中,并在运行时读取该文本文件。您可以通过 [NSBundle pathForResource:ofType:] 访问它。
I use one of two methods. If it's a short shader, I may just put it code:
Or, if it's longer or going to be re-used in other places, I'll put it into a text file in the resources and read the text file at run time. You can get to it via [NSBundle pathForResource:ofType:].
考虑一个 C++ 原始字符串文字;不需要
STRINGIFY
,因为 C++ 的新功能允许您在没有宏的情况下执行类似的操作。我会重新输入一个很好的示例,但这里是一个。
Consider a C++ raw string literal; no
STRINGIFY
is needed since the newer features of C++ allow you to do similar things without macro.I'd retype a good example but here is one.