Opengl 使用bezier曲线的运动路径

Opengl 使用bezier曲线的运动路径,opengl,Opengl,我必须使用贝塞尔曲线创建运动路径。在运行下面的程序时,我沿着控制多边形获取值,该多边形应该是我运动路径的曲线,我可以通过“printf”语句检查它们,但是我实际上没有得到曲线 我做错了什么?我该如何解决 #include "stdafx.h" #include <gl/glut.h> #include <cstdio> struct point { float x; float y; }; // simple linear interpolation betwee

我必须使用贝塞尔曲线创建运动路径。在运行下面的程序时,我沿着控制多边形获取值,该多边形应该是我运动路径的曲线,我可以通过“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;
}
#包括“stdafx.h”
#包括
#包括
结构点
{
浮动x;
浮动y;
};
//两点间的简单线性插值
无效lirp(点和目标、常数点和a、常数点和b、常数浮动t)
{
目的地x=a.x+(b.x-a.x)*t;
目的地y=a.y+(b.y-a.y)*t;
}
//计算bezier曲线上的点。t从0变为1.0
void bezier(点和目标、常数点和a、常数点和b、常数点和c、常数点和d、常数浮点t)
{
ab点、bc点、cd点、abbc点、bccd点;
lirp(ab,a,b,t);//a和b之间的点
lirp(bc,b,c,t);//b和c之间的点
lirp(cd,c,d,t);//c和d之间的点
lirp(abbc,ab,bc,t);//ab和bc之间的点
lirp(bccd,bc,cd,t);//bc和cd之间的点
lirp(dest,abbc,bccd,t);//贝塞尔曲线上的点
}
作废提款(){
glClear(GLU颜色缓冲位);
GL3F(1.0,1.0,1.0);
gl点大小(20);
点a={700100};
点b={650,20};
点c={600,180};
点d={550100};

对于(int i=0;i您正在使用
glOrtho()
设置一个投影体积,该体积仅为1x1。如果您想查看曲线(其尺寸为数百个单位),则需要将其展开


另外,在for循环结束之前,不要
glutSwapBuffers()
;否则你可能只看到一半的点。你还应该将
glBegin()
/
glEnd()
放在循环之外。

好的,但是我还可以用什么来代替glOrtho()?贝塞尔函数只返回“x”和“y”曲线的坐标;这不是一个简单的2D绘图函数吗?
glOrtho
很好。问题出在你对
glOrtho
的参数上。好哇!!成功了。我对glOrtho接受的值的范围不是很清楚。将我的windowsize尺寸传递到右侧和顶部参数就成功了!谢谢你的解释!:完成更多问题:由于我实际上要创建一条由曲线定义的运动路径,如何使其一次移动一个点?我的意思是,所有的点在glVertex接收到它们后立即被绘制。我尝试了以下方法: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);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清洁屏幕和深度缓冲区glLoadIdentity();glVertex3f(p_x,p_y,0.0)}glEnd();glutSwapBuffers();但每次我都会在重复之前得到完整的曲线,而不是一次一个点。哈哈,贝塞尔代码来自我的旧网站:这是过去的爆炸。噢!谢谢你让我了解曲线近似。这解释得太好了。