下载本文示例代码
上一节讲了一般的曲线与曲面的绘制,本节讲NURBS曲线和曲面的绘制。 例11:此例绘制两个相同形状的NURBS曲面,不同之处是一个为线框式,一个是由实多边形组成。运行后可以看到其中的区别,如图十三所示。
#include
#include GLUnurbsObj *theNurb1;GLUnurbsObj *theNurb2;GLfloat ctrlpoints[5][5][3] = {{{-3,0.5,0},{-1,1.5,0},{-2,2,0},{1,-1,0},{-5,0,0}},{{-3,0.5,-1},{-1,1.5,-1},{-2,2,-1},{1,-1,-1},{-5,0,-1}},{{-3,0.5,-2},{-1,1.5,-2},{-2,2,-2},{1,-1,-2},{-5,0,-2}},{{-3,0.5,-3},{-1,1.5,-3},{-2,2,-3},{1,-1,-3},{-5,0,-3}},{{-3,0.5,-4},{-1,1.5,-4},{-2,2,-4},{1,-1,-4},{-5,0,-4}}};//控制点GLfloat mat_diffuse[] = {1.0,0.5,0.1,1.0};GLfloat mat_specular[] = {1.0,1.0,1.0,1.0};GLfloat mat_shininess[] = {100.0};GLfloat light_position[] = {0.0,-10.0,0.0,1.0};void myInit(void){ glClearColor(1.0,1.0,1.0,0.0);//设置背景色 /*为光照模型指定材质参数*/ glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightfv(GL_FRONT,GL_POSITION,light_position);//设置光源参数 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);//设置光照模型参数 /*激活光照*/ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_LEQUAL); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); /*设置特殊效果*/ glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE); glEnable(GL_BLEND); glFrontFace(GL_CW); glShadeModel(GL_SMOOTH); glEnable(GL_LINE_SMOOTH); theNurb1 = gluNewNurbsRenderer();//创建NURBS对象theNurb1 gluNurbsProperty(theNurb1,GLU_SAMPLING_TOLERANCE,25.0); gluNurbsProperty(theNurb1,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON); theNurb2 = gluNewNurbsRenderer();//创建NURBS对象theNurb2 gluNurbsProperty(theNurb2,GLU_SAMPLING_TOLERANCE,25.0); gluNurbsProperty(theNurb2,GLU_DISPLAY_MODE,GLU_FILL);}int spin = 0;/*接收键盘指令*/static void myKey(unsigned char key,int x,int y){ switch(key) { case'd': spin = spin 1; glRotatef(spin,1.0,1.0,0.0); glutPostRedisplay(); break; case 27: exit(0); default: break; }}/*绘制曲面*/void myDisplay(void){ GLfloat knots[10] = {0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0}; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glRotatef(50.0,1.0,1.0,0.0); /*第一个曲面*/ glPushMatrix(); glTranslatef(1.0,0.0,0.0); gluBeginSurface(theNurb1); /*定义曲面形状*/ gluNurbsSurface(theNurb1,10,knots,10,knots,5*3,3,&ctrlpoints[0][0][0],5,5,GL_MAP2_VERTEX_3); gluEndSurface(theNurb1); glPopMatrix(); /*第二个曲面*/ glPushMatrix(); glTranslatef(7.0,0.0,0.0); gluBeginSurface(theNurb2); /*定义曲面形状*/ gluNurbsSurface(theNurb2,10,knots,10,knots,5*3,3,&ctrlpoints[0][0][0],5,5,GL_MAP2_VERTEX_3); gluEndSurface(theNurb2); glPopMatrix(); glutSwapBuffers();}void myReshape(GLsizei w,GLsizei h){ glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.0,(GLfloat)w/(GLfloat)h,1.0,15.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-9.0);}int main(int argc,char ** argv){ /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(600,400); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow("NURBS surface"); /*绘制与显示*/ myInit(); glutKeyboardFunc(myKey); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); /*进入GLUT事件处理循环*/ glutMainLoop(); return(0);} ·GLUnurbsObj* glNewNurbsRenderer()创建一个NURBS对象,并返回一个指向该对象的指针。如果没有足够的内存分配给该对象,则返回值为0。 ·void gluNurbsProperty(GLUnurbsObj* nobj, GLenum property, GLfloat value)设置NURBS属性。 nobj 指向NURBS对象的指针。 property需设置的属性。 value 设置指定属性的值。 ·glBeginSurface及gluEndSurface两个函数一起限定一个NURBS面的定义。返回值均为void,参数均为GLUnurbsObj* nobj,为指向NURBS对象的指针。 ·void gluNurbsSurface(GLUnurbsObj *nobj, Glint knot_count, GLfloat tknot_count, GLfloat *tknot, Glint s_stride, Glint t_stride, GLfloat *ctlarry, GLint sorder, GLint torder,GLenum type) 定义NURBS曲面形状。 nobj 指向NURBS对象的指针。 sknot_count 参数化u方向上的节点数。 sknot 参数化u方向上的非递减节点值。 tknot_count 参数化v方向上的节点数。 tknot 参数化v方向上的非递减节点值。 s_stride在ctlarry中参数化u方向上相邻控制点的偏移量。 t_stride在ctlarry中参数化v方向上相邻控制点的偏移量。 ctlarryNURBS的控制点数组。 sorder参数化u方向上NURBS的阶数,阶数比维数大1。 torder参数化v方向上NURBS的阶数,阶数比维数大1。 type曲面类型。
图十三:NURBS曲面 例12:绘制一个彩色的曲线,曲线闭合成圆。在曲线的边缘绘制8个点,如图十四所示。
#include #include GLUnurbsObj *theNurb;GLfloat ctrlpoints[12][3] = {{4,0,0},{2.828,2.828,0},{0,4,0},{-2.828,2.828,0},{-4,0,0},{-2.828,-2.828,0},{0,-4,0},{2.828,-2.828,0},{4,0,0},{2.828,2.828,0},{0,4,0},{2.828,2.828,0}};//控制点GLfloat color[12][3]={{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{-1.0,1.0,0.0},{-1.0,0.0,0.0},{-1.0,-1.0,0.0},{0.0,-1.0,0.0},{1.0,-1.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{1.0,1.0,0.0}};GLfloat knots[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};void myInit(void){ glClearColor(1.0,1.0,1.0,0.0);//设置背景色 theNurb = gluNewNurbsRenderer();//创建NURBS对象theNurb gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,10);}/*绘制曲线*/void myDisplay(void){ int i; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor3f(0.0,0.0,0.0); glLineWidth(3.0); /*绘制曲线*/ gluBeginCurve(theNurb); gluNurbsCurve(theNurb,15,knots,3,&ctrlpoints[0][0],3,GL_MAP1_VERTEX_3); gluNurbsCurve(theNurb,15,knots,3,&ctrlpoints[0][0],3,GL_MAP1_COLOR_4); gluEndCurve(theNurb); /*绘制点*/ glColor3f(1.0,0.0,0.0); glPointSize(5.0); glBegin(GL_POINTS); for(i = 0;i < 8;i ) glVertex2fv(&ctrlpoints[i][0]); glEnd(); glutSwapBuffers();}void myReshape(GLsizei w,GLsizei h){ glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <=h) glOrtho(-10.0,10.0,-10.0*(GLfloat)h/(GLfloat)w,10.0*(GLfloat)h/(GLfloat)w,-10.0,10.0); else glOrtho(-10.0*(GLfloat)w/(GLfloat)h,10.0*(GLfloat)w/(GLfloat)h,-10.0,10.0,-10.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-9.0);}int main(int argc,char ** argv){ /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(600,400); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow("NURBS curve"); /*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return(0);} ·gluBeginCurve,gluEndCurve限定NURBS曲面。返回值均为void,参数均为GLUnurbsObj* nobj,为指向NURBS对象的指针。 ·void gluNurbsCurve(GLUnurbsObj *nobj, GLint nknots, GLfloat *knot, Glint stride, GLfloat *ctlarray, GLint order,GLenum type)定义曲线形状。 nobj 指向NURBS对象的指针。 nknots 节点数,节点数等于控制点数加上阶数。 knot nknots数组非递减节点值。 stride相邻控制点的偏移量。 Ctlarry指向NURBS的控制点数组的指针。 order NURBS曲线的阶数,阶数比维数大1。 type曲面类型。
图十四:NURBS曲线
上一节讲了一般的曲线与曲面的绘制,本节讲NURBS曲线和曲面的绘制。 例11:此例绘制两个相同形状的NURBS曲面,不同之处是一个为线框式,一个是由实多边形组成。运行后可以看到其中的区别,如图十三所示。
#include #include GLUnurbsObj *theNurb1;GLUnurbsObj *theNurb2;GLfloat ctrlpoints[5][5][3] = {{{-3,0.5,0},{-1,1.5,0},{-2,2,0},{1,-1,0},{-5,0,0}},{{-3,0.5,-1},{-1,1.5,-1},{-2,2,-1},{1,-1,-1},{-5,0,-1}},{{-3,0.5,-2},{-1,1.5,-2},{-2,2,-2},{1,-1,-2},{-5,0,-2}},{{-3,0.5,-3},{-1,1.5,-3},{-2,2,-3},{1,-1,-3},{-5,0,-3}},{{-3,0.5,-4},{-1,1.5,-4},{-2,2,-4},{1,-1,-4},{-5,0,-4}}};//控制点GLfloat mat_diffuse[] = {1.0,0.5,0.1,1.0};GLfloat mat_specular[] = {1.0,1.0,1.0,1.0};GLfloat mat_shininess[] = {100.0};GLfloat light_position[] = {0.0,-10.0,0.0,1.0};void myInit(void){ glClearColor(1.0,1.0,1.0,0.0);//设置背景色 /*为光照模型指定材质参数*/ glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightfv(GL_FRONT,GL_POSITION,light_position);//设置光源参数 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);//设置光照模型参数 /*激活光照*/ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_LEQUAL); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); /*设置特殊效果*/ glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE); glEnable(GL_BLEND); glFrontFace(GL_CW); glShadeModel(GL_SMOOTH); glEnable(GL_LINE_SMOOTH); theNurb1 = gluNewNurbsRenderer();//创建NURBS对象theNurb1 gluNurbsProperty(theNurb1,GLU_SAMPLING_TOLERANCE,25.0); gluNurbsProperty(theNurb1,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON); theNurb2 = gluNewNurbsRenderer();//创建NURBS对象theNurb2 gluNurbsProperty(theNurb2,GLU_SAMPLING_TOLERANCE,25.0); gluNurbsProperty(theNurb2,GLU_DISPLAY_MODE,GLU_FILL);}int spin = 0;/*接收键盘指令*/static void myKey(unsigned char key,int x,int y){ switch(key) { case'd': spin = spin 1; glRotatef(spin,1.0,1.0,0.0); glutPostRedisplay(); break; case 27: exit(0); default: break; }}/*绘制曲面*/void myDisplay(void){ GLfloat knots[10] = {0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0}; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glRotatef(50.0,1.0,1.0,0.0); /*第一个曲面*/ glPushMatrix(); glTranslatef(1.0,0.0,0.0); gluBeginSurface(theNurb1); /*定义曲面形状*/ gluNurbsSurface(theNurb1,10,knots,10,knots,5*3,3,&ctrlpoints[0][0][0],5,5,GL_MAP2_VERTEX_3); gluEndSurface(theNurb1); glPopMatrix(); /*第二个曲面*/ glPushMatrix(); glTranslatef(7.0,0.0,0.0); gluBeginSurface(theNurb2); /*定义曲面形状*/ gluNurbsSurface(theNurb2,10,knots,10,knots,5*3,3,&ctrlpoints[0][0][0],5,5,GL_MAP2_VERTEX_3); gluEndSurface(theNurb2); glPopMatrix(); glutSwapBuffers();}void myReshape(GLsizei w,GLsizei h){ glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.0,(GLfloat)w/(GLfloat)h,1.0,15.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-9.0);}int main(int argc,char ** argv){ /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(600,400); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow("NURBS surface"); /*绘制与显示*/ myInit(); glutKeyboardFunc(myKey); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); /*进入GLUT事件处理循环*/ glutMainLoop(); return(0);} ·GLUnurbsObj* glNewNurbsRenderer()创建一个NURBS对象,并返回一个指向该对象的指针。如果没有足够的内存分配给该对象,则返回值为0。 ·void gluNurbsProperty(GLUnurbsObj* nobj, GLenum property, GLfloat value)设置NURBS属性。 nobj 指向NURBS对象的指针。 property需设置的属性。 value 设置指定属性的值。 ·glBeginSurface及gluEndSurface两个函数一起限定一个NURBS面的定义。返回值均为void,参数均为GLUnurbsObj* nobj,为指向NURBS对象的指针。 ·void gluNurbsSurface(GLUnurbsObj *nobj, Glint knot_count, GLfloat tknot_count, GLfloat *tknot, Glint s_stride, Glint t_stride, GLfloat *ctlarry, GLint sorder, GLint torder,GLenum type) 定义NURBS曲面形状。 nobj 指向NURBS对象的指针。 sknot_count 参数化u方向上的节点数。 sknot 参数化u方向上的非递减节点值。 tknot_count 参数化v方向上的节点数。 tknot 参数化v方向上的非递减节点值。 s_stride在ctlarry中参数化u方向上相邻控制点的偏移量。 t_stride在ctlarry中参数化v方向上相邻控制点的偏移量。 ctlarryNURBS的控制点数组。 sorder参数化u方向上NURBS的阶数,阶数比维数大1。 torder参数化v方向上NURBS的阶数,阶数比维数大1。 type曲面类型。
图十三:NURBS曲面 例12:绘制一个彩色的曲线,曲线闭合成圆。在曲线的边缘绘制8个点,如图十四所示。
#include #include GLUnurbsObj *theNurb;GLfloat ctrlpoints[12][3] = {{4,0,0},{2.828,2.828,0},{0,4,0},{-2.828,2.828,0},{-4,0,0},{-2.828,-2.828,0},{0,-4,0},{2.828,-2.828,0},{4,0,0},{2.828,2.828,0},{0,4,0},{2.828,2.828,0}};//控制点GLfloat color[12][3]={{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{-1.0,1.0,0.0},{-1.0,0.0,0.0},{-1.0,-1.0,0.0},{0.0,-1.0,0.0},{1.0,-1.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{1.0,1.0,0.0}};GLfloat knots[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};void myInit(void){ glClearColor(1.0,1.0,1.0,0.0);//设置背景色 theNurb = gluNewNurbsRenderer();//创建NURBS对象theNurb gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,10);}/*绘制曲线*/void myDisplay(void){ int i; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor3f(0.0,0.0,0.0); glLineWidth(3.0); /*绘制曲线*/ gluBeginCurve(theNurb); gluNurbsCurve(theNurb,15,knots,3,&ctrlpoints[0][0],3,GL_MAP1_VERTEX_3); gluNurbsCurve(theNurb,15,knots,3,&ctrlpoints[0][0],3,GL_MAP1_COLOR_4); gluEndCurve(theNurb); /*绘制点*/ glColor3f(1.0,0.0,0.0); glPointSize(5.0); glBegin(GL_POINTS); for(i = 0;i < 8;i ) glVertex2fv(&ctrlpoints[i][0]); glEnd(); glutSwapBuffers();}void myReshape(GLsizei w,GLsizei h){ glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <=h) glOrtho(-10.0,10.0,-10.0*(GLfloat)h/(GLfloat)w,10.0*(GLfloat)h/(GLfloat)w,-10.0,10.0); else glOrtho(-10.0*(GLfloat)w/(GLfloat)h,10.0*(GLfloat)w/(GLfloat)h,-10.0,10.0,-10.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-9.0);}int main(int argc,char ** argv){ /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(600,400); glutInitWindowPosition(200,200); /*创建窗口*/ glutCreateWindow("NURBS curve"); /*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return(0);} ·gluBeginCurve,gluEndCurve限定NURBS曲面。返回值均为void,参数均为GLUnurbsObj* nobj,为指向NURBS对象的指针。 ·void gluNurbsCurve(GLUnurbsObj *nobj, GLint nknots, GLfloat *knot, Glint stride, GLfloat *ctlarray, GLint order,GLenum type)定义曲线形状。 nobj 指向NURBS对象的指针。 nknots 节点数,节点数等于控制点数加上阶数。 knot nknots数组非递减节点值。 stride相邻控制点的偏移量。 Ctlarry指向NURBS的控制点数组的指针。 order NURBS曲线的阶数,阶数比维数大1。 type曲面类型。
图十四:NURBS曲线
下载本文示例代码
OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面OpenGL编程轻松入门之NURBS曲线和曲面
阅读(394) | 评论(0) | 转发(0) |