绘制B样条曲线

学习完绘制Bezier曲线之后,我开始探索绘制B样条曲线,因为B样条曲线是之后好多东西的基础,所以一定得学会用opengl绘制B样条曲线

这里我先只考虑均匀B样条

先考虑二次均匀B样条,

根据计算公式,其实然后把之前做Bezier曲线的例子挪过来即可,两段B样条一拼,OK

上代码

#include<GL/GLUT.H>  
#include <windows.h>      
#include <math.h>      
#include <gl/GL.h>      

GLint num = 4;
GLfloat p1[] = { -0.8f, 0.1f };
GLfloat p2[] = { -0.4f, 0.6f };
GLfloat p3[] = { 0.2f, 0.8f };
GLfloat p4[] = { 0.7f, 0.2f };


void myDisplay(void){
	glClear(GL_COLOR_BUFFER_BIT);

	glPointSize(2);
	glColor3f(1.0, 0.0, 0.0);

	glBegin(GL_POINTS);
	glVertex2fv(p1);
	glVertex2fv(p2);
	glVertex2fv(p3);
	glVertex2fv(p4);
	glEnd();

	glColor3f(0.0, 0.0, 1.0);
	glBegin(GL_LINE_STRIP);
	glVertex2fv(p1);
	glVertex2fv(p2);
	glVertex2fv(p3);
	glVertex2fv(p4);
	glEnd();

	GLfloat ps1[11][2];
	GLfloat ps2[11][2];

	GLint i = 0;
	for (double t = 0.0; t <= 1.0; t += 0.1)
	{

		double a1 =  pow((1 - t), 2)/2;
		double a2 =  (1 + 2 * t - 2 * t*t)/2;
		double a3 =  t*t/2;
		ps1[i][0] = a1*p1[0] + a2*p2[0] + a3*p3[0];
		ps1[i][1] = a1*p1[1] + a2*p2[1] + a3*p3[1];

		ps2[i][0] = a1*p2[0] + a2*p3[0] + a3*p4[0];
		ps2[i][1] = a1*p2[1] + a2*p3[1] + a3*p4[1];
		i = i + 1;
	}

		glColor3f(1.0, 1.0, 0.0);
		glBegin(GL_LINE_STRIP);
		for (int i = 0; i < 11; i++)
		{
			glVertex2fv(ps1[i]);
		}
		glEnd();

		
		glBegin(GL_LINE_STRIP);
		for (int i = 0; i < 11; i++)
		{
			glVertex2fv(ps2[i]);
		}
		glEnd();
	glFlush();
}


int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("opengl1");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}
其实也就这一块是真正涉及计算的公式

GLfloat ps1[11][2];
	GLfloat ps2[11][2];

	GLint i = 0;
	for (double t = 0.0; t <= 1.0; t += 0.1)
	{

		double a1 =  pow((1 - t), 2)/2;
		double a2 =  (1 + 2 * t - 2 * t*t)/2;
		double a3 =  t*t/2;
		ps1[i][0] = a1*p1[0] + a2*p2[0] + a3*p3[0];
		ps1[i][1] = a1*p1[1] + a2*p2[1] + a3*p3[1];

		ps2[i][0] = a1*p2[0] + a2*p3[0] + a3*p4[0];
		ps2[i][1] = a1*p2[1] + a2*p3[1] + a3*p4[1];
		i = i + 1;
	}

		glColor3f(1.0, 1.0, 0.0);
		glBegin(GL_LINE_STRIP);
		for (int i = 0; i < 11; i++)
		{
			glVertex2fv(ps1[i]);
		}
		glEnd();

		
		glBegin(GL_LINE_STRIP);
		for (int i = 0; i < 11; i++)
		{
			glVertex2fv(ps2[i]);
		}
		glEnd();
后面的分别绘制两段,上效果


然后考虑三次B样条

其实也就是换换计算公式,很容易的,计算部分是

GLint i = 0;
	for (double t = 0.0; t <= 1.0; t += 0.1)
	{

		double a1 =  pow((1 - t), 3)/6;
		double a2 =  (3*t*t*t - 6 * t*t +4)/6;
		double a3 = (-3 * t*t*t + 3 * t*t + 3 * t + 1) / 6;
		double a4 =  t*t*t / 6;
		ps[i][0] = a1*p1[0] + a2*p2[0] + a3*p3[0]+a4*p4[0];
		ps[i][1] = a1*p1[1] + a2*p2[1] + a3*p3[1]+a4*p4[1];

		i = i + 1;
	}

		glColor3f(1.0, 1.0, 0.0);
		glBegin(GL_LINE_STRIP);
		for (int i = 0; i < 11; i++)
		{
			glVertex2fv(ps[i]);
		}
		glEnd();

直接替换二次那里的计算公式就好了

效果如下


怎么样,看来挺容易,然后下一步用opengl实现B样条曲面,应该很容易,然后就可以进行细分部分的实现啦哈哈哈






  • 14
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉风小宇

请我喝个咖啡呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值