Texture is not showing, ClearColor and a time counter are shown

Hello there, I have made a project for an exam with opengl, its a labyrinth with two DisplayList one is Asgard wich show a golden labyrinth with colorful walls, the other is Terra with a more “classic” look.

You have 3 minutes to navigate the game and find the rotating Tesseract you must touch to win, if you fail to accomplish this in 3mins you’re out and game close.

I’ve set a blue clearcolor to check if it was shown correctly and it is, also the timer wich counts the 3 mins is working fine and close the windows if you lose. But there are no texture!
Why I am more confused than I should? This code work from time to time (very rare) showing the game but really laggy and low fps count. Instead in 98% of the case, just the blue clearcolor and the counter.

The code is long but simple, if i’m correct you should see what i cant just by the fact that game is laggy and actually worked at least five time with no code manipulation between a failure and the working game.

This is a beginner issue but I suppose it can be answered by advanced users only as navigating through it may be very difficoult.

#include "GL/freeglut.h"
#include <GL/glut.h>
#include <GL/glu.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

/*create random camera spawn point on matrix*/
static GLint coordinate_random[2];
static GLint coordinate_random_cubo_rotante[3];
static int spin = 0.0;

/*positioning of camera angle*/
static GLfloat Posizione_Della_Camera[] = {3.0, 4.0, 3.0};
static GLfloat Direzione_Della_Camera[] = {0.0, 4.0, 0.0};
static GLfloat camera_angle_y = 0.0;
static GLfloat sposta = 0.2;

/*generating tesseract texture*/
GLUquadricObj *sphere = NULL;

/*light setting with positioning, direction and reflection behaviour*/
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat specularLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat specref[] = { 0.1f, 0.1f, 0.1f, 1.0f };
static GLfloat lightPos[] = { 45.0, 25.0, 0.0, 0.5 };
static GLfloat spotDir[] = {0.0, 0.0, 0.0};

/*display list vars*/
GLuint DLLabirintoAsgardiano;
GLuint DLLabirintoTerrestre;
GLuint DLTesseract;

/*vars for FPS mode view*/
int frame, timer, timebase = 0;
char s[30];

/*display list switching*/
bool asgard = false;
bool terra = true;

/*var used to detect if player win or lose*/
bool vinto = false;
bool perso = false;
bool vinto1 = false;
bool perso1 = false;
bool continua = true;

/*vars for time counting*/
int tempo_rimanente = 0;
int tempo_passato = 0;

#define TRUE  1
#define FALSE 0

/*game time calculation*/
void Tempo_Rimanente(int pid)
{
	if (tempo_rimanente < 120)
		tempo_rimanente++;
	else
		tempo_rimanente = 0;

	tempo_passato++;

	glutTimerFunc(1000, Tempo_Rimanente, 345);
}

/*timer display*/
void printTime(int number, float x, float y)
{
	glDisable(GL_LIGHTING);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix(); // save
	glLoadIdentity();// and clear
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
			
	glLoadIdentity();

	char* buf;
	buf = new char[100];

	if(tempo_passato/60 < 3 && vinto != true && perso != true)
		{
			if( ( tempo_rimanente % 60 ) < 10 )
				snprintf ( buf, 100, "Tempo Di Gioco %d:0%d", tempo_passato/60, tempo_rimanente % 60 );
			else
				snprintf ( buf, 100, "Tempo Di Gioco %d:%d", tempo_passato/60, tempo_rimanente % 60 );
		}
	else if( vinto == true)
			{		
				snprintf( buf, 100, "Hai vinto!!");
				if( tempo_rimanente >= 5)
				vinto1 = true;				 	
			}
		else if( tempo_passato/6 >= 3)
				{
					snprintf ( buf, 100, "Hai Perso!!");
					perso = true;
					tempo_rimanente = 0;
					tempo_passato = 0;
				}
			else if( perso == true)
					{
						snprintf ( buf, 100, "Hai Perso!!");
						if( tempo_rimanente >= 5) 
							perso1 = true;
					}
	
	glRasterPos3f(x, y, -1); 
	glColor3f(1.0, 1.0, 1.0);
	
	glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,(const unsigned char*)buf);

	glMatrixMode(GL_PROJECTION);
	glTranslatef(-10.0, -10.0, -10.0);

	glPopMatrix(); 
	glMatrixMode(GL_MODELVIEW);

	glPopMatrix();

	glEnable(GL_LIGHTING);

	glutPostRedisplay();
}

