本文介绍了无法将着色器链接到OpenGL中的程序对象,无法调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将顶点和片段着色器附加到程序对象,然后尝试链接所述程序. GL_LINK_STATUS返回false.我查看了信息日志,里面有一堆乱码.我检查GL_INFO_LOG_LENGTH,它为0.如何调试这种情况?

I attach a vertex and fragment shader to a program object, then attempt to link the said program. GL_LINK_STATUS returns false. I check the info log, it's a bunch of gibberish characters. I check GL_INFO_LOG_LENGTH, it's 0. How do I debug this situation?

//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
}

我的着色器是最简单的着色器,因为我才刚刚入门.这是顶点着色器:

My shaders are the simplest ones possible, since I'm just starting out.Here's the vertex shader:

#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加载功能:

I use GLFW to create the OpenGL window, and GLEW to load the functions:

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");

推荐答案

抱歉,更新速度非常慢,我一直在忙于其他事情.

Sorry for the very slow update, I've been busy with other things.

无论如何,问题在于,在编写代码时,我已经包括了这一行

Anyways, the problem was that while writing the code, I had included the line

program = glCreateProgram();

但是有时候,在整理时,我不小心删除了它.这就是着色器正在编译但未链接的原因,它们未与程序对象链接.

but at some point, while tidying up, I accidentally deleted it. That's why the shaders were compiling but weren't linking, they weren't being linked to a program object.

这篇关于无法将着色器链接到OpenGL中的程序对象,无法调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:54