View Full Version : Getting garbage chars when reading GLSL files

09-07-2011, 10:56 PM
Hi All .I run into the following problem.I load my shaders from files.The shader program ,when trying to compile, throws these errors for the vertex and fragment shaders:

Vertex info
0(12) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Fragment info
0(10) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

When inspecting the loaded content of the files I can see all kinds of garbage text is attached at the beginnings and the ends of the shader files.Like this one:

#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;

smooth out vec4 theColor;

void main()
gl_Position = position;
theColor = color;

I tried to change the encoding from UTF-8 top ANSI ,also tried to edit outside the visual studio but the problem still persists .Any help on this will be greatly appreciated.

Alfonse Reinheart
09-07-2011, 11:48 PM
Hey, that naming convention looks like some of my code (http://www.arcsynthesis.org/gltut/).

How are you loading the shader? Since that code doesn't look like it has any non-ASCII characters, the encoding would work just the same as UTF-8 or ASCII.

09-07-2011, 11:55 PM
:) .Indeed ,Alfonse,this is your code.I have been learning from your great manual. Look I am using my own "loader" not the one located in the framework class.And it worked till recently.Now it gives me all that garbage and I have no a single clue where the problem may come from.My loading code looks as follows:

void ShaderLoader::loadShaders(char * vertexShaderFile,char *fragmentShaderFile){

vs = loadFile(vertexShaderFile,vlen);
fs = loadFile(fragmentShaderFile,flen);

char *ShaderLoader::loadFile(char *fname,GLint &amp;fSize){
ifstream::pos_type size;
char * memblock;
string text;

// file read based on example in cplusplus.com tutorial
ifstream file (fname, ios::in|ios::binary|ios::ate);
if (file.is_open())
size = file.tellg();
fSize = (GLuint) size;
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
cout << "file " << fname << " loaded" << endl;
cout << "Unable to open file " << fname << endl;
return memblock;

Alfonse Reinheart
09-08-2011, 12:58 AM
file read based on example in cplusplus.com tutorial

That code was in the section marked "Binary files". I would be suspicious of any code that uses ios::binary when loading text files.

Really, I'd suggest going back and using the code I used.

09-08-2011, 01:12 AM
Thanks ,that is what I am going to do.

09-08-2011, 01:28 AM
Solved it as follows :
http://stackoverflow.com/questions/73446...7344802#7344802 (http://stackoverflow.com/questions/7344640/getting-garbage-chars-when-reading-glsl-files/7344802#7344802)

But still can see some garbage before "#version 330" line.Gonna switch to framework loading class.