使用贝塞尔曲线的运动路径

时间:2013-11-28 15:57:35

标签: opengl

我必须使用贝塞尔曲线创建运动路径。在运行下面的程序时,我得到控制多边形的值,它应该是我的运动路径的曲线,我可以通过" printf"来检查它们。声明,但我实际上并没有得到曲线。

我做错了什么,如何解决?

#include "stdafx.h"
#include <gl/glut.h>
#include <cstdio>



struct point
{
float x;
float y;
};

// simple linear interpolation between two points
void lirp(point& dest, const point& a, const point& b, const float t)
{
dest.x = a.x + (b.x - a.x)*t;
dest.y = a.y + (b.y - a.y)*t;
}

// evaluate a point on a bezier-curve. t goes from 0 to 1.0
void bezier(point &dest, const point& a, const point& b, const point& c, const point& d, const float t)
{
point ab, bc, cd, abbc, bccd;

lirp(ab, a, b, t);           // point between a and b
lirp(bc, b, c, t);           // point between b and c
lirp(cd, c, d, t);           // point between c and d
lirp(abbc, ab, bc, t);       // point between ab and bc
lirp(bccd, bc, cd, t);       // point between bc and cd
lirp(dest, abbc, bccd, t);   // point on the bezier-curve

}

void Draw() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(20);

point a = { 700, 100 };
point b = { 650, 20 };
point c = { 600, 180 };
point d = { 550, 100 };

for (int i = 0; i<1000; ++i)    //should plot 1000 points within the control polygons.
{
    point p;
    float t = static_cast<float>(i) / 999.0;
    bezier(p, a, b, c, d, t);
    float p_x = p.x;
    float p_y = p.y;
    printf("%f %f\n", p_x, p_y);
    glBegin(GL_POINTS);
    glVertex3f(p_x, p_y, 0.0);
    glEnd();
    glutSwapBuffers();
}

}

void Timer(int Unused)
{
glutPostRedisplay();
glutTimerFunc(20, Timer, 0);
}



void Init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(200, 100);
glutCreateWindow("Animation Test");
Init();
glutDisplayFunc(Draw);
Timer(0);
glutMainLoop();
return 0;
}

1 个答案:

答案 0 :(得分:1)

您正在设置glOrtho()的投影量,该投放量仅为1x1。如果你想看到你的曲线(其尺寸是数百个单位),你需要扩展它。

另外,在你的for循环之后不要glutSwapBuffers();否则你可能只会看到一半的积分。您还应该将glBegin() / glEnd()放在循环之外。