/*
 * primer.c
 *
 *  Created on: Apr 25, 2016
 *      Author: Vangel V. Ajanovski
 */

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

#include "utils.h"

const int chekor = 9;
float agolX = 0;
float agolY = 0;
float agolZ = 0;
float transZ = 50;
float rotateA = 0;
float rotateAspeed = 0.0f;
float rotateNasa = 70.0f;
int polygonMode = GL_FILL;
float alpha = 1.0f;
float svX = -5, svY = 48, svZ = -30;

static GLuint texKilim;
static GLuint texAluminium;
static GLuint texNASA;
static GLuint texDrvo;
static GLuint texParket;

void kocka(void) // ova e objektot koj go pretstavuva svetloto
{
	glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
	glFrontFace(GL_CW);

	glColor3f(1, 1, 1);
	glBegin(GL_QUADS);
	glNormal3f(0, 0, 1); // za sekoj poedinechen poligon mora da se navede vektor
						 // koj e normalen na ramninata na poligonot, za da se znae
						 // vo koj pravec se odbiva svetloto
	glVertex3f(-10, 10, 0);
	glVertex3f(10, 10, 0);
	glVertex3f(10, -10, 0);
	glVertex3f(-10, -10, 0);

	glNormal3f(0, 0, -1);
	glVertex3f(-10, -10, 13);
	glVertex3f(-10, 10, 13);
	glVertex3f(10, 10, 13);
	glVertex3f(10, -10, 13);

	glNormal3f(0, 1, 0);
	glVertex3f(-10, -10, 0);
	glVertex3f(-10, -10, 13);
	glVertex3f(10, -10, 13);
	glVertex3f(10, -10, 0);

	glNormal3f(-1, 0, 0);
	glVertex3f(10, 10, 0);
	glVertex3f(10, -10, 0);
	glVertex3f(10, -10, 13);
	glVertex3f(10, 10, 13);

	glNormal3f(0, -1, 0);
	glVertex3f(10, 10, 0);
	glVertex3f(10, 10, 13);
	glVertex3f(-10, 10, 13);
	glVertex3f(-10, 10, 0);

	glNormal3f(1, 0, 0);
	glVertex3f(-10, 10, 0);
	glVertex3f(-10, 10, 13);
	glVertex3f(-10, -10, 13);
	glVertex3f(-10, -10, 0);
	glEnd();
}

void mrezha(int chekori) {
	glFrontFace(GL_CCW);
	glBegin(GL_QUADS);
	glNormal3f(0, 0, 1); // edna zaednichka normala za site chetiriagolnici od mrezhata
	float i = 0;
	float j = 0;
	for (i = 0; i < chekori; i++) {
		for (j = 0; j < chekori; j++) {
			glTexCoord2f(i / chekori + 0.0f, j / chekori + 0.0f);
			glVertex3f(i / chekori + 0.0f, j / chekori + 0.0f, 0);
			glTexCoord2f(i / chekori + 1.0f / chekori, j / chekori + 0.0f);
			glVertex3f(i / chekori + 1.0f / chekori, j / chekori + 0.0f, 0);
			glTexCoord2f(i / chekori + 1.0f / chekori,
					j / chekori + 1.0f / chekori);
			glVertex3f(i / chekori + 1.0f / chekori,
					j / chekori + 1.0f / chekori, 0);
			glTexCoord2f(i / chekori + 0.0f, j / chekori + 1.0f / chekori);
			glVertex3f(i / chekori + 0.0f, j / chekori + 1.0f / chekori, 0);
		}
	}
	glEnd();
}

void mrezhaPatos(float chekori) {
   float mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
   float mat_diffuse[] = { 0.9f, 0.9f, 0.9f, 1.0f };
   float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
   float mat_shininess = 0.6f;

   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

   glBindTexture(GL_TEXTURE_2D, texParket);

	glMatrixMode(GL_TEXTURE);
	glPushMatrix();
	glLoadIdentity();
	glScalef(4, 4, 4);
	glMatrixMode(GL_MODELVIEW);
	mrezha(7);
	glBindTexture(GL_TEXTURE_2D, 0);
}

void mrezhaKilim(float chekori) {
   float mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
   float mat_diffuse[] = { 0.9f, 0.9f, 0.9f, 1.0f };
   float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
   float mat_shininess = 0.4f;
   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

   glBindTexture(GL_TEXTURE_2D, texKilim);
   glMatrixMode(GL_TEXTURE);
   glPushMatrix();
   glLoadIdentity();

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

   glColor3f(20, 20, 20);
	mrezha(chekori);
	glPopMatrix();
	glBindTexture(GL_TEXTURE_2D, 0);
}

