/**********************************************************************
* EINFUEHRUNG IN DIE COMPUTERGRAFIK                      BEISPIEL 2.1 *
*                                                                     *
* Kalchmair Franz-Helmut                                     Gruppe 4 *
*                                                                     *
* Date: April 18, 2001                                                *
*                                                                     *
* Cube                                                                *
**********************************************************************/


#include <GL/glut.h>
#include <math.h>
#include <stdio.h>

#define cubeLength 10.0
#define sphereRad 1.0

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; 
GLfloat sphere_diffuse[] = { 0.5, 0.0, 0.7, 1.0 };

void
display(void)
{
  double zOrtho = (sphereRad - cubeLength)/2.0;
  
  double xRot = 50.0;
  double xRotPolar = xRot * M_PI / 180.0;
  double cosXRot = zOrtho*cos(xRotPolar);
  double sinXRot = zOrtho*sin(xRotPolar);
  
  printf("%lf\n", cosXRot);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  glPushMatrix();
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);
    glTranslatef (0.0, -sinXRot, cosXRot);
    printf("%lf %lf %lf\n", zOrtho, -sinXRot, cosXRot);
    glutSolidSphere (sphereRad, 30, 30);
  glPopMatrix();

	glPushMatrix();
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);
    glTranslatef (cubeLength/4.0, -sinXRot+cosXRot/2.0, cosXRot+sinXRot/2.0);
    glutSolidSphere (sphereRad, 30, 30);
  glPopMatrix();

  glPushMatrix();
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);
    glTranslatef (-cubeLength/4.0, -sinXRot-cosXRot/2.0, cosXRot-sinXRot/2.0);
    glutSolidSphere (sphereRad, 30, 30);
  glPopMatrix();

  glPushMatrix();
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);
    glTranslatef (0.0, cosXRot+sinXRot/2.0, sinXRot-cosXRot/2.0);
    glutSolidSphere (sphereRad, 30, 30);
  glPopMatrix();

  glPushMatrix();
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);
    glTranslatef (0.0, cosXRot-sinXRot/2.0, sinXRot+cosXRot/2.0);
    glutSolidSphere (sphereRad, 30, 30);
  glPopMatrix();

  glPushMatrix();
    glRotatef(xRot, 1, 0, 0);
    glColor3f(0.0, 0.9, 0.0);
    glutSolidCube(cubeLength);
  glPopMatrix();
  
  glutSwapBuffers();
}

void
init(void)
{
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_COLOR_MATERIAL);

  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);

  glMatrixMode(GL_PROJECTION);
  gluPerspective(90.0, 1.0, 5.0, 25.0);
  glMatrixMode(GL_MODELVIEW);

  gluLookAt(0.0, 0.0, -15.0,   /* Kameraposition */
    0.0, 0.0, 1.0,             /* Look */
    0.0, 1.0, 0.);             /* Up / Richtung */

}

int
main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow("3D lighted cube");
  glutDisplayFunc(display);
  init();
  glutMainLoop();
  return 0;          
}
