簡體   English   中英

我的着色器初始化代碼有什么問題?

[英]What is wrong with my shader initialization code?

我寫了這個小函數來初始化shader同時嘗試使GLSL掛起。

void createShader(string code, GLuint type) {
    GLint success;
    GLuint errorLogSize = 1024;
    vector<GLchar> errorLog(errorLogSize);

    cout << "trying to add shader, shader version is " << glGetString(GL_SHADING_LANGUAGE_VERSION) << " and opengl version is " << glGetString(GL_VERSION) << endl;

    GLuint program = glCreateProgram();
    GLuint obj = glCreateShader(type);
    if (obj == 0) {
        cout << "failed to create shader" << endl;
        return;
    } else {
        cout << "created shader" << endl;
    }

    const GLchar* p = code.c_str();
    GLint length = strlen(code.c_str());

    cout << "trying to compile shader:" << endl << p << endl << "length: " << length << endl;
    glShaderSource(obj, 1, &p, &length);
    glCompileShader(obj);
    glGetShaderiv(obj, GL_COMPILE_STATUS, &success);
    if (success == 0) {
        glGetProgramInfoLog(program, errorLogSize, NULL, &errorLog[0]);
        cout << "error in shader compiling" << endl;
        for (auto c : errorLog) cout << c;
        cout << endl;
        glGetShaderInfoLog(program, errorLogSize, NULL, &errorLog[0]);
        for (auto c : errorLog) cout << c;
        cout << endl;
    }

    glAttachShader(program, obj);

    glLinkProgram(program);
    glGetProgramiv(program, GL_LINK_STATUS, &success);
    if (success == 0) {
        glGetProgramInfoLog(program, errorLogSize, NULL, &errorLog[0]);
        cout << "error in shader linking" << endl;
        for (auto c : errorLog) cout << c;
        cout << endl;
        glGetShaderInfoLog(program, errorLogSize, NULL, &errorLog[0]);
        for (auto c : errorLog) cout << c;
        cout << endl;
    }

    glValidateProgram(program);
    glGetProgramiv(program, GL_VALIDATE_STATUS, &success);
    if (success == 0) {
        glGetProgramInfoLog(program, errorLogSize, NULL, &errorLog[0]);
        cout << "error in shader validating" << endl;
        for (auto c : errorLog) cout << c;
        cout << endl;
        glGetShaderInfoLog(program, errorLogSize, NULL, &errorLog[0]);
        for (auto c : errorLog) cout << c;
        cout << endl;
    }

    glUseProgram(program);
}

我這樣稱呼它:

createShader("#version 150 out vec4 colorOut; void main() { colorOut = vec4(1.0, 0.0, 0.0, 1.0); }", GL_FRAGMENT_SHADER);

當我運行程序時,它輸出以下內容:

trying to add shader, shader version is 4.30 and opengl version is 4.4.12874 Compatibility Profile Context 14.100.0.0
created shader
trying to compile shader:
#version 150 out vec4 colorOut; void main() { colorOut = vec4(1.0, 0.0, 0.0, 1.0); }
length: 84
error in shader compiling






















error in shader linking
Fragment shader(s) were not successfully compiled before glLinkProgram() was called.  Link failed.










Fragment shader(s) were not successfully compiled before glLinkProgram() was called.  Link failed.










error in shader validating
Fragment shader(s) were not successfully compiled before glLinkProgram() was called.  Link failed.










Fragment shader(s) were not successfully compiled before glLinkProgram() was called.  Link failed.

因此,這幾乎告訴我的是它無法編譯。 但我似乎無法找出原因。 我嘗試搜索類似的情況(編譯時出錯,但日志中沒有任何內容),但找不到任何相關內容。

我希望這段代碼至少可以編譯着色器。 以此指南為指導。 我使用的代碼幾乎是從該指南中復制的一對一。 唯一的不同是他們用兩種不同的功能實現了該功能,並且在嘗試找出代碼的問題時,我添加了一些額外的錯誤處理。

我正在使用freeglut初始化窗口,並包含,鏈接和初始化了問題。

#version 150的末尾需要換行符( \\n )。 #開頭的所有內容都是預處理程序指令,並且預處理程序逐行運行。

更改為此,它應該工作:

createShader("#version 150\n out vec4 colorOut; void main() { colorOut = vec4(1.0, 0.0, 0.0, 1.0); }", GL_FRAGMENT_SHADER);

如果獲取着色器信息日志的代碼中沒有小錯誤,那么您可能會從着色器編譯器中看到或多或少有意義的錯誤消息:

glGetShaderInfoLog(program, errorLogSize, NULL, &errorLog[0]);

glGetShaderInfoLog()的第一個參數是着色器,而不是程序。 在提供的代碼中使用變量命名時,應為:

glGetShaderInfoLog(obj, errorLogSize, NULL, &errorLog[0]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM