For my game project, I have a fog of war shader that hides out-of-view enemies:
uniform sampler2D objtexture;
uniform sampler2D fogtexture;
uniform sampler2D lighttexture;
void main()
{
// Load textures into pixels
vec4 objpixel = texture2D(objtexture, gl_TexCoord[0].xy);
vec4 fogpixel = texture2D(fogtexture, gl_TexCoord[0].xy);
vec4 lightpixel = texture2D(lighttexture, gl_TexCoord[0].xy);
// Draw objects if a lighttexture pixel is fully-transparent
// Otherwise, hide objects behind fog
bool changealpha = bool(ceil(lightpixel.a));
objpixel = vec4((lightpixel.rgb) * float(changealpha) + objpixel.rgb * float(!changealpha), lightpixel.a * float(changealpha) + objpixel.a * float(!changealpha));
objpixel = mix(objpixel, fogpixel, fogpixel.a);
gl_FragColor = objpixel;
}
When running the program from Code::Blocks, everything works fine. Running the executable, it isn't finding fog.frag file. I could include the fog.frag in the download, but I'd rather not have users cheat and edit the shader. As a solution, I tried embedding the shader in my program like the example shown
here.
After running it through a Notepad++ macro to eliminate any human error, my shader now looks like this:
const std::string shaderdata = "uniform sampler2D objtexture;" \
"uniform sampler2D fogtexture;" \
"uniform sampler2D lighttexture;" \
"" \
"void main()" \
"{" \
" // Load textures into pixels" \
" vec4 objpixel = texture2D(objtexture, gl_TexCoord[0].xy);" \
" vec4 fogpixel = texture2D(fogtexture, gl_TexCoord[0].xy);" \
" vec4 lightpixel = texture2D(lighttexture, gl_TexCoord[0].xy);" \
" " \
" // Draw objects if a lighttexture pixel is fully-transparent" \
" // Otherwise, hide objects behind fog" \
" bool changealpha = bool(ceil(lightpixel.a));" \
" objpixel = vec4((lightpixel.rgb) * float(changealpha) + objpixel.rgb * float(!changealpha), lightpixel.a * float(changealpha) + objpixel.a * float(!changealpha));" \
" objpixel = mix(objpixel, fogpixel, fogpixel.a);" \
" " \
" gl_FragColor = objpixel;" \
"}";
Unfortunately, if I compile, I get this error:
Failed to compile fragment shader:
Fragment shader failed to compile with the following errors:
ERROR: 0:1: error(#131) Syntax error: pre-mature EOF parse error
ERROR: error(#273) 1 compilation errors. No code generated
If I print out my shaderdata string, it looks just fine. If I remove all of the comments, line breaks and empty lines, I get the same exact error. Looking at the special characters in Notepad++, all I see are line breaks, tabs and spaces. Could someone please explain what I'm doing wrong?