1
\$\begingroup\$

I have been playing with OpenGL for a while and I came into the wall that I don't know how to pass. I am trying to render a model of an object based on a .obj file. In that file I have position coordinates, uv coordinates and indices of a positions and uv coordinates (faces). I am trying to render the model like so:

  1. Get all the positions from the files
  2. Get all the uv coordinates from the file
  3. Get all the faces.
  4. Generate array of vertices with all the positions and uv coordinates in order defined by indices.
  5. Index the vertices 0,1,2,...
  6. Draw the indexed vertices.

I got blocked when I tried just to render the model without the texture. I have been shown a monstrosity instead of what I am trying to achieve. When I have been drawing the model in the other way (get all the vertices and index them in the order they should be drawn) everything is fine but in this way I cannot texture the model the way I wanted. I am adding the code below:

Reading from the file:

std::vector<float> verts; //container for vertices
std::vector<unsigned int> inds; //container for indexes of vertices
std::vector<unsigned int> texinds; //container for indexes of textures
std::vector<float> texs; //container for textures


bool LoadFromFile(const std::string& path) {
    std::ifstream f(path);
    if (!f.is_open())
        return false;
    while (!f.eof()) {
        char line[128];
        f.getline(line, 128);
        std::strstream s;
        s << line;
        char junk;
        char junk1;
        char junk2; 
        char junk3;
        if ((line[0] == 'v') && (line[1] == 't')) {
            float Textu[2];
            s >> junk >> junk1 >> Textu[0] >> Textu[1];  //ingoring the first 2 characters (vt) before data
            texs.push_back({ Textu[0] });
            texs.push_back({ Textu[1] });
        }
        if (line[0] == 'f') {
            unsigned int Index[6];
            s >> junk >> Index[0] >> junk1 >> Index[1] >> Index[2] >> junk2 >> Index[3] >> Index[4] >> junk3>> Index[5]; //ingoring f and every / between indexes 
            inds.push_back({ Index[0] - 1 });
            texinds.push_back({ Index[1] - 1 });
            inds.push_back({ Index[2] - 1 });
            texinds.push_back({ Index[3] - 1 });
            inds.push_back({ Index[4] - 1 });
            texinds.push_back({ Index[5] - 1 });
        }
        if ((line[0] == 'v') && (line[1] == ' ')) {
            float Vertex[3];
            s >> junk >> Vertex[0] >> Vertex[1] >> Vertex[2];
            verts.push_back({ Vertex[0] });
            verts.push_back({ Vertex[1] });
            verts.push_back({ Vertex[2] });
        }
    }
} 

Creating array of vertices and idexing them:

        float Vertices[89868];
        for (int i = 0; i < inds.size(); i++) {
            Vertices[i] = verts[inds[i]]; //Creating array with the vertices in order defined in the index vector
        }
        unsigned int indices[89868];
        for (int i = 0; i < inds.size(); i++) {
            indices[i] = i;
        }

I understand maybe I have made a stupid mistake somewhere but I am literally incapable of finding it.

\$\endgroup\$
5
  • \$\begingroup\$ Okay i just got it. I have been treating index of the vertex as it would refer to the single position of a vertex instead of whole vertex. \$\endgroup\$
    – Kuba
    Nov 7 '20 at 9:00
  • 5
    \$\begingroup\$ If you've figured out the solution for yourself, consider posting an answer to your question, containing information on what you did to solve the issue. \$\endgroup\$ Nov 7 '20 at 11:04
  • \$\begingroup\$ Yea i understood the mistake i have made but still i have problems with implementation as soon as i get it I will post it there ; ) \$\endgroup\$
    – Kuba
    Nov 7 '20 at 11:07
  • \$\begingroup\$ Not sure if this is reflected in your question: are you aware of primitives and the winding order when drawing ? \$\endgroup\$
    – user144188
    Nov 7 '20 at 13:01
  • \$\begingroup\$ Yes, Yes I am aware of that. \$\endgroup\$
    – Kuba
    Nov 7 '20 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.