4

问题:

一直在努力让我的代码加载外部着色器,但它不起作用。无论我如何重写代码并再次尝试,我每次都会遇到相同的错误。

着色器编译但不会链接,我在这个函数中有什么做错了吗?顺便说一句,错误捕获块工作得很好。

Fragment shader(s) failed to link,  vertex shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code.
ERROR: error(#280) Not all shaders have valid object code.

我正在使用 freeglut、x64 windows 7 和 mingw(而且我没有使用 IDE)

用法:

在我的主循环中GLuint programId = loadShader("vertex.shader", "frag.shader"); ,我启用了它glUseProgram();

GLuint loadShader(const char * vertex_file_path, const char * fragment_file_path) {

    GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    GLuint programId = glCreateProgram();

    GLint status = GL_FALSE;
    int infoLogLength;
    std::string line = "";

    //read in and compile
    std::string vertexShaderCode = "";
    std::ifstream vifs(vertex_file_path);
    if(!vifs.is_open()) {

        while(getline(vifs, line))
            vertexShaderCode += line +"\n";
        vifs.close();
    }

    const char * vertexStream = vertexShaderCode.c_str();

    std::string fragmentShaderCode = "";
    std::ifstream fifs(fragment_file_path);
    if(!fifs.is_open()) {

        while(getline(fifs, line))
            fragmentShaderCode += line +"\n";
        fifs.close();
    }

    const GLchar * fragmentStream = fragmentShaderCode.c_str();

    glShaderSource(vertexShaderId, 1, &vertexStream, NULL);
    glCompileShader(vertexShaderId);   

    glShaderSource(fragmentShaderId, 1, &fragmentStream, NULL);
    glCompileShader(fragmentShaderId);    

    glGetShaderiv(vertexShaderId, GL_COMPILE_STATUS, &status);
    glGetShaderiv(vertexShaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
    std::vector<char> vertexShaderErrorMessage(infoLogLength);
    glGetShaderInfoLog(vertexShaderId, infoLogLength, NULL, &vertexShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &vertexShaderErrorMessage[0]);

    glGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &status);
    glGetShaderiv(fragmentShaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
    std::vector<char> fragmentShaderErrorMessage(infoLogLength);
    glGetShaderInfoLog(fragmentShaderId, infoLogLength, NULL, &fragmentShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &fragmentShaderErrorMessage[0]);

    glAttachShader(programId, vertexShaderId);
    glAttachShader(programId, fragmentShaderId);
    glLinkProgram(programId);
    glUseProgram(programId);

    glGetProgramiv(programId, GL_LINK_STATUS, &status);
    glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLogLength);
    std::vector<char> programErrorMessage(std::max(infoLogLength, int(1)) );
    glGetProgramInfoLog(programId, infoLogLength, NULL, &programErrorMessage[0]);
    fprintf(stdout, "%s\n", &programErrorMessage[0]);

    glDeleteShader(vertexShaderId);
    glDeleteShader(fragmentShaderId);

    return programId;
    }

[顶点着色器]

#version 400

layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;

void main(void) {
    gl_Position = in_Position;
    ex_Color = in_Color;
}

片段着色器

#version 400

in vec4 ex_Color;
out vec4 out_Color;

void main(void) {
   out_Color = ex_Color;
}
4

1 回答 1

3

You only read the files if they failed to open. Right now you're passing empty strings to glShaderSource.

Change the is_open test (better yet, check good() instead of is_open()), and things will probably work.

Change

std::ifstream vifs(vertex_file_path);
if(!vifs.is_open()) { // <<=== BACKWARDS!

    while (getline(vifs, line))
        vertexShaderCode += line + "\n";
    vifs.close();
}

to

{
    std::ifstream vifs(vertex_file_path);
    while(getline(vifs, line))
        vertexShaderCode += line +"\n";
}

Since you're trying to read the whole file, look at some better ways in this question.

于 2012-07-03T21:59:57.937 回答