/*Matrix 40x40, this is the labyrinth*/
int maze[20][20] = 
					{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
					  1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,
					  1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,
					  1,0,0,1,0,0,1,0,0,1,1,1,1,1,1,1,1,0,0,1,
					  1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,
					  1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,
					  1,0,0,1,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,
					  1,0,0,1,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,
					  1,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,
					  1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,
					  1,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,1,
					  1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,
					  1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,
					  1,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,1,0,0,1,
					  1,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,
					  1,0,0,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,
					  1,0,0,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,
					  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
					  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
					  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
					};


/*Texture loading, receive a parameter defined by a char pointer wich identify the name*/

GLuint Carica_Texture( const char * Nome_File)
{
	GLuint texture;
	unsigned char * data;
	FILE * file;
	file = fopen( Nome_File, "rb" );
	int Larghezza, Altezza;
	if ( file == NULL ) return 0;
	Larghezza = 340;
	Altezza = 340;
	data = (unsigned char *)malloc( Larghezza * Altezza * 3 );
	fread( data, Larghezza * Altezza * 3, 1, file );
	fclose( file );
	for(int i = 0; i < Larghezza * Altezza ; ++i)
		{
			int index = i*3;
			unsigned char B,R;
			B = data[index];
			R = data[index+2];

			data[index] = R;
			data[index+2] = B;
		}
	glGenTextures( 1, &texture );
	glBindTexture( GL_TEXTURE_2D, texture );
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	gluBuild2DMipmaps( GL_TEXTURE_2D, 3, Larghezza, Altezza,GL_RGB, GL_UNSIGNED_BYTE, data );
	free( data );
	return texture;
}

/*creating portion of the floor (a tile)*/

void Costruisci_Mattone()
{
	glBegin(GL_QUADS);

	glNormal3f( 0.0, -1.0, 0.0 );
	glTexCoord2f( 1.0f, 1.0f );
	glVertex3f( -1.0f, 1.0f, -1.0f );
	glTexCoord2f( 1.0f, 0.0f );
	glVertex3f( -1.0f,  1.0f,  1.0f );
	glTexCoord2f( 0.0f, 0.0f );
	glVertex3f(  1.0f,  1.0f,  1.0f );
	glTexCoord2f( 0.0f, 1.0f );
	glVertex3f(  1.0f,  1.0f, -1.0f );

	glEnd( );
}

/*funzione utilizzata per creare una porzione di muro*/
void Costruisci_Muro()
{
	glBegin(GL_QUADS);

		/* front face of the tile */
		glNormal3f( 0.0, 0.0, 1.0 );
		glTexCoord2f( 0.0f, 1.0 );
		glVertex3f( -1.0f, -1.0f, 1.0 );
		glTexCoord2f( 1.0, 1.0 );
		glVertex3f(  1.0, -1.0, 1.0 );
		glTexCoord2f( 1.0, 0.0 );
		glVertex3f(  1.0,  1.0, 1.0 );
		glTexCoord2f( 0.0, 0.0 );
		glVertex3f( -1.0,  1.0, 1.0 );

		/* back face of the tile */	
		glNormal3f( 0.0, 0.0, -1.0 );
		glTexCoord2f( 0.0f, 0.0f );
		glVertex3f( -1.0f, -1.0f, -1.0f );
		glTexCoord2f( 0.0f, 1.0f );
		glVertex3f( -1.0f,  1.0f, -1.0f );
		glTexCoord2f( 1.0f, 1.0f );
		glVertex3f(  1.0f,  1.0f, -1.0f );
		glTexCoord2f( 1.0f, 0.0f );
		glVertex3f(  1.0f, -1.0f, -1.0f );

		/* right face of the tile*/
		glNormal3f( 1.0, 0.0, 0.0 );
		glTexCoord2f( 0.0f, 0.0f );
		glVertex3f( 1.0f, -1.0f, -1.0f );
		glTexCoord2f( 0.0f, 1.0f );
		glVertex3f( 1.0f,  1.0f, -1.0f );
		glTexCoord2f( 1.0f, 1.0f );
		glVertex3f( 1.0f,  1.0f,  1.0f );
		glTexCoord2f( 1.0f, 0.0f );
		glVertex3f( 1.0f, -1.0f,  1.0f );

		/* left face of the tile*/
		glNormal3f( -1.0, 0.0, 0.0 );
		glTexCoord2f( 1.0f, 0.0f );
		glVertex3f( -1.0f, -1.0f, -1.0f );
		glTexCoord2f( 0.0f, 0.0f );
		glVertex3f( -1.0f, -1.0f,  1.0f );
		glTexCoord2f( 0.0f, 1.0f );
		glVertex3f( -1.0f,  1.0f,  1.0f );
		glTexCoord2f( 1.0f, 1.0f );
		glVertex3f( -1.0f,  1.0f, -1.0f );
	
	glEnd( );
}


