【问题标题】:Animating a scene using OpenGL 3 in C在 C 中使用 OpenGL 3 为场景设置动画
【发布时间】:2016-04-05 19:27:07
【问题描述】:

我正在使用 OpenGL 3 创建一个小型动画电影,并在 Visual Studio Community 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();
}

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

我已经完成了场景的绘制,但我无法制作动画。在我看来,我在动画中做得很好,也许我的空闲功能中的某个地方不允许我让它工作。我尝试了所有方法,但无法使其正常工作,而且我真的不知道问题出在哪里。请让我知道主要问题是什么。提前致谢。

【问题讨论】:

  • 您提到您正在使用 OpenGL 3...但是看起来您正在使用已弃用的固定管道功能.​​..
  • @rlam12 是吗?你能告诉我是哪一个吗?我是opengl的新手。
  • 我也不是专家,但我可以识别 glTranslatef glMatrixMode 和其他一些正在从标准中删除的函数,OpenGL 3 对所有内容都使用着色器和自定义矩阵但我无法真正说出您的代码有什么问题,再次,不是专家。祝你好运,新年快乐!
  • int main(int argc, char **argv) 是合法的 C。但是,有些人喜欢 int main(int argc, char *argv[])。但是,启用警告是个好建议。
  • 你看过this simple animation使用OpenGL吗? glutPostRedisplay()glViewPort() 跳出来特别有趣。

标签: c opengl animation translate-animation


【解决方案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 代码是可以的。

【讨论】:

  • 很高兴你问到。我刚才已经提供了全局变量。
  • 所以你看到的是你的恐龙形象,但它没有动?
  • 另一个想法:如果你添加了trace-to-file代码,也可以输出时间戳。您也可以每次打开-写入-关闭跟踪文件,这样您就可以在程序处于活动状态时查看该文件。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多