Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Externally Define Preprocessor Macros in GLSL

Tags:

GLSL has a full C-style preprocessor. The only thing that does not work is #include. One of the great features is that that you can used #ifdef to comment out functions and thus create one shader that can be thinned out if certain features are not used.

My Question is:

Is there a way to define a macro from C code?

There seems no way to do that with the openGL interface. The quick hack is to prepend a few lines with #define FOO before the code loaded form file. But it seems kind of backwards.

like image 425
rioki Avatar asked Mar 04 '10 10:03

rioki


2 Answers

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

Something like the following does what you are looking for:

char *sources[2] = { "#define FOO\n", sourceFromFile }; glShaderSourceARB(shader, 2, sources, NULL); 
like image 167
Bahbar Avatar answered Sep 23 '22 08:09

Bahbar


Here is what I use to avoid #version problems. It is Java, and not optimized for perfo, but 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"); 
like image 37
johan d Avatar answered Sep 22 '22 08:09

johan d