语法错误:令牌“<eof>”GLSL与任何着色器</eof>的意外$ end

时间:2012-09-01 12:46:09

标签: c++ opengl

我已经看到了其他类似的问题,我现在通常是由于错误的着色器加载代码,但我尝试了很多不同的加载方式,我总是得到这个错误。这是我正在使用的加载代码(对于Vertex着色器,我对片段着色器执行相同操作):

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()的length参数,但是我得到了相同的错误。我在编译和链接时都会遇到这些错误

如果有帮助,我使用的是Opengl 3.3,GLEW,GLFW,Visual c + + +和Windows 7 64位

1 个答案:

答案 0 :(得分:2)

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