views:

309

answers:

1

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.

+2  A: 

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);
Bahbar
This is a valid solution, but not much better than prepending the source code.
Sean Farrell
well, it depends on what you're after. This is good to prevent some additional memory allocation and copy (which is why I would consider the prepend solution hacky). What would look better to you, and why ?
Bahbar
I thought more along the lines `glDefine("Moo", "Foo")`... but it seems I studied the specs properly...
Sean Farrell