/*Creating tesseract*/
GLuint Costruisci_Il_Cubo_Rotante_DL()
{
	GLuint DL;

	DL = glGenLists(1);

	glNewList( DL, GL_COMPILE);
		
	GLuint my_texture = Carica_Texture( "Tesseract.bmp");
	glBindTexture (GL_TEXTURE_2D, my_texture);
	
	glBegin(GL_QUADS);
		/* front*/
		glTexCoord2f( 0.0, 1.0 );
		glVertex3f( -0.5, -0.5, 0.5 );
		glTexCoord2f( 1.0, 1.0 );
		glVertex3f(  0.5, -0.5, 0.5 );
		glTexCoord2f( 1.0, 0.0 );
		glVertex3f(  0.5,  0.5, 0.5 );
		glTexCoord2f( 0.0, 0.0 );
		glVertex3f( -0.5,  0.5, 0.5 );
	
		/* back */
		glTexCoord2f( 0.0, 0.0 );
		glVertex3f( -0.5, -0.5, -0.5 );	
		glTexCoord2f( 0.0, 1.0 );
		glVertex3f( -0.5,  0.5, -0.5 );
		glTexCoord2f( 1.0, 1.0 );
		glVertex3f(  0.5,  0.5, -0.5 );
		glTexCoord2f( 1.0, 0.0 );
		glVertex3f(  0.5, -0.5, -0.5 );
	
		/*upper*/
		glTexCoord2f( 1.0, 1.0 );
		glVertex3f( -0.5,  0.5, -0.5 );
		glTexCoord2f( 1.0, 0.0 );
		glVertex3f( -0.5,  0.5,  0.5 );
		glTexCoord2f( 0.0, 0.0 );
		glVertex3f(  0.5,  0.5,  0.5 );
		glTexCoord2f( 0.0, 1.0 );
		glVertex3f(  0.5,  0.5, -0.5 );
	
		/* bottom*/
		glTexCoord2f( 0.0, 1.0 );
		glVertex3f( -0.5, -0.5, -0.5 );
		glTexCoord2f( 1.0, 1.0 );
		glVertex3f(  0.5, -0.5, -0.5 );
		glTexCoord2f( 1.0, 0.0 );
		glVertex3f(  0.5, -0.5,  0.5 );
		glTexCoord2f( 0.0, 0.0 );
		glVertex3f( -0.5, -0.5,  0.5 );
	
		/*right*/
		glTexCoord2f( 0.0, 0.0 );
		glVertex3f( 0.5, -0.5, -0.5 );
		glTexCoord2f( 0.0, 1.0 );
		glVertex3f( 0.5,  0.5, -0.5 );
		glTexCoord2f( 1.0, 1.0 );
		glVertex3f( 0.5,  0.5,  0.5 );
		glTexCoord2f( 1.0, 0.0 );
		glVertex3f( 0.5, -0.5,  0.5 );
	
		/*left*/
		glTexCoord2f( 1.0, 0.0 );
		glVertex3f( -0.5, -0.5, -0.5 );
		glTexCoord2f( 0.0, 0.0 );
		glVertex3f( -0.5, -0.5,  0.5 );
		glTexCoord2f( 0.0, 1.0 );
		glVertex3f( -0.5,  0.5,  0.5 );
		glTexCoord2f( 1.0, 1.0 );
		glVertex3f( -0.5,  0.5, -0.5 );
	
	glEnd( );

	glEndList();
	
	return (DL);
}

