/*  w2gl.c   connect the points                         */
/*           mouse to place points, button 3 to connect */

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

static GLfloat points[100][3];
static int n_points = 0;
static int n = 0;
static int width=400;
static int height=300;

void display(void)
{
  char text[]="press button 1 for point, press button 3 to connect";
  char text2[]="click here to exit";
  char *p;
  int i, j;
  
  /* clear window */
  glClear(GL_COLOR_BUFFER_BIT); 
  glLoadIdentity ();
  glColor3f(0.0, 0.0, 0.0);
  
  /* draw rectangle */
  glBegin(GL_LINE_LOOP);
    glVertex2f(-0.95,  0.85);
    glVertex2f(-0.95, -0.85);
    glVertex2f( 0.95, -0.85);
    glVertex2f( 0.95,  0.85);
  glEnd();

  /* draw text, in its own context */
  glPushMatrix();
    glLoadIdentity ();
    glColor3f(0.0, 0.0, 0.0);
    glEnable(GL_LINE_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glTranslatef(-0.95, 0.89, 0.0);
    glScalef(0.00055, 0.00055, 0.0);
    for(p=text; *p; p++)
      glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  glPopMatrix();

  /* draw text2, in its own context */
  glPushMatrix();
    glLoadIdentity ();
    glColor3f(0.0, 0.0, 0.0);
    glEnable(GL_LINE_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glTranslatef(-0.6, -0.97, 0.0);
    glScalef(0.00055, 0.00055, 0.0);
    for(p=text2; *p; p++)
      glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  glPopMatrix();
  
  /* Draw the points */
  glColor3f(1.0, 0.0, 0.0);
  glPointSize(4.0);
  for(i=0; i<n; i++)
  {
    glBegin(GL_POINTS);
      glVertex3fv(points[i]);
    glEnd();
  }

  glColor3f(0.0, 0.0, 1.0);
  for(j=0; j<n_points-1; j++)
  {
    for(i=j+1; i<n_points; i++)
    {
      glBegin(GL_LINES);
        glVertex3fv(points[j]);
        glVertex3fv(points[i]);
      glEnd();
    }
  }
  
  glFlush(); 
}

/* This routine handels mouse events */
static void mouse(int button, int state, int x, int y)
{
  float wx, wy;
  
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  {
    /* Translate back to our coordinate system */
    wx = (2.0 * x) / (float)(width - 1) - 1.0;
    wy = (2.0 * (height - 1 - y)) / (float)(height - 1) - 1.0;
    printf("x=%d, y=%d, wx=%g, wy=%g \n", x, y, wx, wy);
    if(wy < -0.85) exit(0);

    /* See if space for more points */
    if (n >= 99) return;
    /* reset if already connected */
    if(n_points != 0)
    {
      n_points=0;
      n=0;
    }
    /* Save the point */
    points[n][0] = wx;
    points[n][1] = wy;
    points[n][2] = 0.0;
    n++;
  }
  if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  {
    /* connect points */
    n_points=n;
  }
  glutPostRedisplay();
}

/* This routine handles window resizes */
void reshape(int w, int h)
{
  width = w;
  height = h;
  /* Set the transformations */
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glViewport(0, 0, w, h);
}

void init()
{
  /* set clear color to white */
  glClearColor (1.0, 1.0, 1.0, 0.0);
  /* set fill  color to black */
  glColor3f(0.0, 0.0, 0.0);

  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  glMatrixMode (GL_MODELVIEW);
  glViewport(0, 0, width, height);
}

int main(int argc, char* argv[])
{
  glutInit(&argc,argv);
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
  glutInitWindowSize(width, height);
  glutInitWindowPosition(0,0); 
  
  glutCreateWindow(argv[0]); 
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMouseFunc(mouse);
  init();
  glutMainLoop();
  return 0;
}

