在 C 中使用 OpenGL 3 动画场景

Animating a scene using OpenGL 3 in C

我正在使用 OpenGL 3 制作一部小动画电影,并在 visual studio 社区 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; 
}

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

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();
}

我的全局变量 //帧计数器 整数帧,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;

我已经绘制了场景,但我无法制作动画。在我看来,我在动画中做得很好,也许我的空闲函数中的某个地方不允许我让它工作。我尝试了一切,但我无法让它工作,我不知道问题出在哪里。请让我知道主要问题是什么。提前致谢。

我假设大多数命名的变量都是全局的。由于您没有向我们展示初始值,我假设它们都为零(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。)

没有你的 headers,我们无法判断出什么问题。

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

最好的情况是我无需自己编译并尝试猜测您遗漏了什么,OpenGL 代码就可以了。

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