/*Terra display list creation*/
GLuint Terra() 
{
	GLuint MattoniDL, SiepeDL, PavimentoDL, SiepiDL, CieloDL, LunaDL, loopDL;

	MattoniDL = glGenLists(1);
	SiepeDL = glGenLists(1);
	PavimentoDL = glGenLists(1);
	SiepiDL = glGenLists(1);
	CieloDL = glGenLists(1);
	LunaDL = glGenLists(1);
	loopDL = glGenLists(1);

	glNewList(MattoniDL,GL_COMPILE);
		Costruisci_Mattone();
	glEndList();

	glNewList(SiepeDL, GL_COMPILE);
		Costruisci_Muro();
	glEndList();

	glNewList(PavimentoDL ,GL_COMPILE);

		GLuint my_texture = Carica_Texture( "pavimento.bmp");
		glBindTexture (GL_TEXTURE_2D, my_texture);

		for (int yc = 0; yc < 20; yc++)
			{
				for (int xc = 0; xc < 20; xc++)
					{
						if( maze[xc][yc] != 1)
							{
								glPushMatrix();
									glTranslatef( xc*2.0, 0.0, yc*2.0 );
									glCallList(MattoniDL);
								glPopMatrix();
							}
					}
			}

	glEndList();
	
	glNewList(SiepiDL ,GL_COMPILE);

		my_texture = Carica_Texture( "siepi.bmp" );
		glBindTexture (GL_TEXTURE_2D, my_texture);

		GLfloat y = 2.0;

		for( int i = 0; i < 3; i++)
			{
				for (int yc = 0; yc < 20; yc++)
					{
						for (int xc = 0; xc < 20; xc++)
							{
								if (maze[ xc][yc] == 1)
									{
										glPushMatrix();
											glTranslatef( xc*2.0, y, yc*2.0 );
											glCallList(SiepeDL);
										glPopMatrix();
									}
							}
					}
				y = y + 2;
			}

	glEndList();

	glNewList(CieloDL ,GL_COMPILE);

		my_texture = Carica_Texture( "cielo.bmp");
		glBindTexture (GL_TEXTURE_2D, my_texture);

		glPushMatrix();
			glTranslatef( 40.0, 0.0, 40.0 );
			sphere = gluNewQuadric();
			gluQuadricDrawStyle(sphere, GLU_FILL);
			gluQuadricTexture(sphere, GL_TRUE);
			gluQuadricNormals(sphere, GLU_SMOOTH);
			gluSphere(sphere, 65.0, 1000, 1000);
		glPopMatrix();

	glEndList();

	glNewList(LunaDL ,GL_COMPILE);

		my_texture = Carica_Texture( "luna.bmp");
		glBindTexture (GL_TEXTURE_2D, my_texture);

		glPushMatrix();
			glLightfv(GL_LIGHT0,GL_POSITION, lightPos);
			glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir);
			glDisable(GL_LIGHTING);
			glTranslatef( 45.0, 25.0, 0.0 );
			sphere = gluNewQuadric();
			gluQuadricDrawStyle(sphere, GLU_FILL);
			gluQuadricTexture(sphere, GL_TRUE);
			gluQuadricNormals(sphere, GLU_SMOOTH);
			gluSphere(sphere, 0.8, 50, 50);
			glEnable(GL_LIGHTING);
		glPopMatrix();

	glEndList();


	glNewList(loopDL ,GL_COMPILE);

		glCallList(PavimentoDL);
		glCallList(SiepiDL);
		glCallList(CieloDL);
		glCallList(LunaDL);

	glEndList();

	return(loopDL);
}

