在OpenGL中输出地球OBJ时的错误

发布于 2025-02-04 18:28:33 字数 3233 浏览 2 评论 0 原文

我想打印出地球OBJ,但它像图片一样一直奇怪地打印出来。

顶点在中间以空形式打印。

_mesh = new Mesh(“ OBJ \\ 13902_EARTH_V1_L3.OBJ”,“ OBJ \\ earth_diff.jpg”);

首先,首先输入这样的文件名。在那儿,_mesh是我创建的网格类的对象。

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

这是网格类的构造函数。 OPNE函数输出OBJ,负载文本读取纹理。只需查看开放函数即可。

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();

}

在代码中,_vertices和_ faces是我创建的类向量。这只是以这种格式的文本文件,可以进行矢量计算。

​/I.SSTATIC.NET/E1L1S.JPG“ ALT =“在此处输入图像说明”>

我没有读过那里的VN值,我没有阅读f的第三个值。然后,至少要输入顶点值,但是为什么会这样打印呢?

这就是我不穿纹理时的样子。

所有其他球体对象均以该形式打印。我们如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文