GLSL shader embedded in C++

Hi!

I’m trying to embed a GLSL shader in my C++ code ( to avoid deploying a myShader.vsh ) as:

static std::string g_vs =
"\
    void main()\
    {\
	gl_Position = ftransform();\
	gl_TexCoord[0] = gl_MultiTexCoord0;\
    }\
";

which I pass to OpenGL API as:

const char* vv = g_vs.c_str();
glShaderSourceARB(l_shader.vs, 1, &vv, 0);

and generated the following error with glGetInfoLogARB() :

error C0000: syntax error, unexpected $end at token “<invalid atom>”
(1) : error C0501: type name expected at token “<invalid atom>”

I use WinXP SP2, Detonator 81.85 on a GF6800

Any ideas why? If I remove the TABS and spaces and newlines all is ok…

If the c_str function returns a null terminated string, that’s not it.
Then check all the \ are at the end of the line, but that should have already resulted in a C++ compiler error.
Try glGetShaderSource() to see if the string you sent is what you expected.
I would recommend to include newlines at the end of every shader source line to make the infolog output more useful.

Hmmmmmmmm… curious … if I add "
" instead of “” works ok…

Looks like GLSL compiler doesn’t like the TAB “0x9” symbol too…

I usually do like this when embedding:

static std::string g_vs =
"    void main()"
"    {"
"	gl_Position = ftransform();"
"	gl_TexCoord[0] = gl_MultiTexCoord0;"
"    }";

Or use a small tool that converts a text file into a .h file :wink:

It is even customizable :slight_smile:

 
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstdarg>

using namespace std;

FILE*		fp_in	= 0;
FILE*		fp_out	= 0;

string		in_file		= "";
string		out_file	= "";
string		array_name	= "";


void	out (const char*  format, ...)  
{
	char printBuffer[1024];
	va_list list;

	va_start(list, format);
	
	vsprintf(printBuffer, format, list);

	va_end(list);

#ifndef NDEBUG
	printf(printBuffer);
#endif

	fprintf(fp_out,printBuffer);

}

void	write_file_header()
{
	out("// header file generated by txt2h, 2003-2006 by ScottManDeath
");
	out("#ifndef  TXT_HEADER_%s
",array_name.c_str());
	out("#define  TXT_HEADER_%s
",array_name.c_str());
}

void	write_file_footer()
{
	out("#endif  // #ifdef TXT_HEADER_%s
",array_name.c_str());
}

void	make_c_string(string& in)
{

	string	out;

	for (size_t i=0; i < in.size(); ++i)
	{
		char c = in[i];
		if ( '"' == c)
			out +="\\\"";
		else if ('\\' == c)
			out +="\\\\";
		else
			out +=c;

	}
	in = out;
}


void	write_array_header()
{
	out("char %s [] =
",array_name.c_str());
}

void	write_array_footer()
{
	out(";
");
}

void	write_line(const string& line)
{
	out("\"%s\
\"
",line.c_str());

}
int main(int argc, char** args)
{
	if(argc != 4)
	{
		printf("syntax error, usage :  txt2h array_name infile outfile");
		exit(0xff);
	}

	array_name = args[1];
	in_file = args[2];
	out_file = args[3];
	
	if (fp_in = fopen (in_file.c_str(),"rt"))
	{
		if( fp_out = fopen (out_file.c_str(),"wt"))
		{
			write_file_header();
			out("

");

			write_array_header();

			char buff[1024];
			while (fgets(buff,sizeof(buff),fp_in))
			{
				string s(buff);
				s = s.substr(0,s.find('
'));

				make_c_string(s);

				write_line(s);

			}
			write_array_footer();
			out("

");
			write_file_footer();

			fclose(fp_out);
		}
		else
		{
			printf("error opening %s
",out_file.c_str());
			exit(0xff);
		}
		fclose(fp_in);
	}
	else
	{
		printf("error opening %s
",in_file.c_str());
		exit(0xff);
	}


#ifndef NDEBUG
	printf("press the <any> key to exit...
");
	getchar();
#endif
}

 

Originally posted by santyhammer:
[b]Hi!

I’m trying to embed a GLSL shader in my C++ code ( to avoid deploying a myShader.vsh ) as:

static std::string g_vs =
"\
    void main()\
    {\
	gl_Position = ftransform();\
	gl_TexCoord[0] = gl_MultiTexCoord0;\
    }\
";

[/b]
Your last backslash… means C expects something the next line… but you gave nothing but the end of a string…

I think the problem here is that the shader is not terminated with a newline character.

The C string constant you posted will be compiled into a single line string without a newline at the end, and I think some GLSL implementations don’t like this.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.