OpenGL:绘制B样条曲线

绘制B样条曲线:


#include <stdlib.h>

#include <GL/glut.h>

//

#if 0



/// the points of the curve - these are the same as the bezier curve

/// points demonstrated in the bezier curve example.

float Points[4][3] = {

	{ 10,10,0 },

	{  5,10,2 },

	{ -5,0,0 },

	{-10,5,-2}

};

#define NUM_POINTS 4



//	The following sets of 4 indices are the curves that need to

//	be drawn to create a clamped cubic b-spline. In total there

//	are 5 curve segments to draw.

//

//	0 0 0 1

//    0 0 1 2

//      0 1 2 3

//        1 2 3 3

//          2 3 3 3

//

//	Remember this when trying to understand knot vectors!!

//



#else



float Points[9][3] = {



	{ 10,5,0 },

	{  5,10,0 },

	{ -5,15,0 },

	{ -10,-5,0 },

	{ 4,-4,0 },

	{ 10,5,0 },

	{  5,10,0 },

	{ -5,15,0 },

	{ -10,-5,0 }









};

#define NUM_POINTS 9





//若绘制过首尾控制点的曲线  

//	0 0 0 1

//    0 0 1 2

//      0 1 2 3

//        1 2 3 4

//          2 3 4 5

//            3 4 5 6

//              4 5 6 6

//                5 6 6 6

//

//	Remember this when trying to understand knot vectors!!

//

//若绘制首尾相接的平滑曲线 ,即为当前绘制

//      0 1 2 3

//        1 2 3 4

//          2 3 4 5

//            3 4 5 6



#endif



// the level of detail for the curve

unsigned int LOD=20;



///

#define NUM_SEGMENTS (NUM_POINTS-3)





//

float* GetPoint(int i) {

	// return 1st point

	if (i<0) {

		return	Points[0];

	}



	if (i<NUM_POINTS)

		return Points[i];

	// return last point

	return Points[NUM_POINTS-1];

}



//------------------------------------------------------------	OnKeyPress()

//

void myIdle(void) 

{

	glutPostRedisplay();

}



//------------------------------------------------------------	OnDraw()

//

void OnDraw() {



	// clear the screen & depth buffer

	glClear(GL_COLOR_BUFFER_BIT);



	// clear the previous transform

	glLoadIdentity();



	// set the camera position

// 	gluLookAt(	1,10,30,	//	eye pos

// 		0,0,0,	//	aim point

// 		0,1,0);	//	up direction



// 	glColor3f(0.5,0.2,0);

 	glPointSize(3);

// 

// 	// draw curve hull

	glColor3f(0.3,0,0.5);

	glBegin(GL_LINE_STRIP);

	for(int i=0;i!=NUM_POINTS;++i) {

		glVertex3fv( Points[i] );

	}

	glEnd();





	glColor3f(0,1,0);



	// begin drawing our curve

	glBegin(GL_LINE_STRIP);



	for(int start_cv=0,j=0;j<NUM_SEGMENTS;j++,start_cv++)

	{

		// for each section of curve, draw LOD number of divisions

		for(int i=0;i!=LOD;++i) {



			// use the parametric time value 0 to 1 for this curve

			// segment.

			float t = (float)i/LOD;



			// the t value inverted

			float it = 1.0f-t;



			// calculate blending functions for cubic bspline

			float b0 = it*it*it/6.0f;

			float b1 = (3*t*t*t - 6*t*t +4)/6.0f;

			float b2 = (-3*t*t*t +3*t*t + 3*t + 1)/6.0f;

			float b3 =  t*t*t/6.0f;



			// calculate the x,y and z of the curve point

			float x = b0 * GetPoint( start_cv + 0 )[0] +

				b1 * GetPoint( start_cv + 1 )[0] +

				b2 * GetPoint( start_cv + 2 )[0] +

				b3 * GetPoint( start_cv + 3 )[0] ;



			float y = b0 * GetPoint( start_cv + 0 )[1] +

				b1 * GetPoint( start_cv + 1 )[1] +

				b2 * GetPoint( start_cv + 2 )[1] +

				b3 * GetPoint( start_cv + 3 )[1] ;



			float z = b0 * GetPoint( start_cv + 0 )[2] +

				b1 * GetPoint( start_cv + 1 )[2] +

				b2 * GetPoint( start_cv + 2 )[2] +

				b3 * GetPoint( start_cv + 3 )[2] ;



			// specify the point

			glVertex2f( x,y );

		}

	}



	// we need to specify the last point on the curve

	//glVertex3fv( Points[NUM_POINTS-1] );

	glEnd();



	// draw CV's

	glBegin(GL_POINTS);

	for(int i=0;i!=NUM_POINTS;++i) {

		glVertex3fv( Points[i] );

	}

	glEnd();



	// currently we've been drawing to the back buffer, we need

	// to swap the back buffer with the front one to make the image visi
  • 8
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在OpenGL绘制B样条曲线,需要进行以下步骤: 1.定义控制点:B样条曲线是通过一组控制点定义的,因此需要定义控制点的坐标。 2.计算B样条基函数:B样条曲线是通过一组基函数计算得到的,因此需要先计算B样条基函数。 3.计算曲线上的点:根据控制点和B样条基函数,计算曲线上的点。 4.绘制曲线:通过OpenGL的绘图函数,将曲线上的点绘制出来。 下面是一个简单的OpenGL B样条曲线绘制的代码示例: ``` #include <GL/glut.h> #include <vector> std::vector<GLfloat> ctrlPoints = { -4.0, 0.0, 0.0, 1.0, -2.0, 4.0, 0.0, 1.0, 2.0,-4.0, 0.0, 1.0, 4.0, 0.0, 0.0, 1.0 }; GLfloat knots[] = { 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 }; GLUnurbsObj *theNurb; void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); theNurb = gluNewNurbsRenderer(); gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0); gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); gluBeginCurve(theNurb); gluNurbsCurve(theNurb, 8, knots, 4, &ctrlPoints[0], 4, GL_MAP1_VERTEX_4); gluEndCurve(theNurb); 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 | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } ``` 这个示例代码使用了OpenGL的NURBS库来绘制B样条曲线。在`init`函数中,我们使用`gluNewNurbsRenderer`函数创建了一个NURBS对象,并设置了一些属性。在`display`函数中,我们使用`gluBeginCurve`函数开始绘制曲线,然后使用`gluNurbsCurve`函数计算曲线上的点,并使用`gluEndCurve`函数结束绘制。在`reshape`函数中,我们设置了视口和投影矩阵。 注意,在实际应用中,我们可能需要自己实现B样条基函数的计算,并使用OpenGL的绘图函数绘制曲线上的点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值