繁体   English   中英

在某些情况下,多个 OpenGL 纹理不起作用?

[英]Multiple OpenGL Textures not working in some instances?

我正在尝试加载 OBJ 文件以在 OpenGL 中使用它们。 OBJ 文件中的每个对象都有自己的纹理。 在我的代码中,我将文件的每个对象拆分为一个结构体,如下所示:

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;
};

Texture类是一个简单的类,使加载和处理纹理更容易。

在模型的类中,我有一个std::vectorObjModelComponent

我正在像这样渲染对象:

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);
    }
}

Texture 类看起来像这样:

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);
}

这是组件的生成方式:

    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");
            }
    }

这是 VAO 和数组缓冲区的生成方式:

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);
    }

你可以在 GitHub 上找到我的完整代码: https : //github.com/Kuechenzwiebel/SDL-OpenGL-Tests

问题是,显示在我的对象上的纹理是最后一个渲染对象使用的纹理。

更新:删除纹理析构函数中的glDeleteTextures ,结果显示所有组件上显示的纹理是用于最后一个组件的纹理。

我真的不明白为什么这不起作用,我从其他原语复制它,只使用一种纹理。 一切正常。

我找到了一种解决方法,而不是使用单独的 ArrayBuffers 和 VAO,我只使用其中的一个,并存储新对象开始的索引。 在那里我改变了使用的纹理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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