void mrezhaZidSoDrvo(float chekori) {
	glBindTexture(GL_TEXTURE_2D, texDrvo);
	glMatrixMode(GL_TEXTURE);
	glPushMatrix();
	glLoadIdentity();
	glScalef(3, 2, 2);
	glMatrixMode(GL_MODELVIEW);
	mrezha(chekori);
	glBindTexture(GL_TEXTURE_2D, 0);
}

void mrezhaZaZidSoMural(float chekori) {
	glBindTexture(GL_TEXTURE_2D, texNASA);
	glMatrixMode(GL_TEXTURE);
	glLoadIdentity();
	glTranslatef(0.5, 0.5, 0);
	glRotatef(rotateNasa, 0, 0, 1);
	glScalef(-1, 1, 1);
	glScalef(1.5, 1.5, 1.5);
	glTranslatef(-0.5, -0.5, 0);
	glMatrixMode(GL_MODELVIEW);
	mrezha(chekori);
	glBindTexture(GL_TEXTURE_2D, 0);
}

void skalilo_bezier(void) {
	glFrontFace(GL_CW);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glScalef(0.1, 0.02, 0.1);
	glRotatef(0, 0, 1, 0);
	GLfloat ctrlPoints[3][3][3] = { { { 0.0f, 0.0f, 0.0f },
			{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f } },
			{ { -3.0f, 0.0f, -4.0f }, { 0.0f, 4.0f, -7.0f },
					{ 3.0f, 0.0f, -4.0f } }, { { -4.0f, 0.0f, -10.0f }, { 0.0f,
					0.0f, -10.0f }, { 4.0f, 0.0f, -10.0f } } };
	glMap2f(GL_MAP2_VERTEX_3, 0.0f, 5.0f, 3, 3, 0.0f, 5.0f, 9, 3,
			&ctrlPoints[0][0][0]);
	glEnable(GL_MAP2_VERTEX_3);

	float mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
	float mat_diffuse[] = { 0.9f, 0.9f, 0.9f, 1.0f };
	float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	float mat_shininess = 0.3f;

	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

	glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
	glColor4f(0.9f, 0.9f, 0, alpha);

	glNormal3f(0, 1, 0);
	glMapGrid2f(5, 0.0f, 5.0f, 5, 0.0f, 5.0f);
	glEvalMesh2(GL_FILL, 0, 5, 0, 5);
	glPopMatrix();
}

void soba() {
   float mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
   float mat_diffuse[] = { 0.9f, 0.9f, 0.9f, 1.0f };
   float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
   float mat_shininess = 0.5f;

   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);

	// eden po eden zidovite od sobata
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   glColor3f(0.5, 0.5, 0.5);
	glPushMatrix();
	glRotatef(180, 0, 1, 0);
	glTranslatef(-50, -50, 50);
	glScalef(100, 100, 100);
	mrezhaZaZidSoMural(11);
	glPopMatrix();
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glColor3f(1, 1, 1);
	glPushMatrix();
	glTranslatef(-50, -50, -50);
	glRotatef(90, 1, 0, 0);
	glScalef(100, 100, 100);
	mrezhaPatos(11);
	glPopMatrix();

	glColor3f(1, 1, 1);
	glPushMatrix();
	glTranslatef(-50, 50, 50);
	glScalef(100, 100, 100);
	glRotatef(-90, 1, 0, 0);
	mrezha(20);
	glPopMatrix();

	glColor3f(0.1f, 0.1f, 1.0f);
	glPushMatrix();
	glRotatef(90, 0, 1, 0);
	glTranslatef(-50, -50, 50);
	glScalef(100, 100, 100);
	mrezhaZidSoDrvo(11);
	glPopMatrix();

	glColor3f(1.0f, 0.2f, 0.0f);
	glPushMatrix();
	glTranslatef(-50, -50, -50);
	glRotatef(-90, 0, 1, 0);
	glScalef(100, 100, 100);
	mrezhaZidSoDrvo(11);
	glPopMatrix();

	glColor3f(1.0f, 0.2f, 0.0f);
	glPushMatrix();
	glTranslatef(-50, -50, 50);
	glScalef(100, 100, 100);
	mrezhaZidSoDrvo(11);
	glPopMatrix();
}

