0

This is what my cube looks like at the moment:

Fill mode:

fill mode

Line mode:

line mode

As you can see the vertex positions in relation to the indices are almost scattered. What could I possibly be doing wrong?

I use these this code for setting up the buffers:

struct  Mesh
    {
        CBuint v_vbo, vn_vbo, vt_vbo;
        CBuint ebo;

        Mesh() {}
        Mesh(std::vector <CBuint>& i, std::vector <vec3>& positions, std::vector <vec3>& normals, std::vector <vec2>& tex_coords)
        {
            // Generate
            glGenBuffers(1, &v_vbo);
            glGenBuffers(1, &vn_vbo);
            glGenBuffers(1, &vt_vbo);
            glGenBuffers(1, &ebo);

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

            glEnableVertexAttribArray(0);
            glBindBuffer(GL_ARRAY_BUFFER, v_vbo);
            glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(vec3), &positions[0], GL_STATIC_DRAW);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

            glEnableVertexAttribArray(1);
            glBindBuffer(GL_ARRAY_BUFFER, vn_vbo);
            glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec3), &normals[0], GL_STATIC_DRAW);
            glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

            glEnableVertexAttribArray(2);
            glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
            glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(vec2), &tex_coords[0], GL_STATIC_DRAW);
            glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);

            glBufferData(GL_ELEMENT_ARRAY_BUFFER, i.size() * sizeof(CBuint), &i[0], GL_STATIC_DRAW);    
        }

        ~Mesh()
        {
            glDeleteBuffers(1, &v_vbo);
            glDeleteBuffers(1, &vn_vbo);
            glDeleteBuffers(1, &vt_vbo);
            glDeleteBuffers(1, &ebo);
        }
    };

Before-hand, I store the vertex and index data by iterating through each key character in the .obj file like so:

case 'f':
                CBuint v_i[3];
                CBuint vn_i[3];
                CBuint vt_i[3];

                sscanf_s((&line[i])[0].c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v_i[0], &vt_i[0], &vn_i[0], &v_i[1], &vt_i[1], &vn_i[1], &v_i[2], &vt_i[2], &vn_i[2]);

                // Faces
                _f.push_back(Face(v_i[0], vt_i[0], vn_i[0], v_i[1], vt_i[1], vn_i[1], v_i[2], vt_i[2], vn_i[2]));

                // Indices
                _indices.push_back(v_i[0] - 1);
                _indices.push_back(vt_i[0] - 1);
                _indices.push_back(vn_i[0] - 1);

                _indices.push_back(v_i[1] - 1);
                _indices.push_back(vt_i[1] - 1);
                _indices.push_back(vn_i[1] - 1);

                _indices.push_back(v_i[2] - 1);
                _indices.push_back(vt_i[2] - 1);
                _indices.push_back(vn_i[2] - 1);
                break;
            }

Then finally, I render the cube by calling:

        glBindVertexArray(_vao);
        glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

I don't understand what I'm doing wrong here. Can anyone see an error?

P.S: I'm avoiding glDrawArrays for efficiency purposes.

genpfault
  • 47,669
  • 9
  • 68
  • 119
DrStrange
  • 64
  • 7
  • 1
    Edit in a [mcve]. For all we know `cube.obj` could be fubar or a geometry shader could be fuzting with things. – genpfault Oct 05 '16 at 20:06
  • cube.obj is a file - .obj is the Wavefront format that contains vertex data for generating 3D models. What more information shall I add? – DrStrange Oct 05 '16 at 20:36
  • Edit in a [mcve]. Single file, self-contained, using common, [widely-available](http://freeglut.sourceforge.net/) and/or [easily-used](http://glm.g-truc.net/) libraries. Feel free to use something like [this](http://stackoverflow.com/a/14887071/44729) to build off of. – genpfault Oct 05 '16 at 20:55
  • Thank you - I found your links quite helpful although I was hoping to avoid using glDrawArrays due to the inefficiency for multiple material construction. – DrStrange Oct 05 '16 at 22:31

0 Answers0