PDA

View Full Version : GLSL shader embedded in C++



santyhamer
01-30-2006, 05:19 PM
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...

Relic
01-30-2006, 11:14 PM
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.

santyhamer
01-31-2006, 02:01 PM
Hmmmmmmmm... curious .. if I add "\n\" instead of "\" works ok...

Looks like GLSL compiler doesn't like the TAB "0x9" symbol too...

PsychoLns
02-01-2006, 06:57 AM
I usually do like this when embedding:

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

ScottManDeath
02-04-2006, 12:12 AM
Or use a small tool that converts a text file into a .h file ;)

It is even customizable :)


#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\n");
out("#ifndef TXT_HEADER_%s\n",array_name.c_str());
out("#define TXT_HEADER_%s\n",array_name.c_str());
}

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

void make_c_string(string&amp; 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 [] =\n",array_name.c_str());
}

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

void write_line(const string&amp; line)
{
out("\"%s\\n\"\n",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("\n\n");

write_array_header();

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

make_c_string(s);

write_line(s);

}
write_array_footer();
out("\n\n");
write_file_footer();

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


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

jide
02-14-2006, 03:01 AM
Originally posted by santyhammer:
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;\
}\
";Your last backslash........... means C expects something the next line...... but you gave nothing but the end of a string...

Overmind
02-16-2006, 07:58 AM
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.