gpt4 book ai didi

c++ - 我的着色器初始化代码有什么问题?

转载 作者:行者123 更新时间:2023-11-30 00:50:14 25 4
gpt4 key购买 nike

我在尝试掌握 GLSL 的同时编写了这个小函数来初始化 shader

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.

几乎所有这些都告诉我它无法编译。但我似乎无法找出原因。我尝试搜索类似的情况(编译错误但日志中没有任何内容),但找不到任何相关内容。

我希望这段代码至少可以编译着色器。我用了this guide作为指导。我使用的代码几乎是一对一地从该指南中复制的。唯一的区别是他们在两个不同的函数中实现了它,我在试图找出我的代码有什么问题时添加了一些额外的错误处理。

我正在使用 freeglut 来初始化我的窗口,我包含、链接和初始化了 glew。

最佳答案

#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]);

关于c++ - 我的着色器初始化代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25730697/

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