以下是使用OpenGL读取obj文件并判断其模型类型的示例代码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <GL/glut.h>

// 用于存储顶点坐标的结构体
struct Vertex {
    float x, y, z;
};

// 存储所有的顶点坐标
std::vector<Vertex> vertices;
// 存储面的顶点索引
std::vector<std::vector<int>> faces;

// 绘制模型
void drawModel() {
    glBegin(GL_TRIANGLES);
    for (int i = 0; i < faces.size(); i++) {
        if (faces[i].size() == 3) { // 三角形
            for (int j = 0; j < 3; j++) {
                int vertexIndex = faces[i][j];
                glVertex3f(vertices[vertexIndex].x, vertices[vertexIndex].y, vertices[vertexIndex].z);
            }
        } else if (faces[i].size() == 4) { // 四边形
            for (int j = 0; j < 4; j++) {
                int vertexIndex = faces[i][j];
                glVertex3f(vertices[vertexIndex].x, vertices[vertexIndex].y, vertices[vertexIndex].z);
            }
        } else { // 多边形
            for (int j = 0; j < faces[i].size(); j++) {
                int vertexIndex = faces[i][j];
                glVertex3f(vertices[vertexIndex].x, vertices[vertexIndex].y, vertices[vertexIndex].z);
            }
        }
    }
    glEnd();
}

// 读取obj文件
void readObjFile(const std::string& filename) {
    std::ifstream file(filename);
    if (!file.is_open()) {
        std::cerr << "Failed to open file: " << filename << std::endl;
        return;
    }

    std::string line;
    while (std::getline(file, line)) {
        if (line.substr(0, 2) == "v ") { // 顶点坐标
            std::istringstream stream(line.substr(2));
            Vertex vertex;
            stream >> vertex.x >> vertex.y >> vertex.z;
            vertices.push_back(vertex);
        } else if (line.substr(0, 2) == "f ") { // 面
            std::istringstream stream(line.substr(2));
            std::vector<int> face;
            int vertexIndex;
            while (stream >> vertexIndex) {
                face.push_back(vertexIndex - 1); // obj文件的索引从1开始,需要减1
                stream.ignore(1); // 忽略顶点索引之间的斜杠
            }
            faces.push_back(face);
        }
    }
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(0.0, 0.0, 0.0);
    drawModel();
    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)width / (double)height, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    glutCreateWindow("OBJ Model Viewer");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    readObjFile("model.obj");

    glutMainLoop();

    return 0;
}

该程序使用OpenGL绘制读取的obj文件中的模型,并根据面的顶点数判断模型类型(三角形、四边形或多边形)。在读取obj文件时,顶点坐标存储在vertices向量中,面的顶点索引存储在faces向量中。drawModel函数根据面的顶点数绘制模型。readObjFile函数读取obj文件并解析顶点坐标和面的顶点索引。display和reshape函数用于绘制和调整窗口大小。最后,main函数初始化OpenGL窗口并调用readObjFile函数来读取obj文件。

OpenGL读取OBJ文件并判断模型类型(三角形、四边形、多边形)

原文地址: https://www.cveoy.top/t/topic/o8Ex 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录