无法将着色器链接到OpenGL中的程序对象,无法调试

6tdlim6h  于 2023-10-18  发布在  其他
关注(0)|答案(2)|浏览(103)

我附加了一个顶点和片段着色器到一个程序对象,然后尝试链接该程序。GL_LINK_STATUS返回false。我查了信息日志,全是些乱七八糟的字符。我检查GL_INFO_LOG_LENGTH,它是0。我如何调试这种情况?

//this is the end of my LoadBasicShaders function
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);

glLinkProgram(program);

GLint status;

glGetProgramiv(program, GL_LINK_STATUS, &status);

if (status == GL_FALSE)
{
    GLint logLength = 0;
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
    std::vector<GLchar> log(logLength + 1);
    glGetProgramInfoLog(program, logLength, &logLength, &log[0]);

    fprintf(stderr, "%s\n\n", log);
    //logLength returns 0, log returns seemingly random chars

    return -3;//just my error code
}

我的着色器是最简单的,因为我刚刚开始。下面是顶点着色器:

#version 330

layout(location = 0) in vec4 position;
void main()
{
    gl_Position = position;
}

这是片段着色器:

#version 330

out vec4 outputColor;
void main()
{
    outputColor = vec4(1.0, 1.0, 1.0, 1.0);
}

我使用GLFW创建OpenGL窗口,GLEW加载函数:

if (!glfwInit())
{/*error checking*/}

    window = glfwCreateWindow(800, 600, "Swash", NULL, NULL);

if (!window)
{/*more error checking*/}

glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);

if (glewInit() != GLEW_OK)
{/*you guessed it*/}
//don't call anything that involves nonstandard OpenGL functions before this point

GLuint shaderProgram;

int shaderLoadResult = LoadBasicShaders(shaderProgram, "../res/vert.shader", "../res/frag.shader");
u4dcyp6a

u4dcyp6a1#

所有这些(这就是我发布MCVE的意思):

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cstdlib>

struct GlDebugOutput
{
    static void Install()
    {
        if( !glewIsSupported( "GL_ARB_debug_output" ) ) 
        {
            std::cerr << "GL_ARB_debug_output not supported" << std::endl;
            return;
        }
        glEnable( GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB );
        glDebugMessageCallbackARB( DebugCallback, 0 );
        glDebugMessageControlARB( GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE );
    }

private:
    static const char* Source( GLenum source )
    {
        switch( source )
        {
        case GL_DEBUG_SOURCE_API_ARB                : return "API";
        case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB      : return "WINDOW_SYSTEM";
        case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB    : return "SHADER_COMPILER";
        case GL_DEBUG_SOURCE_THIRD_PARTY_ARB        : return "THIRD_PARTY";
        case GL_DEBUG_SOURCE_APPLICATION_ARB        : return "APPLICATION";
        case GL_DEBUG_SOURCE_OTHER_ARB              : return "OTHER";
        default                                     : return "Unknown source";
        }
    }

    static const char* Type( GLenum type )
    {
        switch( type )
        {
        case GL_DEBUG_TYPE_ERROR_ARB                : return "ERROR";
        case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB  : return "DEPRECATED_BEHAVIOR";
        case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB   : return "UNDEFINED_BEHAVIOR";
        case GL_DEBUG_TYPE_PORTABILITY_ARB          : return "PORTABILITY";
        case GL_DEBUG_TYPE_PERFORMANCE_ARB          : return "PERFORMANCE";
        case GL_DEBUG_TYPE_OTHER_ARB                : return "OTHER";
        default                                     : return "Unknown type";
        }
    }

    static const char* Severity( GLenum severity )
    {
        switch( severity )
        {
        case GL_DEBUG_SEVERITY_HIGH_ARB     : return "HIGH";
        case GL_DEBUG_SEVERITY_MEDIUM_ARB   : return "MEDIUM";
        case GL_DEBUG_SEVERITY_LOW_ARB      : return "LOW";
        default                             : return "Unknown severity";
        }
    }

    static void APIENTRY DebugCallback
        (
        GLenum source,
        GLenum type,
        GLuint id,
        GLenum severity,
        GLsizei length,
        const GLchar* message,
        const void* userParam
        )
    {
        std::cerr << "GL_DEBUG" 
            << ": " << Source( source ) 
            << ": " << Type( type ) 
            << ": " << Severity( severity ) 
            << ": " << message 
            << std::endl;
    }
};

