我有一个准确的初始化程序,我在其中创建一个OpenGL程序并附加几个着色器。一切都达到了我尝试创建属性的程度。简单地说 - glGetAttribLocation
会返回-1
。
网上没有多少关于此值的可能原因。
// Window.h
GLuint glutProgram;
GLint glutCoordinateAttribute;
// Window.cpp
window.glutProgram = glCreateProgram();
// Shaders
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
// Vertex shader
const char *vsSource =
"#version 120\n"
"attribute vec2 coord2d;\n"
"void main(void) {\n"
" gl_Position = vec4(coord2d, 0.0, 1.0);\n"
"};";
// Fragment shader
const char *fsSource =
"#version 120\n"
"void main(void) {\n"
" gl_FragColor[0] = 0.2;\n"
" gl_FragColor[1] = 0.2;\n"
" gl_FragColor[2] = 0.2;\n"
"};";
glShaderSource(vs, 1, &vsSource, NULL);
glShaderSource(fs, 1, &fsSource, NULL);
glCompileShader(vs);
glCompileShader(fs);
GLint compileError = GL_FALSE;
glGetShaderiv(vs, GL_COMPILE_STATUS, &compileError);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compileError);
// Link resources
glAttachShader(window.glutProgram, vs);
glAttachShader(window.glutProgram, fs);
glLinkProgram(window.glutProgram);
glGetProgramiv(window.glutProgram, GL_LINK_STATUS, &linkError);
glGetProgramiv(window.glutProgram, GL_ATTACHED_SHADERS, &linkError);
const char *name = "coord2d";
std::cout << "window.glutCoordinateAttribute: " << window.glutCoordinateAttribute << std::endl;
window.glutCoordinateAttribute = glGetAttribLocation(window.glutProgram, name);
std::cout << "window.glutCoordinateAttribute: " << window.glutCoordinateAttribute << std::endl;
执行时执行以下程序:
window.glutCoordinateAttribute: 0
window.glutCoordinateAttribute: -1
答案 0 :(得分:0)
感谢@derhass发表的评论,我发现了这个问题。我正在读取着色器编译状态的返回值错误。事实证明它们没有编译,因此编译器无法找到我的命名属性。
我的着色器代码的问题是;
阻止后的额外main
。
const char *vsSource =
"#version 120\n"
"attribute vec2 coord2d;\n"
"void main(void) {\n"
" gl_Position = vec4(coord2d, 0.0, 1.0);\n"
"};"; // <- this is the problem
从引号内删除它最终得到了着色器进行编译。我不知道为什么那个分号在那里很重要,因为大多数语言都忽略了空语句。
这是两个着色器之一的最终工作版本。
const char *vsSource =
"#version 120\n"
"attribute vec2 coord2d;\n"
"void main(void) {\n"
" gl_Position = vec4(coord2d, 0.0, 1.0);\n"
"}";