[ACCEPTED]-Externally Define Preprocessor Macros in GLSL-glsl

Accepted answer
Score: 25

You don't really need to prepend it to the 4 code you loaded. That's why there are multiple 3 strings in the glShaderSourceARB API.

Something 2 like the following does what you are looking 1 for:

char *sources[2] = { "#define FOO\n", sourceFromFile };
glShaderSourceARB(shader, 2, sources, NULL);
Score: 7

Here is what I use to avoid #version problems. It 2 is Java, and not optimized for perfo, but 1 it works perfectly:

// extract shader sourcecode from file
StringBuilder shaderSource = Tools.readTextRessource(shaderSrcFile.url);

// apply defined values
int versionIndex = shaderSource.indexOf("#version");
if(versionIndex == -1) err("missing #version xxx in shader "+shaderSrcFile.name());
int defineInsertPoint = shaderSource.indexOf("\n", versionIndex)+1;

for (int i = defined.size() - 1; i >= 0; --i)
    shaderSource.insert(defineInsertPoint, "#define " + defined.get(i) + "\n");

More Related questions