Why does attempt to load obj file into a game fail with distorted model

123 Views Asked by At

I use a library called rapidobj and attempt to load obj file into the game problem is that whenever i try, i get a distorted model that doesn't look right:

enter image description here

even with indice for loop start value changed to "1" which is starting point of indices in obj format, it still doesn't retrieve it correctly.

// public domain, cc by sa is bad
    auto objFile = rapidobj::ParseFile(location);
    for(unsigned int i = 0; i < objFile.attributes.positions.size(); i+=3)
        this->bufferVertices.push_back(glm::vec3(objFile.attributes.positions[i], objFile.attributes.positions[i + 1], objFile.attributes.positions[i + 2]));

    for(unsigned int i = 0; i < objFile.shapes.size(); i++) {
        for(unsigned e = 0; e < objFile.shapes[i].mesh.indices.size(); e++)
            this->indices.push_back(objFile.shapes[i].mesh.indices[e].position_index);
    }
    for(unsigned i = 0; i < indices.size(); i++)
        this->vertices.push_back(this->bufferVertices[this -> indices [i]]);

What i attempt to do with this piece of code is load vertices into an array then use indices and "duplicate" the correct (susposed to work but it doesn't) into the main vertices array, I have done this before and this is first time Im using this library and for some reason I just can't get model displayed here correctly what im possibly doing wrong here.

When checked through renderdoc it can be seen model is partially loaded correctly but then a value keeps repeating for no reason, breaking the model.

There is my buffers:

// public domain cc by sa is awful

glGenVertexArrays(1, &(this->vao));
glBindVertexArray(this->vao);
glEnableVertexAttribArray(0);
glGenBuffers(1, &(this->vbo[0]));
glBindBuffer(GL_ARRAY_BUFFER, this->vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(this->vertices.data())*sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);


glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);

glEnableVertexAttribArray(1);
glGenBuffers(1, &(this->vbo[1]));
glBindBuffer(GL_ARRAY_BUFFER, this->vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(this->position.data()) * sizeof(glm::vec3), position.data(), GL_DYNAMIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
glVertexAttribDivisor(1, 1);
1

There are 1 best solutions below

2
drk1 On

Turns out size passed to buffer is wrong and should be changed to

glBufferData(GL_ARRAY_BUFFER, sizeof(this->vertices.data())*sizeof(glm::vec3), vertices.data() * vertices.size(), GL_STATIC_DRAW);

During small size tests old code didnt had any issues essentially hiding this issue and causing a 4 day headache. Rubbery ducky effect after posting it to stackoverflow magically found a solution.