OpenGL中如何绘制Bezier曲线和NURBS曲线

一、Bezier曲线

主要注意两个函数:glMap1glEvalCoord1

1.voidglMap1{fd}(GLenumtarget,TYPEu1,TYPEu2,GLintstride,GLintorder,constTYPE*points);

功能:定义求值器。 

参数:target:指出了控制顶点的意义以及在points参数中需要提供多少值。

points:可以指向控制点集、RGBA颜色值或是纹理坐标串等。

u1、u2:限定了变量U的取值范围,通常是从0变化到1。

stride:表示跨度(在每块存储区内浮点数或双精度数的个数,即两个控制点间的偏移量)。

order:阶数,等于次数加1,与控制点数相等。

2.voidglEvalCoord1{fd}[v](TYPEu)

功能:该函数将产生曲线坐标值并将其绘制。

参数:u:为定义域内的任意值,每调用一次将只产生一个坐标,此坐标值也是任意的。

但目前较多采用的是定义均匀间隔曲线坐标值,依次调用glMapGrid1*()和glEvalMesh1()可以获得等间隔值。这两个函数分别用来定义一个一维网格和计算相应的坐标值。

另外,曲线定义后必须再glEnable()函数显式启动后才能起作用,其参数与target保持一致。在使用完毕后通过glDisable()函数将其关闭。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <GL/glut.h>
#include <stdlib.h>
GLfloat ctrlpoints[9][3] = {{0,-0.2,0},{-1.2,-0.5,0},{-1.6,-1,0},{-1.4,-1.5,0},{-1,-2.2,0},{-0.5,-2.7,0},{-0.35,-3.2,0},{-0.6,-3.7,0},{-1.6,-4.2,0}};//控制点

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 9, &ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
}

void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);//(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
/* The following code displays the control points as dots. */
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < 9; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-5.0, 5.0, -5.0*(GLfloat)h/(GLfloat)w,
5.0*(GLfloat)h/(GLfloat)w, -5.0, 5.0);
else
glOrtho(-5.0*(GLfloat)w/(GLfloat)h,
5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

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

二、NURBS曲线

主要注意函数:

1.voidgluNurbsCurve(GLUnurbsObj*nobj,GLintnknots,GLfloat*knot,Glintstride,GLfloat*ctlarray,GLintorder,GLenumtype)

功能:定义曲线形状。

参数:nobj:指向NURBS对象的指针。

nknots:节点数,节点数=控制点数+阶数。

knot:nknots数组非递减节点值。

stride:跨度,相邻控制点的偏移量。

Ctlarry:指向NURBS的控制点数组的指针。

order:NURBS曲线的阶数,阶数=次数+1。

type:曲线、面类型。

2.voidgluNurbsProperty(GLUnurbsObj*nobj,GLenumproperty,GLfloatvalue)

功能:设置NURBS属性。

参数:nobj:指向NURBS对象的指针。

property:需设置的属性。

value:设置指定属性的值。

3.gluBeginCurvegluEndCurve限定NURBS曲面。返回值均为void,参数均为GLUnurbsObj*nobj,为指向NURBS对象的指针。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <windows.h>
#include <GL/glut.h>
GLUnurbsObj *theNurb;
GLfloat ctrlpoints[9][3] = {{0,-0.2,0},{-1.2,-0.5,0},{-1.6,-1,0},{-1.4,-1.5,0},{-1,-2.2,0},{-0.5,-2.7,0},{-0.35,-3.2,0},{-0.6,-3.7,0},{-1.6,-4.2,0}};//控制点
GLfloat color[9][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,-1.0,0.0}};

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,13,knots,3,&ctrlpoints[0][0],4,GL_MAP1_VERTEX_3);
gluNurbsCurve(theNurb,13,knots,3,&color[0][0],4,GL_MAP1_COLOR_4);
gluEndCurve(theNurb);
/*绘制点*/
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
glBegin(GL_POINTS);
for(i = 0;i < 9;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);
}

The Why·Liam·Blog by WhyLiam is licensed under a Creative Commons BY-NC-ND 4.0 International License.

WhyLiam创作并维护的Why·Liam·Blog采用创作共用保留署名-非商业-禁止演绎4.0国际许可证

本文首发于Why·Liam·Blog (https://blog.naaln.com),版权所有,侵权必究。

本文永久链接:https://blog.naaln.com/2013/04/how-to-draw-bezier-curves-and-nurbs-curves-in-opengl/