0

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>"
genpfault
  • 51,148
  • 11
  • 85
  • 139
norbas
  • 67
  • 2
  • 8
  • 5
    Pop open your shader file in a hex editor and make sure your text editor didn't plop down a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark). – genpfault May 26 '22 at 18:29
  • 4
    Also, don't faff around with inserting newlines via a `getline()` loop, [slurp the entire file into a string in one go](https://stackoverflow.com/a/5341145/44729). – genpfault May 26 '22 at 18:33

1 Answers1

0

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.

norbas
  • 67
  • 2
  • 8