Textures misbehaving

Can anyone give me a steer as to what may be going wrong here? The model loads in other software and shows the proper texture.

Impossible to help you with this; you need to show your texture loading code.

1 Like

thanks mhagain and appreciate the advice. I’ll post the code this evening and if you have any thoughts I’d be pleased to hear them!

Apologies for the delay, this is what is going on in my MeshRenderer class, it takes a pointer to a mesh object.

I also have a custom debug object which takes a string and prints it along with the GL error, so ignore any debug.*** lines

    void draw() {

        // bind appropriate textures
        unsigned int diffuseNr = 1;
        unsigned int specularNr = 1;
        unsigned int normalNr = 1;
        unsigned int heightNr = 1;
        unsigned int shininessNr = 1;
        for (unsigned int i = 0; i < mesh->textures.size(); i++)
            glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
            // retrieve texture number (the N in diffuse_textureN)
            string number;
            string name = mesh->textures[i].type;
            if (name == "texture_diffuse")
                number = std::to_string(diffuseNr++);
            else if (name == "texture_specular")
                number = std::to_string(specularNr++); // transfer unsigned int to string
            else if (name == "texture_normal")
                number = std::to_string(normalNr++); // transfer unsigned int to string
            else if (name == "texture_height")
                number = std::to_string(heightNr++); // transfer unsigned int to string
            else if (name == "texture_shininess")
                number = std::to_string(shininessNr++); // transfer unsigned int to string

            // now set the sampler to the correct texture unit
            mesh->shader->setInt(name + number, i);
            debug.printGLError(name + number);
            // and finally bind the texture
            glBindTexture(GL_TEXTURE_2D, mesh->textures[i].id);
            debug.printGLError(name + number);

        mesh->shader->setMat4("model", mesh->transform->modelMatrix());
        mesh->shader->setMat4("projection", sc->projectionMatrix());

        glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(mesh->indices.size()), GL_UNSIGNED_INT, 0);
        debug.printGLError("glDrawElements(GL_TRIANGLES, mesh->indices.size(), GL_UNSIGNED_INT, 0);");
        glBindTexture(GL_TEXTURE_2D, 0);

But you still haven’t shown your texture-loading code.

Is your texture a solid primary colour (red/green/blue)? One common mistake which could cause this kind of error is loading an RGB (not RGBA) texture whose width isn’t a multiple of 4 without calling glPixelStorei(GL_UNPACK_ALIGNMENT, 1). The default alignment is 4, so each row needs to be a multiple of 4 bytes; for an RGB8 texture, that means that the width must be a multiple of 4; RGBA8 textures are inherently 4-byte aligned.

This was the problem, thank you!

I am using stb_image.h to load textures

 unsigned char* data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 4);

The last argument needed to be changed to 3.

 unsigned char* data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 3);