/*Asgard display list creation*/
GLuint Asgard() 
{
	GLuint MattoniDL, MuroDL, PavimentoDL, MuriDL, CieloDL, LunaDL, loopDL;

	MattoniDL = glGenLists(1);
	MuroDL = glGenLists(1);
	PavimentoDL = glGenLists(1);
	MuriDL = glGenLists(1);
	CieloDL = glGenLists(1);
	LunaDL = glGenLists(1);
	loopDL = glGenLists(1);

	glNewList(MattoniDL,GL_COMPILE);
		glEnable(GL_BLEND);
		glBlendFunc(GL_ONE, GL_ONE);	
		Costruisci_Mattone();
		glDisable(GL_BLEND);
	glEndList();

	glNewList(MuroDL, GL_COMPILE);
		Costruisci_Muro();
	glEndList();

	glNewList(PavimentoDL ,GL_COMPILE);

		GLuint my_texture = Carica_Texture( "Bifrost.bmp");
		glBindTexture (GL_TEXTURE_2D, my_texture);

		for (int yc = 0; yc < 20; yc++)
			{
				for (int xc = 0; xc < 20; xc++)
					{
						if( maze[xc][yc] != 1)
							{
								glPushMatrix();
									glTranslatef( xc*2.0, 0.0, yc*2.0 );
									glCallList(MattoniDL);
								glPopMatrix();
							}
					}
			}

	glEndList();
	
	glNewList(MuriDL ,GL_COMPILE);

		my_texture = Carica_Texture( "Gold.bmp");
		glBindTexture (GL_TEXTURE_2D, my_texture);

		GLfloat y = 2.0;

		for( int i = 0; i < 3; i++)
			{
				for (int yc = 0; yc < 20; yc++)
					{
						for (int xc = 0; xc < 20; xc++)
							{
								if (maze[ xc][yc] == 1)
									{
										glPushMatrix();
											glTranslatef( xc*2.0, y, yc*2.0 );
											glCallList(MuroDL);
										glPopMatrix();
									}
							}
					}
				y = y + 2;
			}

	glEndList();

	glNewList(CieloDL ,GL_COMPILE);

		my_texture = Carica_Texture( "CieloAsgard.bmp");
		glBindTexture (GL_TEXTURE_2D, my_texture);

		glPushMatrix();
			glTranslatef( 40.0, 0.0, 40.0 );
				sphere = gluNewQuadric();
				gluQuadricDrawStyle(sphere, GLU_FILL);
				gluQuadricTexture(sphere, GL_TRUE);
				gluQuadricNormals(sphere, GLU_SMOOTH);
				gluSphere(sphere, 65.0, 1000, 1000);
	glPopMatrix();

	glEndList();

	glNewList(LunaDL ,GL_COMPILE);

		my_texture = Carica_Texture( "luna.bmp");
		glBindTexture (GL_TEXTURE_2D, my_texture);

		glPushMatrix();
				glLightfv(GL_LIGHT0,GL_POSITION, lightPos);
				glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir);
				glDisable(GL_LIGHTING);
				glTranslatef( 45.0, 25.0, 0.0 );
				sphere = gluNewQuadric();
				gluQuadricDrawStyle(sphere, GLU_FILL);
				gluQuadricTexture(sphere, GL_TRUE);
				gluQuadricNormals(sphere, GLU_SMOOTH);
				gluSphere(sphere, 0.8, 50, 50);
				glEnable(GL_LIGHTING);
		glPopMatrix();

	glEndList();


	glNewList(loopDL ,GL_COMPILE);

		glCallList(MuriDL);
		glCallList(CieloDL);
		glCallList(LunaDL);
		glCallList(PavimentoDL);

	glEndList();

	return(loopDL);
}

