// Program to draw a box in three dimensions
// by D. W. Hyatt  - Supercomputer Applications

#include <stdlib.h>
#include <GL/glut.h>
int wide=200, high=200, winX=10, winY=10;

// This routine draws a basic box in 3D.  The glOrtho() command sets the
// drawing volume to a cube ranging from -1 to +1 in all three directions
// whereas the box itself ranges from -0.5 to +0.5, which is located
// inside of this volume.

void DrawBox() 
{ 
    double x,y;
    glColor3f(1.0, 1.0, 0.0);  // Set Drawing Color to Yellow

    // For top and bottom of box
    glBegin( GL_LINE_STRIP );  // Begin drawing connected lines

      // Bottom of Box
      glVertex3f(-0.5, -0.5, -0.5);
      glVertex3f(-0.5, -0.5, 0.5);
      glVertex3f(0.5, -0.5, 0.5);
      glVertex3f(0.5, -0.5, -0.5);
      glVertex3f(-0.5, -0.5, -0.5);

      // Top of Box
      glVertex3f(-0.5, 0.5, -0.5);
      glVertex3f(-0.5, 0.5, 0.5);
      glVertex3f(0.5, 0.5, 0.5);
      glVertex3f(0.5, 0.5, -0.5);
      glVertex3f(-0.5, 0.5, -0.5);

    glEnd();	// Stop drawing connected lines

    // For side edges of box
    glBegin( GL_LINES );  // Start drawing line segments 

     glVertex3f(-0.5, 0.5, -0.5);
     glVertex3f(-0.5, -0.5, -0.5);

     glVertex3f(-0.5, -0.5, 0.5);
     glVertex3f(-0.5, 0.5, 0.5);

     glVertex3f(0.5, -0.5, 0.5);
     glVertex3f(0.5, 0.5, 0.5);

     glVertex3f(0.5, -0.5, -0.5);
     glVertex3f(0.5, 0.5, -0.5);

    glEnd();  // Stop drawing line segments

   glFlush();  // Forces writing all of the OpenGL actions to the screen.
} 

void display(void)
{ 
   glClear(GL_COLOR_BUFFER_BIT);  // Clear Background 

  // glLoadIdentity();  // Initialize Transformation Matrix

   glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);  // Set viewing dimensions

   // The following rotate operations are done to the transformation matrix
   //   prior to drawing of the Box
   // The glRotatef(d,x,y,z) requires four parameters: the amount of rotation
   //   in degrees and the axis of rotation (x, y, or z).

//   glPushMatrix();  // New frame of reference

     glRotatef(30.0, 0.0, 0.0, 1.0);   // Rotate 30 degrees around the z-axis
     glRotatef(20.0, 0.0, 1.0, 0.0);   // Rotate 20 degrees around the y-axis
     glRotatef(40.0, 1.0, 0.0, 0.0);   // Rotate 40 degrees around the z-axis

     DrawBox();   // Call Box Routine

 // glPopMatrix();  // Return to old frame of reference
}

void init(void)
{ 
   glClearColor(0.5, 0.5, 0.5, 0.0);  // Draw gray background
   
   glMatrixMode(GL_PROJECTION);  // Initialize viewing values 

}

/* Declare initial window size, position, and display mode */

int main(int argc, char* argv[])
{ 
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 
   glutInitWindowSize(wide, high);
   glutInitWindowPosition(winX,winY); 
   glutCreateWindow("My Window");
   init();

   glutDisplayFunc(display);
   glutMainLoop();
   return 0;
}
