C++ OpenGL glGetError 1281 错误值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24507571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenGL glGetError 1281 bad value
提问by aze
I am using OpenGL with vertices and shaders, nothing got displayed on my screen so i used glGetError to debug : I got an error 1281(bad value) on one of my buffer called color_array_buffer, here is the section i am talking about :
我正在使用带有顶点和着色器的 OpenGL,屏幕上没有显示任何内容,所以我使用 glGetError 进行调试:我在一个名为 color_array_buffer 的缓冲区上收到错误 1281(错误值),这是我正在谈论的部分:
GLenum error = glGetError();
if(error) {
cout << error << endl;
return ;
} else {
cout << "no error yet" << endl;
}
//no error
// Get a handle for our "myTextureSampler" uniform
GLuint TextureID = glGetUniformLocation(shaderProgram, "myTextureSampler");
if(!TextureID)
cout << "TextureID not found ..." << endl;
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
sf::Texture::bind(texture);
// Set our "myTextureSampler" sampler to user Texture Unit 0
glUniform1i(TextureID, 0);
// 2nd attribute buffer : UVs
GLuint vertexUVID = glGetAttribLocation(shaderProgram, "color");
if(!vertexUVID)
cout << "vertexUVID not found ..." << endl;
glEnableVertexAttribArray(vertexUVID);
glBindBuffer(GL_ARRAY_BUFFER, color_array_buffer);
glVertexAttribPointer(vertexUVID, 2, GL_FLOAT, GL_FALSE, 0, 0);
error = glGetError();
if(error) {
cout << error << endl;
return ;
}
//error 1281
And here is the code where i link my buffer to the array :
这是我将缓冲区链接到数组的代码:
if (textured) {
texture = new sf::Texture();
if(!texture->loadFromFile("textures/simple.jpeg"/*,sf::IntRect(0, 0, 128, 128)*/))
std::cout << "Error loading texture !!" << std::endl;
glGenBuffers(1, &color_array_buffer);
glBindBuffer(GL_ARRAY_BUFFER, color_array_buffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec3), &uvs[0], GL_STATIC_DRAW);
}
and my values of uvs :
以及我对 uvs 的价值观:
uvs[0] : 0.748573-0.750412
紫外线[0]:0.748573-0.750412
uvs[1] : 0.749279-0.501284
紫外线[1]:0.749279-0.501284
uvs[2] : 0.99911-0.501077
紫外线[2]:0.99911-0.501077
uvs[3] : 0.999455-0.75038
紫外线[3]:0.999455-0.75038
uvs[4] : 0.250471-0.500702
紫外线[4]:0.250471-0.500702
uvs[5] : 0.249682-0.749677
紫外线[5]:0.249682-0.749677
uvs[6] : 0.001085-0.75038
紫外线[6]:0.001085-0.75038
uvs[7] : 0.001517-0.499994
紫外线[7]:0.001517-0.499994
uvs[8] : 0.499422-0.500239
紫外线[8]:0.499422-0.500239
uvs[9] : 0.500149-0.750166
紫外线[9]:0.500149-0.750166
uvs[10] : 0.748355-0.99823
紫外线[10]:0.748355-0.99823
uvs[11] : 0.500193-0.998728
紫外线[11]:0.500193-0.998728
uvs[12] : 0.498993-0.250415
紫外线[12]:0.498993-0.250415
uvs[13] : 0.748953-0.25092
紫外线[13]:0.748953-0.25092
Am i doing something wrong, if someone could help me that would be great.
我做错了什么吗,如果有人可以帮助我,那就太好了。
回答by Reto Koradi
Your check for glGetAttribLocation()
failing to find the attribute is incorrect:
您对glGetAttribLocation()
未能找到该属性的检查不正确:
GLuint vertexUVID = glGetAttribLocation(shaderProgram, "color");
if(!vertexUVID)
cout << "vertexUVID not found ..." << endl;
glGetAttribLocation()
returns a GLint
(not GLuint
), and the result is -1
if an attribute with the given name is not found in the program. Since you assign the value to an unsigned variable, it will end up being the largest possible unsigned, which is then an invalid argument if you pass it to glEnableVertexAttribArray()
afterwards.
glGetAttribLocation()
返回 a GLint
(not GLuint
),结果是-1
如果在程序中找不到具有给定名称的属性。由于您将该值分配给一个无符号变量,它最终将成为可能的最大无符号变量,如果您glEnableVertexAttribArray()
随后将其传递给该变量,则该参数将是无效参数。
Your code should look like this instead:
您的代码应如下所示:
GLint vertexUVID = glGetAttribLocation(shaderProgram, "color");
if(vertexUVID < 0)
cout << "vertexUVID not found ..." << endl;
Note that 0
is a perfectly valid attribute location.
请注意,这0
是一个完全有效的属性位置。