GLSL code stringify
I just started few weeks ago with GLSL and I was trying to find the best way to include shader code into my intro.
I found out many ways, the two following ways were easy to use just having a little text converter, but hard to manage changes because you always mistake while entering the separators:
GLchar* vs="void main(void)"
"{"
"gl_TexCoord[0] = gl_MultiTexCoord0;"
"gl_Position = ftransform();"
"}";
This one is even harder to read:
GLchar *vs_test ="void main()\r\n\
{\r\n\
gl_Position = ftransform();\r\n\
}";
But finally, I found a much simpler and good looking way to stringify the shader:
#define STRINGIFY(A) #A
GLchar* vs_blur=STRINGIFY(
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
});
3 comments
I keep using the per line "" version because
1. it allows indenting with zero cost.
2. it allows C comments with zero cost.
3. it allows for #ifdef and other C preprocessors
GLchar *vs_test ="void main()"
"{"
// this is a C comment wih 0 cost
"float c = 0.0;"
// note the indenting
"for( int i=0; i<4; i++ )"
"{"
"c += float( i );"
"}"
"gl_Color = vec4( f );"
"gl_Position = ftransform();"
#ifdef TESTCODE
"gl_Normal = vec(0.0,1.0,0.0);"
#endif
"}";
That's great, but... I think it won't work with "commas"(,), does it? Maybe you could use variadic macros: #define STR(...) #__VA_ARGS__ That should work with anything.
Cool