WEB开发网
开发学院软件开发VC OpenGL编程轻松入门之NURBS曲线和曲面(2) 阅读

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

 2007-05-06 19:58:30 来源:WEB开发网   
核心提示:例12:绘制一个彩色的曲线,曲线闭合成圆,OpenGL编程轻松入门之NURBS曲线和曲面(2),在曲线的边缘绘制8个点,如图十四所示,order NURBS曲线的阶数,阶数比维数大1, #include <windows.h> #inclu

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

Tags:OpenGL 编程 轻松

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接