【问题标题】:GLUT animation with glTimerFuncGLUT 动画与 glTimerFunc
【发布时间】:2012-08-16 00:23:13
【问题描述】:

我一直在用 freeglut(在 Linux 上的虚拟机中)尝试一些简单的绘图和动画。到现在为止,所有构建和工作的东西都很好。我最近的尝试是用glTimerFunc 移动一个正方形。尽管它使用gcc stack.c -lGL -lglut -o stack 构建时没有任何错误,但动画本身不起作用。我已经查看了我能找到的每个带有 glut 的动画示例,但没有发现我的代码有任何问题。谁能向我解释我的错误是什么?

(编辑:工作代码见下文)

#include <stdio.h>
#include <stdlib.h>
#include <GL/freeglut.h>

int dx = 0;

#define TIMERSECS 100

void animate(int value) {
  glutTimerFunc(TIMERSECS, animate, 1);
  if (dx > 0.5) {
    dx = -0.5;
  }
  else {
    dx += 0.1;
  }
  glutPostRedisplay();
}

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT);

  glColor3f(0.0, 0.0, 0.5);

  glBegin(GL_POLYGON);
    glVertex2d(-0.5+dx, 0.5);
    glVertex2d(-0.5+dx, -0.5);
    glVertex2d(0.5+dx, -0.5);
    glVertex2d(0.5+dx, 0.5);
  glEnd();

  glutSwapBuffers();
}

void initialize(void) {
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glShadeModel(GL_SMOOTH);
}

void main(int argc, char *argv[]) {

  glutInit(&argc, argv);
  glutInitWindowPosition(100, 100);
  glutInitWindowSize(500, 500);

  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  glutCreateWindow(argv[0]);

  initialize();
  glutDisplayFunc(display);
  glutTimerFunc(TIMERSECS, animate, 0);
  glutPostRedisplay();

  glutMainLoop();
}

编辑

@datenwolf:我看了other question你这次回答得更仔细,并从那里拿了一些代码,效果很好!

这是新版本:

#include <stdio.h>
#include <stdlib.h>
#include <GL/freeglut.h>

int factor=100; // factor the animation is slowed down by

double dx = 0;

void animate(double speed);

static double ftime(void) {
    struct timeval t;
    gettimeofday(&t, NULL);

    return 1.0*t.tv_sec + 1e-6*t.tv_usec;
}

static double last_T;

static void idle(void) {
  const double now_T = ftime();
  const double delta_T = now_T - last_T;
  last_T = now_T;

  const double speed = delta_T * 60;

  animate(speed);

  glutPostRedisplay();
}

void animate(double speed) {
  if (dx > 1.5) {
    dx = -1.5;
  }
  else {
    dx += speed/factor;
  }
  glutPostRedisplay();
}

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT);

  glColor3f(0.0, 0.0, 0.5);

  glBegin(GL_POLYGON);
    glVertex2d(-0.5+dx, 0.5);
    glVertex2d(-0.5+dx, -0.5);
    glVertex2d(0.5+dx, -0.5);
    glVertex2d(0.5+dx, 0.5);
  glEnd();

  glutSwapBuffers();
}

void initialize(void) {
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glShadeModel(GL_SMOOTH);
}

void main(int argc, char *argv[]) {

  glutInit(&argc, argv);
  glutInitWindowPosition(100, 100);
  glutInitWindowSize(500, 500);

  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  glutCreateWindow(argv[0]);

  initialize();
  glutDisplayFunc(display);
  glutIdleFunc(idle);
  glutPostRedisplay();

  glutMainLoop();
}

Dankeschön für deine Hilfe!

【问题讨论】:

    标签: c opengl freeglut


    【解决方案1】:

    您不应将事件计时器用于动画。相反,您应该从空闲函数调用 glutPostDisplay 并测量调用显示函数之间的时间,并以此为基础动画时间。

    【讨论】:

    • 你的意思是glutPostRedisplay?我应该使用 glutIdleFunc 制作动画吗? (我试过了,但也没有用)
    • @teashorts:你应该在空闲函数中调用 glutPostRedisplay,或者将它自己注册为空闲函数。然后测量显示函数调用之间的时间;为此使用全局变量。
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多