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 );
If you want to load your shaders from files in your app bundle, you can get the file paths using the
NSBundleobject (in Objective-C), or using the CoreFoundationCFBundleobject (in pure C).Either way, you are using Apple-specific APIs. The only thing you’re getting by using
CFBundleinstead ofNSBundleis 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_shaderSourceForNameusing the appropriate API for that platform.