Search code examples
c++openglglsliostream

OpenGL shader reader returns garbage characters


Why does this function:

string Utils::readShaderFile(const char* filePath) {
    string content;
    ifstream fileStream(filePath, ios::in);
    string line = "";

    while (!fileStream.eof()) {
        getline(fileStream, line);
        content.append(line + "\n");
    }

    fileStream.close();
    return content;
}

returns

 ■#version 430

void main(void)
{
        if (gl_VertexID == 0) gl_Position = vec4(0.25, -0.25, 0.0, 1.0);
        else if (gl_VertexID == 1) gl_Position = vec4(-0.25, -0.25, 0.0, 1.0);
        else gl_Position = vec4(0.25, 0.25, 0.0, 1.0);
}

with some garbage character at the first line?

I believe this is the reason why I get shader compilation errors:

Vertex Shader compilation error.
Shader Info Log: 0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Solution

  • So, the problem was with the fact that I created my shader .glsl files with Command's Prompt echo "" >> shader.glsl, which set some weird character at the beginning of the file.

    After I have created the file through VS2019 add item option it worked well.