gpt4 book ai didi

c++ - 没有错误消息的 OpenGL 着色器链接错误

转载 作者:行者123 更新时间:2023-11-30 01:08:29 25 4
gpt4 key购买 nike

很长一段时间以来,我一直在努力解决这个问题,但始终无法解决问题。我只想编译/运行我的 OpenGL 程序,但我的着色器没有正确链接,而且我没有收到有用的错误消息。在一种情况下,错误纯粹是空的,而在另一种情况下,由于某种原因它是 ??y?

要创建我的程序并编译我的着色器,我有以下代码:

GLuint buildProgram(const char* vertexShaderFile, const char* fragmentShaderFile)
{
std::string vs = fileContents(vertexShaderFile);
std::string fs = fileContents(fragmentShaderFile);
return createProgram(vs.c_str(), fs.c_str());
}

着色器看起来像

#version 330
out vec4 out_color;
void main()
{
out_color = vec4(0.5, 0.5, 0.5, 0.5);
}

#version 330
in vec4 in_Position;
void main()
{
gl_Position = in_Position;
}

GLuint createProgram(const char* vertexSource, const char* fragmentSource)
{
GLuint vertexshader = createShader(vertexSource, GL_VERTEX_SHADER);
GLuint fragmentshader = createShader(fragmentSource, GL_FRAGMENT_SHADER);
GLuint program = glCreateProgram();

glAttachShader(program, vertexshader);
glAttachShader(program, fragmentshader);

glLinkProgram(program);
GLint ret;
checkShader(vertexshader, GL_LINK_STATUS, &ret, "unable to link vertex shader");
checkShader(fragmentshader, GL_LINK_STATUS, &ret, "unable to link fragment shader");

glUseProgram(program);
return program;
}

GLuint createShader(const char* source, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint isCompiled = 0;
checkShader(shader, GL_COMPILE_STATUS, &isCompiled, "shader failed to compile");
if (isCompiled == GL_FALSE)
{
glDeleteShader(shader);
return 0;
}
return shader;
}

我尝试通过以下方式从着色器生成错误消息

void checkShader(GLuint shader, GLuint type, GLint* ret, const char* onfail)
{
switch(type) {
case(GL_COMPILE_STATUS):
glGetShaderiv(shader, type, ret);
if(*ret == false) {
int infologLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
GLchar buffer[infologLength];
GLsizei charsWritten = 0;
std::cout << onfail << std::endl;
glGetShaderInfoLog(shader, infologLength, &charsWritten, buffer);
std::cout << buffer << std::endl;
}
break;
case(GL_LINK_STATUS):
glGetProgramiv(shader, type, ret);
if(*ret == false) {
int infologLength = 0;
glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
GLchar buffer[infologLength];
GLsizei charsWritten = 0;
std::cout << onfail << std::endl;
glGetProgramInfoLog(shader, infologLength, &charsWritten, buffer);
std::cout << buffer << std::endl;
}
break;
default:
break;
};
}

运行 GLuint program = buildProgram("shaders/test.vert", "shaders/test.frag"); 给我以下输出:

unable to link vertex shader

unable to link fragment shader

??y?

error build program error 502 : GL_INVALID_OPERATION

有没有人猜到为什么我的着色器编译正常但不链接?

最佳答案

您正在着色器 对象 上调用getProgramiv(...,GL_LINK_STATUS,...)。当您尝试获取有关程序(其中有只有一个)。对于 GL_LINK_STATUS 情况,以这种方式命名函数也没有意义。

关于c++ - 没有错误消息的 OpenGL 着色器链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42470649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com