在我的引擎中有多个纹理后,我遇到了一个问题。
我的渲染代码目前如下所示:
void Renderer::render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
checkGlError("glClear");
for (vector<Node*>::const_iterator it = renderArray_.begin(); it != renderArray_.end();
it++) {
Node* node = *it;
EntityModel* entity =
static_cast<EntityModel*>(resourceManager_->getResource(
(*it)->getEntity()));
if (entity == 0 || entity->getVertices() == 0 || entity->getVertices()->size() == 0) {
LOGI("Empty entity %s.", node->getName().c_str());
continue;
}
Resource* resource = resourceManager_->getResource(node->getShader());
Shader* shader = static_cast<Shader*>(resource);
Resource* resource2 = resourceManager_->getResource(entity->getTexture());
Image* image = static_cast<Image*>(resource2);
mat4 proj;
Matrix::projection3D(proj, 45.0f,
(float) nScreenWidth_ / nScreenHeight_, 0.1f, 100.0f);
mat4 res;
Matrix::multiply(proj, node->getMatrix(), res);
// Select shader program to use.
glUseProgram(shader->getId());
checkGlError("glUseProgram");
int matrix = glGetUniformLocation(shader->getId(), "uWVP");
int texture = glGetUniformLocation(shader->getId(), "texture_0");
checkGlError("glGetUniformLocation");
int textureCoords = glGetAttribLocation(shader->getId(), "attrTexCoords");
int vertices = glGetAttribLocation(shader->getId(), "attrPos");
checkGlError("glGetAttribLocation");
// Load vertex positions.
glUniformMatrix4fv(matrix, 1, false, res);
glVertexAttribPointer(vertices, 3, GL_FLOAT, GL_FALSE, 0,
&(*entity->getVertices())[0]);
glEnableVertexAttribArray(vertices);
checkGlError("Loading vertex positions");
glVertexAttribPointer(textureCoords, 2, GL_FLOAT, GL_FALSE, 0,
&(*entity->getTextureCoords())[0]);
checkGlError("glVertexAttribPointer");
glEnableVertexAttribArray(textureCoords);
checkGlError("glEnableVertexAttribArray");
// Bind the texture.
glActiveTexture(GL_TEXTURE0);
checkGlError("glActiveTexture");
glBindTexture(GL_TEXTURE_2D, image->getId());
checkGlError("glBindTexture");
glUniform1i(image->getId(), 0); <====================ERROR HERE!!!
checkGlError("glUniform1i");
glDrawArrays(GL_TRIANGLES, 0, entity->getVertices()->size() / 3);
checkGlError("glDrawArrays");
}
}
我的纹理加载代码:
void Image::compile() {
glGenTextures(1, &id_);
glBindTexture(GL_TEXTURE_2D, id_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA,
GL_UNSIGNED_BYTE, (GLvoid*) image_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
如果我只加载一个纹理,一切正常。我有五个对象使用相同的纹理为每个对象重复渲染方法的内容,并且一切似乎都运行良好。但是,正如我添加具有不同纹理的第六个对象一样,该对象没有被渲染,并且我不断在 glUniform1i 函数上获得 GL_INVALID_OPERATION。我还检查了http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml的原因,为什么它不起作用,但似乎没有找到。
有什么想法可能是错的吗?