AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

OpenGL编程轻松入门之NURBS曲线和曲面

51自学网 2015-08-30 http://www.wanshiok.com

  例12:绘制一个彩色的曲线,曲线闭合成圆。在曲线的边缘绘制8个点,如图十四所示。

#include <windows.h>
#include <GL/glut.h>

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曲线

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。

上一篇:OpenGL编程轻松入门之菜单管理  下一篇:Visual&nbsp;C++设计超强仿QQ自动伸缩窗口