#include "globals.h"



void LBA_MAP_GL::face(double X,double Y,double Z,double texture_x,double texture_y,double h,int a,int b,int c,bool hidden)
{
    double vertex[20][3];
    double uv[24][2];
    for(int i=0;i<20;i++)
    vertex[i][0]=vertex[i][1]=vertex[i][2]=0.;

    vertex[ 0][0]=X+0.;vertex[ 0][1]=Y*h+h ,vertex[ 0][2]=Z+0.;
    vertex[ 1][0]=X+0.;vertex[ 1][1]=Y*h+h ,vertex[ 1][2]=Z+0.;
    vertex[ 2][0]=X+0.;vertex[ 2][1]=Y*h+h ,vertex[ 2][2]=Z+1.;
    vertex[ 3][0]=X+0.;vertex[ 3][1]=Y*h+h ,vertex[ 3][2]=Z+1.;
    vertex[ 4][0]=X+0.;vertex[ 4][1]=Y*h+0.,vertex[ 4][2]=Z+0.;
    vertex[ 5][0]=X+0.;vertex[ 5][1]=Y*h+0.,vertex[ 5][2]=Z+0.;
    vertex[ 6][0]=X+0.;vertex[ 6][1]=Y*h+0.,vertex[ 6][2]=Z+0.;
    vertex[ 7][0]=X+0.;vertex[ 7][1]=Y*h+0.,vertex[ 7][2]=Z+0.;
    vertex[ 8][0]=X+1.;vertex[ 8][1]=Y*h+h ,vertex[ 8][2]=Z+0.;
    vertex[ 9][0]=X+1.;vertex[ 9][1]=Y*h+h ,vertex[ 9][2]=Z+0.;
    vertex[10][0]=X+0.;vertex[10][1]=Y*h+0.,vertex[10][2]=Z+1.;
    vertex[11][0]=X+0.;vertex[11][1]=Y*h+0.,vertex[11][2]=Z+1.;
    vertex[12][0]=X+1.;vertex[12][1]=Y*h+h ,vertex[12][2]=Z+1.;
    vertex[13][0]=X+1.;vertex[13][1]=Y*h+h ,vertex[13][2]=Z+1.;
    vertex[14][0]=X+1.;vertex[14][1]=Y*h+h ,vertex[14][2]=Z+1.;
    vertex[15][0]=X+1.;vertex[15][1]=Y*h+h ,vertex[15][2]=Z+1.;
    vertex[16][0]=X+1.;vertex[16][1]=Y*h+0.,vertex[16][2]=Z+0.;
    vertex[17][0]=X+1.;vertex[17][1]=Y*h+0.,vertex[17][2]=Z+0.;
    vertex[18][0]=X+1.;vertex[18][1]=Y*h+0.,vertex[18][2]=Z+1.;
    vertex[19][0]=X+1.;vertex[19][1]=Y*h+0.,vertex[19][2]=Z+1.;




// UV not shared
  /*  uv[0][0]=23;   uv[0][1]=0;
    uv[1][0]=24;   uv[1][1]=0;
    uv[2][0]=0;    uv[2][1]=11;
    uv[3][0]=0.;   uv[3][1]=12;
    uv[4][0]=23.;  uv[4][1]=14;
    uv[5][0]=24.;  uv[5][1]=14.;
    uv[6][0]=23.;  uv[6][1]=15;
    uv[7][0]=24.;  uv[7][1]=15;
    uv[8][0]=47.;  uv[8][1]=11;
    uv[9][0]=47.;  uv[9][1]=12;
    uv[10][0]=0;   uv[10][1]=25.;
    uv[11][0]=0;   uv[11][1]=26;
    uv[12][0]=23.; uv[12][1]=22.;
    uv[13][0]=24;  uv[13][1]=22.;
    uv[14][0]=23.; uv[14][1]=23.;
    uv[15][0]=24.; uv[15][1]=23.;
    uv[16][0]=47.; uv[16][1]=25.;
    uv[17][0]=47.; uv[17][1]=26.;
    uv[18][0]=23.; uv[18][1]=37.;
    uv[19][0]=24.; uv[19][1]=37.;*/



//UV shared + OFFSET 0.5
/*    uv[0][0]=23.5;   uv[0][1]=0.5;
    uv[1][0]=23.5;   uv[1][1]=0.5;
    uv[2][0]=0.5;    uv[2][1]=11;
    uv[3][0]=0.5;   uv[3][1]=13.;
    uv[4][0]=23.5;  uv[4][1]=13.5;
    uv[5][0]=23.5;  uv[5][1]=13.5;
    uv[6][0]=23.5;  uv[6][1]=15.5;
    uv[7][0]=23.5;  uv[7][1]=15.5;
    uv[8][0]=46.5;  uv[8][1]=11;
    uv[9][0]=46.5;  uv[9][1]=13.;
    uv[10][0]=0.5;   uv[10][1]=24.5;
    uv[11][0]=0.5;   uv[11][1]=26;
    uv[12][0]=23.5; uv[12][1]=21.5;
    uv[13][0]=23.5;  uv[13][1]=21.5;
    uv[14][0]=22.5; uv[14][1]=24.;
    uv[15][0]=24.5; uv[15][1]=24.;
    uv[16][0]=46.5; uv[16][1]=24.5;
    uv[17][0]=46.5; uv[17][1]=26;
    uv[18][0]=22.5; uv[18][1]=36.5;
    uv[19][0]=24.5; uv[19][1]=36.5;
*/
//UV shared + OFFSET 0.5 MIRRORED
/*    uv[0][0]=23.5;   uv[0][1]=0.5;
    uv[1][0]=23.5;   uv[1][1]=0.5;
    uv[2][0]=0.5;    uv[2][1]=11;
    uv[3][0]=0.5;   uv[3][1]=13.;
    uv[4][0]=23.5;  uv[4][1]=13.5;
    uv[5][0]=23.5;  uv[5][1]=13.5;
    uv[6][0]=23.5;  uv[6][1]=15.5;
    uv[7][0]=23.5;  uv[7][1]=15.5;
    uv[8][0]=46.5;  uv[8][1]=11;
    uv[9][0]=46.5;  uv[9][1]=13.;
    uv[10][0]=0.5;   uv[10][1]=24.5;
    uv[11][0]=0.5;   uv[11][1]=26;
    uv[12][0]=23.5; uv[12][1]=21.5;
    uv[13][0]=23.5;  uv[13][1]=21.5;
    uv[14][0]=22.5; uv[14][1]=24.;
    uv[15][0]=24.5; uv[15][1]=24.;
    uv[16][0]=46.5; uv[16][1]=24.5;
    uv[17][0]=46.5; uv[17][1]=26;
    uv[18][0]=22.5; uv[18][1]=36.5;
    uv[19][0]=24.5; uv[19][1]=36.5;
    uv[20][0]=uv[14][0]; uv[20][1]=uv[14][1];
    uv[21][0]=uv[15][0]; uv[21][1]=uv[15][1];
    uv[22][0]=uv[18][0]; uv[22][1]=uv[18][1];
    uv[23][0]=uv[19][0]; uv[23][1]=uv[19][1];
*/
//UV shared + OFFSET 0.5 NOT MIRRORED
    uv[0][0]=23.5;   uv[0][1]=0.5;
    uv[1][0]=23.5;   uv[1][1]=0.5;
    uv[2][0]=0.5;    uv[2][1]=11;
    uv[3][0]=0.5;   uv[3][1]=13.;
    uv[4][0]=23.5;  uv[4][1]=13.5;
    uv[5][0]=23.5;  uv[5][1]=13.5;
    uv[6][0]=23.5;  uv[6][1]=15.5;
    uv[7][0]=23.5;  uv[7][1]=15.5;
    uv[8][0]=46.5;  uv[8][1]=11;
    uv[9][0]=46.5;  uv[9][1]=13.;
    uv[10][0]=0.5;   uv[10][1]=24.5;
    uv[11][0]=0.5;   uv[11][1]=26;
    uv[12][0]=23.5; uv[12][1]=21.5;
    uv[13][0]=23.5;  uv[13][1]=21.5;
    uv[14][0]=22.5; uv[14][1]=24.;
    uv[15][0]=24.5; uv[15][1]=24.;
    uv[16][0]=46.5; uv[16][1]=24.5;
    uv[17][0]=46.5; uv[17][1]=26;
    uv[18][0]=22.5; uv[18][1]=36.5;
    uv[19][0]=24.5; uv[19][1]=36.5;

// UV shared
   /* uv[0][0]=23.5;   uv[0][1]=0;
    uv[1][0]=23.5;   uv[1][1]=0;
    uv[2][0]=0;    uv[2][1]=11.5;
    uv[3][0]=0.;   uv[3][1]=11.5;
    uv[4][0]=23.5;  uv[4][1]=14.5;
    uv[5][0]=23.5;  uv[5][1]=14.5;
    uv[6][0]=23.5;  uv[6][1]=14.5;
    uv[7][0]=23.5;  uv[7][1]=14.5;
    uv[8][0]=47.;  uv[8][1]=11.5;
    uv[9][0]=47.;  uv[9][1]=11.5;
    uv[10][0]=0;   uv[10][1]=25.5;
    uv[11][0]=0;   uv[11][1]=25.5;
    uv[12][0]=23.5; uv[12][1]=22.5;
    uv[13][0]=23.5;  uv[13][1]=22.5;
    uv[14][0]=23.5; uv[14][1]=22.5;
    uv[15][0]=23.5; uv[15][1]=22.5;
    uv[16][0]=47.; uv[16][1]=25.5;
    uv[17][0]=47.; uv[17][1]=25.5;
    uv[18][0]=23.5; uv[18][1]=37.;
    uv[19][0]=23.5; uv[19][1]=37.;*/

// UV shared + offset 0.5
   /* uv[0][0]=23.5;   uv[0][1]=0;
    uv[1][0]=23.5;   uv[1][1]=0;
    uv[2][0]=0.5;    uv[2][1]=11.5;
    uv[3][0]=0.5;   uv[3][1]=11.5;
    uv[4][0]=23.5;  uv[4][1]=14.5;
    uv[5][0]=23.5;  uv[5][1]=14.5;
    uv[6][0]=23.5;  uv[6][1]=14.5;
    uv[7][0]=23.5;  uv[7][1]=14.5;
    uv[8][0]=46.5;  uv[8][1]=11.5;
    uv[9][0]=46.5;  uv[9][1]=11.5;
    uv[10][0]=0.5;   uv[10][1]=25.5;
    uv[11][0]=0.5;   uv[11][1]=25.5;
    uv[12][0]=23.5; uv[12][1]=22.5;
    uv[13][0]=23.5;  uv[13][1]=22.5;
    uv[14][0]=23.5; uv[14][1]=22.5;
    uv[15][0]=23.5; uv[15][1]=22.5;
    uv[16][0]=46.5; uv[16][1]=25.5;
    uv[17][0]=46.5; uv[17][1]=25.5;
    uv[18][0]=23.5; uv[18][1]=36.5;
    uv[19][0]=23.5; uv[19][1]=36.5;*/
    double ab[3];
    ab[0]=vertex[b][0]-vertex[a][0];
    ab[1]=vertex[b][1]-vertex[a][1];
    ab[2]=vertex[b][2]-vertex[a][2];
    double ac[3];
    ac[0]=vertex[c][0]-vertex[a][0];
    ac[1]=vertex[c][1]-vertex[a][1];
    ac[2]=vertex[c][2]-vertex[a][2];
    double n[3];
    n[0]=ab[1]*ac[2]-ac[1]*ab[2];
    n[1]=ab[2]*ac[0]-ac[2]*ab[0];
    n[2]=ab[0]*ac[1]-ac[0]*ab[1];
    double l=sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
    double g[3];
    g[0]=(vertex[a][0]+vertex[b][0]+vertex[c][0])/3.;
    g[1]=(vertex[a][1]+vertex[b][1]+vertex[c][1])/3.;
    g[2]=(vertex[a][2]+vertex[b][2]+vertex[c][2])/3.;
double f=0.5;

//glEnable(GL_LIGHTING);
//glBegin(GL_TRIANGLES);
LBA_FACE face;
face.v[0]=vertex[a][0];
face.v[1]=vertex[a][1];
face.v[2]=vertex[a][2];
face.v[3]=vertex[b][0];
face.v[4]=vertex[b][1];
face.v[5]=vertex[b][2];
face.v[6]=vertex[c][0];
face.v[7]=vertex[c][1];
face.v[8]=vertex[c][2];
if(hidden)
{
face.vt[0]=0.;
face.vt[1]=0.;
face.vt[2]=0.;
face.vt[3]=0.;
face.vt[4]=0.;
face.vt[5]=0.;

}
else
{
face.vt[0]=(texture_x+uv[a][0])/2048.;
face.vt[1]=(texture_y+uv[a][1])/2048.;
face.vt[2]=(texture_x+uv[b][0])/2048.;
face.vt[3]=(texture_y+uv[b][1])/2048.;
face.vt[4]=(texture_x+uv[c][0])/2048.;
face.vt[5]=(texture_y+uv[c][1])/2048.;
}
face.vn[0]=n[0]/l;
face.vn[1]=n[1]/l;
face.vn[2]=n[2]/l;
lba_face.push_back(face);
//,vertex[a][1],vertex[a][2]);
//    lba_face.push_back();
glNormal3d(face.vn[0],face.vn[1],face.vn[2]);
glTexCoord2d(face.vt[0],face.vt[1]);
glVertex3d(face.v[0],face.v[1],face.v[2]);

glNormal3d(face.vn[0],face.vn[1],face.vn[2]);
glTexCoord2d(face.vt[2],face.vt[3]);
glVertex3d(face.v[3],face.v[4],face.v[5]);

glNormal3d(face.vn[0],face.vn[1],face.vn[2]);
glTexCoord2d(face.vt[4],face.vt[5]);
glVertex3d(face.v[6],face.v[7],face.v[8]);

/*    glNormal3d(n[0]/l,n[1]/l,n[2]/l);glTexCoord2d((texture_x+uv[a][0]) /2048.,(texture_y+uv[a][1])/2048.);glVertex3d(vertex[a][0],vertex[a][1],vertex[a][2]);
    glNormal3d(n[0]/l,n[1]/l,n[2]/l);glTexCoord2d((texture_x+uv[b][0]) /2048.,(texture_y+uv[b][1])/2048.);glVertex3d(vertex[b][0],vertex[b][1],vertex[b][2]);
    glNormal3d(n[0]/l,n[1]/l,n[2]/l);glTexCoord2d((texture_x+uv[c][0]) /2048.,(texture_y+uv[c][1])/2048.);glVertex3d(vertex[c][0],vertex[c][1],vertex[c][2]);*/
//glEnd();
//glDisable(GL_LIGHTING);

  /*  glBegin(GL_LINES);
    glColor3d(0,0,1);
    glVertex3d(g[0],g[1],g[2]);
    glColor3d(1,0,0);
    glVertex3d(g[0]+n[0]*f/l,g[1]+n[1]*f/l,g[2]+n[2]*f/l);
    glEnd();
    glColor3d(1,1,1);*/

}



