Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:55:21+00:00 2026-06-06T22:55:21+00:00

I encountered a problem after having more than one texture in my engine. My

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:55:23+00:00Added an answer on June 6, 2026 at 10:55 pm

    This piece of code

    glUniform1i(image->getId(), 0);
    

    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

    glActiveTexture(GL_TEXTURE0 + active_textue);
    
    /* ... */
    
    glUniform1i(glGetUniformLocation(program, "samplername"), active_texture);
    

    or if you’re using predefined locations in your shader

    glUniform1i(texture_sample_location, active_texture);
    

    EDIT: D’oh, bad mistake on my side. The uniforms take the offset to the GL_TEXTURE0 token of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I encountered a problem after switching from server-side JavaScript to CoffeeScript in a Node.js
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
I have encountered a problem in my application. I have two forms, one that
I'm having this same problem : How can I truncate a VARCHAR to the
I'm having a hard time with this problem, but I've narrowed it down to
After having learnt the hard way that shared variables are currently not guarded by
I'm having an issue with this route and not sure what my problem is
I'm having a problem with FBDialog. Apparently after the either the email or the
I encountered the following problem after upgrading to the latest version 1.8.1 of data.table
I encountered a problem while I am trying to display an image after I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.