通过VC++和OpenGL显示STL文件

老师给留了作业,通过OpenGL将STL文件显示出来。自己初步学习了OpenGL的知识,又学了一些VC++知识,参考了一些资料,最后写出来了。

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int nums = 0;
float* facets;//use the float only
float* vectors;
int angle = 0;
int xyz[3] = { 0,0,0 };
//light
GLfloat lightAmb[] = { 0.f,0.f,0.f,1.f };
GLfloat lightDiff[] = { 1.f,1.f,1.f,1.f };
GLfloat lightSpec[] = { 1.f,1.f,1.f,1.f };
GLfloat lightPos[] = { 1.f,1.f,1.f,0.f };
//material
GLfloat goldAmb[] = { 0.24725f,0.1995f,0.0745f,1.f };
GLfloat goldDiff[] = { 0.75164f,0.60648f,0.22648f,1.f };
GLfloat goldSpec[] = { 0.628281f,0.555802f,0.366065f,1.f };
GLfloat goldShin = 51.2f;
void  dataGet() {
    int amountFacet = 0, amountVectors = 0;
    long size = 0;
    int nlines = 0;
    FILE* file;
    errno_t err;
    err = fopen_s(&file, "D:\\downloads\\Stanford_Bunny\\Stanford_Bunny\\bunny.stl", "r");
    fseek(file, 0L, SEEK_END);
    size = ftell(file);
    fclose(file);
    err = fopen_s(&file, "D:\\downloads\\Stanford_Bunny\\Stanford_Bunny\\bunny.stl", "r");
    //读取Standford Bunny的STL文件
    for (int i = 0; i < size; i++) {//get the amount of lines
        if (getc(file) == '\n') {
            nlines++;
        }
    }
    nums = nlines / 7;//get the amount of triangles
    amountFacet = 3 * nums;
    amountVectors = 9 * nums;
    facets = new float[3 * nums];
    vectors = new float[9 * nums];
    rewind(file);
    while (getc(file) != '\n');
    for (int i = 0, j = 0; i < nlines; i++, j++) {
    //获取各个三角面的顶点值和法向量值
        double x = 0.0, y = 0.0, z = 0.0;
        int re = 0;
        re = fscanf_s(file, "%*s %*s %lf %lf %lf\n", &x, &y, &z);
        if (re == 3) {
            facets[i] = x;
            i++;
            facets[i] = y;
            i++;
            facets[i] = z;
        }//the facet's data
        fscanf_s(file, "%*s %*s");
        re = fscanf_s(file, "%*s %lf %lf %lf", &x, &y, &z);
        if (re == 3) {
            vectors[j] = x;
            j++;
            vectors[j] = y;
            j++;
            vectors[j] = z;
            j++;
        }//the first vector
        re = fscanf_s(file, "%*s %lf %lf %lf", &x, &y, &z);
        if (re == 3) {
            vectors[j] = x;
            j++;
            vectors[j] = y;
            j++;
            vectors[j] = z;
            j++;
        }//the second vector
        re = fscanf_s(file, "%*s %lf %lf %lf", &x, &y, &z);
        if (re == 3) {
            vectors[j] = x;
            j++;
            vectors[j] = y;
            j++;
            vectors[j] = z;
        }//the third vector
        fscanf_s(file, "%*s");
        fscanf_s(file, "%*s");
    }
}
void drawTriangle() {//绘制三角形
    for (int x = 1; x <= nums; x++) {
        int i = 3 * (x - 1);
        int j = 9 * (x - 1);
        glBegin(GL_TRIANGLES);
        glVertex3d(vectors[j], vectors[j + 1], vectors[j + 2]);
        j = j + 3;
        glVertex3d(vectors[j], vectors[j + 1], vectors[j + 2]);
        j = j + 3;
        glVertex3d(vectors[j], vectors[j + 1], vectors[j + 2]);
        glEnd();
    }
}
void reshape(int w, int h) {
    glViewport(0.0, 0.0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(350000000000000000000000000.0, (double)w / h, 5.0, 2000000000000000000.0);//显示视角不知道如何控制比价好,一点点调出这个值
    glMatrixMode(GL_MODELVIEW);
}
void idle() {
    glutPostRedisplay();
}
void myDisplay()
{
    static int angle = 0;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0, 1.0, 1.0, 0.0);//background color
    glLoadIdentity();
    gluLookAt(0.0, -160.0, 0.0, 10.0, 50.0, 0.0, 0.0, 0.0, -1.0);
    glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
    glPushMatrix();
    glRotatef(float(angle), 0.f, 0.f, 1.f);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, goldAmb);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, goldDiff);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, goldSpec);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, goldShin);
    drawTriangle();
    glPopMatrix();  
    glutSwapBuffers();
    if (++angle >= 360) {
        angle = 0;
    }//图像进行旋转
}
void init() {
    glClearColor(1.f, 1.f, 1.f, 1.f);
    glClearDepth(1.f);
    glEnable(GL_DEPTH_TEST);

    glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiff);
    glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
}
void main(int argc, char* argv[]) {
    dataGet();
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    //glutInitDisplayMode(GLUT_RGBA );
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("test");
    init();
    glutDisplayFunc(myDisplay);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    glutMainLoop();
    system("pause");
}

参考文章链接:
读取STL模型,并用opengl显示出来
基于 VC++和 OpenGL 的 STL 文件读取显示

你可能感兴趣的:(opengl学习入门)