/*init function*/
void init(void)
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
	glEnable(GL_DEPTH_TEST);
	DLLabirintoTerrestre = Terra();
	DLLabirintoAsgardiano = Asgard();
	DLTesseract = Costruisci_Il_Cubo_Rotante_DL();
	glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
	glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);
	glEnable(GL_LIGHT0);
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
	glMateriali(GL_FRONT, GL_SHININESS,1);
}

/*graphic contex function*/
void display(void)
{

	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

	glEnable(GL_TEXTURE_2D);

	spin = ( spin + 1 ) % 360;
	glutPostRedisplay();

	glPushMatrix();

		gluLookAt(Posizione_Della_Camera[0], Posizione_Della_Camera[1], Posizione_Della_Camera[2], Posizione_Della_Camera[0] + Direzione_Della_Camera[0],Direzione_Della_Camera[1],Posizione_Della_Camera[2] + Direzione_Della_Camera[2], 0.0, 1.0, 0.0);
		
		if( asgard == true)
			glCallList(DLLabirintoAsgardiano);
		else
			glCallList(DLLabirintoTerrestre);
	
		printTime(tempo_rimanente, -0.9, 0.7);
	
		glPushMatrix();

			glDisable(GL_LIGHTING);

			glTranslatef( coordinate_random_cubo_rotante[0], coordinate_random_cubo_rotante[1], coordinate_random_cubo_rotante[2]);

			if( vinto == true)
				{	
					GLuint my_texture = Carica_Texture( "Gemma.bmp");
					glBindTexture (GL_TEXTURE_2D, my_texture);
					glRotatef( spin, 0.0, 1.0, 0.0);
					gluSphere( sphere, 0.5, 100, 100);
				}
			else 
				{
					glRotatef( spin, 0.0, 1.0, 0.0);
					glCallList(DLTesseract);
				}
			glEnable(GL_LIGHTING);

		glPopMatrix();

//		frame++;
//		timer=glutGet(GLUT_ELAPSED_TIME);
//		if (timer - timebase > 1000) {
//			sprintf(s,"FPS:%4.2f",frame*1000.0/(timer-timebase));
//			glutSetWindowTitle(s);
//			timebase = timer;
//			frame = 0;
//		}
	
	glPopMatrix();

	glutSwapBuffers();
}

/* collision handling */
int Ricerca_Collisioni(GLfloat x, GLfloat z, GLfloat w)
{
	unsigned int i, j;
	unsigned int minx, minz, maxx, maxz;

	// converting coordinates from pixel to something matrix-related
	minx = x / 2;
	minz = z / 2;

	maxx = (x + w - 1) / 2;
	maxz = (z + w - 1) / 2;


	// Tiles collision
	for (i = minx; i <= maxx ; i++)
		{
			for (j = minz ; j <= maxz ; j++)
				{
					if (maze[i][j] == 1)
						return 1;
				}
		}

	/*max and min coordinates of the camera*/
	GLfloat minx_camera = Posizione_Della_Camera[0] - 1.2;
	GLfloat minz_camera = Posizione_Della_Camera[2] - 1.2;
	GLfloat maxx_camera = Posizione_Della_Camera[0] + 1.2;
	GLfloat maxz_camera = Posizione_Della_Camera[2] + 1.2;

	/*max and min coordinates of tesseract*/
	GLfloat minx_cubo = coordinate_random_cubo_rotante[0] - 0.5;
	GLfloat minz_cubo = coordinate_random_cubo_rotante[2] - 0.5;
	GLfloat maxx_cubo = coordinate_random_cubo_rotante[0] + 0.5;
	GLfloat maxz_cubo = coordinate_random_cubo_rotante[2] + 0.5;

	//Collision handler between camera and tesseract
	if((minx_camera < maxx_cubo && maxx_camera > minx_cubo) && (maxz_camera > minz_cubo && minz_camera < maxz_cubo))
		{
			tempo_rimanente = 0;
			vinto = true;
			return 1;
		}
	
	//If time is up, collision pops everywhere to stop camera from moving
	if( perso == true)
		return 1;

	//No collision
	return 0;
}