LBA_MAP_GL::LBA_MAP_GL(int NUM_MAP,int LBA2)
    {
        int i,j,k;
        int offset_x,offset_y;
        lba_map=new LBA_MAP(NUM_MAP,LBA2);
        texture_map=new unsigned char[2048*2048*4];
        LBA_SHARED_BRICK shared_brick1[lba_map->number_brick];
        LBA_SHARED_BRICK shared_brick2[lba_map->number_brick];

//        shared_brick1.reserve(17903);
 //       shared_brick2.reserve(17903);
        for(int i=0;i<lba_map->number_brick;i++)
          shared_brick1[i].id=-1,shared_brick1[i].dx=0,shared_brick1[i].dz=0;
        for(int i=0;i<lba_map->number_brick;i++)
          shared_brick2[i].id=-1,shared_brick2[i].dx=0,shared_brick2[i].dz=0;



for(int Z=0;Z<64;Z++)
for(int X=0;X<64;X++)
for(int Y=0;Y<25;Y++)
if(lba_map->brick_list[lba_map->grid->info_brick[X][Z][Y].index_brick]!=126)//LBA2 temple of bu 2nd scene : some inivsible walls have same brick index (127) than a shaped brick (layout 183 object 52 brick 1)
{
//    if(lba_map->grid->info_brick[X][Z][Y].shape==3)printf("%d %d %d %d\n",X,Y,Z,lba_map->brick_list[lba_map->grid->info_brick[X][Z][Y].index_brick]);
    if(lba_map->grid->info_brick[X][Z][Y].index_brick!=-1)
    {
        if(
        X<63&&
        lba_map->grid->info_brick[X+1][Z][Y].shape!=1&&
        lba_map->grid->info_brick[X+1][Z][Y].index_brick!=-1&&
        (lba_map->grid->info_brick[X][Z][Y].shape==4||
         lba_map->grid->info_brick[X][Z][Y].shape==9||
         lba_map->grid->info_brick[X][Z][Y].shape==12||
         lba_map->grid->info_brick[X][Z][Y].shape==3)&&
        lba_map->grid->info_brick[X][Z][Y].object==lba_map->grid->info_brick[X+1][Z][Y].object)
        {
            shared_brick1[lba_map->grid->info_brick[X][Z][Y].index_brick].id=lba_map->grid->info_brick[X+1][Z][Y].index_brick;
            shared_brick1[lba_map->grid->info_brick[X][Z][Y].index_brick].dx=1;
            shared_brick1[lba_map->grid->info_brick[X][Z][Y].index_brick].dz=0;
        }
       if(
          Z<63&&
           lba_map->grid->info_brick[X][Z+1][Y].shape!=1&&
           lba_map->grid->info_brick[X][Z+1][Y].index_brick!=-1&&
          (lba_map->grid->info_brick[X][Z][Y].shape==5||
           lba_map->grid->info_brick[X][Z][Y].shape==7||
           lba_map->grid->info_brick[X][Z][Y].shape==2||
           lba_map->grid->info_brick[X][Z][Y].shape==13||
           lba_map->grid->info_brick[X][Z][Y].shape==10)&&//&&lba_map->grid->info_brick[X][Z][Y].object==lba_map->grid->info_brick[X][Z+1][Y].object)
          lba_map->grid->info_brick[X][Z][Y].object==lba_map->grid->info_brick[X][Z+1][Y].object)
        {
            shared_brick1[lba_map->grid->info_brick[X][Z][Y].index_brick].id=lba_map->grid->info_brick[X][Z+1][Y].index_brick;
            shared_brick1[lba_map->grid->info_brick[X][Z][Y].index_brick].dx=0;
            shared_brick1[lba_map->grid->info_brick[X][Z][Y].index_brick].dz=1;
        }
        if(
        X<63&&
        lba_map->grid->info_brick[X+1][Z][Y].shape!=1&&
        lba_map->grid->info_brick[X+1][Z][Y].index_brick!=-1&&
       (lba_map->grid->info_brick[X][Z][Y].shape==7||
        lba_map->grid->info_brick[X][Z][Y].shape==10)&&
       lba_map->grid->info_brick[X][Z][Y].object==lba_map->grid->info_brick[X+1][Z][Y].object)
        {
            shared_brick2[lba_map->grid->info_brick[X][Z][Y].index_brick].id=lba_map->grid->info_brick[X+1][Z][Y].index_brick;
            shared_brick2[lba_map->grid->info_brick[X][Z][Y].index_brick].dx=1;
            shared_brick2[lba_map->grid->info_brick[X][Z][Y].index_brick].dz=0;
        }



    }
}



        for(i=0;i<lba_map->number_brick;i++)
        {
            offset_x=(i%42);offset_x*=48;
            offset_y=(i/42);offset_y*=38;
            for(j=0;j<38;j++)
            for(k=0;k<48;k++)
            {
              texture_map[((offset_y+j)*2048+offset_x+k)*4+0]=lba_map->palet->couleur[lba_map->brick[i].pixel[j][k]][0];
              texture_map[((offset_y+j)*2048+offset_x+k)*4+1]=lba_map->palet->couleur[lba_map->brick[i].pixel[j][k]][1];
              texture_map[((offset_y+j)*2048+offset_x+k)*4+2]=lba_map->palet->couleur[lba_map->brick[i].pixel[j][k]][2];
              if(lba_map->brick[i].pixel[j][k]==0)
              texture_map[((offset_y+j)*2048+offset_x+k)*4+3]=0;
              else
              texture_map[((offset_y+j)*2048+offset_x+k)*4+3]=255;
            }

            if(shared_brick1[i].id!=-1)
            {
                for(j=0;j<38;j++)
                for(k=0;k<48;k++)
                {
                    int k2=k-(shared_brick1[i].dx-shared_brick1[i].dz)*24;
                    int j2=j-(shared_brick1[i].dx+shared_brick1[i].dz)*12;
                    if(j2>=0&&j2<38&&k2>=0&&k2<48&&lba_map->brick[shared_brick1[i].id].pixel[j2][k2]!=0)
                    {

                        texture_map[((offset_y+j)*2048+offset_x+k)*4+0]=lba_map->palet->couleur[lba_map->brick[shared_brick1[i].id].pixel[j2][k2]][0];
                        texture_map[((offset_y+j)*2048+offset_x+k)*4+1]=lba_map->palet->couleur[lba_map->brick[shared_brick1[i].id].pixel[j2][k2]][1];
                        texture_map[((offset_y+j)*2048+offset_x+k)*4+2]=lba_map->palet->couleur[lba_map->brick[shared_brick1[i].id].pixel[j2][k2]][2];
                        texture_map[((offset_y+j)*2048+offset_x+k)*4+3]=255;
                    }
                }
            }
            if(shared_brick2[i].id!=-1)
            {
                for(j=0;j<38;j++)
                for(k=0;k<48;k++)
                {
                    int k2=k-(shared_brick2[i].dx-shared_brick2[i].dz)*24;
                    int j2=j-(shared_brick2[i].dx+shared_brick2[i].dz)*12;
                    if(j2>=0&&j2<38&&k2>=0&&k2<48&&lba_map->brick[shared_brick2[i].id].pixel[j2][k2]!=0)
                    {

                        texture_map[((offset_y+j)*2048+offset_x+k)*4+0]=lba_map->palet->couleur[lba_map->brick[shared_brick2[i].id].pixel[j2][k2]][0];
                        texture_map[((offset_y+j)*2048+offset_x+k)*4+1]=lba_map->palet->couleur[lba_map->brick[shared_brick2[i].id].pixel[j2][k2]][1];
                        texture_map[((offset_y+j)*2048+offset_x+k)*4+2]=lba_map->palet->couleur[lba_map->brick[shared_brick2[i].id].pixel[j2][k2]][2];
                        texture_map[((offset_y+j)*2048+offset_x+k)*4+3]=255;
                    }
                }
            }
        }






       glGenTextures(1, &texture_name);
//        glColorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_FALSE);
       glEnable(GL_TEXTURE_2D);
       glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
           glColor3f(1,1,1);
//        glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
        glBindTexture(GL_TEXTURE_2D, texture_name);

       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_NEAREST);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                       GL_NEAREST);
       texture_map[0]=0;
       texture_map[1]=0;
       texture_map[2]=0;
       texture_map[3]=255;
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2048,
                    2048, 0, GL_RGBA, GL_UNSIGNED_BYTE,
                    texture_map);









    int texture_x,texture_y;
