//  This program shows how to open a single window and plot a colored 
//  Pixel for every coordinate in the window
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include <GL/glut.h>

#define initial_width 360             // Initial Window Width 
#define initial_height 360            // Initial Window Height

int main_window;          // Main Window ID 
float xmax=2.0, xmin=-2.0, ymax=2.0, ymin=-2.0;  // X-axis and y-axis values

// Draw Background Colors 

DrawGraph()
{ int row, col;               // Loop Variables for Pixel Positions 
  float x, y, xstep, ystep, r, g, b;

  xstep = (xmax-xmin)/359.0;  // X-distance between pixels (MaxPixel - 1)
  ystep = (ymax-ymin)/359.0;  // Y-distance between pixels (MaxPixel - 1)

  glBegin(GL_POINTS);
  y = ymin;                        // Initialize First Row y-value
  for (row=0; row<360; row++){     // Go to each Row
    x = xmin;                      // Initialize Start of Column 
    for (col=0; col<360; col++){   // Go to each Column in the Row
        r = 0.5 * (1 + sin( col / 90.0));          // Calculate Red
        g = 0.5 * (1 + sin ( (row + col) / 60.0)); // Calculate Green
        b = 0.5 * (1 + cos(row /30.0));            // Calculate Blue
        glColor3f(r, g, b);      // Set RGB Value for Pixel
        glVertex3f(x, y, 0.0);   // Plot (X,y) Coordinate
        x += xstep;  // Find out next x-coordinate in row
    }
    y += ystep;    // Go to y-coordinate of the next Row
  }
  glEnd();
} 

// Draw a set of axes

DrawAxes()
{  glBegin(GL_LINES);                         // Start Drawing Lines
    glColor3f(0.0, 0.0, 0.0);                 // Set Drawing Color
    glVertex3f(xmax,0.0, 0.0);                // Starting Point for x-axis
    glVertex3f(xmin, 0.0, 0.0);               // Ending Point
    glVertex3f(0.0, ymax,0.0);                // Staring Point for y-axis
    glVertex3f(0.0, ymin, 0.0);               // Ending Point
  glEnd();
 
}

// Main Display Function 
void display1(void)
{ 
  glClear(GL_COLOR_BUFFER_BIT);
  DrawGraph();
  DrawAxes();                                 
  glFlush();                                  // Force all drawing
}

void init(void)
{ /* select clearing (background) color   */
   glClearColor(0.0, 0.0, 0.0, 0.0);

/* initialize viewing values */
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(xmax, xmin, ymax, ymin, -1.0, 1.0);
}
                               

int main(int argc, char **argv)
{
  glutInit(&argc, argv);                      // Primary Initialize  
  glutInitDisplayMode(GLUT_RGB);              // Initialize RGB mode 
  glutInitWindowSize(initial_width, initial_height);  // Initialize window
  main_window = glutCreateWindow("Colors 2b ");  // Create main window 
  init();				      // My initialization
  glutDisplayFunc(display1);                  // Pass pointer to display
  glutMainLoop();                             // Enter main event loop
  return 0;             /* ANSI C requires main to return int. */
}
