#include<GL/glut.h>
#include<GL/glu.h>
#include<GL/gl.h>
#include <stdlib.h>
#include <stdio.h>

GLuint listName;

void init (void)
{
   listName = glGenLists (1);
   glNewList (listName, GL_COMPILE);
      glColor3f (1.0, 0.0, 0.0);  /*  colore corrente rosso  */
      glBegin (GL_TRIANGLES);
      glVertex2f (0.0, 0.0);
      glVertex2f (1.0, 0.0);
      glVertex2f (0.0, 1.0);
      glEnd ();
      glTranslatef (1.5, 0.0, 0.0); /*  traslazione verso destra  */
   glEndList ();
   glShadeModel (GL_FLAT);
}

void drawLine (void)
{
   glBegin (GL_LINES);
   glVertex2f (0.0, 0.5);
   glVertex2f (15.0, 0.5);
   glEnd ();
}

void display(void)
{
   GLuint i;
  
   glClear (GL_COLOR_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glColor3f (0.0, 1.0, 0.0);  /*  colore corrente verde  */
   for (i = 0; i < 10; i++)    /*  disegna 10 triangoli    */
      glCallList (listName);

   drawLine ();  /*  la linea viene disegnata in rosso a destra 
                     rispetto ai triangoli  */
   glPopMatrix();
   glFlush ();
}

void reshape(int w, int h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   printf("w=%d h=%d\n",w,h);
   if (w <= h) 
      gluOrtho2D (-2.0, 2.0, -2.0 * (GLfloat) h/(GLfloat) w, 2.0 * (GLfloat) h/(GLfloat) w);
   else 
      gluOrtho2D (-2.0 * (GLfloat) w/(GLfloat) h, 2.0 * (GLfloat) w/(GLfloat) h, -2.0, 2.0); 
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

/*  Main Loop
 */
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize(650, 50);
   glutCreateWindow(argv[0]);
   init ();
   glutReshapeFunc (reshape);
   glutDisplayFunc (display);
   glutKeyboardFunc (keyboard);
   glutMainLoop();
   return 0;
}
