Getting garbage characters when reading GLSL files

I ran into the following problem. I load my shaders from files. A shader program when trying to compile these errors for vertex and fragment shaders:

Vertex information

0 (12): error C0000: syntax error, unexpected $ undefined in token ""

Fragment Information

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

When checking the downloaded contents of the files, I see that all kinds of garbage text are attached at the beginning and at the ends of the shader files. Like this:

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

Shader loading methods are 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 the external visual studio, but the problem still persists. Any help on this would be greatly appreciated.

+2
source share
2 answers

It looks like all you have to do is allocate another byte of memory in which you can put zero ('\ 0'):

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

change

I changed my code to use fSize in an array, not size , since it is GLint, which is just a typedef over an integer. In addition, I tried this fix on my machine, and it works, as far as I can tell - there is no garbage in the beginning, but not in the end.

+2
source

You are using C ++, so I suggest you use this. Instead of reading into a dedicated dedicated char array, I suggest you read 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; } 

This automatically takes care of all the memory allocation and the correct demarcation - the key word is RAII: resource allocation is initialization. You can later load 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)); } 
+8
source

Source: https://habr.com/ru/post/1379829/


All Articles