/*  setwindow.cpp
 *  This program demonstrates the ploting of a function using
 *  the setWindow and setViewport functions.
 *  Click the right button to exit.
 */
#include << X11/Xlib.h>
#include << math.h>
#include << GL/glut.h>
#include << stdlib.h>
#include << stdio.h>

const int screenWidth = 640;
const int screenHeight = 480;        

// setWindow 
void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
}

// setViewport
void setViewport(int left, int right, int bottom, int top)
{
glViewport(left, bottom, right - left, top - bottom);
}

void myInit(void) 
{
   glClearColor (1.0, 1.0, 1.0, 0.0);   // set black background color
   glColor3f (0.0f, 0.0f, 0.0f);       // set the drawing color
   glPointSize (2.0);                  // set dot size 2 x 2
   setWindow(0.0, screenWidth, 0.0, screenHeight);
}

void myMouse(int button, int state, int x, int y)
{
  switch (button) {
     case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
           exit (-1);
        break;
     default:
        break;
  }
}

// plot the sinc function, using world coordinates
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);     // clear the screen
setWindow(-5.0, 5.0, -0.3, 1.0);           // set the window
setViewport(0, 640, 0, 480);               // set the viewport
glBegin(GL_LINE_STRIP);
for(GLfloat x = -4.0; x < 4.0; x += 0.1)     // draw the plot
glVertex2f(x, sin(3.14159 * x) / (3.14159 * x));
glEnd();
glFlush();
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);                            // initialize the toolkit
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);     // set display mode
   glutInitWindowSize (screenWidth, screenHeight);   // set screen window size
   glutInitWindowPosition (100, 150);       // set window position on screen
   glutCreateWindow (argv[0]);              // open the screen window
   myInit ();
   glutDisplayFunc(myDisplay);              // register redraw function
   glutMouseFunc(myMouse);                  // register myMouse function
   glutMainLoop();                          // go into a perpetual loop
   return 0;
}