OpenGL glGetError 1281 错误值不正确

9

我正在使用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

这里是我将缓冲区链接到数组的代码:

    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);
    }

我的uvs值:

uvs[0]:0.748573-0.750412

uvs[1]:0.749279-0.501284

uvs[2]:0.99911-0.501077

uvs[3]:0.999455-0.75038

uvs[4]:0.250471-0.500702

uvs[5]:0.249682-0.749677

uvs[6]:0.001085-0.75038

uvs[7]:0.001517-0.499994

uvs[8]:0.499422-0.500239

uvs[9]:0.500149-0.750166

uvs[10]:0.748355-0.99823

uvs[11]:0.500193-0.998728

uvs[12]:0.498993-0.250415

uvs[13]:0.748953-0.25092

我做错了什么吗?如果有人能帮助我,那就太好了。


所以我试图缩小错误范围,问题出在glEnableVertexAttribArray(vertexUVID)上,这会触发那个错误,但我不明白为什么。 - aze
1个回答

17

您的检查glGetAttribLocation()未能找到属性的方法是不正确的:

GLuint vertexUVID = glGetAttribLocation(shaderProgram, "color");
if(!vertexUVID)
    cout << "vertexUVID not found ..." << endl;

glGetAttribLocation() 返回一个 GLint(而不是 GLuint),如果程序中没有找到给定名称的属性,则结果为 -1。由于你将该值分配给无符号变量,它最终将成为最大可能的无符号值,如果之后将其传递给 glEnableVertexAttribArray(),那么它将是无效参数。

你的代码应该像这样:

GLint vertexUVID = glGetAttribLocation(shaderProgram, "color");
if(vertexUVID < 0)
    cout << "vertexUVID not found ..." << endl;

请注意,0 是一个完全有效的属性位置。


2
不是说负属性索引一定是错误。它可能只是被编译器优化掉了。 - genpfault
@genpfault: 我不认为我说过这是一个错误。我写的是“失败”,意思是调用未能找到指定名称的属性。啊,好吧,我一开始说“错误”了。现在重新措辞了。 - Reto Koradi
没关系,只是提醒未来的读者注意优化细节。 - genpfault
你说得对,我的变量因为代码优化而找不到了。问题是它在片段着色器中没有被使用,但在顶点着色器中被使用了。我没想到会有这样的影响,谢谢提醒。 - aze
我在像素着色器中注释了一个变量以进行调试,结果 glGetAttribLocation 开始返回该属性的 -1 值。我没有预料到着色器编译器会这样做,这让我感到惊讶。 - Hatoru Hansou

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接