我正在使用带有顶点和着色器的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); 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,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
我做错了什么,如果有人可以帮助我那会很棒.
解决方法
您检查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是完全有效的属性位置.