#include "SDL.h"
#include "SDL_opengl.h" 

#undef main

typedef float Vector3D[3];

float cameraX=-0.5, cameraY=1, cameraZ=2;


void Block(Vector3D v0, Vector3D v1,Vector3D v2, Vector3D v3, Vector3D v4, Vector3D v5, Vector3D v6, Vector3D v7)
{
	/*        
	   v6 ----- v5 
	  / |      /|              
  	 /  |     / |              
	/   v7---/--v4            
	v1-/--- v0  /             
	| /     |  /              
	|/      | /               
	v2 ---- v3                
	
	*/

	glBegin(GL_QUADS);	

	glColor3f(0.75, 0.5, 0); // braun
	glVertex3fv(v0);    // vorne
	glVertex3fv(v1);
	glVertex3fv(v2);
	glVertex3fv(v3);

	glColor3f(0, 0, 1); // blau
	glVertex3fv(v5); // hinten
	glVertex3fv(v6);
	glVertex3fv(v7);
	glVertex3fv(v4);

	glColor3f(0.5, 0.5, 0.5); // grau
	glVertex3fv(v0);     // oben
	glVertex3fv(v5);
	glVertex3fv(v6);
	glVertex3fv(v1);

	glColor3f(0.5, 0, 0.5); // lila
	glVertex3fv(v3); // unten
	glVertex3fv(v4);
	glVertex3fv(v7);
	glVertex3fv(v2);

	glColor3f(1, 0, 0); // rot
	glVertex3fv(v1); // links
	glVertex3fv(v6);
	glVertex3fv(v7);
	glVertex3fv(v2);

	glColor3f(0, 1, 0); // gruen
	glVertex3fv(v0);    // rechts
	glVertex3fv(v5);
	glVertex3fv(v4);
	glVertex3fv(v3);

	glEnd();					
}

void InitGL(int Width, int Height)	        
{

	glViewport (0, 0, Width,Height);
	glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//glOrtho(-10.0, 10.0, -10.0, 10.0, -5.0, 5.0);
	gluPerspective(60.0,(double)Width/Height,0.1,100000.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

/* Hier wird gemalt */
void DrawGLScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(cameraX, cameraY, cameraZ, /* Wo steht die Kamera? (XYZ) */ 
		0, 0, 0, /* Schau zum Koordinatenursprung */ 
		0, 1, 0); /* Wie rum haelt man die Kamera? */

	{
		Vector3D v0={0.5,0.5,0.5};
		Vector3D v1={0,0.5,0.5};
		Vector3D v2={0,0,0.5};
		Vector3D v3={0.5,0,0.5};
		Vector3D v4={0.5,0,-0.5};
		Vector3D v5={0.5,0.5,-0.5};
		Vector3D v6={0,0.5,-0.5};
		Vector3D v7={0,0,-0.5};

		Block(v0,v1,v2,v3,v4,v5,v6,v7);
	}


	// Hier Buffer wechseln
	SDL_GL_SwapBuffers();
}

int main(int argc, char **argv) 
{  
	int done;

	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}

	if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL ) {
		fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
		SDL_Quit();
		exit(2);
	}

	SDL_WM_SetCaption("Kleine Studien zur Linearen Algebra", NULL);

	InitGL(640, 480);
	done = 0;

	DrawGLScene();

	while ( ! done ) 
	{
		SDL_Event event;
		DrawGLScene();
			
		while ( SDL_PollEvent(&event) ) 
		{
			if ( event.type == SDL_QUIT ) 
			{
				done = 1;
			}
			if ( event.type == SDL_KEYDOWN ) 
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_ESCAPE:
						done=1;
					break;
					case SDLK_UP:
						cameraY += 0.1;
					break;
					case SDLK_DOWN:
						cameraY -= 0.1;
					break;
					case SDLK_RIGHT:
						cameraX += 0.1;
					break;
					case SDLK_LEFT:
						cameraX -= 0.1;
					break;
					case SDLK_n:
						cameraZ +=0.1;
					break;
					case SDLK_m:
						cameraZ -=0.1;
					break;
				}
				printf("%e %e %e\n",cameraX,cameraY,cameraZ);
			}
		}
	}
	SDL_Quit();
	return 1;
}
