Opengl教程之读取stl文件并绘制在picturecontrol控件内

Opengl教程之读取stl并绘制在picturecontrol控件内

By Cracent

作为机械专业方向的人员,我们经常需要用SolidWorks构建模型。有些时候我们被要求将这些模型通过OPENGL进行表达。STL 是三维模型常用的文件格式,其使用三角面片来表示三维实体模型。本文进行将进行STL模型可视化研究,并开放源代码。

 

一、我们使用SolidWorks建立一个一字连接件并另存为STL格式

Opengl教程之读取stl文件并绘制在picturecontrol控件内_第1张图片

 Opengl教程之读取stl文件并绘制在picturecontrol控件内_第2张图片

二、配置相应环境

三、建立CMyStatic类
2.1
添加类
2.2 类向导
2.2.1添加消息处理函数OnTimer()
2.2.2 重写虚函数PreSubclassWindow()
2.3 添加其他函数与变量

三、OpenGl使用 
3.1
函数内容
3.2插入picture control控件, 并为其添加控制变量

 

以上几步请参考我的另一篇文章:《Opengl教程之读取obj并绘制在picturecontrol控件内

 

四、stl文件读取

void CMyStatic::GLFunc4()

{

    int max = 0;

    bool isbegin = false;

    long size = 0;

    int nlines = 0;

    int count1 = 0;

    int count2 = 0;

    FILE* file = fopen("2.stl","r");

    fseek(file, 0L, SEEK_END);

    size = ftell(file);

    fclose(file);

    file = fopen("2.stl","r");

    for (int i = 0;i < size;i++)

    {

        if (getc(file) == '\n')

        {

            nlines++;

        }

    }

    num = nlines / 7;

    rewind(file);

    while (getc(file) != '\n');

    verts = new float[9 * num];

    vnorms = new float[9 * num];

    for (int i = 0;i < num;i++)

    {

        char x[200] = "";

        char y[200] = "";

        char z[200] = "";

        if (3 != fscanf(file, "%*s %*s %80s %80s %80s\n", x, y, z))

        {

            break;

        }

        vnorms[count1] = vnorms[count1 + 3] =vnorms[count1 + 6] = atof(x);

        count1++;

        vnorms[count1] = vnorms[count1 + 3] =vnorms[count1 + 6] = atof(y);

        count1++;

        vnorms[count1] = vnorms[count1 + 3] =vnorms[count1 + 6] = atof(z);

        count1 += 7;

        fscanf(file, "%*s %*s");

        if (3 != fscanf(file, "%*s %80s %80s %80s\n", x, y, z))

        {

            break;

        }

        if (isbegin == false)

        {

            isbegin = true;

            max = atof(z);

        }

        verts[count2] = atof(x);

        count2++;

        verts[count2] = atof(y);

        count2++;

        verts[count2] = atof(z);

        count2++;

        if (3 != fscanf(file, "%*s %80s %80s %80s\n", x, y, z))

        {

            break;

        }

        verts[count2] = atof(x);

        count2++;

        verts[count2] = atof(y);

        count2++;

        verts[count2] = atof(z);

        count2++;

        if (3 != fscanf(file, "%*s %80s %80s %80s\n", x, y, z))

        {

            break;

        }

        verts[count2] = atof(x);

        count2++;

        verts[count2] = atof(y);

        count2++;

        verts[count2] = atof(z);

        count2++;

        fscanf(file, "%*s");

        fscanf(file, "%*s");

    }

 

}

五、STL文件显示

void CMyStatic::GLFunc5()

{

    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    gluPerspective(0, 640 / 480, 0.01f,1000.0f);//计算窗口的外观比例

    //glEnable(GL_POINT_SMOOTH);

    //glEnable(GL_DEPTH_TEST);

    //LightShine();

    //glEnable(GL_NORMALIZE);

 

    glTranslatef(-0.0f, -0.0f, -COpengl10基本环境Dlg::m_gltran-10);

    static int RotateDegTriangle = 1;

    glRotatef(RotateDegTriangle++,RotateDegTriangle++, RotateDegTriangle++, 0.0f);

    glPushMatrix();

    int m_div = 1;

    for (int i = 0; i < 9*num; i++)

    {

        glBegin(GL_TRIANGLES);

        glColor3f(1.0f, 0.0f, 0.0f);

        glNormal3f(vnorms[i] / m_div,vnorms[i+1] / m_div, vnorms[i+2] / m_div);

        glVertex3f((-verts[i]+ verts[1]) /m_div, (-verts[i+1]+ verts[2]) / m_div, (-verts[i+2]+ verts[3]) / m_div);

        i += 3;

        glColor3f(0.0f, 1.0f, 0.0f);

        glNormal3f(vnorms[i] / m_div, vnorms[i +1] / m_div, vnorms[i + 2] / m_div);

        glVertex3f((-verts[i] + verts[1]) /m_div, (-verts[i + 1] + verts[2]) / m_div, (-verts[i + 2] + verts[3]) / m_div);

        i += 3;

        //glColor3f(0.0f, 0.0f, 1.0f);

        glNormal3f(vnorms[i] / m_div, vnorms[i +1] / m_div, vnorms[i + 2] / m_div);

        glVertex3f((-verts[i] + verts[1]) /m_div, (-verts[i + 1] + verts[2]) / m_div, (-verts[i + 2] + verts[3]) / m_div);

        i += 2;

        glEnd();

    }

    glPopMatrix();

}

六、运行程序,我们可以看到如下画面

Opengl教程之读取stl文件并绘制在picturecontrol控件内_第3张图片

Opengl教程之读取stl文件并绘制在picturecontrol控件内_第4张图片

至此,本次可视化研究告一段落,欢迎交流。

你可能感兴趣的:(mfc,STL,OpenGL,三维,可视化)