简体   繁体   中英

Multiple OpenGL Textures not working in some instances?

I am trying to load OBJ files to use them in OpenGL. Every object in the OBJ file has its own texture. In my code I split up every object of the file into a struct that is defended like this:

struct ObjModelComponent {
    std::vector<glm::vec3> vertices;
    std::vector<glm::vec2> uvs;
    std::vector<glm::vec3> normals;

    Texture tex;
    ArrayBuffer vertex, uv, normal;
    GLuint VAO;
};

The Texture class is a simple class to make loading and handling a texture easier.

In the class of the model I have a std::vector of my ObjModelComponent .

I'm rendering the Object like this:

void ObjModel::render() {
    for(int i = 0; i < components.size(); i++) {
        components[i].vertex.activate();
        components[i].uv.activate();
        components[i].normal.activate();

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, components[i].tex.getTextureID());
        shader->sendInt(0, components[i].tex.getTextureName());

        glBindVertexArray(components[i].VAO);
        shader->sendMat4(*data->projection, "projection");
        shader->sendMat4(data->viewMat, "view");
        shader->sendMat4(model, "model");

        glDrawArrays(GL_TRIANGLES, 0, int(components[i].vertices.size()));
        glBindVertexArray(0);
    }
}

The Texture class looks like this:

Texture::Texture(std::string fileName, TextureType type):
textureName("tex"), fileName(fileName), type(type) {
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);

    unsigned char *image = SOIL_load_image(fileName.c_str(), &texWidth, &texHeight, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    switch (type) {
        case TEXTURE_GENERATE_MIPMAP:
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);

            glGenerateMipmap(GL_TEXTURE_2D);
            break;

        case TEXTURE_NO_MIP_MAP:
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        default:
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            break;
    }

    glBindTexture(GL_TEXTURE_2D, 0);

    SOIL_free_image_data(image);
}

Texture::Texture() {

}

Texture& Texture::operator=(const Texture &other) {
    this->fileName = other.fileName;
    this->textureName = other.textureName;
    this->type = other.type;

//    glDeleteTextures(1, &tex);

    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);

    unsigned char *image = SOIL_load_image(fileName.c_str(), &texWidth, &texHeight, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    switch (type) {
        case TEXTURE_GENERATE_MIPMAP:
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);

            glGenerateMipmap(GL_TEXTURE_2D);
            break;

        case TEXTURE_NO_MIP_MAP:
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        default:
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            break;
    }

    glBindTexture(GL_TEXTURE_2D, 0);

    SOIL_free_image_data(image);

    return *this;
}

Texture::~Texture() {
    glDeleteTextures(1, &tex);
}

GLuint Texture::getTextureID() {
    return tex;
}

void Texture::setTextureName(std::string name) {
    textureName = name;
}

std::string Texture::getTextureName() {
    return textureName;
}

glm::vec2 Texture::getTextureSize() {
    return glm::vec2(texWidth, texHeight);
}

This is how the components are generated:

    for(int i = 0; i < fileLines.size(); i++) {
            if(fileLines[i].substr(0, 2) == "o ") {
                if(!first) {
                    tmpComp.tex = tmpTex;
                    components.emplace_back(tmpComp);
                    componentIndex++;
                }

                first = false;

                tmpTex = Texture(hg::substr(path, 0, int(path.find_last_of("/"))) + "/" + hg::substr(fileLines[i], 2, int(fileLines[i].length())) + ".png");
            }
    }

This is how the VAOs and array buffers are generated:

for(int i = 0; i < components.size(); i++) {
        glGenVertexArrays(1, &components[i].VAO);
        glBindVertexArray(components[i].VAO);

        components[i].vertex.setData(components[i].vertices.data(), sizeof(glm::vec3) * components[i].vertices.size(), 0);
        components[i].uv.setData(components[i].uvs.data(), sizeof(glm::vec2) * components[i].uvs.size(), 1);
        components[i].normal.setData(components[i].normals.data(), sizeof(glm::vec3) * components[i].normals.size(), 2);

        components[i].vertex.activate();
        components[i].uv.activate();
        components[i].normal.activate();

        glBindVertexArray(0);
    }

You can find my complete code on GitHub: https://github.com/Kuechenzwiebel/SDL-OpenGL-Tests

The problem is, that the texture displayed on my object is that, that was used by the last object that was rendered.

Update : Removing the glDeleteTextures in the destructor of the Texture, yields in the result, that the texture displayed on all components is that that was used for the last component.

I really don't understand why this isn't working, I copied it from other primitives, that only use one texture. And there everything is working fine.

I found a workaround, instead of having separate ArrayBuffers and VAOs, I only use one of each and also store at which index a new object begins. There I change the texture that is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM