如何使对象在OpenGL中遵循路径?

时间:2017-11-21 16:50:02

标签: c++ animation opengl glut bezier

我有一个在Blender中制作的对象,我需要它遵循一条路径,路径是我生成的Bezier曲线,然后我将所有曲线点保存在列表中。所以我List<Point> Points事情就是我不知道如何使对象遵循那个点列表,我在glutIdleFunc中做了但是后来我意识到这是错的,因为它每次程序空闲时都会遍历列表...所以我想使用glutTimerFunc每100毫秒迭代一次列表,但是如何?我试过了,但它把对象画得很奇怪。

以下是一些代码:

void timer(int value)
{
   std::string str2 = "Body";
   std::list<Point>::iterator itBezier0 = BezierPoints.begin(), itBezier1 = BezierPoints.begin();
    ++itBezier1;

   for(std::list<Object>::iterator it = OBJ.begin(); it!=OBJ.end(); ++it)
    {
        if(it->name.compare(str2) == 1)
        { //BODY
            for(list<Face>::iterator it2 = it->faces.begin(); it2 != it->faces.end(); ++it2)
            {   //FACES LIST
                for(list<Vertex>::iterator it3 = it2->vertices.begin(); it3 != it2->vertices.end(); ++it3)
                {
                    it3->X = it3->X + (itBezier1->X - itBezier0->X);
                    it3->Y = it3->Y + (itBezier1->Y - itBezier0->Y);
                    it3->Z = it3->Z + (itBezier1->Z - itBezier0->Z);
                }

            }
        }
        else
        {
            for(list<Face>::iterator it2 = it->faces.begin(); it2 != it->faces.end(); ++it2)
            {   //FACES LIST
                for(list<Vertex>::iterator it3 = it2->vertices.begin(); it3 != it2->vertices.end(); ++it3)
                {
                    it3->X = it3->X + (itBezier1->X - itBezier0->X);
                    it3->Y = it3->Y + (itBezier1->Y - itBezier0->Y);
                    it3->Z = it3->Z + (itBezier1->Z - itBezier0->Z);

                }

            }
        }

    }

    glutPostRedisplay();
    glutTimerFunc(100, timer, 1);
}

这是主要的:

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize (1280, 720);
    glutInitWindowPosition (0, 0);
    glutCreateWindow ("BB-8");
    init ();
    OBJ = ReadObject();
    glutDisplayFunc(display);
    glutIdleFunc(spinBody);
    glutTimerFunc(100, timer, 1);
    glutSpecialFunc(keySpecial); // Tell GLUT to use the method "keyPressed" for key presses
    glutSpecialUpFunc(keySpecialUp); // Tell GLUT to use the method "keyUp" for key up events
    glutMainLoop();
    return 0;

}

0 个答案:

没有答案