/**********************************************************************

* EINFUEHRUNG IN DIE COMPUTERGRAFIK                      BEISPIEL 3.3 *

*                                                                     *

* Kalchmair Franz-Helmut                                     Gruppe 4 *

*                                                                     *

* Date: May/5/2001                                                    *

*                                                                     *

* Cube with animation                                                 *

**********************************************************************/





#include <GL/glut.h>

#include <math.h>

#include <stdio.h>



//#include <GL/glos.h>

#include <GL/gl.h>

#include <GL/glu.h>

#include <stdlib.h>



#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 };



float azimut = 45.0f;

float elevation = 45.0f;



void KeyPressed(int key, int x, int y)

{



	switch(key)

	{

		case GLUT_KEY_LEFT:

			if (azimut==0.0f) azimut=355.0f; else azimut -= 5.0f;

			break;

		case GLUT_KEY_RIGHT:

			if (azimut==355.0f) azimut=0.0f; else azimut += 5.0f;

			break;

		case GLUT_KEY_DOWN:

			if (elevation>-85.0f) elevation -= 5.0f;

			break;

		case GLUT_KEY_UP:

			if (elevation<85.0f) elevation += 5.0f;

			break;

		default:;

	}

  //printf("azimut: %f, elevation: %f\n", azimut, elevation);

  glutPostRedisplay();

}	



void initCube()

{

	float blue[] ={0.0,0.0,1.0,1.0};

	float green[] ={0.0,1.0,0.0,1.0};

  float cubeLength = 10.0f;

  float sphereRad = 1.0f;

  double zOrtho = (sphereRad - cubeLength)/2.0;

  double xRot = 0.0f;

  double xRotPolar = xRot * M_PI / 180.0;

  double cosXRot = zOrtho*cos(xRotPolar);

  double sinXRot = zOrtho*sin(xRotPolar);

	

  glNewList(1, GL_COMPILE);

  

  glPushMatrix();

    glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);

    glTranslatef (0.0, -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();

    glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);

    glTranslatef (sinXRot, -cosXRot, 0.0);

    glutSolidSphere (sphereRad, 30, 30);

  glPopMatrix();



  glPushMatrix();

    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);

    glTranslatef (-cosXRot-sinXRot/2.0, sinXRot-cosXRot/2.0, cosXRot/2.0);

    glutSolidSphere (sphereRad, 30, 30);

  glPopMatrix();



  glPushMatrix();

    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);

    glTranslatef (-cosXRot-sinXRot/2.0, sinXRot-cosXRot/2.0, -cosXRot/2.0);

    glutSolidSphere (sphereRad, 30, 30);

  glPopMatrix();



  glPushMatrix();

    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);

    glTranslatef (-cosXRot-sinXRot/2.0, -sinXRot+cosXRot/2.0, cosXRot/2.0);

    glutSolidSphere (sphereRad, 30, 30);

  glPopMatrix();



  glPushMatrix();

    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sphere_diffuse);

    glTranslatef (-cosXRot-sinXRot/2.0, -sinXRot+cosXRot/2.0, -cosXRot/2.0);

    glutSolidSphere (sphereRad, 30, 30);

  glPopMatrix();



  glPushMatrix();

    glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);

    glTranslatef (cosXRot, 0.0, 0.0);

    glutSolidSphere (sphereRad, 30, 30);

  glPopMatrix();



  glPushMatrix();

    glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);

    glTranslatef (0.0, -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 (-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, GL_DIFFUSE, green);

    glRotatef(xRot, 1, 0, 0);

    glutSolidCube(cubeLength);

  glPopMatrix();



  printf("%f %f\n", sinXRot, cosXRot);

  glEndList();

}



void

display(void)

{

  double camX = cos(M_PI*elevation/180)*sin(M_PI*azimut/180)*15;

  double camY = sin(M_PI*elevation/180)*15;

  double camZ = cos(M_PI*elevation/180)*cos(M_PI*azimut/180)*15;



  printf("%f, %f, %f\n", camX, camY, camZ);



  glClearColor(1.0,1.0,1.0,0), 

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

	glLoadIdentity();

  

  initCube();



  gluLookAt(camX, camY, camZ, 

    0.0, 0.0, 1.0,

    0.0, 1.0, 0.0);



  glCallList(1);



  glutSwapBuffers();

}



void

init(void)

{

	static GLfloat pos[4]={-10.10, 0.0,5.0,0.0};



  glEnable(GL_DEPTH_TEST);

	glEnable(GL_NORMALIZE);

	glLightfv(GL_LIGHT0, GL_POSITION, pos);



  glEnable(GL_LIGHTING);

  glEnable(GL_LIGHT0);

	glShadeModel(GL_FLAT);



  glMatrixMode(GL_PROJECTION);

	glLoadIdentity();

  gluPerspective(90.0, 1.0, 5.0, 25.0);

  glMatrixMode(GL_MODELVIEW);



}



int

main(int argc, char **argv)

{

  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

  glutCreateWindow("animated 3D lighted cube");

  init();

  glutDisplayFunc(display);

  glutSpecialFunc(KeyPressed);

  glutMainLoop();

  return 0;          

}

