首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于bezier曲线的运动路径

基于bezier曲线的运动路径
EN

Stack Overflow用户
提问于 2013-11-28 15:57:35
回答 1查看 1.2K关注 0票数 0

我必须用bezier曲线创建一个运动路径。在运行下面的程序时,我得到控制多边形上的值,它应该是我的运动路径的曲线,我可以通过"printf“语句来检查它们,但是我实际上没有得到曲线。

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

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-28 16:05:00

您正在使用glOrtho()设置一个投影卷,它仅为1x1。如果你想看到你的曲线(它的尺寸是几百个单位),你需要展开它。

另外,在执行循环之后不要使用glutSwapBuffers(),否则可能只看到一半的点。您还应该将glBegin()/glEnd()放在循环之外。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20270366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档