#include <GL/glut.h>
#include <iostream>
using namespace std;

void init();
void reshape (int w, int h);
void display();

int main(int argc, char ** argv)
{
  cout << "Starte: Kapitel 2 - Rendering\n";
  // Initialize GLUT
  glutInit(&argc, argv);
  glutInitWindowSize(800,600);
  glutInitWindowPosition(10,50);
  // Set initial display mode
  glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
  glutCreateWindow("AR-Buch - Kapitel 2 - Rendering");

  init();

  // Hand display funtion to GLUT
  glutDisplayFunc(display);
  // Hand reshape function to GLUT
  glutReshapeFunc(reshape);

  cout << "Entering main loop";
  // Tell the program we are not done yet
  glutMainLoop();
}

// Initialize GLUT settings
void init()
{
    glEnable (GL_BLEND);
    // Background color
    glClearColor(1,1,1,1);
}

// Execute on window resize (and thus on window open, too)
void reshape (int w, int h)
{
  glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  glFrustum (-1.0, 1.0, -0.66, 0.66, 1.5, 20.0);
  glMatrixMode (GL_MODELVIEW);
}

// Do all the drawings
void display()
{
  // Clear the screen
  glClear(GL_COLOR_BUFFER_BIT);

  // Switch to modify Model-View Matrix
  glMatrixMode (GL_MODELVIEW);

  // Clear the matrix
  glLoadIdentity ();             

  // Viewing transformation: either gluLookAt or gl[Load|Mult]MAtrix
  // gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  GLdouble trafo[16]={1.0, 0.0, 0.0, 0.0,
                      0.0, 1.0, 0.0, 0.0,
                      0.0, 0.0, 1.0, 0.0,
                      0.0, 0.0, -5.0, 1.0};
  glMultMatrixd(trafo);

  glPushMatrix();
    // glTranslatef(0.0, 0.0, -5.0);
    glRotatef(30.0, 0.0, 1.0, 0.0);
    glScalef (1.0, 2.0, 1.0);
    glColor3f (0.0, 0.0, 0.0);
    glutWireCube (1.0);

    glTranslatef(1.5, 0.0, 0.0);
    glutWireCube (1.0);
  glPopMatrix();

  glTranslatef(-2.0, 0.0, 0.0);
  for(int i=0;i<5;i++){
    glPushMatrix();
      glRotatef(72.0*i,0.0,0.0,1.0);
      glTranslatef(0.0,0.6,0.0);
      glutWireCube (0.3);
    glPopMatrix();
  }

  // Demonstrate, that gluLookAt does NOT move the camera,
  // it only executes inverted transformations
  // gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  //Draw everything to the screen
  glFlush();
  //Tell the program to refresh
  glutPostRedisplay();
}