void skalila(float chekor) {
   float mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
   float mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
   float mat_specular[] = { 0.2f, 0.2f, 0.2f, 1.0f };
   float mat_shininess = 0.25;

   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

	float stepa = 4.0 * M_PI / chekor;
	float stepv = 1.0 / (chekor + 1);
	float a = 0;

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glTranslatef(0, stepv, 0);
	glColor3f(0.5f, 0.5f, 0.5f);
	while (a <= 4.0 * M_PI) {
		skalilo_bezier();
		glRotatef(stepa * 180.0 / M_PI, 0, 1, 0);
		glTranslatef(0, stepv, 0);
		a += stepa;
	}
	glPopMatrix();
}

/* procedura za iscrtuvanje na eden stolb so visina 1 i radius 1 (cilindar) */
void stolb(float chekor) {
	float stepa = 2.0 * M_PI / chekor;
	float a = 0.0;

   float mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
   float mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
   float mat_specular[] = { 0.2f, 0.2f, 0.2f, 1.0f };
   float mat_shininess = 0.25;

   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

   glBindTexture(GL_TEXTURE_2D, texAluminium);
   glMatrixMode(GL_TEXTURE);
   glLoadIdentity();

	glMatrixMode(GL_MODELVIEW);
	glFrontFace(GL_CW);

	glColor3f(1, 1, 1);
	glPushMatrix();
	while (!(a > 2.0 * M_PI)) {
		/* slichno kako kaj skalite, no se koristat chetiriagolnici */
		glBegin(GL_QUAD_STRIP);
		for (float i = 0; i <= 1; i += 1 / chekor / 2) {
			glNormal3f(1, 0, 0);
			glTexCoord2f(0, i);
			glVertex3f(1, i, 0);

			glNormal3f(cos(stepa), 0, sin(stepa));
			glTexCoord2f(1, i);
			glVertex3f(cos(stepa), i, sin(stepa));
		}
		glEnd();
		glRotatef(stepa * 180.0 / M_PI, 0, 1, 0);
		a += stepa;
	}
	glPopMatrix();

	glBindTexture(GL_TEXTURE_2D, 0);
}

void predmetiVoSoba() {
	glPushMatrix();
	glTranslatef(-35, -49, 35);
	glScalef(70, 70, 70);
	glRotatef(-90, 1, 0, 0);
	mrezhaKilim(10);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(-25, -50, -25);
	glScalef(3, 100, 3);

	glColor3f(1, 1, 1);
	stolb(7);
	glScalef(5, 1, 5);
	skalila(15);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(-25, -50, 25);
	glScalef(3, 100, 3);
	glColor3f(1, 1, 1);
	stolb(7);
	glScalef(5, 1, 5);
	skalila(15);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(25, -50, 25);
	glScalef(3, 100, 3);
	glColor3f(1, 1, 1);
	stolb(7);
	glScalef(5, 1, 5);
	skalila(15);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(25, -50, -25);
	glScalef(3, 100, 3);
	glColor3f(1, 1, 1);
	stolb(7);
	glScalef(5, 1, 5);
	skalila(15);
	glPopMatrix();

}

void display(void) {
	glPolygonMode(GL_FRONT_AND_BACK, polygonMode);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//Mestenje pogled
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0, 0, transZ, 0, 0, 0, 0, 1, 0);
	glRotatef(rotateA, 0, 1, 0);

	// repozicioniranje na svetloto
	GLfloat svetlo0_position[] = { svX, svY, svZ, 1 };
	glLightfv(GL_LIGHT0, GL_POSITION, svetlo0_position);
	//GLfloat spotdir[]= {0,-20,0};
	//glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotdir);
	glPushMatrix();
	glTranslatef(svX, svY, svZ);
	glScalef(0.1, 0.1, 0.1);
	kocka();
	glPopMatrix();

	soba();
	predmetiVoSoba();

	// iscrtaj i smeni buffer
	glutSwapBuffers();
}

