简体   繁体   English

OpenGL 着色器无法编译

[英]OpenGL Shader fails to compile

I'm currently trying to get into OpenGL.我目前正在尝试进入 OpenGL。 Unfortunately my shaders always fail to compile.不幸的是,我的着色器总是无法编译。 The textfiles storing the shaders are successfully read into the string and passed to the glShaderSource - function.存储着色器的文本文件成功读入字符串并传递给 glShaderSource - 函数。 Since everything up to this point works i think my error is somewhere in the shader files themselves (vertex shader, fragment shader).由于到目前为止一切正常,我认为我的错误出在着色器文件本身(顶点着色器、片段着色器)中。 I tried to output the error code, but i only get strange symbols.我试图输出错误代码,但我只得到奇怪的符号。

    void GLSLProgram::compileShaders(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilepath)
{
    _programID = glCreateProgram();
    _vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    _fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    if (_vertexShaderID == 0){
        fatalError("Vertex shader failed to be created !");
    }
    _fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    if (_fragmentShaderID == 0){
        fatalError("Fragment shader failed to be created !");
    }

    compileShader(vertexShaderFilePath, _vertexShaderID);
    compileShader(fragmentShaderFilepath, _fragmentShaderID);

    return;
}

void GLSLProgram::compileShader(const std::string& filePath, GLuint& id)
{
    std::ifstream vertexFile(filePath); 

    if (vertexFile.fail()){
        fatalError("Failed to open " + filePath);
        perror(filePath.c_str());
    }

    std::string fileContents = "";
    std::string line;

    while (std::getline(vertexFile, line)){
        fileContents.append(line + '\n');
    }

    vertexFile.close();
    const GLchar* contentsPtr = fileContents.c_str();
    glShaderSource(id, 1, &contentsPtr, nullptr); 

    glCompileShader(id);

    GLint success = 0;
    glGetShaderiv(id, GL_COMPILE_STATUS, &success); //returns success of most recent compilation
    if (success == GL_FALSE)
    {
        GLint maxLength = 0;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);

        // The maxLength includes the NULL character
        std::vector<char> errorLog(maxLength);
        glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);

        // Provide the infolog in whatever manor you deem best.
        // Exit with failure.
        glDeleteShader(id); // Don't leak the shader.

        std::printf("%s\n", errorLog);
        fatalError("shader " + filePath + "failed to compile");

    }
}

Fragment shader:片段着色器:

#version 130

out vec3 color;

void main(){
    color = vec3(1.0, 0.0, 0.0);
}

Vertex Shader:顶点着色器:

#version 130

in vec2 vertexPosition;

void main() {
    gl_Position.xy = vertexPosition;
    gl_Positionsition.z = 0.0;
    gl_Position.w = 1.0;
}

Your printf is broken.你的 printf 坏了。 errorLog is passed as a vector< char > type which is not a valid type when you write "%s" in the format string. errorLog 作为 vector<char> 类型传递,当您在格式字符串中写入 "%s" 时,该类型不是有效类型。

Instead of:代替:

std::printf("%s\n", errorLog);

...write: ...写:

std::printf("%s\n", &(errorLog[0]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM