I encountered a problem after having more than one texture in my engine.
My render code currently looks like this:
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");
}
}
My texture loading code:
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);
}
If I have only one texture loaded, everything works fine. I have five objects use same texture repeating the contents of render method for each object, and everything seems to run fine. But, just as I add sixth object with different texture, the object isn’t rendered and I keep getting GL_INVALID_OPERATION on glUniform1i function.
I also checked http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml for reasons, why it doesn’t work, but didn’t seemed to find any.
Any ideas what might be wrong?
This piece of code
is wrong anyway. You do not bind texture IDs to a sampler but a the texture unit. Also the first parameter of the uniform is the location ID, which is not the texture ID as well. The correct code would more look like
or if you’re using predefined locations in your shader
EDIT: D’oh, bad mistake on my side. The uniforms take the offset to the GL_TEXTURE0 token of course.