(a)Bezier曲线 (b) Bezier曲面
1. 绘制Bezier曲线
#include <GL/glut.h>
GLfloat ctrlpoints[4][3] =
{{ -4.0, -4.0, 0.0}, { -2.0, 3.0, 0.0},
{2.0, 4.5, 0.0}, {3.0, -3.0, 0.0}};
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
//下行用于定义曲线函数
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3); //将当前曲线函数激活
}
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
//下面用求值器按20等分计算Bezier曲线上的点
glColor3f(0.0, 0.0, 0.0);
glLineWidth(2);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 20; i++)
glEvalCoord1f((GLfloat) i/20.0); //相当于调用了glVertex*()
glEnd();
//下面绘制控制多边形
glLineWidth(1);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i < 4; 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();
}
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);
glutMainLoop();
return 0;
}
2. 绘制Bezier曲面
#include <GL/glut.h>
GLfloat ctrlpoints[4][4][3] = {
{{-3, 0, 4.0}, {-2, 0, 2.0}, {-1, 0, 0.0}, {0, 0, 2.0}},
{{-3, 1, 1.0}, {-2, 1, 3.0}, {-1, 1, 6.0}, {0, 1, -1.0}},
{{-3, 2, 4.0}, {-2, 2, 0.0}, {-1, 2, 3.0}, {0, 2, 4.0}},
{{-3, 3, 0.0}, {-2, 3, 0.0}, {-1, 3, 0.0}, {0, 3, 0.0}}
};
void display(void)
{
int i, j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glPushMatrix ();
glRotatef(85.0, 1.0, 1.0, 1.0);
for (j = 0; j <= 20; j++)
{
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 20; i++)
glEvalCoord2f((GLfloat)i/20.0, (GLfloat)j/20.0); //调用求值器
glEnd();
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 20; i++)
glEvalCoord2f((GLfloat)j/20.0, (GLfloat)i/20.0); //调用求值器
glEnd();
}
glPopMatrix ();
glFlush();
}
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
//下行的代码用控制点定义Bezier曲面函数
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlpoints[0][0][0]);
glEnable(GL_MAP2_VERTEX_3); //激活该曲面函数
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0); //构造平行投影矩阵
}
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 ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}