#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#define kWindowWidth    400
#define kWindowHeight   300

typedef void GLvoid;                    // GLvoid type deprecated

//void InitGL();
void InitGL(int Width, int Height);
void DrawGLScene(void);
void ReSizeGLScene(int Width, int Height);

int main(int argc, char** argv)
{
        glutInit(&argc, argv);
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize (kWindowWidth, kWindowHeight);
        glutInitWindowPosition (100, 100);
        glutCreateWindow (argv[0]);

        InitGL(640,480);

        glutDisplayFunc(DrawGLScene);
        glutReshapeFunc(ReSizeGLScene);

        glutMainLoop();

        return 0;
}

void DrawGLScene(void)          // Here's Where We Do All The Drawing
{
        // Clear The Screen And The Depth Buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Reset The View
        glLoadIdentity();

        // Move Left 1.5 Units And Into The Screen 6.0
        glTranslatef(-1.5f,0.0f,-6.0f);

        // Drawing Using Triangles
        glBegin(GL_TRIANGLES);
                glVertex3f( 0.0f, 1.0f, 0.0f);  // Top
                glVertex3f(-1.0f,-1.0f, 0.0f);  // Bottom Left
                glVertex3f( 1.0f,-1.0f, 0.0f);  // Bottom Right
        glEnd();                                // Finished Drawing Triangle

        glTranslatef(3.0f,0.0f,0.0f);           // Move Right 3 Units

        // Draw A Quad
        glBegin(GL_QUADS);
                glVertex3f(-1.0f, 1.0f, 0.0f);  // Top Left
                glVertex3f( 1.0f, 1.0f, 0.0f);  // Top Right
                glVertex3f( 1.0f,-1.0f, 0.0f);  // Bottom Right
                glVertex3f(-1.0f,-1.0f, 0.0f);  // Bottom Left
        glEnd();                                // Done Drawing The Quad
        return;                                 // Keep Going
}

// We call this right after our OpenGL window is created.
void InitGL(int Width, int Height)
{
  // This Will Clear The Background Color To Black
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

  // Enables Clearing Of The Depth Buffer
  glClearDepth(1.0);

  glDepthFunc(GL_LESS);         // The Type Of Depth Test To Do
  glEnable(GL_DEPTH_TEST);      // Enables Depth Testing
  glShadeModel(GL_SMOOTH);      // Enables Smooth Color Shading

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();             // Reset The Projection Matrix

  // Calculate The Aspect Ratio Of The Window
  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);

  glMatrixMode(GL_MODELVIEW);
}


/* The function called when our window is resized
  (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
  if (Height==0)
    // Prevent A Divide By Zero If The Window Is Too Small
    Height=1;

  // Reset The Current Viewport And Perspective Transformation
  glViewport(0, 0, Width, Height);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
  glMatrixMode(GL_MODELVIEW);
} 
