OpenGL Shader Compilation Issue — Unexpected EOF

前端 未结 3 1671
误落风尘
误落风尘 2021-02-08 20:00

So I decided to try writing a simple OpenGL app using Java, just to see how it compared to my other efforts, and I\'m running into an issue where my shaders refuse to compile. T

3条回答
  •  太阳男子
    2021-02-08 20:06

    OK, now I see the problem. Your loading code doesn't work. But don't worry; a lot of people get confused when they see that glShaderSource takes an array of strings. I'm guessing that you saw someone write their shaders in C/C++ like this:

    const char *myShader[] = {
        "#version 330\n",
        "\n",
        "in vec3 in_position;\n",
        ...
    };
    

    And they uploaded the shader with, glShaderSource(shader, ARRAY_COUNT(myShader), myShader, NULL, 0);

    While this is legal, that's not really what the feature is for. Because GLSL does not have a #include mechanism, glShaderSource can take multiple strings. Each string is intended to be a shader file. The shader compiler then effectively concatenates the strings together like #include does before compiling.

    Now, because of this, you can have each line as a separate string. However, look back that that C/C++ code. See what is at the end of each line? That '\n' character.

    That's what is not at the end of the lines you are loading. Because I'm pretty sure BufferedReader.readline does not keep the end-of-line character. So your shader looks like:

    //Minimal vertex shader#version 330in vec3 in_Position;in vec3 in_Color;out vec3 var_Color;...
    

    Your entire shader is seen as one big single-line comment. Hence the unexpected EOF: OpenGL never saw anything to compile ;)

    You shouldn't be reading the file line by line. Just load the whole thing up into a single string. Then pass it to OpenGL. Alternatively, you can look at this previous answer about JOGL; it should show you how to do it correctly (though I would hope that BufferedReader would have some way to read the whole file as a string rather than line-by-line.

提交回复
热议问题