在C

时间:2016-01-01 01:35:58

标签: c opengl animation translate-animation

我正在使用OpenGL 3创建一个小型动画电影,并在2015年的视觉工作室社区中实现它。我试图制作一个至少在第一个时间向右移动的恐龙动画,但我无法以某种方式使其动画。

这是我的主要内容:

#include "glut.h"
#include "globaldeclare.h"
#include "dino.h"
#include "scene1.h"
#include "scene1_animation.h"
#include <math.h>

void init(void)
{
    glClearColor(0.0, 204.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 500, 0, 500);
    glMatrixMode(GL_MODELVIEW);

}

void myIdle() {
    frame++;
    scene1_dino_idle();
    glutPostRedisplay();
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    //Animation happens here
    if (scene_counter == 1) {
        scene1();
        scene1_animation();
    }

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(1300, 800);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Walking with dinosaur");
    glutDisplayFunc(display);
    glutIdleFunc(myIdle);
    init();
    glutMainLoop();
    return 0; 
}

这是scene1发生动画的地方(但它没有发生。):)

void scene1_dino_idle() {
    //idle
    if (dino_slidey > 0 && dino_count == 1) dino_movey = 1;
    if (dino_movey == 1 && dino_count == 1) dino_slidey -= 2;//1
    if (dino_slidey < -30 && dino_count == 1) dino_movey = 0;
    if (dino_movey == 0 && dino_count == 1) dino_slidey += 2;//1

    if (dino_slidex > 1000 && dino_count == 1) { dino_movex = 1; dino_count = 2; dinoleg_count = 2; }
    if (dino_movex == 1 && dino_count == 1) dino_slidex -= 6;//4
    if (dino_slidex < -1600 && dino_count == 1) dino_movex = 0;
    if (dino_movex == 0 && dino_count == 1) dino_slidex += 6;//4
}

void scene1_dino_animation() {
    //moving to the right
    glPushMatrix();
    if (dino_count == 1) {
        glTranslatef(dino_slidex, dino_slidey, 0);
        glTranslatef(0, 100, 0);
        glScalef(0.6, 0.6, 1);
    }
    glScalef(dinoR_lost, dinoR_lost, 0);
    dino();
    glPopMatrix();
}

void scene1_animation() {
    scene1_dino_animation();
}

我的全局变量     //框架计数器     int frame,scene_counter = 1;

//dino animation
float dino_movex, dino_movey, dino_count, dino_slidex, dino_slidey, dinoR_lost, dinoL_lost;

//dino leg
float dinoleg_angle = 0, dinoleg_move_x, dinoleg_move_y, dinoleg_count = 0;

我已完成绘制场景,但我无法为其设置动画。在我看来,我在动画中做得很好,也许在我的空闲功能的某个地方不允许我使它工作。我尝试了一切,但我无法使其工作,我不知道问题究竟在哪里。请告诉我主要问题是什么。提前致谢。

2 个答案:

答案 0 :(得分:1)

我假设命名的大多数变量都是全局变量。由于您没有向我们显示初始值,因此我假设它们全部为零(scene_counterdino_count除外;如果其中任何一个为零,则不会发生任何事情),并且逐步完成几次迭代。 (请原谅此跟踪的拥挤布局。)这些是每次拨打scene1_dino_idle()时入口处的值:

  scene_counter   dino_movey   dino_slidey  dinoleg_count
frame  | dino_count | dino_movex | dino_slidex   |
  |    |      |     |      |     |      |        |
  1    1*     1*    0      0     0      0        0
  2    1      1     0      0     2      6        0
  3    1      1     1      0     0     12        0
  4    1      1     1      0    -2     18        0
  5    1      1     1      0    -4     24        0
. . .
  f    1      1     1      0 (f-3)*-2 (f-1)*6    0
. . .
 18    1      1     1      0   -30    102        0
 19    1      1     0      0   -30    108        0
 20    1      1     0      0   -28    114        0
. . .

从这一点看来,似乎如果您遇到问题,那么因为scene_counterdino_count(或两者)都为零。另一种可能性是你的全局变量存在范围问题;如果你的声明中某处有拼写错误,C可能会将其中一个或多个视为局部变量。 (在某些版本的C中,未明确声明的变量将默认为int。)

如果没有标题,我们无法说出错误。

由于某些调试器与图形代码相关性较差,您可以考虑添加调试代码以将变量跟踪到文件中,就像我上面手动执行的那样。

最好的我可以告诉我自己没有编译它并试图猜测你遗漏了什么,OpenGL代码没问题。

答案 1 :(得分:1)

我设法让它发挥作用。在我绘制的一些场景中,只有一个dino但是它没有移动所以我从scene1.h中移除了dino() scene1()。之后它就失踪了,但我注意到一些点移动(我不知道那是从哪里来的)。现在我通过在glutPostRedisplay()函数中添加scene1_dino_idle()来修复它。然后我删除此代码glScalef(dinoR_lost, dinoR_lost, 0);,以便显示dino。谢谢大家的帮助。新年快乐。