3

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 ""

Fragment info

0(10) : error C0000: syntax error, unexpected $undefined at token ""

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; }ýýýý««««««««þîþîþîþ 

The methods loading the shaders look as follows:

void ShaderLoader::loadShaders(char * vertexShaderFile,char *fragmentShaderFile){ vs = loadFile(vertexShaderFile,vlen); fs = loadFile(fragmentShaderFile,flen); } char *ShaderLoader::loadFile(char *fname,GLint &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); file.close(); cout << "file " << fname << " loaded" << endl; text.assign(memblock); } else { cout << "Unable to open file " << fname << endl; exit(1); } return memblock; } 

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.

1
  • 2
    Looks like your string isn't getting null terminated properly. Commented Sep 8, 2011 at 7:39

2 Answers 2

8

You're using C++, so I suggest you leverage that. Instead of reading into a self allocated char array I suggest you read into a std::string:

#include <string> #include <fstream> std::string loadFileToString(char const * const fname) { std::ifstream ifile(fname); std::string filetext; while( ifile.good() ) { std::string line; std::getline(ifile, line); filetext.append(line + "\n"); } return filetext; } 

That automatically takes care of all memory allocation and proper delimiting -- the keyword is RAII: Resource Allocation Is Initialization. Later on you can upload the shader source with something like

void glcppShaderSource(GLuint shader, std::string const &shader_string) { GLchar const *shader_source = shader_string.c_str(); GLint const shader_length = shader_string.size(); glShaderSource(shader, 1, &shader_source, &shader_length); } void load_shader(GLuint shaderobject, char * const shadersourcefilename) { glcppShaderSource(shaderobject, loadFileToString(shadersourcefilename)); } 
Sign up to request clarification or add additional context in comments.

Comments

4

It looks like all you have to do is allocate one more byte of memory in which you can place a null ('\0'):

... memblock = new char[1 + fSize]; file.seekg (0, ios::beg); file.read (memblock, size); file.close(); memblock[size] = '\0'; ... 

edit

I changed my code to use fSize in the array rather than size, since it is a GLint, which is just a typedef over an integer. Also, I tried this fix on my machine, and it works as far as I can tell - no junk at the beginning, and none at the end.

6 Comments

I actually tried putting memblock[size] = '\0'; before the file.close(); line and it does remove the garbage from the last line.But it adds such a garbage to the beginning of the first line instead.Also I can't write "new char[size + 1]; " because size is not of int type.It gives an error.
I forgot that size is of type pos_type, not int. However, you should usually be able to cast it to an int (unless you have GLSL of several GB!). So just write new char[1 + (int)size] instead. As far as junk at the beginning of the file, I'm not sure why that would happen.
Thanks @Ken ,I cast it and the program has compiled but as I said-now I am getting the garbage in the beginning of the file .
Cool ,thanks a lot .Even that I am still having some garbage before "#version 330" line ,now the shaders compile fine !
That's very strange that you would have garbage there - maybe there's a problem with how you're inspecting the text? It would indeed be strange if the code actually compiles with garbage mixed in!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.