C++ GLGetAttributeLocation返回-1,为什么?
我有一个barebones glut初始化器,在其中我创建了一个OpenGL程序并附加了几个着色器。在我尝试创建一个属性之前,一切都正常。简单地说-C++ GLGetAttributeLocation返回-1,为什么?,c++,opengl,C++,Opengl,我有一个barebones glut初始化器,在其中我创建了一个OpenGL程序并附加了几个着色器。在我尝试创建一个属性之前,一切都正常。简单地说-glGetAttriblLocation返回-1 关于这个值的可能原因,网络上没有太多可读的内容 // Window.h GLuint glutProgram; GLint glutCoordinateAttribute; // Window.cpp window.glutProgram = glCreate
glGetAttriblLocation
返回-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;
感谢@derhass发布的评论,我发现了这个问题。我读取着色器编译状态的返回值时出错。结果是它们没有编译,因此编译器找不到我的命名属性 我的着色器代码的问题是额外的
代码>在主块之后
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
我会在每次操作后检查状态机的错误状态。类似于:如果(glGetError()!=GL_NO_ERROR){fprint(stderr,“第%d\n行出错”,“第%d\n行出错”)}
是否检查编译器错误和链接错误?-1表示编译器决定不使用该属性或编译程序失败,您也从不检查编译器错误或链接错误
标记以前的注释。这可能是因为编译器决定不使用该属性
@MorganWilde:“我确实检查了编译器错误和链接错误,两者都为零。”。请注意,这些值将被解释为布尔值,零表示GL_FALSE
,因此它根本没有编译/链接。
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"
"}";