#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]);
/* void glMap1{fd}(GLenum target, TYPE u1, TYPE u2, GLint stride, GLint order, const TYPE* points) 一维求值器 target:求值器生成的值的类型,指出了控制顶点的意义以及在points参数中需要提供多少值。 GL_MAP1_VERTEX_3: xyz坐标 GL_MAP1_VERTEX_4:xyzw坐标 GL_MAP1_COLOR_4: RGBA GL_MAP1_INDEX: 颜色索引 GL_MAP1_NORMAL: 法线坐标 GL_MAP1_TEXTURE_COORD_{}:纹理坐标,{1,2,3,4} u1、u2:生成x方向上的n个数,通常是从0变化到1。 stride:表示跨度(在每块存储区内浮点数或双精度数的个数,即两个控制点间的偏移量,对于三维的点,不管其坐标值是单精度还是双精度,stride的值都是3)。 order: 阶数,等于Bernstein函数的阶次加1,与控制点数相等,对3次双项式,order的值为4。 (stride, order):生成x方向上,order个控制点,每个控制点由stride个数表示。
points:用于传递控制点数组的起始地址,可以指向控制点集、RGBA颜色值或是纹理坐标串等。 */ 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*() /* glEvalCoord1{fd}(TYPE u) 该函数将产生曲线坐标值并将其绘制 参数u为当前参数u的离散取值 */ glEnd(); //glMapGrid1f(20.0, 0.0, 1.0); //glEvalMesh1(GL_LINE, 0,
20.0);
//下面绘制控制多边形 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; }
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200616183601183.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjI4NTI1,size_16,color_FFFFFF,t_70)