#include <GL/gl.h>
#include <GL/glu.h>
#include "aux.h"
#define GRAD 360

static int day = 0, year = 0, monat = 0;

static float links = -200.0, rechts = 200.0, oben = 200.0 , unten = -200.0;

void monatAdd (void) { monat = (monat + 1) % GRAD; }
void monatSubtract (void) { monat = (monat - 1) % GRAD; }

void dayAdd (void) { day = (day + 1) % GRAD; }
void daySubtract (void) { day = (day - 1) % GRAD; }

void yearAdd (void) { year = (year + 1) % GRAD; }
void yearSubtract (void) { year = (year - 1) % GRAD; }

void zoom(void) { float w = rechts - links ; float h = oben - unten;
		h = h/4; w = w/4; links += w; rechts -= w; oben -= h; unten += h;
		printf("links = %f rechts = %f oben = %f unten = %f\n",links,rechts,oben,unten);
		myReshape(); }
void back(void) { float w = rechts - links ; float h = oben - unten;
		h = h/2; w = w/2; links -= w; rechts += w; oben += h; unten -= h;
		printf("links = %f rechts = %f oben = %f unten = %f\n",links,rechts,oben,unten);
		myReshape(); }
void up(void) { float w = rechts - links ; float h = oben - unten;
		h = h/4;   unten += h; oben += h;
		printf("links = %f rechts = %f oben = %f unten = %f\n",links,rechts,oben,unten);
		myReshape(); }
void down(void) { float w = rechts - links ; float h = oben - unten;
		h = h/4;   unten -= h; oben -= h;
		printf("links = %f rechts = %f oben = %f unten = %f\n",links,rechts,oben,unten);
		myReshape(); }
void left(void) { float w = rechts - links ; float h = oben - unten;
		w = w/4;   links -= w; rechts -= w;
		printf("links = %f rechts = %f oben = %f unten = %f\n",links,rechts,oben,unten);
		myReshape(); }
void right(void) { float w = rechts - links ; float h = oben - unten;
		w = w/4;   links += w; rechts += w;
		printf("links = %f rechts = %f oben = %f unten = %f\n",links,rechts,oben,unten);
		myReshape(); }

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f (1.0, 1.0, 1.0);
    glPushMatrix();
/*	draw sun	*/
    auxWireSphere(1.392); /* mio km */
/*	draw smaller planet	*/
    glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
    glTranslatef (149.6, 0.0, 0.0); /* abstand erde sonne = 149.6 mio km */


    glPushMatrix();
	glRotatef (5.0,  0.0, 0.0, 1.0 ); /* mondebene */
	glRotatef ((GLfloat) monat, 0.0, 1.0, 0.0);
	glTranslatef(0.386,0.0, 0.0);
	auxWireSphere(0.003);
    glPopMatrix();


    glRotatef(27.0, 0.0, 0.0 , 1.0 ); /* kippen */
    glRotatef(90.0, 1.0, 0.0 , 0.0 ); 
    glRotatef ((GLfloat) day, 0.0, 0.0, 1.0);
    
    auxWireSphere(0.012);
    glPopMatrix();
    glFlush();
}

void myinit (void) { glShadeModel (GL_FLAT); }

void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(links,rechts,unten, oben,-200.0,200.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef (0.0, 0.0, -5.0);
}

int main(int argc, char** argv)
{
    auxInitDisplayMode (AUX_SINGLE | AUX_RGB);
    auxInitPosition (0, 0, 500, 500);
    auxInitWindow (argv[0]);
    myinit ();
    auxKeyFunc (AUX_y, yearSubtract);
    auxKeyFunc (AUX_x, yearAdd);
    auxKeyFunc (AUX_v, monatAdd);
    auxKeyFunc (AUX_b, monatSubtract);
    auxKeyFunc (AUX_n, dayAdd);
    auxKeyFunc (AUX_m, daySubtract);
    auxKeyFunc (AUX_z, zoom);
    auxKeyFunc (AUX_u, back);
    auxKeyFunc (AUX_UP, up);
    auxKeyFunc (AUX_DOWN, down);
    auxKeyFunc (AUX_LEFT, left);
    auxKeyFunc (AUX_RIGHT, right);
    auxReshapeFunc (myReshape);
    auxMainLoop(display);
}