OpenGL实现 贝塞尔三次方曲线

134 篇文章 1 订阅
21 篇文章 1 订阅

它的公式:点击打开链接

http://www.cnblogs.com/zhouxin/p/3436740.html

代码如下:

#include "math.h"
#include "GL/Gl.h"
#include "GL/glut.h"

typedef struct PointTag {
	GLfloat x;
	GLfloat y;
} Point;

static const Point BezierPoint[4] = {
    {-0.8f, -0.5f},
    {-0.5f,  0.5f},
    {-0.5f, -0.7f},
    { 1.0f,  0.0f},
};


void myInit()
{
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(700, 400);
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity(); //Move to the Center
    gluOrtho2D(-0.0f, 0.0f, -0.0f, 0.0f);
}

Point drawBezier(Point A,Point B,Point C,Point D,double t)
{
	Point P;
	P.x=pow((1 - t), 3) * A.x + 3 * t * pow((1 - t), 2) * B.x + 3 * (1 - t) * pow(t, 2) * C.x + pow(t, 3) * D.x;
	P.y=pow((1 - t), 3) * A.y + 3 * t * pow((1 - t), 2) * B.y + 3 * (1 - t) * pow(t, 2) * C.y + pow(t, 3) * D.y;
	return P;
}

void drawBerize()
{
    glClear(GL_LINE_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_LINES);
    Point tempPoint = BezierPoint[0];
    for (double t = 0.0f;t < 1.0f; t += 0.1f) {
        Point endPoint=drawBezier(BezierPoint[0], BezierPoint[1], BezierPoint[2], BezierPoint[3], t);
        glVertex2f(tempPoint.x, tempPoint.y);
        glVertex2f(endPoint.x, endPoint.y);
        tempPoint = endPoint;
    }
    glEnd();
    glFlush();
    glutSwapBuffers();
}

void idleFunc() {
    drawBerize();
}


int main(int argc, char* argv[])
{
	glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,150);
	glutCreateWindow("三次Bezier曲线");
	glutDisplayFunc(drawBerize);
    glutIdleFunc(idleFunc);
	myInit();
	glutMainLoop();
	return 0;
}


例子2:

    switch(moveStep) {
        case 0:
            break;
        case 1:
            {
                if(tParam < 1.0f) {
                    Point endPoint = drawBezier(BezierPoint[0], BezierPoint[1], BezierPoint[2], BezierPoint[3], tParam);
                    LineMotion_Init2(&berizeMotion, tempPoint.x, tempPoint.y, endPoint.x, endPoint.y, 1, 10);
                    moveStep = 2;
                    tempPoint = endPoint;
                }
            }
            break;
        case 2:
            {
                VINT32 idx = 0;
                VINT32 idy = 0;
                VINT32 isEnd = LineMotion_Generator2(&berizeMotion, &idx, &idy, time);
                if(isEnd == 1) {
                    tParam += 0.01f;
                    DEBUG_CONSOLE<<"Over\n";
                    moveStep = 1;
                } else {
                    DEBUG_CONSOLE<<"X: "<<berizeMotion.iObjCurX<<"\n";
                    DEBUG_CONSOLE<<"Y: "<<berizeMotion.iObjCurY<<"\n";
                    test_exit->setX(berizeMotion.iObjCurX);
                    test_exit->setY(berizeMotion.iObjCurY);
                }
            }
            break;
        case 3:
            break;
        default:
            break;
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值