我目前正在尝试使用OpenGl和C++编写基本的图形引擎。这样做时,OpenGl表现得很奇怪,没有明显的解释。它声称着色器无法编译,但是随后拒绝给出任何解释原因的错误。具体来说,当我调用glGetProgramiv时,返回值为0,但是glGetProgramInfoLog给出了一个空字符串¹。

这是一个最小的完整可验证示例:

#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <sstream>
#include <stdexcept>
#include <fstream>

GLuint loadShader(const std::string &path, GLenum type);

int main(int argc, char *argv[]){
    //show window & other housekeeping
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_Window *window = SDL_CreateWindow("test window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    SDL_GLContext glContext = SDL_GL_CreateContext(window);
    SDL_GL_MakeCurrent(window, glContext);
    GLenum c = glewInit();
    if(c != GLEW_OK){
        std::ostringstream sout;
        sout << "Failed to initialize OpenGl: " << glewGetErrorString(c);
        throw std::runtime_error(sout.str());
    }

    //**** -- BEGIN IMPORTANT BIT -- ****//
    //This is where the shaders are loaded

    //load shaders
    GLuint vertexShader = loadShader("shader.vert", GL_VERTEX_SHADER);
    GLuint fragmentShader = loadShader("shader.frag", GL_FRAGMENT_SHADER);

    //create & link program
    GLuint program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);
    glLinkProgram(program);

    //check for errors
    GLint success;
    glGetProgramiv(program, GL_LINK_STATUS, &success);
    if(!success){
        GLchar infoLog[512];
        GLint size; //gives 0 when checked in debugger
        glGetProgramInfoLog(program, 512, &size, infoLog);
        throw std::runtime_error(std::string("Failed to link shader program: ") + infoLog);
    }

    //cleanup
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    //**** -- END IMPORTANT BIT -- ****//

    //update loop
    bool isClosed = false;
    while(!isClosed){
        SDL_GL_SwapWindow(window);

        SDL_Event e;
        while(SDL_PollEvent(&e)){
            switch(e.type){
                case SDL_QUIT:
                    isClosed = true;
                    break;
            }
        }
    }
}

GLuint loadShader(const std::string &path, GLenum type){
    //load file
    std::ifstream fin(path);
    std::string line, total = "";

    while(std::getline(fin, line)){
        total += (line + "\n");
    }

    //reformat source into OpenGl's ridiculous format
    const char *cArray = total.c_str();
    const char **pointerToCArray = &cArray;

    //create shader
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, pointerToCArray, nullptr);
    glCompileShader(shader);

    //check for erros
    GLint success;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
    if(!success){
        GLchar infoLog[512];
        glGetShaderInfoLog(shader, 512, nullptr, infoLog);
        throw std::runtime_error(std::string("Failed to compile shader: ") + infoLog);
    }
}

MCVE testshader.vert:
#version 330 core

layout (location = 0) in vec3 position;

void main()
{
    gl_Position = vec4(position.x, position.y, position.z, 1.0);
}

MCVE testshader.frag:
#version 330 core

out vec4 color;

void main()
{
    color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

运行此命令时,除了短暂闪烁窗口外,我还得到以下输出:
terminate called after throwing an instance of 'std::runtime_error'
  what():  Failed to link shader program:
Aborted (core dumped)

附加说明:
  • 我正在使用Ubuntu 16.10 x64
  • 我正在使用Nvidia GTX 1070,驱动程序版本为375.39
  • SDL和glew版本分别是从libsdl2-devlibglew-dev软件包安装的版本。我正在使用pkg-config进行链接。

  • 在研究解决此错误的过程中,我主要发现了各种论坛和StackOverflow帖子,这些帖子在其他人的代码中发现了特定的错误。这些都不适用于我的代码,发现此错误后,我将我的代码与this假定有效的示例进行了比较。此示例还可以在我的计算机上编译并正确运行,从而排除了任何类型的配置错误。

    ¹:glGetProgramInfoLog不可能对外部结果无所作为,并且infoLog数组恰巧在其第一个地址处分配了一个空终止符,因为即使glGetProgramInfoLog之前的大小返回值,即使glGetProgramInfoLog的大小返回也返回0叫做。

    最佳答案

    我发现了问题:loadShader()方法中缺少返回语句。但是,正如我在尝试解决此问题时所遇到的那样,这没有帮助。因此,我建议所有 future 阅读此书的人都找到一个工作示例(即this),该示例与您尝试执行的操作类似,并将其代码的较小部分逐渐复制/粘贴到您的代码中(或Visa-反之亦然)执行一种二进制搜索来查找您的错误。

    10-04 14:27