1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| "textetext.h"
//une texture de texte, en fait c'est 256 texture différente
class textetext
{
public:
GLuint Numb[256];
textetext();
bool Charge(const int l, const int h, char s[]);
};
"textetext.cpp"
map<string,textetext*> TXTS_map; //la map de toute les textures de texte chargée
textetext* TXTS_text; //la texture de texte courante
textetext::textetext()
{
glGenTextures(256, Numb);
};//quand on créé la texture de texte on ce donne 256 numéro de texture
//pour charger une texture
bool textetext::Charge(const int l, const int h, char s[])
{
//la c'est le bordel un peu, il faut normalement h=l=256
unsigned char lu[l*h*3];
unsigned char tx[256][l/16][h/16][3];
int u,i,j;
FILE *f = fopen(s, "rb");
if (f)
{
fread(lu, l*h*3, 1, f);
fclose(f);
}
else return 0;
//on charge la texture
for(u=0;u<256;u++)//pour chacun des 256 caractére
{
for(i=0;i<l/16;i++)//on découpe un carré 16 *16
for(j=0;j<h/16;j++)//dans la texture d'origine et à l'endroit voulu
{
tx[u][15-j][i][0]=lu[16*3*(u%16)+(u-u%16)*256*3+3*i+256*3*j];
tx[u][15-j][i][1]=lu[16*3*(u%16)+(u-u%16)*256*3+3*i+256*3*j+1];
tx[u][15-j][i][2]=lu[16*3*(u%16)+(u-u%16)*256*3+3*i+256*3*j+2];
}
glBindTexture(GL_TEXTURE_2D,Numb[u]);//on attache la texture qu'on va loadé
//on load :
glTexImage2D(GL_TEXTURE_2D, 0,3, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, tx[u]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
}
return 1;
//on a chargé la texture
};
//pour ajouter une texture (une fonte)
bool TXTS_charge_font(string nom, char chemin[], int l, int h)
{
textetext *txt=new (textetext);//on créé la nouvelle fonte
txt->Charge(l,h,chemin);//on la charge
TXTS_map.insert(make_pair(nom,txt));//on l'insert dans la liste des fonte existante
};
bool TXTS_font(string nom)//pour utiliser une fonte
{
TXTS_text = TXTS_map[nom];//la fonte courante est celle de la map
};
void TXTS_print(double x, double y, const char t[])
{//pour afficher
int i=0;
double dy=.05,dx=.05;
//on active le texturing
float ambientProperties[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float diffuseProperties[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float positionProperties[4] = {0.0f, 0.0f, 0.0f, 0.0f};
glLightfv( GL_LIGHT0, GL_AMBIENT, ambientProperties);
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
glLightfv( GL_LIGHT0, GL_POSITION, positionProperties);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
float Blanc[4] = {1.0f, 1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, Blanc);
glEnable(GL_TEXTURE_2D);
//on parcour la chaine de caractére jusqu'a ca fin
while(t[i]!='\0'){
//on attache la texture qui correspond au caractére
glBindTexture(GL_TEXTURE_2D,TXTS_text->Numb[t[i]]);
//et on fait un carré
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex3d(x+i*dx,y,0);
glTexCoord2d(1,0); glVertex3d(x+i*dx+dx,y,0);
glTexCoord2d(1,1); glVertex3d(x+i*dx+dx,y+dy,0);
glTexCoord2d(0,1); glVertex3d(x+i*dx,y+dy,0);
glEnd();
i++;
}
}; |
Partager