0

I want to print out the earth obj, but it keeps printing strangely like a picture.

enter image description here

The vertexes are printed in an empty form in the middle.

_mesh = new Mesh("obj\\13902_Earth_v1_l3.obj", "obj\\Earth_diff.jpg");

First, enter the file name like this. Over there, _mesh is the object of the Mesh class that I created.

    Mesh(char* obj, char* texture) {
        open(obj);
        loadTexture(texture, _textureIndex);
    }

This is the constructor of the Mesh class. The Opne function outputs obj, and loadTexture reads the texture. just look at the Open function.

void Mesh::open(char* file)
{
    FILE* fp;
    char buffer[100] = { 0 };
    Vec3<double> pos;
    int index[3], tex[3], empty[3];
    int id = 0;
    _minBound.Set(1000000.0);
    _maxBound.Set(-1000000.0);

    fopen_s(&fp, file, "r");
    while (fscanf(fp, "%s %lf %lf %lf", buffer, &pos[0], &pos[1], &pos[2]) != EOF)
    {
        // v 0.2 0.3 0.1
        // vt
        if (buffer[0] == 'v' && buffer[1] == NULL) {
            for (int i = 0; i < 3; i++) {
                if (_minBound[i] > pos[i]) _minBound[i] = pos[i];
                if (_maxBound[i] < pos[i]) _maxBound[i] = pos[i];
            }

            _vertices.push_back(new Vertex(id, pos));
        }
    }

    // read texture coordinate of vertics
    id = 0;
    fseek(fp, 0, SEEK_SET);
    while (fscanf(fp, "%s %lf %lf", &buffer, &pos[0], &pos[1]) != EOF) {
        if (!strcmp(buffer, "vt")) {
            _textureCoords.push_back(new Texture(pos[0], 1.0 -  pos[1], 0.0));
        }
    }

    // read faces
    id = 0;
    fseek(fp, 0, SEEK_SET);
    while (fscanf(fp, "%s %d/%d/%d %d/%d/%d %d/%d/%d", &buffer, &index[0], &tex[0], &empty[0],  &index[1], &tex[1], &empty[1], &index[2], &tex[2], &empty[2]) != EOF) {
        if (buffer[0] == 'f' && buffer[1] == NULL) {
            auto v0 = _vertices[index[0] - 1];
            auto v1 = _vertices[index[1] - 1];
            auto v2 = _vertices[index[2] - 1];
            _faces.push_back(new Face(id++, v0, v1, v2, tex[0] - 1, tex[1] - 1, tex[2] - 1));
            //_faces.push_back(new Face(index++, _vertices[v_index[0] - 1], _vertices[v_index[1] - 1], _vertices[v_index[2] - 1]));

        }
    }


    fclose(fp);

    moveToCenter(10.0);
    makeList();
    computeNormal();

}

In the code, _vertices and _faces are class vectors that I've created. It's simply a text file in this format that's made vector computation.

enter image description here

enter image description here

enter image description here

enter image description here

And I didn't read the value of vn there, and I didn't read the third value of f. Then at least the vertex value would have been entered well, but why does it print out like that?

This is what it looks like when I don't put on textures.

enter image description here

All other sphere objects are printed in that form. How do we solve this?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Keastmin
  • 85
  • 6
  • In this case the faces are quads instead of triangles and have 4 indices instead of 3. You need to make 2 triangles for each quad: i[0], i[1], i[2] and i[0], i[2], i[3] – Rabbid76 Jun 04 '22 at 06:00
  • The same phenomenon occurs even after changing the face part by adding an index. ```_faces.push_back(new Face(id++, v0, v1, v2, v3, tex[0] - 1, tex[1] - 1, tex[2] - 1, tex[3] - 1));``` – Keastmin Jun 04 '22 at 06:17
  • Please read your own code. You do not even read the 4th index. `fscanf(fp, "%s %d/%d/%d %d/%d/%d %d/%d/%d"` just reads the 3 indices. – Rabbid76 Jun 04 '22 at 06:21
  • Of course, I edited it like this. ```while (fscanf(fp, "%s %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &buffer, &index[0], &tex[0], &empty[0], &index[1], &tex[1], &empty[1], &index[2], &tex[2], &empty[2], &index[3], &tex[3], &empty[3]) != EOF)``` – Keastmin Jun 04 '22 at 06:32
  • I have also edited the constructor of the face class. – Keastmin Jun 04 '22 at 06:33
  • So it's solved now? – Rabbid76 Jun 04 '22 at 06:40
  • Unfortunately, it has not been resolved. – Keastmin Jun 04 '22 at 06:41
  • It's my first time studying opengl, so I'm just frustrated. – Keastmin Jun 04 '22 at 06:42
  • First, I find out how to print two obj files on one screen. The goal is to print out the moon orbiting around the Earth – Keastmin Jun 04 '22 at 06:49
  • Oh, it's solved! Thanks a lot. Rabbid76!!!! I'm so grateful to you. – Keastmin Jun 04 '22 at 06:59

0 Answers0