c++ - 模型中的顶点重复消除

标签 c++ opengl hash

当以 .obj 格式导入模型时,许多多边形共享顶点并因此浪费内存,我想做的是通过仅保存唯一顶点来删除重复项。

顶点散列

/// Template specialization for hashing of a Vec3
namespace std {
template<typename T>
struct hash<Vec3<T>> {
    void hash_combine(size_t &seed, const size_t &hash) const {
        seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }

    size_t operator() (const Vec3<T> &vec) const {
        auto hasher = hash<float>{};
        auto hashed_x = hasher(vertex.position.x);
        auto hashed_y = hasher(vertex.position.y);
        auto hashed_z = hasher(vertex.position.z);
        auto hashed_color_r = hasher(vertex.color.r);
        auto hashed_color_g = hasher(vertex.color.g);
        auto hashed_color_b = hasher(vertex.color.b);
        auto hashed_color_a = hasher(vertex.color.a);
        auto hashed_texcoord_x = hasher(vertex.texCoord.x);
        auto hashed_texcoord_y = hasher(vertex.texCoord.y);
        auto hashed_normal_x = hasher(vertex.normal.x);
        auto hashed_normal_y = hasher(vertex.normal.y);
        auto hashed_normal_z = hasher(vertex.normal.z);

        size_t seed = 0;
        hash_combine(seed, hashed_x);
        hash_combine(seed, hashed_y);
        hash_combine(seed, hashed_z);
        hash_combine(seed, hashed_texcoord_x);
        hash_combine(seed, hashed_texcoord_y);
        hash_combine(seed, hashed_normal_x);
        hash_combine(seed, hashed_normal_y);
        hash_combine(seed, hashed_normal_z);
        return seed;
    }
};
}

使用 tinyobjcloader 导入网格

Mesh Renderer::load_mesh_from_file(std::string filepath) {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err;
auto success = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filepath.c_str());
if (!success) { SDL_Log("Failed loading mesh %s: %s", filepath.c_str(), err.c_str()); return Mesh(); }

std::unordered_map<Vertex<float>, size_t> unique_vertices{};
Mesh mesh{};
for (auto shape : shapes) { // Shapes
    size_t index_offset = 0;
    for (auto face : shape.mesh.num_face_vertices) { // Faces (polygon)
        for (auto v = 0; v < face; v++) {
            tinyobj::index_t idx = shape.mesh.indices[index_offset + v];
            Vertex<float> vertex{};
            float vx = attrib.vertices[3 * idx.vertex_index + 0];
            float vy = attrib.vertices[3 * idx.vertex_index + 1];
            float vz = attrib.vertices[3 * idx.vertex_index + 2];
            vertex.position = {vx, vy, vz};

            float tx = attrib.vertices[3 * idx.texcoord_index + 0];
            float ty = attrib.vertices[3 * idx.texcoord_index + 1];
            vertex.texCoord = {tx, ty};

            float nx = attrib.normals[3 * idx.normal_index + 0];
            float ny = attrib.normals[3 * idx.normal_index + 1];
            float nz = attrib.normals[3 * idx.normal_index + 2];
            vertex.normal = {nx, ny, nz};

            // These two lines work just fine (includes all vertices)
            // mesh.vertices.push_back(vertex);
            // mesh.indices.push_back(mesh.indices.size());

            // Check for unique vertices, models will contain duplicates
            if (unique_vertices.count(vertex) == 0) {
                unique_vertices[vertex] = mesh.indices.size();
                mesh.vertices.push_back(vertex);
                mesh.indices.push_back(mesh.indices.size());
            } else {
                mesh.indices.push_back(unique_vertices.at(vertex));
            }
        }
        index_offset += face;
    }
}

SDL_Log("Number of vertices: %lu for model %s", mesh.vertices.size(), filepath.c_str());
return mesh;
}

第一个图像是包含所有顶点的情况。

enter image description here

这是我只使用唯一顶点的情况。

enter image description here

有什么想法吗?

最佳答案

if (unique_vertices.count(vertex) == 0) {

unique_vertices[vertex] = mesh.vertices.size();

mesh.indices.push_back(mesh.vertices.size());

mesh.vertices.push_back(顶点);

说明:索引是指向顶点位置的“指针”。为此,您需要获取写入顶点数据的索引,而不是索引数据的索引。

关于c++ - 模型中的顶点重复消除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40256769/

相关文章:

c++ - 带引用键的 unordered_map

c++ - 带 Eclipse CDT + MinGW + GLEW + GLFW 的 OpenGL : Undefined References

arrays - 在 ruby​​ 中将数组转换为散列

c# - 生成密码哈希

C++相互类依赖

c++ - 在 C++ 中检查命令行输入是否有错误

c++ - vector 指针内存分配 vector

opengl - OpenGL 是 "state machine"吗?

c++ - SDL 无法创建 OpenGL 窗口

ruby - Rails 3.2 遍历数组