struct Program
{
    static GLuint Load( const char* vert, const char* geom, const char* frag )
    {
        GLuint prog = glCreateProgram();
        if( vert ) AttachShader( prog, GL_VERTEX_SHADER, vert );
        if( geom ) AttachShader( prog, GL_GEOMETRY_SHADER, geom );
        if( frag ) AttachShader( prog, GL_FRAGMENT_SHADER, frag );
        glLinkProgram( prog );
        CheckStatus( prog );
        return prog;
    }

private:
    static void CheckStatus( GLuint obj )
    {
        GLint status = GL_FALSE;
        if( glIsShader(obj) ) glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
        if( glIsProgram(obj) ) glGetProgramiv( obj, GL_LINK_STATUS, &status );
        if( status == GL_TRUE ) return;
        GLchar log[ 1 << 15 ] = { 0 };
        if( glIsShader(obj) ) glGetShaderInfoLog( obj, sizeof(log), NULL, log );
        if( glIsProgram(obj) ) glGetProgramInfoLog( obj, sizeof(log), NULL, log );
        std::cerr << log << std::endl;
        exit( EXIT_FAILURE );
    }

    static void AttachShader( GLuint program, GLenum type, const char* src )
    {
        GLuint shader = glCreateShader( type );
        glShaderSource( shader, 1, &src, NULL );
        glCompileShader( shader );
        CheckStatus( shader );
        glAttachShader( program, shader );
        glDeleteShader( shader );
    }
};

#define GLSL(version, shader) "#version " #version "\n" #shader

const char* vert = GLSL
( 
    330,
    layout( location = 0 ) in vec4 position;
    void main()
    {
        gl_Position = position;
    }
);

const char* frag = GLSL
( 
    330,
    out vec4 outputColor;
    void main()
    {
        outputColor = vec4( 1.0, 1.0, 1.0, 1.0 );
    }
);

GLuint VAO;
GLuint VBO;
GLuint prog;
void init()
{
    glGenVertexArrays( 1, &VAO );
    glBindVertexArray( VAO );

    glGenBuffers( 1,&VBO );
    glBindBuffer( GL_ARRAY_BUFFER, VBO );
    float verts[] =
    {
        -1.0, -1.0,
         1.0, -1.0,
         0.0,  1.0,
    };
    glBufferData( GL_ARRAY_BUFFER, sizeof( verts ), verts, GL_STATIC_DRAW );

    prog = Program::Load( vert, NULL, frag );
    glUseProgram( prog );

    glEnableVertexAttribArray( 0 );
    glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, 0 );
}

void display()
{
    glClearColor( 0, 0, 0, 1 );
    glClear( GL_COLOR_BUFFER_BIT );

    glUseProgram( prog );
    glBindVertexArray( VAO );
    glDrawArrays( GL_TRIANGLES, 0, 3 );
}

void glfwErrorCallback( int error, const char* description )
{
    std::cerr << "GLFW error: " << description << std::endl;
}

int main( int argc, char** argv )
{
    glfwSetErrorCallback( glfwErrorCallback );

    if( GL_FALSE == glfwInit() )
    {
        return EXIT_FAILURE;
    }
    std::cout << "GLFW version : " << glfwGetVersionString() << std::endl;

    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE );
    glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
    GLFWwindow* window = glfwCreateWindow( 640, 480, "Test", NULL, NULL );
    if( NULL == window )
    {
        glfwTerminate();
        return EXIT_FAILURE;
    }
    glfwMakeContextCurrent( window );

    glfwSwapInterval( 1 );

    glewExperimental = GL_TRUE;
    const GLenum glewErr = glewInit();
    if( GLEW_OK != glewErr )
    {
        std::cerr << "glewInit() failed: " << glewGetErrorString( glewErr ) << std::endl;
        glfwTerminate();
        return EXIT_FAILURE;
    }
    // consume spurious GL error from GLEW init
    glGetError();

    GlDebugOutput::Install();

    init();

    while( !glfwWindowShouldClose( window ) )
    {
        glfwPollEvents();

        int w, h;
        glfwGetFramebufferSize( window, &w, &h );
        glViewport( 0, 0, w, h );

        display();

        glfwSwapBuffers( window );
    }

    return EXIT_SUCCESS;
}

如果你的OpenGL实现和/或着色器有任何问题,程序应该对stderr尖叫血腥谋杀并退出。

fnvucqvd

fnvucqvd2#

很抱歉更新得很慢,我一直在忙碌其他的事情。
无论如何,问题是在编写代码时,我包含了

program = glCreateProgram();

但在整理的时候我不小心把它删了这就是为什么着色器在编译,但没有链接,它们没有链接到程序对象。

相关问题