void initTextures(void) {
	glEnable(GL_TEXTURE_2D);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	//na nekoi instalacii ovie konstanti ne se definirani
	#define GL_CLAMP_TO_EDGE 			0x812D
	#define GL_CLAMP_TO_BORDER			0x812D

	// tekstura kilim - od datoteka
	Image *imageKilim;

	imageKilim = (Image *) malloc(1700000);
	if (imageKilim == NULL) {
		printf("Error allocating space for image");
		exit(0);
	}

	//vnimavajte za providni sliki treba da se koristi ovaa load funkcija
	if (!ImageLoadWithAlpha("kilimt.bmp", imageKilim)) {
		exit(1);
	}

	glGenTextures(1, &texKilim);
	glBindTexture(GL_TEXTURE_2D, texKilim);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, imageKilim->sizeX, imageKilim->sizeY, 0,
	GL_RGBA, GL_UNSIGNED_BYTE, imageKilim->data);

	// tekstura drvo - od datoteka
	Image *imageDrvo;

	imageDrvo = (Image *) malloc(1700000);
	if (imageDrvo == NULL) {
		printf("Error allocating space for image");
		exit(0);
	}

	if (!ImageLoad("drvo1.bmp", imageDrvo)) {
		exit(1);
	}

	glGenTextures(1, &texDrvo);
	glBindTexture(GL_TEXTURE_2D, texDrvo);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, imageDrvo->sizeX, imageDrvo->sizeY, 0,
	GL_RGB, GL_UNSIGNED_BYTE, imageDrvo->data);

	// tekstura parket - od datoteka
	Image *imageParket;

	imageParket = (Image *) malloc(1700000);
	if (imageParket == NULL) {
		printf("Error allocating space for image");
		exit(0);
	}

	if (!ImageLoad("parket.bmp", imageParket)) {
		exit(1);
	}

	glGenTextures(1, &texParket);
	glBindTexture(GL_TEXTURE_2D, texParket);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, imageParket->sizeX, imageParket->sizeY, 0,
	GL_RGB, GL_UNSIGNED_BYTE, imageParket->data);

	// tekstura NASA - od datoteka
	Image *imageNASA;

	imageNASA = (Image *) malloc(1700000);
	if (imageNASA == NULL) {
		printf("Error allocating space for image");
		exit(0);
	}

	if (!ImageLoad("NASA.bmp", imageNASA)) {
		exit(1);
	}

	glGenTextures(1, &texNASA);
	glBindTexture(GL_TEXTURE_2D, texNASA);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, imageNASA->sizeX, imageNASA->sizeY, 0,
	GL_RGB, GL_UNSIGNED_BYTE, imageNASA->data);

	// tekstura aluminium - od datoteka
	Image *imageAluminium;

	imageAluminium = (Image *) malloc(1700000);
	if (imageAluminium == NULL) {
		printf("Error allocating space for image");
		exit(0);
	}

	if (!ImageLoad("brushed_aluminium2.bmp", imageAluminium)) {
		exit(1);
	}

	glGenTextures(1, &texAluminium);
	glBindTexture(GL_TEXTURE_2D, texAluminium);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, imageAluminium->sizeX,
			imageAluminium->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE,
			imageAluminium->data);
}

void initLights(void) {
	glEnable(GL_LIGHTING);
	glEnable(GL_COLOR_MATERIAL);

	// globalno svetlo
	GLfloat global_ambient[] = { 0.1f, 0.1f, 0.1f, 1 };
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);

	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

	// Svetlo 0
	glEnable(GL_LIGHT0);
	GLfloat svetlo0_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
	GLfloat svetlo0_diffuse[] = { 30.0f, 30.0f, 30.0f, 1.0f };
	GLfloat svetlo0_specular[] = { 10.0f, 10.0f, 10.0f, 1.0f };
	glLightfv(GL_LIGHT0, GL_AMBIENT, svetlo0_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, svetlo0_diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, svetlo0_specular);
}

void init(void) {
	glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1, 1, -1, 1, 0.76, 1000);

	glEnable(GL_DEPTH_TEST);

	glShadeModel(GL_SMOOTH);

	initLights();

	initTextures();

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void keyboard(unsigned char key, int x, int y) {
	printf("%d %f %f %f %f \n", key, svX, svY, svZ, transZ);
	switch (key) {
	case 27:
		exit(0);
		break;
	case 'W':
		transZ += 0.5f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'S':
		transZ -= 0.5f;
		if (transZ < 0)
			transZ = 0;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'w':
		transZ += 0.1f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 's':
		transZ -= 0.1f;
		if (transZ < 0)
			transZ = 0;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'A':
		rotateAspeed += 1.0f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'a':
		rotateAspeed += 0.1f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'D':
		rotateAspeed -= 1.0f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'd':
		rotateAspeed -= 0.1f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case '1':
		polygonMode = GL_LINE;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case '2':
		polygonMode = GL_FILL;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case '3':
		rotateNasa++;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case '4':
		rotateNasa--;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case '5':
		alpha -= 0.01f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case '6':
		alpha += 0.01f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'i':
		svY += 1.0f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'k':
		svY -= 1.0f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'j':
		svX -= 1.0f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'l':
		svX += 1.0f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'y':
		svZ += 1.0f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	case 'h':
		svZ -= 1.0f;
			glutPostRedisplay();  // obnovi go prikazot
		break;
	}
}

void idle(void) {
	rotateA += rotateAspeed;
	glutPostRedisplay();
}

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Primer");
	init();
	glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);
	glutIdleFunc(idle);
	glutMainLoop();
	return 0;
}
