opengl 语法错误:任何着色器在标记““GLSL处出现意外的$end< EOF>

nxagd54h  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(102)

我看到过其他类似的问题,我知道这通常是由于错误的着色器加载代码,但我已经尝试了很多不同的方法来加载它们,我总是得到这个错误。下面是我目前使用的加载代码(对于顶点着色器,我对片段着色器做了同样的事情):

GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
if(VertexShaderStream.is_open()){
    std::string Line = "";
    while(getline(VertexShaderStream, Line))
        VertexShaderCode += "\n" + Line;
    VertexShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;

// Compile Vertex Shader
printf("Compiling shader : %s\n", vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);

// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);

字符串
我也试过这个:

GLuint VertexShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    std::ifstream shaderFile(vertex_file_path);
    //Error out here.
    stringstream shaderData;
    shaderData << shaderFile.rdbuf();  //Loads the entire string into a string stream.
    shaderFile.close();
    const std::string &VertexShaderCode = shaderData.str(); //Get the string stream as a std::string.
    GLint Result = GL_FALSE;
    int InfoLogLength;
    printf("Compiling shader : %s\n", vertex_file_path);
    char const * VertexSourcePointer = VertexShaderCode.c_str();
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
    glCompileShader(VertexShaderID);

    // Check Vertex Shader
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> VertexShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);


这是链接代码:

// Link the program
fprintf(stdout, "Linking program\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);

// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);


我还尝试将(GLint *)VertexShaderCode.size()而不是NULL传递给glShaderSource()的长度参数,但我得到了相同的错误。
如果它有帮助,我使用OpenGL 3.3,GLEW,GLFW,Visual c++ 2010和Windows 7 64位

46qrfjad

46qrfjad1#

你是如何将长度参数传递给glShaderSource的。你执行的类型转换应该会触发一个厚脂肪警告。正确的方法是将size()的结果放入一个变量中。然后给予指向该变量的指针给glShaderSource。

相关问题