// simpleevaluator.cpp
// by Glenn G. Chappell
// March 2004
//
// For CS 481/681
// Very Simple OpenGL Evaluator Demo


#include <iostream>
using std::cerr;
using std::endl;
#include <stdlib.h>
//using std::exit;
#include <GL/glut.h> // GLUT stuff, includes OpenGL headers as well

// Global variables
// Window/viewport
const int startwinsize = 400; // Starting window width & height, in pixels

// Keyboard
const int ESCKEY = 27;        // ASCII value of escape character

// For Bezier curve
const int numcontrolpts = 4;  // Number of control points
                              // Below are coord's of control points
                              // (Must use 3 coord's each, even in 2-D.)
GLfloat controlpts[numcontrolpts][3] = {
   {-0.9, -0.9,  0.0},
   {-0.5,  0.2,  0.0}, 
   { 0.9, -0.9,  0.0},
   { 0.9,  0.9,  0.0}
};

const int numdrawsegs = 20;   // Number of segments drawn in the Bezier curve


// display
// The GLUT display function
void display()
{
   // Clear screen
   glClear(GL_COLOR_BUFFER_BIT);

   // Draw Bezier curve
   glColor3d(0.9, 0.1, 0.1);
   glLineWidth(3.0);

   glEvalMesh1(GL_LINE, 0, numdrawsegs);

   // Here, I used an evaluator grid.
   // Code to do the same thing, "manually", is as follows:

   //glBegin(GL_LINE_STRIP);
   //   for (int i=0; i<=numdrawsegs; ++i)
   //      glEvalCoord1d(GLdouble(i)/numdrawsegs);
   //glEnd();

   glutSwapBuffers();
}


// reshape
// The GLUT reshape function
void reshape(int w, int h)
{
   glViewport(0, 0, w, h);

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   // Set up coordinate system so that aspect ratios are always correct,
   //  and the region from -1..1 in x & y always just fits in the viewport.
   if (w > h)
      gluOrtho2D(-double(w)/h, double(w)/h, -1., 1.);
   else
      gluOrtho2D( -1., 1., -double(h)/w, double(h)/w);

   glMatrixMode(GL_MODELVIEW);  // Always go back to modelview mode
}


// keyboard
// The GLUT keyboard function
void keyboard(unsigned char key, int x, int y)
{
   switch (key)
   {
   case ESCKEY:  // ESC: Quit
      exit(0);
      break;
   }
}


// idle
// The GLUT idle function
void idle()
{
   // Print OpenGL errors, if there are any (for debugging)
   if (GLenum err = glGetError())
   {
      cerr << "OpenGL ERROR: " << gluErrorString(err) << endl;
   }
}


// init
// Initializes GL states
// Called by main
void init()
{
   // Set background color
   glClearColor(1.0, 1.0, 1.0, 0.0);

   // Set up Bezier curve evaluator
   glMap1f(GL_MAP1_VERTEX_3,   // target: 1-d [curve], 3 coord's per pt
           0.0, 1.0,           // start & end param value
           3,                  // "stride": pts stored 3 GLfloat's apart
           numcontrolpts,      // no. of control points
           &controlpts[0][0]); // control pt data
   glEnable(GL_MAP1_VERTEX_3); // Enable this evaluator

   // Set up grid for evaluator.
   // This only needs to be done when using glEvalMesh1.
   glMapGrid1d(numdrawsegs, 0.0, 1.0);
}


int main(int argc, char ** argv)
{
   // Initialize OpenGL/GLUT
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

   // Make a window
   glutInitWindowSize(startwinsize, startwinsize);
   glutInitWindowPosition(50, 50);
   glutCreateWindow("CS 481/681 - Simple OpenGL Evaluator Demo");

   // Initialize GL states & register callbacks
   init();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutIdleFunc(idle);

   // Do something
   glutMainLoop();

   return 0;
}
