-1

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);
drk1
  • 65
  • 11
  • Your context seems to be a bit too narrow, to ask such question here. – πάντα ῥεῖ Sep 14 '22 at 19:25
  • Are you sure the OBJ is well-formed? I.e. that none of the indices are out of bounds or anything like that? – Miles Budnek Sep 14 '22 at 19:27
  • @Miles Budnek It's a default cube exported from the blender using obj (legacy) option, it is tringulated, I also tried bunch of different models from internet, also i checked the obj file itself to check anything outside of bounds or wrong but can't see any, looking at github issues of rapidobj there doesn't seems to be too much issue reports about it and i doubt it is a bug rather a incorrect use of library but can't find what im doing is wrong, i have done similar duplication based loading back then. – drk1 Sep 14 '22 at 19:30
  • What's position_index? – user253751 Sep 14 '22 at 21:02

1 Answers1

0

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.

drk1
  • 65
  • 11