int X,Y,Z;
int x,y;

      double h=0.5;






int cmpt=0;
brick_number=cmpt;
list_name=glGenLists(1);
printf("liste %d",list_name);

glNewList(list_name, GL_COMPILE);

    glBegin(GL_TRIANGLES);
for(Z=0;Z<64;Z++)
for(X=0;X<64;X++)
for(Y=0;Y<25;Y++)

{

if(lba_map->grid->info_brick[X][Z][Y].index_brick!=-1)
{
      cmpt++;
      texture_x=(lba_map->grid->info_brick[X][Z][Y].index_brick)%42;
      texture_x*=48;
      texture_y=(lba_map->grid->info_brick[X][Z][Y].index_brick)/42;
      texture_y*=38;

    switch(lba_map->grid->info_brick[X][Z][Y].shape)
    {
        case 1:
/*          face(X,Y,Z,texture_x,texture_y,h,3,11,18);
            face(X,Y,Z,texture_x,texture_y,h,18,14,3);
            face(X,Y,Z,texture_x,texture_y,h,15,19,17);
            face(X,Y,Z,texture_x,texture_y,h,17,9,15);
            face(X,Y,Z,texture_x,texture_y,h,0,2,12);
            face(X,Y,Z,texture_x,texture_y,h,13,8,1);
            face(X,Y,Z,texture_x,texture_y,h,2,0,4);
            face(X,Y,Z,texture_x,texture_y,h,4,10,2);
            face(X,Y,Z,texture_x,texture_y,h,1,8,16);
            face(X,Y,Z,texture_x,texture_y,h,16,5,1);
UNDER FACE  face(X,Y,Z,texture_x,texture_y,h,7,17,19);
            face(X,Y,Z,texture_x,texture_y,h,18,11,6);*/
            face(X,Y,Z,texture_x,texture_y,h,3,11,18,false);
            face(X,Y,Z,texture_x,texture_y,h,18,14,3,false);
            face(X,Y,Z,texture_x,texture_y,h,15,19,17,false);
            face(X,Y,Z,texture_x,texture_y,h,17,9,15,false);
            face(X,Y,Z,texture_x,texture_y,h,0,2,12,false);
            face(X,Y,Z,texture_x,texture_y,h,13,8,1,false);
/*            face(X,Y,Z,texture_x,texture_y,h,2,0,4,true);
            face(X,Y,Z,texture_x,texture_y,h,4,10,2,true);
            face(X,Y,Z,texture_x,texture_y,h,1,8,16,true);
            face(X,Y,Z,texture_x,texture_y,h,16,5,1,true);*/
            break;
        case 2:
            face(X,Y,Z,texture_x,texture_y,h,11,18,14,false);
            face(X,Y,Z,texture_x,texture_y,h,11,13,8,false);
            face(X,Y,Z,texture_x,texture_y,h,8,6,11,false);
            face(X,Y,Z,texture_x,texture_y,h,19,17,9,false);
            face(X,Y,Z,texture_x,texture_y,h,9,15,19,false);
            face(X,Y,Z,texture_x,texture_y,h,9,16,5,false);
            break;
        case 3:
            face(X,Y,Z,texture_x,texture_y,h,15,19,17,false);
            face(X,Y,Z,texture_x,texture_y,h,2,12,17,false);
            face(X,Y,Z,texture_x,texture_y,h,17,7,2,false);
            face(X,Y,Z,texture_x,texture_y,h,11,18,14,false);
            face(X,Y,Z,texture_x,texture_y,h,14,3,10,false);
            face(X,Y,Z,texture_x,texture_y,h,2,4,10,false);
            break;
        case 4:
            face(X,Y,Z,texture_x,texture_y,h,11,18,8,false);
            face(X,Y,Z,texture_x,texture_y,h,8,1,11,false);
            face(X,Y,Z,texture_x,texture_y,h,19,17,9,false);
            face(X,Y,Z,texture_x,texture_y,h,1,8,16,false);
            face(X,Y,Z,texture_x,texture_y,h,16,5,1,false);
            face(X,Y,Z,texture_x,texture_y,h,0,4,10,false);
            break;
        case 5:
            face(X,Y,Z,texture_x,texture_y,h,19,17,0,false);
            face(X,Y,Z,texture_x,texture_y,h,0,2,19,false);
            face(X,Y,Z,texture_x,texture_y,h,3,11,18,false);
            face(X,Y,Z,texture_x,texture_y,h,1,16,5,false);
            face(X,Y,Z,texture_x,texture_y,h,2,0,4,false);
            face(X,Y,Z,texture_x,texture_y,h,4,10,3,false);
            break;
        case 6:
            face(X,Y,Z,texture_x,texture_y,h,4,2,12,false);
            face(X,Y,Z,texture_x,texture_y,h,13,8,5,false);
            face(X,Y,Z,texture_x,texture_y,h,3,11,18,false);
            face(X,Y,Z,texture_x,texture_y,h,18,14,3,false);
            face(X,Y,Z,texture_x,texture_y,h,19,17,9,false);
            face(X,Y,Z,texture_x,texture_y,h,9,15,19,false);
            face(X,Y,Z,texture_x,texture_y,h,5,9,16,false);
            face(X,Y,Z,texture_x,texture_y,h,4,3,10,false);
            break;
        case 7:
            face(X,Y,Z,texture_x,texture_y,h,19,8,1,false);
            face(X,Y,Z,texture_x,texture_y,h,0,2,18,false);
            face(X,Y,Z,texture_x,texture_y,h,3,11,18,false);
            face(X,Y,Z,texture_x,texture_y,h,19,17,9,false);
            face(X,Y,Z,texture_x,texture_y,h,1,8,16,false);
            face(X,Y,Z,texture_x,texture_y,h,16,5,1,false);
            face(X,Y,Z,texture_x,texture_y,h,10,2,0,false);
            face(X,Y,Z,texture_x,texture_y,h,0,4,10,false);
            break;
        case 8:
            face(X,Y,Z,texture_x,texture_y,h,0,4,10,false);
            face(X,Y,Z,texture_x,texture_y,h,10,13,8,false);
            face(X,Y,Z,texture_x,texture_y,h,8,1,10,false);
            face(X,Y,Z,texture_x,texture_y,h,11,18,14,false);
            face(X,Y,Z,texture_x,texture_y,h,15,19,17,false);
            face(X,Y,Z,texture_x,texture_y,h,17,9,15,false);
            face(X,Y,Z,texture_x,texture_y,h,1,8,16,false);
            face(X,Y,Z,texture_x,texture_y,h,16,5,1,false);
            break;
        case 9:
            face(X,Y,Z,texture_x,texture_y,h,16,0,2,false);
            face(X,Y,Z,texture_x,texture_y,h,2,12,16,false);
            face(X,Y,Z,texture_x,texture_y,h,3,11,18,false);
            face(X,Y,Z,texture_x,texture_y,h,18,14,3,false);
            face(X,Y,Z,texture_x,texture_y,h,15,19,17,false);
            face(X,Y,Z,texture_x,texture_y,h,16,5,1,false);
            face(X,Y,Z,texture_x,texture_y,h,10,2,0,false);
            face(X,Y,Z,texture_x,texture_y,h,0,4,10,false);
            break;
        case 10:
            face(X,Y,Z,texture_x,texture_y,h,6,11,12,false);
            face(X,Y,Z,texture_x,texture_y,h,13,17,7,false);
            face(X,Y,Z,texture_x,texture_y,h,11,18,14,false);
            face(X,Y,Z,texture_x,texture_y,h,15,19,17,false);
            break;
        case 11:
            face(X,Y,Z,texture_x,texture_y,h,0,11,18,false);
            face(X,Y,Z,texture_x,texture_y,h,19,17,1,false);
            face(X,Y,Z,texture_x,texture_y,h,0,4,10,false);
            face(X,Y,Z,texture_x,texture_y,h,1,5,16,false);
            break;
        case 12:
            face(X,Y,Z,texture_x,texture_y,h,11,18,9,false);
            face(X,Y,Z,texture_x,texture_y,h,8,4,10,false);
            face(X,Y,Z,texture_x,texture_y,h,19,17,9,false);
            face(X,Y,Z,texture_x,texture_y,h,8,16,5,false);
            break;
        case 13:
            face(X,Y,Z,texture_x,texture_y,h,3,19,17,false);
            face(X,Y,Z,texture_x,texture_y,h,16,5,2,false);
            face(X,Y,Z,texture_x,texture_y,h,2,4,10,false);
            face(X,Y,Z,texture_x,texture_y,h,3,18,11,false);
            break;


    }

/*
if(lba_map->grid->info_brick[X][Z][Y].shape==15)
{

glNormal3d(1,2,0);glTexCoord2d((texture_x+1.) /2048.,(texture_y+12.)/2048.);
glNormal3d(1,2,0);glTexCoord2d((texture_x+24.)/2048.,(texture_y+37.)/2048.);
glNormal3d(1,2,0);glTexCoord2d((texture_x+46.)/2048.,(texture_y+26.)/2048.);
glNormal3d(1,2,0);glTexCoord2d((texture_x+23. )/2048.,(texture_y+1.)/2048.);
}
if(lba_map->grid->info_brick[X][Z][Y].shape==14)
{
glNormal3d(0,2,1);glTexCoord2d((texture_x+2.) /2048.,(texture_y+26.)/2048.);
glNormal3d(0,2,1);glTexCoord2d((texture_x+23.)/2048.,(texture_y+36.)/2048.);
glNormal3d(0,2,1);glTexCoord2d((texture_x+46.)/2048.,(texture_y+12.)/2048.);
glNormal3d(0,2,1);glTexCoord2d((texture_x+24. )/2048.,(texture_y+1.)/2048.);
}
}*/
    }
   }    glEnd();

	glEndList();

    printf("total briques lba_map : %d",cmpt);

