/* Lesson 2
 *
 * Second.c                Version 1.0
 *
 * Topics:  2d Polygon
 *          rotate, scale
 *          Keyboard input
 *
 * Draw a 2d triangle using gouraud shading model
 * rotate the image by pressing the X,Y, or Z keys
*/

#include <gl.h>        /* the graphics library */
#include <device.h>    /* keyboard defines and others */

float xrot, yrot, zrot;        /* rotation values */
int rotate_mode;               /* Boolean check for rotation mode */
float scaler = 40.0;           /* scale value */

     /* define some basic RGB colors */
int red[3]          = {255,0,0};
int green[3]        = {0,255,0};
int blue[3]         = {0,0,255};
int black[3]        = {0,0,0};
int white[3]        = {255,255,255};


  /* define the corners of a triangle */
int triangle_0[2]= { -1, -1 },
    triangle_1[2]= {  0,  1 },
    triangle_2[2]= {  1, -1 };

/********************************************************************/
main()
{
     
    initalize_graphics();     
    
    qdevice(RIGHTMOUSE);          /* Set the 3 Mouse buttons as input */
    qdevice(MIDDLEMOUSE);
    qdevice(LEFTMOUSE);
    
    while(!getbutton(ESCKEY)) {   /* Cycle through main loop until ESC key is pressed */

      pushmatrix();               /* push in a new matrix to use */
        
        c3i(black);               /* set the background color  */
        clear();                  /* clear the window using last color */

        check_the_keyboard();     /* check for keyboard input */
                  
        pushmatrix();             /* push a new matrix so we can draw on it */
        
          translate( 50.0, 50.0, 0.0 );  /* move to the middle of the window */
          scale(scaler, scaler, 1.0);    /* increase the X and Y scale by 40 */
          rot( xrot, 'x');               /* rotate the matrix on the X plane */
          rot( yrot, 'y');               /*   "     "     "    "  "  Y   "   */
          rot( zrot, 'z');               /*   "     "     "    "  "  Z   "   */

          draw_triangle();               /* draw a triangle */
          
        popmatrix();                     /* pop out of the matrix */

        text_message();
        
          
      popmatrix();                /* pop out of the last matrix */
      
      swapbuffers();            
    }

}

/********************************************************************/
draw_triangle()
{
        bgnpolygon();         /* tell the computer we are starting a polygon */
          c3i(red);           /* set the current color to red */
          v2i(triangle_0);    /* set the first vertice of the polygon */
          c3i(green);         /* set the current color to green */
          v2i(triangle_1);    /* set the second vertice of the polygon */
          c3i(blue);          /* set the current color to blue */
          v2i(triangle_2);    /* set the third vertice of the polygon */
        endpolygon();         /* tell the computer we are closing the polygon */                              
}


/********************************************************************/
check_the_keyboard()
{
  
    /* change the rotation values according to what keys or mouse buttons are pressed */
    
  if(getbutton(LEFTSHIFTKEY) || getbutton(RIGHTSHIFTKEY)) {
     if(getbutton(XKEY) ) xrot--;
     if(getbutton(YKEY) ) yrot--;
     if(getbutton(ZKEY) ) zrot--;
     if(getbutton(SKEY) ) scaler--;
  } else { 
     if(getbutton(XKEY) ) xrot++;
     if(getbutton(YKEY) ) yrot++;
     if(getbutton(ZKEY) ) zrot++;
     if(getbutton(SKEY) ) scaler++;
  }


 if(getbutton(LEFTSHIFTKEY) || getbutton(RIGHTSHIFTKEY)) {
     if( getbutton(LEFTMOUSE)   && !getbutton(MIDDLEMOUSE) ) xrot--;
     if( getbutton(MIDDLEMOUSE) && !getbutton(RIGHTMOUSE) && !getbutton(LEFTMOUSE) ) yrot--;
     if( getbutton(RIGHTMOUSE)  && !getbutton(MIDDLEMOUSE) ) zrot--;
   } else { 
     if( getbutton(LEFTMOUSE)   && !getbutton(MIDDLEMOUSE) ) xrot++;
     if( getbutton(MIDDLEMOUSE) && !getbutton(RIGHTMOUSE) && !getbutton(LEFTMOUSE) ) yrot++;
     if( getbutton(RIGHTMOUSE)  && !getbutton(MIDDLEMOUSE) )  zrot++;
 }

     if( getbutton(MIDDLEMOUSE) && getbutton(RIGHTMOUSE) ) scaler++;
     if( getbutton(MIDDLEMOUSE) && getbutton(LEFTMOUSE)  ) scaler--;




  if(getbutton(RKEY) ) 
      rotate_mode = 1;

  if(rotate_mode) {
       xrot++; yrot++; zrot++; scaler++;
  }
  
    /* make sure the rotation values arn't greater then 360 */
  if( xrot > 360.0 ) xrot = 0.0;
  if( yrot > 360.0 ) yrot = 0.0;
  if( zrot > 360.0 ) zrot = 0.0;
  if( scaler > 60.0) scaler = -60.0;   /* this will create a mirror image */
  

}

/*******************************************************************/
text_message()
{
        c3i(white);
        cmov2( 3.0, 97.0 );
        charstr("Iris Tutorial");
        cmov2( 3.0, 95.0 );
        charstr("Press X, Y, or Z to rotate, S for Scale, and the Shift keys to reverse");
        cmov2( 3.0, 93.0 );
        charstr("Press R for rotate mode, and ESC to quit");        
}


/*******************************************************************/
initalize_graphics()
{
   
    prefposition(10, 990, 10, 990);     /* preset the window size, the format is */
                                        /* (x1,x2,y1,y2)                         */
    winopen("2D Triangle");             /* Open a window and name it 'First' */
    doublebuffer();                     /* Use doublebuffer mode */
    RGBmode();                          /* Use RGB color mode */
    gconfig();                          /* Configure the window */

    ortho2( 0.0, 100.0, 0.0, 100.0 );   /* cast an orthographic projection over the window */
                                        /* format is (right, left, bottom, top)            */
       
 }

