#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>

/* For Santa Clara's loader */
#define BRAINDAMAGED_LOADER

#ifdef BRAINDAMAGED_LOADER
#define cosf(x) (float)cos((float)(x))
#endif

/* Called when window needs to be redrawn */
void redraw()
{
    glClear(GL_COLOR_BUFFER_BIT); /* clear screen to black */
    /* just draw a boring red triangle */
    glBegin(GL_TRIANGLES);
    glColor3f(1.f, 0.f, 0.f);
    glVertex2f(-.5f, -.5f);
    glVertex2f( .5f, -.5f);
    glVertex2f( 0.f, .5f);
    glEnd();
    glutSwapBuffers(); /* double buffer; call sleeps until refresh time */
}

/* 
** Called when nothing else is happening. Moves the object around using
** transforms. All operations are happening on the current transform
** which by default is the modelview matrix.
*/
void anim()
{
    static float angle = 0.f;
    float x;
    angle += .1f;
    x = .5f * cosf(angle);
    glPushMatrix(); /* save current matrix */
    glTranslatef(x, 0.f, 0.f); /* translate it */
    redraw(); /* draw with the translated coordinates */
    glPopMatrix(); /* restore the matrix to it's original (identity) state */
}



/* Parse arguments, and set up interface between OpenGL and window system */
main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(512, 512);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    (void)glutCreateWindow("simple animation program");
    glutDisplayFunc(redraw);
    glutIdleFunc(anim);
    glutMainLoop();
}