/*
FILE *f=fopen("map.obj","wb");

fprintf(f,"o lba_map.obj\n");
//fprintf(f,"g default\n");
//fprintf(f,"mtllib o lba_map.obj\n");
for(int i=0;i<lba_face.size();i++)
{
fprintf(f,"v %f %f %f\n",lba_face[i].v[0],lba_face[i].v[1],lba_face[i].v[2]);
fprintf(f,"v %f %f %f\n",lba_face[i].v[3],lba_face[i].v[4],lba_face[i].v[5]);
fprintf(f,"v %f %f %f\n",lba_face[i].v[6],lba_face[i].v[7],lba_face[i].v[8]);
}
for(int i=0;i<lba_face.size();i++)
{
fprintf(f,"vt %f %f\n",lba_face[i].vt[0],lba_face[i].vt[1]);
fprintf(f,"vt %f %f\n",lba_face[i].vt[2],lba_face[i].vt[3]);
fprintf(f,"vt %f %f\n",lba_face[i].vt[4],lba_face[i].vt[5]);
}
for(int i=0;i<lba_face.size();i++)
{
fprintf(f,"vn %f %f %f\n",lba_face[i].vn[0],lba_face[i].vn[1],lba_face[i].vn[2]);
fprintf(f,"vn %f %f %f\n",lba_face[i].vn[0],lba_face[i].vn[1],lba_face[i].vn[2]);
fprintf(f,"vn %f %f %f\n",lba_face[i].vn[0],lba_face[i].vn[1],lba_face[i].vn[2]);
}
for(int i=0;i<lba_face.size();i++)
//fprintf(f,"f %d/%d/%d %d/%d/%d %d/%d/%d\n",i*3+0,i*3+1,i*3+2,i*3+0,i*3+1,i*3+2,i*3+0,i*3+1,i*3+2);
fprintf(f,"f %d/%d/%d %d/%d/%d %d/%d/%d\n",i*3+1,i*3+1,i*3+1,i*3+2,i*3+2,i*3+2,i*3+3,i*3+3,i*3+3);

tgaSave("texture.tga",2048,2048,32,texture_map);
/* int tgaSave(    char            *filename,
                 short int       width,
                 short int       height,
                 unsigned char   pixelDepth,
                 unsigned char   *imageData*/

/*fclose(f);*/

    }










