我的引擎中有多个纹理后遇到问题。
我的渲染代码目前如下所示:
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原因,为什么它不起作用,但似乎没有找到任何原因。
任何想法可能出错?
答案 0 :(得分:8)
这段代码
glUniform1i(image->getId(), 0);
无论如何都是错的。您不会将纹理ID绑定到采样器,而是绑定到纹理单元。统一的第一个参数是位置ID,它也不是纹理ID。正确的代码看起来更像
glActiveTexture(GL_TEXTURE0 + active_textue);
/* ... */
glUniform1i(glGetUniformLocation(program, "samplername"), active_texture);
或者如果您在着色器中使用预定义位置
glUniform1i(texture_sample_location, active_texture);
编辑: D'哦,我身边的错误。当然,制服将偏移量赋予GL_TEXTURE0标记。