A few of my GLSL fragment shaders share common functions and constants. I can't find any #import-like functionality in GLSL (understandably), but it'd be super-cool if I could emulate that in Xcode's build stage. It could be as simple as appending a common file to all my fragment shaders before copying them to my resources folder.
But my script-fu is weak. Anyone want to help me out or point me in the right direction?
I don't know very much about XCode, but it shouldn't be too difficult to implement in your own code.
This is exceptionally easy if you load the code line-by-line. As each line is loaded, check if is has the form #import "filename"
. If so, then load that file before continuing. Something like
this C++ code:
string loadGLSL(string fileName)
{
ifstream inputStream(fileName);
string fileContents, line;
while(inputFile.good())
{
getline(inputStream, line);
if(isImportDirective(line))
loadGLSL(getFileNamePart(line));
else
fileContents += line + '\n';
}
}
I'll let you work out the details of isImportDirective
and getFileNamePart
, but it shouldn't be too difficult at all. I also didn't pay attention to multiple #import
s of the same file - such a redundancy should be check for if you wish to achieve functionality analagous to Objective-C's #import
.
Of course, you could also load the entire file first, then search for the string #import
and then swap the required file contents for that line. Whatever you think works best.
Hope this helps.