Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:39:18+00:00 2026-06-10T02:39:18+00:00

I’ve been trying out some simple drawings and animations with freeglut (on Linux in

  • 0

I’ve been trying out some simple drawings and animations with freeglut (on Linux in virtual machine). Until now everything built and worked just fined. My latest try was moving a square around with glTimerFunc. Although it built without any errors with gcc stack.c -lGL -lglut -o stack, the animation itself doesn’t work. I’ve looked at every example of an animation with glut I could find, but didn’t see any problems with my code. Could anyone explain to me what my mistake is?

(edit: see below for working code)

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

EDIT

@datenwolf: I looked at the other question you answered more carefully this time and took some of the code from there, works great!

Here’s the new version:

#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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:39:20+00:00Added an answer on June 10, 2026 at 2:39 am

    You should not use a event timer for animation. Instead you should call glutPostDisplay from the idle function and measure the time between calls to the display function and base the animation time on this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.