Loading big OBJ files results in model corruption?

余生长醉 提交于 2019-12-13 06:27:00

问题


I wrote a obj loader. It's very simple but I'm sure it works. I checked values in file and the ones printed by the loader and they match. Problem occurs when I try to load a big obj file like stanford dragon or Bunny. Utah teapot (3664 verts) works perfectly fine, however dragon (50k verts) is rendered horribly wrong:


As You can see something is wrong with the dragon. It seems like only part of triangles is being rendered.

P.S. All models have been triangulated.

Mesh loading:

void Mesh::load(float* buffer, unsigned int size, char fields, char target) {
        unsigned int vbo;
        glGenBuffers(1, &vbo);

        glBindVertexArray(vao);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexAttribPointer(target, fields, GL_FLOAT, false, sizeof(float) * fields, 0);
        glBufferData(GL_ARRAY_BUFFER, size, buffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);

        vbos[target] = vbo;
    }

    void Mesh::loadIndices(void* buffer, unsigned int size) {
        glGenBuffers(1, &ibo);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, buffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    }

and mesh:

mesh = new Mesh();
    mesh->numIndices = indices.size();
    mesh->load(positions, pos.size() * sizeof(float), 3, MESH_TARGET_POS);
    mesh->loadIndices(ind, indices.size() * sizeof(unsigned int));

indices and pos are std::vectors, positions and ind are just content of those vectors copied into heap allocated memory (just in case it'd change something but no, it didn't change anything). load method takes in pointer to data, size in bytes, fields in vector (3d vector - 3 fields, 2d vector - 2 fields) and lastly an integer that tells which attribute is this data (MESH_TARGET_POS - attribute 1, vertex positions).

That code has nothing to do (I believe) with the problem. Every model that's less than 10k vertices works fine, even the dragon in lower polycount works great.

来源:https://stackoverflow.com/questions/53968204/loading-big-obj-files-results-in-model-corruption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!