String with multiline in c++

I’m asking for an advice. I’m writing inline shaders this way in OFW.
Nice thing is you can actually read the multiline code.
Is there any downside of this kind of string declaration? maybe doesn’t work on some compilers?
Thank you

	string vertShader =
	R"(
	varying vec2 texcoord0;
	void main() {
		gl_Position = ftransform();
		texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);
	}
	)";

	string fragShader =
	R"(
	uniform int x, y, w, h;
	uniform sampler2DRect tex0;
	uniform sampler2DRect mask0;
	varying vec2 texcoord0;

	void main (void)
	{
		vec4 cor = texture2DRect(tex0, texcoord0);
//		vec4 cor = texture2DRect(tex0, texcoord0).rgb;
//		cor.a = texture2DRect(mask0, texcoord0).r;

		vec2 xy = gl_FragCoord.xy;
		int xx = int(xy.x);
		int yy = int(xy.y);
		bool limpa = false;
		if (xx > x && yy > y) {
			if (xx < (x+w) && yy < (y+h)) {
				limpa = true;
			}
		}
		gl_FragColor = limpa ? vec4(0.0, 0.0, 0.0, 0.0) : cor;
	}
	)";

usually you add a \ at the end of each line in the string.
If you dont want to make such you can use a macro, like the one named STRINGIFY in the example at ofFolder/examples/gl/multiTextureShaderExample
STRINGIFY is declared at the top of ofApp.cpp

1 Like

using R"is recommended over macros like stringify or having to use \ at the end of every line since the line breaks are also parsed directly from the code while using the traditional stringify macro or \ the final result is only one line.

R" is what we use in the core for the latest shaders we’ve added and the idea is to move everything to use that

3 Likes