OpengGL B样条曲面绘制

#include 

//定义控制点points
GLfloat ctrlpoints[4][4][3] = {
    { {1.0,2.4,0.8},{1.1,0.6,0.4}, {1.2,0.2,0.0},{2.0,-1.0,0.4}},//points0 

    { {2.6,1.0,0.8},{2.8,-0.4,0.4},{3.0,-0.8,0.0},{3.8,-1.6,0.4}},//points1

    { {5.2,1.0,0.0},{5.4,-0.4,0.0},{5.6,-0.8,0.0},{6.4,-1.6,0.0}},//points2
    
    { {7.4,2.4,0.0},{7.4,0.6,0.0},{7.8,0.2,0.0},{8.8,-0.6,0.2}} //points3
};

/* Nurbs曲面的对象指针 */
GLUnurbsObj  *theNurb;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //清楚颜色缓冲
    glColor3f(0.0, 0.0, 0.0);
    //glPushMatrix()和glPopMatrix()的配对使用可以消除上一次的变换对本次变换的影响
    glPushMatrix();

    //绘制控制线
    glLineWidth(1.5f); //设置线宽1.5
    glColor3f(0.0, 0.0, 1.0); //设置线颜色为蓝色
    for (int i = 0; i < 4; i++)
    {
        //设置类型为连续直线
        glBegin(GL_LINE_STRIP);
        //绘制列向的控制线
        for (int j = 0; j < 4; j++)
            glVertex3fv(ctrlpoints[i][j]);
        glEnd();
        //绘制横向的控制线
        glBegin(GL_LINE_STRIP);
        for (int j = 0; j < 4; j++)
            glVertex3fv(ctrlpoints[j][i]);
        glEnd();
    }

    //绘制控制点
    glPointSize(5.0f); //设置点的大小为5.0
    glColor3f(1.0, 0.0, 0.0); //设置点颜色为红色
    glBegin(GL_POINTS); //模式为POINTS
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            glVertex3fv(ctrlpoints[i][j]);
        }
    }
    glEnd();

    //绘制B样条控制曲面
    GLfloat knots[8] = { 0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0 }; //B样条控制向量
    glLineWidth(1.0f); //设置线宽为1.0
    glColor3f(0.0, 0.0, 0.0); //线的颜色为黑色

    /* 创建一个Nurbs曲面的对象  并返回一个指向这个新对象的指针 */
    theNurb = gluNewNurbsRenderer();
    /* 设置该对象的属性 */
    gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0); 
    gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
    gluBeginSurface(theNurb);//开始绘制
    gluNurbsSurface(theNurb, //NURBS曲面对象
        8, knots,  //u向节点数目和节点数组指针
        8, knots,  //v向节点数目和节点数组指针
        4 * 3,  //u向的跨度 列优先保存 t_stride = 4 * s_count
        3,   //v向的跨度
        &ctrlpoints[0][0][0], //曲面控制点指针
        4, 4, //u方向、v方向阶数
        GL_MAP2_VERTEX_3 //曲面的类型
    );

    gluEndSurface(theNurb); //结束绘制

    for (int j = 0; j <= 8; j++)
    {
        glBegin(GL_LINE_STRIP);
        for (int i = 0; i <= 30; i++)
            glEvalCoord2f((GLfloat)i / 30.0, (GLfloat)j / 8.0);
        glEnd();
        glBegin(GL_LINE_STRIP);
        for (int i = 0; i <= 30; i++)
            glEvalCoord2f((GLfloat)j / 8.0, (GLfloat)i / 30.0);
        glEnd();
    }
    
    glPopMatrix();
    glFlush();
}

void reshape(int w, int h) 
{ 
    glViewport(0, 0, (GLsizei)w, (GLsizei)h); /* 设置观察窗口的大小 */ 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    /* 设置正投影, 6个参数分别表示, 左修剪平面坐标, 右修剪平面坐标, 
    下修剪平面坐标, 上修剪平面坐标, 近修剪平面坐标, 远修剪平面坐标 */
    glOrtho(0.0, 10.0, -5.0 * (GLfloat)h / (GLfloat)w, 
        5.0 * (GLfloat)h / (GLfloat)w, -5.0, 5.0); 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    //激活该曲面函数
    glEnable(GL_MAP2_VERTEX_3); 
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB |
        GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    init();
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

你可能感兴趣的:(OpengGL B样条曲面绘制)