今天,用opengl实现bezier曲线的绘制,但是当我将控制点增加到九个的时候,程序死活绘制不出图像。
后来经过一番折腾,终于知道,是xp系统的问题,程序在win7上可以正常运行。
下面我附上我的主要程序
第一段是利用glEvalCoord1f()
函数先生成离散点,然后用直线链接。
glShadeModel(GL_FLAT); glColor3f(1.0,0.0,0.0); GLfloat controlPoint[9][3] = { {-1.0f,0.0f,0.0f},{-0.75f,0.707f,0.0f},{-0.5f,1.0f,0.0f}, {-0.25f,0.707f,0.0f},{0.0f,0.0f,0.0f},{0.25f,-0.707f,0.0f}, {0.5f,-1.0f,0.0f},{0.75f,-0.707f,0.0f},{1.0f,0.0f,0.0f} }; glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,8,(const float*)controlPoint); glEnable(GL_MAP1_VERTEX_3); glBegin(GL_LINE_STRIP); for(int i = 0;i <= 100; i++) glEvalCoord1f((GLfloat)i/(GLfloat)100.0); glEnd(); glDisable(GL_MAP1_VERTEX_3);
第二段是利用glMapGrid1d();对u参数进行自动等分。再用glEvalMesh1()函数产生一系列的点并利用这些顶点坐标最终绘制一条曲线。
glShadeModel(GL_FLAT); glColor3f(1.0,0.0,0.0); GLfloat controlPoint[9][3] = { {-1.0f,0.0f,0.0f},{-0.75f,0.707f,0.0f},{-0.5f,1.0f,0.0f}, {-0.25f,0.707f,0.0f},{0.0f,0.0f,0.0f},{0.25f,-0.707f,0.0f}, {0.5f,-1.0f,0.0f},{0.75f,-0.707f,0.0f},{1.0f,0.0f,0.0f} }; glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,8,(const float*)controlPoint); glEnable(GL_MAP1_VERTEX_3); glMapGrid1f(50,0.0f,1.0f); glEvalMesh1(GL_LINE,0,50); glDisable(GL_MAP1_VERTEX_3);