section_logo

GLSL code stringify

05-08-2009 at 20:29:41 | 3 comments

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();
});

More info

05-08-2009 at 20:29:41 | 3 comments

3 comments


#1 posted by r3D at 2009-08-05 11:41:40

Cool

#2 posted by iq at 2009-08-05 14:09:58

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
"}";

#3 posted by pplux at 2009-08-05 15:58:43

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.

leave a comment


name

your email (will not be show)

comment

security code