/*random player spawn*/
void genera_coordinate_random()
{
	coordinate_random[0] = rand() % 40;
	coordinate_random[1] = rand() % 40;
	while(Ricerca_Collisioni( coordinate_random[0], coordinate_random[1], 3.0))
		{
			coordinate_random[0] = rand() % 40;
			coordinate_random[1] = rand() % 40;
		}

	Posizione_Della_Camera[0] = coordinate_random[0];
	Posizione_Della_Camera[2] = coordinate_random[1];

	coordinate_random_cubo_rotante[0] = rand() % 40;
	coordinate_random_cubo_rotante[1] = 4;
	coordinate_random_cubo_rotante[2] = rand() % 40;

	while(Ricerca_Collisioni( coordinate_random_cubo_rotante[0], coordinate_random_cubo_rotante[2], 2.0))
		{
			coordinate_random_cubo_rotante[0] = rand() % 40;
			coordinate_random_cubo_rotante[2] = rand() % 40;
		}

	maze[coordinate_random_cubo_rotante[0]/2][coordinate_random_cubo_rotante[2]/2] = 2;
}

/*reshape func*/
void reshape (int w, int h)
{
		glViewport (0, 0, (GLsizei) w, (GLsizei) h);
		glMatrixMode (GL_PROJECTION);
		glLoadIdentity ();//rimpiazza la matrice corrente con quella di identità
		gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 600.0);
		glMatrixMode(GL_MODELVIEW);//gestisce le trasformazioni di viewing e modeling
		glLoadIdentity();
}

/*keyboard interaction*/
void keyboard (unsigned char key, int x, int y)
{
	GLfloat xc, zc;

	switch (key)
		{
			case 'a':
				if( vinto == false && perso == false)
					{
						camera_angle_y -= 0.05;
						Direzione_Della_Camera[0] = sin(camera_angle_y);
						Direzione_Della_Camera[2] = -cos(camera_angle_y);
						glutPostRedisplay();
						break;
					}
			case 'd':
				if( vinto == false && perso == false)
					{
						camera_angle_y += 0.05;
						Direzione_Della_Camera[0] = sin(camera_angle_y);
						Direzione_Della_Camera[2] = -cos(camera_angle_y);
						glutPostRedisplay();
       					break;
					}
			case 'w':
				xc = (Posizione_Della_Camera[0] + Direzione_Della_Camera[0]);
				zc = ( Posizione_Della_Camera[2] + Direzione_Della_Camera[2]);
				if(!Ricerca_Collisioni( xc, zc, 3.0))
					{
						Posizione_Della_Camera[0] += Direzione_Della_Camera[0] * sposta;
						Posizione_Della_Camera[2] += Direzione_Della_Camera[2] * sposta;
					}
				glutPostRedisplay();
				break;
			case 's':
				xc = (Posizione_Della_Camera[0] - Direzione_Della_Camera[0]);
				zc = ( Posizione_Della_Camera[2] - Direzione_Della_Camera[2]);
				if(!Ricerca_Collisioni( xc, zc, 3.0))
					{
						Posizione_Della_Camera[0] -= Direzione_Della_Camera[0] * sposta;
						Posizione_Della_Camera[2] -= Direzione_Della_Camera[2] * sposta;
					}
				glutPostRedisplay();
				break;
			case 'T':
				terra = true;
				asgard = false;
				break;
			case 'A':
				terra = false;
				asgard = true;	
				break;
		default:
			glutIdleFunc(NULL);
			break;
	}
}

/*funzione main*/
int main(int argc, char** argv)
{
	srand(time (0));
	genera_coordinate_random();
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize ( 1250, 1250);
	glutInitWindowPosition (5, 5);
	glutCreateWindow ("Labirinto");
	init ();
	glutTimerFunc(0, Tempo_Rimanente, 345);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);

	while( perso1 == false && vinto1 == false )
		glutMainLoopEvent();
	
	return 0;
}

It may be something related to depth buffer.
Resizing or moving the window doesnt change nothing.
If it works, moving is absolutely laggy but timer works just great.
Clear color is shown correctly.
This code worked more than once but it’s like winning a lottery.

This is what I know, any idea? Thanks in advance for support.