OpenGL链接器错误,链接与未编译的着色器

时间:2015-11-24 00:53:39

标签: c++ opengl

我试图创建一个只绘制三角形的OpenGL应用程序,但是我得到了链接器错误:链接到未编译的着色器。从调用glLinkProgram:

#include <GL/glew.h>
#include <GL/glut.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <assert.h>
using namespace std;

GLuint VBO;
void draw();
void CreateVBO();
GLuint MakeShader(const string& shaderFile);
GLuint CreateProgram();
string GetCode(const string& path);

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize(768, 512);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Test");

    glutDisplayFunc(draw);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    GLenum var = glewInit();
    if(var != GLEW_OK){
        cerr << "Couldn't initialize glew" << endl;
        return -1;
    }

    CreateVBO();

    glutMainLoop();
    return 0;
}

void draw(){
    glClear(GL_COLOR_BUFFER_BIT);

    static GLuint program = CreateProgram();
    glUseProgram(program);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);
    glutSwapBuffers();
}

GLuint CreateProgram(){
    GLuint program = glCreateProgram();
    vector<GLuint> shaders;
    shaders.push_back(MakeShader("shade.frag"));
    shaders.push_back(MakeShader("shader.vert"));
    for(size_t i = 0; i!= shaders.size(); ++i){
        glAttachShader(program, shaders[i]);
    }
    glLinkProgram(program);

    GLint linked;
    glGetProgramiv(program, GL_LINK_STATUS, &linked);
    if(linked == GL_FALSE){
        GLint infoLogLength;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);

        GLchar* strInfoLog = new GLchar[infoLogLength+1];
        glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
        printf("Linker failure: %s \n", strInfoLog);
        delete strInfoLog;
    }
    return program;
}

string GetCode(const string &path){
    ifstream file(path);
    string ret = "";
    string line;
    if(file.is_open()){
        while(getline(file, line)){
            ret += line + '\n';
        }
    }
    return ret;
}

GLuint MakeShader(const string& shaderFile){
    string extension = shaderFile.substr(shaderFile.length()-4, 4);
    GLenum type;
    string shaderType;
    if(extension == "vert")
    {
        type = GL_VERTEX_SHADER;
        shaderType = "Vertex";
    }
    else if(extension == "frag")
    {
        type = GL_FRAGMENT_SHADER;
        shaderType = "Fragment";
    }

    const char* code = (GetCode(shaderFile)).c_str();
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &code, NULL);

    glCompileShader(shader);

    GLint status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if(status != GLEW_OK){
        GLint logSize;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize);
        GLchar* errorLog = new GLchar[logSize+1];
        glGetShaderInfoLog(shader, logSize, NULL, errorLog);
        printf("Compile failed in %s shader: %s \n", shaderType.c_str(), errorLog);
        delete errorLog;
    }
    return shader;
}

void CreateVBO(){
    float Vertices[12]= {
        0.0f, 0.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f
    };

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}

对不起,如果我的问题是noobish,但我有点挣扎于OpenGL

0 个答案:

没有答案