Skip to content
Snippets Groups Projects
Select Git revision
  • a04f6dcd0eaf248c9ae92a67453cdec2f6cc749c
  • master default
2 results

pendulum-2.c

  • Forked from Peter Gerwinski / ainf
    Source project has a limited visibility.
    pendulum-2.c 1.01 KiB
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/freeglut.h>
    #include <math.h>
    #include "opengl-magic.h"
    
    #define phi0 1.0    /* geg. Anfangswert */
    #define omega0 0.0  /* geg. Anfangswert */
    #define dt 0.05
    #define g 9.81
    #define l 1.0
    
    double t = 0.0;
    double phi = phi0;
    double omega = omega0;
    
    void draw (void)
    {
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      set_material_color (0.8, 0.8, 1.0);
      glPushMatrix ();
      glRotatef (90, 1.0, 0.0, 0.0);
      glTranslatef (0.0, 0.0, -0.75);
      glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
      glTranslatef (0.0, 0.0, -1.0);
      glutSolidCylinder (0.01, l, 13, 1);
      glutSolidSphere (0.1, 31, 10);
      glPopMatrix ();
      glFlush ();
    }
    
    void timer_handler (int value)
    {
      t += dt;
      phi += dt * omega;
      omega += dt * (-g / l) * sin (phi);
      glutPostRedisplay ();
      glutTimerFunc (50, timer_handler, 0);
    }
    
    int main (int argc, char **argv)
    {
      init_opengl (&argc, argv, "Pendulum");
      glutDisplayFunc (draw);
      glutTimerFunc (50, timer_handler, 0);
      glutMainLoop ();
      return 0;
    }