I’ll put here the cose of my keybord function and of my display func:
void keyboard (unsigned char key, int x, int y){
switch (key) {
case ' ':
transl = true;
break;
case 'm':
start_move = true;
break;
case 'z':
start_move = false;
break;
case 's'://muoviti in indietro
if(!detectCollision() || collision_forward == true){
collision_back = false;
eye.set(eye.getX() + (sin(anglex/57) * 5), 5, eye.getZ() + (cos(anglex/57) * 5));
}
else
collision_back = true;
break;
case 'w'://muoviti avanti
if(!detectCollision() || collision_back == true){
collision_forward = false;
eye.set(eye.getX() - (sin(anglex/57) * 5), 5, eye.getZ() - (cos(anglex/57) * 5));
}
else
collision_forward = true;
break;
case 'd'://ruota a sinistra
anglex = ( anglex + 5 ) % 360;
break;
case 'a'://ruota a destra
anglex = ( anglex - 5 ) % 360;
break;
case 'p':
ambient[0] = ambient[0] + 0.01;
ambient[1] = ambient[1] + 0.01;
ambient[2] = ambient[2] + 0.01;
break;
case 'l':
ambient[0] = ambient[0] - 0.01;
ambient[1] = ambient[1] - 0.01;
ambient[2] = ambient[2] - 0.01;
break;
case 'o':
diffuse[0] = diffuse[0] + 0.01;
diffuse[1] = diffuse[1] + 0.01;
diffuse[2] = diffuse[2] + 0.01;
break;
case 'k':
diffuse[0] = diffuse[0] - 0.01;
diffuse[1] = diffuse[1] - 0.01;
diffuse[2] = diffuse[2] - 0.01;
break;
case 'i':
specular[0] = specular[0] + 0.01;
specular[1] = specular[1] + 0.01;
specular[2] = specular[2] + 0.01;
break;
case 'j':
specular[0] = specular[0] - 0.01;
specular[1] = specular[1] - 0.01;
specular[2] = specular[2] - 0.01;
break;
case 'n':
modalita_navigatore = !modalita_navigatore;
if(modalita_navigatore){
saved_eye.set(eye.getX(), eye.getY(), eye.getZ());
saved_focus.set(focus.getX(), focus.getY(), focus.getZ());
}
else{
eye.set(saved_eye.getX(), saved_eye.getY(), saved_eye.getZ());
focus.set(saved_focus.getX(), saved_focus.getY(), saved_focus.getZ());
}
break;
case 27:
exit(0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye.getX(), eye.getY(), eye.getZ(), focus.getX(), focus.getY(), focus.getZ(), normal.getX(), normal.getY(), normal.getZ());
glutPostRedisplay();
}
/**
* disegna tutti i componenti della scena.
*/
void disp()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
// Create light components
GLfloat ambientLight[] = { ambient[0], ambient[1], ambient[2], ambient[3] };
GLfloat diffuseLight[] = { diffuse[0], diffuse[1], diffuse[2], diffuse[3] };
GLfloat specularLight[] = { specular[0], specular[1], specular[2], specular[3] };
// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glPushMatrix();
//se vuoi muovere la telecamera a destra o sinistra
//allora aggiungi queste trasformazioni
if(!modalita_navigatore){
glTranslatef(eye.getX(), eye.getY(), eye.getZ());
glRotatef(anglex, 0, 1, 0);
//lo sposto con il centro in zero e applico le 3 rotazioni
glTranslatef(-eye.getX(), -eye.getY(), -eye.getZ());
}
if(modalita_navigatore){
//lo rimetto nella posizione di partenza
glTranslatef( ((num_square * size)/2), (num_square*size)/2, (num_square*size)/2);
glRotatef(anglex, 1, 0, 0);
glRotatef(angley, 0, 1, 0);
glRotatef(anglez, 0, 0, 1);
//lo sposto con il centro in zero e applico le 3 rotazioni
glTranslatef(- ((num_square * size)/2), - (num_square*size)/2, - (num_square*size)/2);
}
//crea la luce e la posiziona
glPushMatrix();
drawLight();
//disabilita la luce nell'oggetto
glDisable (GL_LIGHTING);
//disegna l'oggetto pieno
glColor3f (1.0, 1.0, 0.0);
glutSolidSphere(0.8, 48, 48);
//ritorna alla modalità wired
//riabilita la luce
glEnable (GL_LIGHTING);
glPopMatrix();
//crea una sfera rossa al centro
float mcolor[] = { 1.0f, 0.0f, 0.0f, 0.25f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mcolor);
glutSolidSphere(10, 50, 50);
//crea un'altra sfera più a sinistra
glPushMatrix();
glTranslatef(-100, 10, 0);
float rcolor[] = { 1.0f, 0.0f, 1.0f, 0.25f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rcolor);
glutSolidSphere(10, 50, 50);
glPopMatrix();
//crea un cubo con di sopra un cono
glPushMatrix();
//crea cono
glPushMatrix();
glTranslatef(100, 50, 0);
glRotatef(-90, 1, 0, 0);
float zcolor[] = { 1.0f, 1.0f, 0.0f, 0.25f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, zcolor);
glutSolidCone(12, 22, 34, 34);
glPopMatrix();
//crea cubo
glTranslatef(100, 25, 0);
float ccolor[] = { 0.0f, 1.0f, 0.0f, 0.25f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, ccolor);
glutSolidCube(50);
glPopMatrix();
//crea il terreno su cui disporre gli oggetti
drawFloor();
glPopMatrix();
glutSwapBuffers();
}