I want to make an .OBJ loader

  .obj, c++, opengl

I made an .OBJ loader with reference to the reference site.
I would like to know how to set the normals and UV coordinates correctly.
Comment section below ///// I want to know what to do with the internal code.
I imported the .OBJ file with a blender.

What I tried

Set vertex data directly without using index

What I want to know

I set the normal and UV coordinates, but I want to know the cause of the lighting being strange as shown in the reference image. In other words, I think the normals and UV coordinates are not set correctly.

reference site : http://www.opengl-tutorial.org/jp/beginners-tutorials/tutorial-7-model-loading/

reference image: imgur.com/a/u7ApUOm

Github: https://github.com/Shigurechan/GL/tree/2d0637094ec1ec58d84288422b3e9db3b88e97d8

Mode.cpp

// ##################################### .objファイル読み込み #####################################
void FrameWork::D3::LoadObj(const char *fileName, ObjFile &attribute)
{
      ObjFile obj;

      std::vector<int> vertexIndex;
      std::vector<int> uvIndex;
      std::vector<int> normalIndex;

      std::vector<glm::vec3> vertex;
      std::vector<glm::vec2> uv;
      std::vector<glm::vec3> normal;

      FILE *file = fopen(fileName, "r");

      if (file == NULL)
      {
            std::cerr << ".OBJファイルが開けません: " << fileName << std::endl;
            assert(0);
      }
      else
      {
            while (true)
            {
                  char line[500];

                  int res = fscanf(file, "%s", line);

                  if (res == EOF)
                  {
                        break;
                  }

                  if (strcmp(line, "v") == 0)
                  {
                        glm::vec3 vert;
                        fscanf(file, "%f %f %fn", &vert.x, &vert.y, &vert.z);
                        vertex.push_back(vert);
//                       obj.vertex.push_back(vert);
                  }
                  else if (strcmp(line, "vt") == 0)
                  {
                        glm::vec2 u;
                        fscanf(file, "%f %fn", &u.x, &u.y);
                        //u.y = 1.0f - u.y;
//                        obj.uv.push_back(u);
                        uv.push_back(u);
                  }
                  else if (strcmp(line, "vn") == 0)
                  {

                        glm::vec3 norm;
                        fscanf(file, "%f %f %fn", &norm.x, &norm.y, &norm.z);
//                        obj.normal.push_back(norm);
                        normal.push_back(norm);
                  }
                  else if (strcmp(line, "f") == 0)
                  {

                        std::string vertex1, vertex2, vertex3;
                        unsigned int v[3], u[3], n[3];
                        int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &v[0], &u[0], &n[0], &v[1], &u[1], &n[1], &v[2], &u[2], &n[2]);
                        if (matches != 9)
                        {
                              printf("File can't be read by our simple parser : ( Try exporting with other optionsn");
                              assert(0);
                        }
                        vertexIndex.push_back(v[0]);
                        vertexIndex.push_back(v[1]);
                        vertexIndex.push_back(v[2]);
                        obj.vertexIndex.push_back(v[0] - 1);
                        obj.vertexIndex.push_back(v[1] - 1);
                        obj.vertexIndex.push_back(v[2] - 1);

                        uvIndex.push_back(u[0]);
                        uvIndex.push_back(u[1]);
                        uvIndex.push_back(u[2]);
                        obj.uvIndex.push_back(u[0]);
                        obj.uvIndex.push_back(u[1]);
                        obj.uvIndex.push_back(u[2]);

                        normalIndex.push_back(n[0]);
                        normalIndex.push_back(n[1]);
                        normalIndex.push_back(n[2]);                                                
                        obj.normalIndex.push_back(n[0] - 1);
                        obj.normalIndex.push_back(n[1] - 1);
                        obj.normalIndex.push_back(n[2] - 1);
                  }
            }
////////////////////////////////////////////////////////////////////////////////////////
            for( unsigned int i = 0; i < vertexIndex.size(); i++ )
            {

                  // Get the indices of its attributes
                  unsigned int vi = vertexIndex[i];
                  unsigned int ui = uvIndex[i];
                  unsigned int ni = normalIndex[i];
                  

                  glm::vec3 v = vertex[ vi - 1 ];
                  glm::vec2 u = uv[ ui - 1];
                  glm::vec3 n = normal[ ni - 1];
                  
                  obj.vertex.push_back(v);
                  obj.uv.push_back(u);
                  obj.normal.push_back(n);
            }
///////////////////////////////////////////////////////////////////////////////////////
    }
      attribute = obj;
}

Source: Windows Questions C++

LEAVE A COMMENT