不使用任何 Apple API 加载 GLSL 着色器

发布于 2024-12-25 03:24:34 字数 415 浏览 3 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一笑百媚生 2025-01-01 03:24:34

如果您想从应用程序包中的文件加载着色器,您可以使用 NSBundle 对象(在 Objective-C 中)或使用 CoreFoundation CFBundle 获取文件路径对象(纯 C 语言)。

无论哪种方式,您都在使用 Apple 特定的 API。使用 CFBundle 而不是 NSBundle 唯一得到的就是更多的样板代码。

如果您不想使用任何 Apple API,那么您的选择是将着色器嵌入为文字字符串,或者连接到 Internet 上的服务器并下载它们(使用 Unix 套接字 API)。

真正需要做的是定义一个接口,您的 RenderingEngine 通过该接口获取其着色器的源代码,并在您将 RenderingEngine 移植到的每个平台上使用适当的特定于平台的 API 来实现该接口。界面可以非常简单,如下所示:

RenderingEngineShaderSourceInterface.h

#ifdef __cplusplus
extern "C" {
#endif

// You are responsible for freeing the C string that this function returns.
extern char const *RenderingEngine_shaderSourceForName(char const *name);

#ifdef __cplusplus
}
#endif

然后创建 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 CoreFoundation CFBundle object (in pure C).

Either way, you are using Apple-specific APIs. The only thing you're getting by using CFBundle instead of NSBundle 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

#ifdef __cplusplus
extern "C" {
#endif

// You are responsible for freeing the C string that this function returns.
extern char const *RenderingEngine_shaderSourceForName(char const *name);

#ifdef __cplusplus
}
#endif

Then you create RenderingEngineShaderSource_Windows.cpp, RenderingEngineShaderSource_iOS.m, RenderingEngineShaderSource_Linux.cpp, etc. Each one implements RenderingEngine_shaderSourceForName using the appropriate API for that platform.

夏有森光若流苏 2025-01-01 03:24:34

我使用两种方法之一。如果它是一个短着色器,我可能只是把它放在代码中:

const char   shader[] = 
"uniform vec4 blah;\n" // Note, no semicolon here - it does the right thing
"main ()\n"
"{\n"
...rest of code
"}\n";

或者,如果它更长或要在其他地方重复使用,我会将它放入资源中的文本文件中,并在运行时读取该文本文件。您可以通过 [NSBundle pathForResource:ofType:] 访问它。

I use one of two methods. If it's a short shader, I may just put it code:

const char   shader[] = 
"uniform vec4 blah;\n" // Note, no semicolon here - it does the right thing
"main ()\n"
"{\n"
...rest of code
"}\n";

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:].

余生共白头 2025-01-01 03:24:34

考虑一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文