4 * Gestion des textures sous OpenGL
9 * Les textures sont toujours initialisee avec des parametres par defaut
10 * texture 1D: WRAP_S = CLAMP
11 * MAG_FILTER = NEAREST
12 * generation de texture automatique en OBJECT_LINEAR
15 * texture 2D: WRAP_S/T = REPEAT
16 * MAG/MIN_FILTER = LINEAR
17 * generation de texture automatique en OBJECT_LINEAR
20 * texture 2D MipMap: WRAP_S/T = REPEAT
22 * MIN_FILTER = LINEAR_MIPMAP_NEAREST
23 * generation de texture automatique en OBJECT_LINEAR
28 * Pour utiliser le binding des textures il faut que les 2 extensions
30 * o GL_EXT_texture_object:
31 * - glBindTextureEXT, glGenTexturesEXT, glDeleteTexturesEXT
35 * Ce package a ete teste sur SGI, OSF, SUN, HP et WNT.
38 * Historique des modifications
39 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 * 22-05-97: PCT ; Creation
41 * 18-06-97: FMN ; Ajout entete
42 * 20-06-97: PCT ; Correction bug parametres par defaut texture 1D
43 * 30-06-97: PCT ; Correction bug rechargement de la texture courante
44 * 04-07-97: PCT ; suppression de l'utilisation de libimage.a de SGI
45 * 01-08-97: PCT ; suppression InitializeTextureBox()
46 * 04-08-97: FMN,PCT ; Portage WNT
47 * 05-08-97: FMN ; ajout GetTextureData...
48 * 10-09-97: PCT ; ajout commentaires. GetTexture() ne doit pas
49 * etre utilisee dans Cas.Cade ( le chargement est
50 * fait par Graphic3d )
51 * 06-10-97: FMN ; Portage HP
52 * 14-10-97: FMN ; Ajout OpenGl_Extension
53 * 22-10-97: FMN ; Meilleure gestion de l'extension glXGetCurrentDisplayEXT
54 * 04-11-97: FMN ; Gestion des differentes versions GLX
55 * 19-11-97: FMN ; Ajout GetCurrentDisplay plus simple que glXGetCurrentDisplayEXT
56 * 04-12-97: FMN ; On suppose que l'on travaille en OpenGL1.1 (cf OpenGl_Extension)
57 * 17-12-97: FMN ; Probleme compilation SGI
58 * 17-12-97: FMN ; Probleme sur Optimisation sur MyBindTextureEXT()
59 * Le test sur la texture courante doit tenir compte du contexte.
60 * 22-07-98: FGU ; Ajout fonctions TransferTexture_To_Data() et TransferData_To_Texture()
63 /*----------------------------------------------------------------------*/
65 #define IMP141100 /*GG_Allocates and free texture block count
69 /*----------------------------------------------------------------------*/
78 #include <OpenGl_tgl_all.hxx>
79 #include <OpenGl_txgl.hxx>
80 #include <OpenGl_Extension.hxx>
81 #include <OpenGl_ImageBox.hxx>
82 #include <OpenGl_TextureBox.hxx>
83 #include <OpenGl_Memory.hxx>
86 /*----------------------------------------------------------------------*/
94 #define GROW_TEXTURES 8
95 #define GROW_TEXTURES_DATA 8
96 #define GROW_CONTEXT 8
99 #define glGenTextures glGenTexturesEXT
100 #define glDeleteTextures glDeleteTexturesEXT
101 #define glBindTexture glBindTextureEXT
105 #define max(a,b) ((a) > (b)) ? (a) : (b);
108 /*----------------------------------------------------------------------*/
113 typedef enum {TEXDATA_NONE, TEXDATA_1D, TEXDATA_2D, TEXDATA_2DMM} texDataStatus;
114 typedef enum {TEX_NONE, TEX_ALLOCATED} texStatus;
116 typedef GLfloat SizeType[4];
118 typedef int TextureDataID;
119 #define TEXTUREDATA_ERROR -1
123 char imageFileName[128];
124 int imageWidth, imageHeight;
126 texDataStatus status;
129 IMPLEMENT_MEMORY_OPERATORS
137 GLDRAWABLE *drawable;
139 char *use_bind_texture;
150 GLfloat scalex, scaley;
151 GLfloat transx, transy;
154 IMPLEMENT_MEMORY_OPERATORS
158 /*----------------------------------------------------------------------*/
160 * Variables statiques
163 static texDraw *textab = NULL;
164 static int textures_count = 0;
165 static int textures_size = 0;
167 static texData *texdata = NULL;
168 static int textures_data_count = 0;
169 static int textures_data_size = 0;
171 static TextureDataID current_texture_data = TEXTUREDATA_ERROR;
172 static TextureID current_texture = TEXTUREBOX_ERROR;
174 static GLfloat sgenparams[] = { 1.0 ,0.0 ,0.0 ,0.0};
175 static GLfloat tgenparams[] = { 0.0 ,1.0 ,0.0 ,0.0};
177 static GLenum status2type[] = { GL_NONE, GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_2D };
179 static GLboolean init_extension = GL_FALSE;
180 static GLboolean use_bind_texture = GL_FALSE;
182 /*----------------------------------------------------------------------*/
188 /*----------------------------------------------------------------------*/
190 * recherche l'existence de datas de texture par son nom
192 static TextureDataID FindTextureData(char *FileName)
196 for (i=0; i<textures_data_size; i++)
197 if ( texdata[i].status!=TEXDATA_NONE && strcmp(FileName, texdata[i].imageFileName)==0 )
200 return TEXTUREDATA_ERROR;
203 /*----------------------------------------------------------------------*/
205 * recherche un emplacement de data texture libre
207 static TextureDataID FindFreeTextureData(void)
211 /* ya encore de la place ? */
212 if (textures_data_count == textures_data_size)
214 textures_data_size += GROW_TEXTURES_DATA;
215 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
216 texdata = (texData*)realloc(texdata, textures_data_size*sizeof(texData));
218 texdata = cmn_resizemem<texData>(texdata, textures_data_size);
220 if (texdata == NULL) return TEXTUREDATA_ERROR;
222 for (i=textures_data_count; i<textures_data_size; i++)
223 texdata[i].status = TEXDATA_NONE;
225 /* on a deja assez perdu de temps comme ca => retourne un ID rapidement... */
226 return textures_data_count++;
229 /* recherche d'un ID libre */
230 for (i=0; i<textures_data_size; i++)
231 if (texdata[i].status == TEXDATA_NONE)
234 textures_data_count++;
236 textures_data_count = max(textures_data_count,i+1);
241 return TEXTUREDATA_ERROR;
244 /*----------------------------------------------------------------------*/
246 * recherche un emplacement de texture libre
248 static TextureID FindFreeTexture(void)
252 /* ya encore de la place ? */
253 if (textures_count == textures_size)
255 textures_size += GROW_TEXTURES;
256 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
257 textab = (texDraw*)realloc(textab, textures_size*sizeof(texDraw));
259 textab = cmn_resizemem<texDraw>(textab, textures_size);
261 if (textab == NULL) return TEXTUREBOX_ERROR;
263 for (i=textures_count; i<textures_size; i++)
264 textab[i].status = TEX_NONE;
266 /* on a deja assez perdu de temps comme ca => retourne un ID rapidement... */
267 return textures_count++;
270 for (i=0; i<textures_size; i++)
271 if (textab[i].status == TEX_NONE)
276 textures_count = max(textures_count,i+1);
281 return TEXTUREBOX_ERROR;
284 /*----------------------------------------------------------------------*/
286 * regarde si la texture a ete definie pour le contexte courant
288 static int FindTextureContext(TextureID ID)
292 GLCONTEXT cur = GET_GL_CONTEXT();
293 for (i=0; i<textab[ID].context_count; i++)
294 if (textab[ID].context[i] == cur)
297 return TEXTUREBOX_ERROR;
300 /*----------------------------------------------------------------------*/
302 * chargement d'une texture suivant son type
304 static void LoadTexture(TextureID ID)
308 data = textab[ID].data;
309 switch (texdata[data].status)
312 glTexImage1D(GL_TEXTURE_1D, 0, 4,
313 texdata[data].imageWidth, 0,
314 GL_RGBA, GL_UNSIGNED_BYTE, texdata[data].image);
318 glTexImage2D(GL_TEXTURE_2D, 0, 4,
319 texdata[data].imageWidth, texdata[data].imageHeight, 0,
320 GL_RGBA, GL_UNSIGNED_BYTE, texdata[data].image);
324 gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
325 texdata[data].imageWidth,
326 texdata[data].imageHeight,
327 GL_RGBA, GL_UNSIGNED_BYTE, texdata[data].image);
334 /*----------------------------------------------------------------------*/
336 * les parametres d'initialisation d'une texture
337 * NE PAS METTRE DANS UNE DISPLAY LIST POUR L'INSTANT ( pb avec les matrices )
339 static void SetTextureParam(TextureID ID)
345 data = textab[ID].data;
346 glGetIntegerv(GL_MATRIX_MODE, &cur_matrix);
349 * MISE EN PLACE DE LA MATRICE DE TEXTURE
351 glMatrixMode(GL_TEXTURE);
353 /* if (textab[ID].Gen != GL_SPHERE_MAP)
355 glScalef(textab[ID].scalex, textab[ID].scaley, 1.0);
356 glTranslatef(-textab[ID].transx, -textab[ID].transy, 0.0);
357 glRotatef(-textab[ID].angle, 0.0, 0.0, 1.0);
362 * GENERATION AUTOMATIQUE DE TEXTURE
364 switch (textab[ID].Gen)
366 case GL_OBJECT_LINEAR:
367 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
368 glTexGenfv(GL_S, GL_OBJECT_PLANE, textab[ID].Plane1);
369 if (texdata[data].status != TEXDATA_1D)
371 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
372 glTexGenfv(GL_T, GL_OBJECT_PLANE, textab[ID].Plane2);
377 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
378 if (texdata[data].status != TEXDATA_1D)
379 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
383 glMatrixMode(GL_MODELVIEW);
387 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
388 glTexGenfv(GL_S, GL_EYE_PLANE, textab[ID].Plane1);
390 if (texdata[data].status != TEXDATA_1D)
392 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
393 glTexGenfv(GL_T, GL_EYE_PLANE, textab[ID].Plane2);
402 * RENDU DE LA TEXTURE AVEC LES LUMIERES
404 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textab[ID].Light);
408 * LISSAGE DE LA TEXTURE
410 switch (texdata[data].status)
414 glTexParameteri(texdata[data].type, GL_TEXTURE_MAG_FILTER, textab[ID].Render);
415 glTexParameteri(texdata[data].type, GL_TEXTURE_MIN_FILTER, textab[ID].Render);
419 if (textab[ID].Render == GL_NEAREST)
421 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
422 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
426 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
427 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
438 switch (texdata[data].status)
441 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, textab[ID].Wrap);
446 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textab[ID].Wrap);
447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textab[ID].Wrap);
453 glMatrixMode(cur_matrix);
456 /*----------------------------------------------------------------------*/
458 * simulation du glGenTexturesEXT pour un context
460 static void MyGenTextureEXT(TextureID ID)
462 #if defined(GL_EXT_texture_object)
467 Context = textab[ID].context_count;
468 data = textab[ID].data;
472 use_bind_texture = QueryExtension("GL_EXT_texture_object");
474 init_extension = GL_TRUE;
476 printf("GL_EXT_texture_object %d\n", use_bind_texture);
480 if (use_bind_texture)
483 printf("MyGenTextureEXT::gen texture\n");
486 textab[ID].context[Context] = GET_GL_CONTEXT();
487 textab[ID].drawable[Context] = GET_GLDEV_CONTEXT();
488 textab[ID].use_bind_texture[Context] = (char)use_bind_texture;
489 glGenTextures(1, &textab[ID].number[Context]);
490 glBindTexture(texdata[data].type, textab[ID].number[Context]);
492 textab[ID].context_count++;
495 #endif /* GL_EXT_texture_object */
496 if (current_texture_data != textab[ID].data)
500 /*----------------------------------------------------------------------*/
502 * simulation du glBindTextureEXT
504 static void MyBindTextureEXT(TextureID ID, int Context)
506 /* si l'extension est presente, c'est facile */
507 #if defined(GL_EXT_texture_object)
509 data = textab[ID].data;
511 if (textab[ID].status == TEXDATA_NONE)
515 printf("MyBindTextureEXT::ID %d data %d context %d Current %d\n", ID, textab[ID].data, Context, current_texture_data);
518 if (textab[ID].use_bind_texture[Context])
520 /* OCC11904 - make sure that the correct texture is bound before drawing */
521 GLenum aParamName = textab[ID].status == TEXDATA_1D ?
522 GL_TEXTURE_BINDING_1D : GL_TEXTURE_BINDING_2D;
524 glGetIntegerv( aParamName, &aCurrTex );
525 if ( textab[ID].number[Context] != aCurrTex )
528 printf("MyBindTextureEXT::bind texture\n");
530 glBindTexture( texdata[data].type, textab[ID].number[Context] );
533 /* sinon on reset tout */
535 #endif /* GL_EXT_texture_object */
537 /* OCC11904 - make sure that the correct texture is bound before drawing */
538 GLenum aParamName = textab[ID].status == TEXDATA_1D ?
539 GL_TEXTURE_BINDING_1D : GL_TEXTURE_BINDING_2D;
541 glGetIntegerv( aParamName, &aCurrTex );
542 if ( textab[ID].number[Context] != aCurrTex )
545 printf("MyBindTextureEXT::chargement sans bind de la texture\n");
552 /*----------------------------------------------------------------------*/
554 * installation de la texture pour le dernier contexte
556 static int InstallTextureInContext(TextureID ID)
559 printf("InstallTextureInContext::installation de la texture dans le context\n");
563 /* ya encore de la place dans le tableau de context ? */
564 if (textab[ID].context_count == textab[ID].context_size)
567 printf("InstallTextureInContext::allocation dans le context\n");
569 textab[ID].context_size += GROW_CONTEXT;
570 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
572 (GLuint*)realloc(textab[ID].number, textab[ID].context_size*sizeof(GLuint));
574 (GLCONTEXT*)realloc(textab[ID].context, textab[ID].context_size*sizeof(GLCONTEXT));
575 textab[ID].drawable =
576 (GLDRAWABLE*)realloc(textab[ID].drawable, textab[ID].context_size*sizeof(GLDRAWABLE));
577 textab[ID].use_bind_texture =
578 (char*)realloc(textab[ID].use_bind_texture, textab[ID].context_size);
581 cmn_resizemem<GLuint>(textab[ID].number, textab[ID].context_size);
583 cmn_resizemem<GLCONTEXT>(textab[ID].context, textab[ID].context_size);
584 textab[ID].drawable =
585 cmn_resizemem<GLDRAWABLE>(textab[ID].drawable, textab[ID].context_size);
586 textab[ID].use_bind_texture =
587 cmn_resizemem<char>(textab[ID].use_bind_texture, textab[ID].context_size);
589 if ( (textab[ID].number == NULL) ||
590 (textab[ID].context == NULL) ||
591 (textab[ID].drawable == NULL) ||
592 (textab[ID].use_bind_texture == NULL) )
594 /* erreur => libere tout */
595 free(textab[ID].number);
596 free(textab[ID].context);
597 free(textab[ID].drawable);
598 free(textab[ID].use_bind_texture);
599 textab[ID].context_size = 0;
601 return TEXTUREBOX_ERROR;
608 printf("InstallTextureInContext::context ok\n");
613 /*----------------------------------------------------------------------*/
614 static TextureID GetTexture(char *FileName, texDataStatus status)
620 /* essait de trouver la texture */
621 i = FindTextureData(FileName);
622 if (i == TEXTUREDATA_ERROR)
625 printf("GetTexture::la texture %s n'existe pas => chargement\n", FileName);
627 /* creation d'une texture */
628 i = FindFreeTextureData();
629 if (i == TEXTUREDATA_ERROR) return TEXTUREBOX_ERROR;
631 texdata[i].share_count = 0;
632 strcpy(texdata[i].imageFileName, FileName);
633 texdata[i].image = (GLubyte *)read_texture(FileName,
634 &texdata[i].imageWidth,
635 &texdata[i].imageHeight,
637 if (texdata[i].image == NULL) return TEXTUREBOX_ERROR;
639 texdata[i].status = status;
640 texdata[i].type = status2type[status];
643 j = FindFreeTexture();
644 if (j != TEXTUREBOX_ERROR)
647 printf("GetTexture::installation texture pour obj %d\n", j);
649 textab[j].context_count = 0;
650 textab[j].context_size = 0;
651 textab[j].number = NULL;
652 textab[j].drawable = NULL;
653 textab[j].context = NULL;
654 textab[j].use_bind_texture = NULL;
656 textab[j].status = TEX_ALLOCATED;
657 texdata[i].share_count++;
659 SetTextureDefaultParams(j);
662 printf("GetTexture::texture %s(%d) texture %d count=%d\n", texdata[i].imageFileName, i, j, texdata[i].share_count);
666 if (texdata[i].share_count != 0)
667 free(texdata[i].image);
673 /*----------------------------------------------------------------------*/
674 static TextureID GetTextureData(char *FileName, texDataStatus status, const GLint width, const GLint height, const void *data)
679 /* essait de trouver la texture */
680 i = FindTextureData(FileName);
681 if (i == TEXTUREDATA_ERROR)
684 printf("GetTextureData::la texture %s n'existe pas => chargement\n", FileName);
686 /* creation d'une texture */
687 i = FindFreeTextureData();
688 if (i == TEXTUREDATA_ERROR) return TEXTUREBOX_ERROR;
690 texdata[i].share_count = 0;
691 strcpy(texdata[i].imageFileName, FileName);
692 texdata[i].image = (GLubyte *)malloc(width*height*4*sizeof(GLubyte));
693 memcpy(texdata[i].image, data, (width*height*4));
694 texdata[i].imageWidth = width;
695 texdata[i].imageHeight = height;
697 if (texdata[i].image == NULL) return TEXTUREBOX_ERROR;
699 texdata[i].status = status;
700 texdata[i].type = status2type[status];
703 j = FindFreeTexture();
704 if (j != TEXTUREBOX_ERROR)
707 printf("GetTextureData::installation texture pour obj %d\n", j);
709 textab[j].context_count = 0;
710 textab[j].context_size = 0;
711 textab[j].number = NULL;
712 textab[j].drawable = NULL;
713 textab[j].context = NULL;
714 textab[j].use_bind_texture = NULL;
716 textab[j].status = TEX_ALLOCATED;
717 texdata[i].share_count++;
719 SetTextureDefaultParams(j);
722 printf("GetTextureData::texture %s(%d) texture %d count=%d\n", texdata[i].imageFileName, i, j, texdata[i].share_count);
726 if (texdata[i].share_count != 0)
727 free(texdata[i].image);
732 /*----------------------------------------------------------------------*/
734 * Fonctions publiques
738 /*----------------------------------------------------------------------*/
740 GLboolean IsTextureValid(TextureID ID)
742 if ((ID<0) | (ID>=textures_count))
746 return textab[ID].status == TEX_ALLOCATED;
751 /*----------------------------------------------------------------------*/
753 TextureID GetTexture1D(char *FileName)
756 printf("GetTexture1D::loading 1d %s \n", FileName);
758 return GetTexture(FileName, TEXDATA_1D);
761 /*----------------------------------------------------------------------*/
763 TextureID GetTexture2D(char *FileName)
766 printf("GetTexture2D::loading 2d %s \n", FileName);
768 return GetTexture(FileName, TEXDATA_2D);
771 /*----------------------------------------------------------------------*/
773 TextureID GetTexture2DMipMap(char *FileName)
776 printf("GetTexture2DMipMap::loading 2dmm %s \n", FileName);
778 return GetTexture(FileName, TEXDATA_2DMM);
781 /*----------------------------------------------------------------------*/
783 TextureID GetTextureData1D(char *FileName, const GLint width, const GLint height, const void *data)
786 printf("GetTextureData1D::loading 1d %s \n", FileName);
788 return GetTextureData(FileName, TEXDATA_1D, width, height, data);
791 /*----------------------------------------------------------------------*/
793 TextureID GetTextureData2D(char *FileName, const GLint width, const GLint height, const void *data)
796 printf("GetTextureData2D::loading 2d %s \n", FileName);
798 return GetTextureData(FileName, TEXDATA_2D, width, height, data);
801 /*----------------------------------------------------------------------*/
803 TextureID GetTextureData2DMipMap(char *FileName, const GLint width, const GLint height, const void *data)
806 printf("GetTextureData2DMipMap::loading 2dmm %s \n", FileName);
808 return GetTextureData(FileName, TEXDATA_2DMM, width, height, data);
812 /*----------------------------------------------------------------------*/
814 void SetCurrentTexture(TextureID ID)
818 if (!IsTextureValid(ID)) return;
820 context = FindTextureContext(ID);
822 /* la texture n'existe pas dans ce contexte */
823 if (context == TEXTUREBOX_ERROR)
826 printf("SetCurrentTexture::installation texture %d dans context\n", ID);
828 /* si on a une erreur pendant l'installation dans le context
829 * alors on installe la texture sans bind */
830 if (InstallTextureInContext(ID) == TEXTUREBOX_ERROR)
837 /*oui, alors on bind directement */
841 printf("SetCurrentTexture: utilisation du bind %d\n", ID);
843 MyBindTextureEXT(ID, context);
847 current_texture = ID;
848 current_texture_data = textab[ID].data;
851 /*----------------------------------------------------------------------*/
853 void FreeTexture(TextureID ID)
857 #if defined(GL_EXT_texture_object)
858 GLCONTEXT cur_context;
859 GLDRAWABLE cur_drawable;
861 #endif /* GL_EXT_texture_object */
863 if (!IsTextureValid(ID)) return;
865 data = textab[ID].data;
866 texdata[data].share_count--;
867 if (texdata[data].share_count == 0)
869 /* liberation des datas de la textures */
870 free(texdata[data].image);
872 #if defined(GL_EXT_texture_object)
875 /* liberation de la texture dans tous les contextes */
876 cur_drawable = GET_GLDEV_CONTEXT();
877 for (i=0; i<textab[ID].context_count; i++)
880 printf("FreeTexture::liberation de %d\n", ID);
883 if (textab[ID].use_bind_texture[i])
885 GL_MAKE_CURRENT(GetCurrentDisplay(),
886 textab[ID].drawable[i],
887 textab[ID].context[i]);
889 /*This check has been added to avoid exception,
890 which is raised when trying to delete textures when no rendering context
892 cur_context = GET_GL_CONTEXT();
894 glDeleteTextures(1, &textab[ID].number[i]);
900 GL_MAKE_CURRENT(GetCurrentDisplay(),cur_drawable,cur_context);
902 texdata[data].status = TEXDATA_NONE;
904 textures_data_count--;
906 if( data+1 == textures_data_count ) textures_data_count--;
908 free(textab[ID].context);
909 free(textab[ID].drawable);
910 free(textab[ID].use_bind_texture);
911 free(textab[ID].number);
913 } /* use_bind_texture */
914 #endif /* GL_EXT_texture_object */
917 textab[ID].status = TEX_NONE;
921 if( ID+1 == textures_count ) textures_count--;
924 current_texture_data = TEXTUREDATA_ERROR;
927 /*----------------------------------------------------------------------*/
929 void EnableTexture(void)
931 if (!IsTextureValid(current_texture)) return;
933 switch (texdata[current_texture_data].status)
936 if (textab[current_texture].Gen != GL_NONE)
937 glEnable(GL_TEXTURE_GEN_S);
938 glEnable(GL_TEXTURE_1D);
943 if (textab[current_texture].Gen != GL_NONE)
945 glEnable(GL_TEXTURE_GEN_S);
946 glEnable(GL_TEXTURE_GEN_T);
948 glEnable(GL_TEXTURE_2D);
955 /*----------------------------------------------------------------------*/
957 void DisableTexture(void)
959 if ( !IsTextureEnabled() )
961 if ( !IsTextureValid( current_texture ) )
964 switch (texdata[current_texture_data].status)
967 if (textab[current_texture].Gen != GL_NONE)
968 glDisable(GL_TEXTURE_GEN_S);
969 glDisable(GL_TEXTURE_1D);
974 if (textab[current_texture].Gen != GL_NONE)
976 glDisable(GL_TEXTURE_GEN_S);
977 glDisable(GL_TEXTURE_GEN_T);
979 glDisable(GL_TEXTURE_2D);
986 /*----------------------------------------------------------------------*/
988 GLboolean IsTextureEnabled(void)
990 GLboolean isEnabled1D= GL_FALSE, isEnabled2D= GL_FALSE;
991 glGetBooleanv( GL_TEXTURE_1D, &isEnabled1D );
992 glGetBooleanv( GL_TEXTURE_2D, &isEnabled2D );
993 return isEnabled1D || isEnabled2D;
996 /*----------------------------------------------------------------------*/
998 void SetTextureModulate(TextureID ID)
1000 if (!IsTextureValid(ID)) return;
1002 textab[ID].Light = GL_MODULATE;
1005 /*----------------------------------------------------------------------*/
1007 void SetTextureDecal(TextureID ID)
1009 if (!IsTextureValid(ID)) return;
1011 textab[ID].Light = GL_DECAL;
1014 /*----------------------------------------------------------------------*/
1016 void SetTextureClamp(TextureID ID)
1018 if (!IsTextureValid(ID)) return;
1020 textab[ID].Wrap = GL_CLAMP;
1023 /*----------------------------------------------------------------------*/
1025 void SetTextureRepeat(TextureID ID)
1027 if (!IsTextureValid(ID)) return;
1029 textab[ID].Wrap = GL_REPEAT;
1032 /*----------------------------------------------------------------------*/
1034 /* gestion de la facon d'appliquer la texture */
1035 void SetModeObject(TextureID ID, GLfloat sparams[4], GLfloat tparams[4])
1037 if (!IsTextureValid(ID)) return;
1039 textab[ID].Gen = GL_OBJECT_LINEAR;
1040 if (sparams != NULL) memcpy(textab[ID].Plane1, sparams, sizeof(sgenparams));
1041 else memcpy(textab[ID].Plane1, sgenparams, sizeof(sgenparams));
1043 if (texdata[textab[ID].data].status != TEXDATA_1D) {
1044 if (tparams != NULL) memcpy(textab[ID].Plane2, tparams, sizeof(tgenparams));
1045 else memcpy(textab[ID].Plane2, tgenparams, sizeof(tgenparams));
1049 /*----------------------------------------------------------------------*/
1051 void SetModeSphere(TextureID ID)
1053 if (!IsTextureValid(ID)) return;
1055 textab[ID].Gen = GL_SPHERE_MAP;
1058 /*----------------------------------------------------------------------*/
1060 void SetModeEye(TextureID ID, GLfloat sparams[4], GLfloat tparams[4])
1062 if (!IsTextureValid(ID)) return;
1064 textab[ID].Gen = GL_EYE_LINEAR;
1065 if (sparams != NULL) memcpy(textab[ID].Plane1, sparams, sizeof(sgenparams));
1066 else memcpy(textab[ID].Plane1, sgenparams, sizeof(sgenparams));
1068 if (texdata[textab[ID].data].status != TEXDATA_1D) {
1069 if (tparams != NULL) memcpy(textab[ID].Plane2, tparams, sizeof(tgenparams));
1070 else memcpy(textab[ID].Plane2, tgenparams, sizeof(tgenparams));
1074 /*----------------------------------------------------------------------*/
1076 void SetModeManual(TextureID ID)
1078 if (!IsTextureValid(ID)) return;
1080 textab[ID].Gen = GL_NONE;
1083 /*----------------------------------------------------------------------*/
1085 void SetRenderNearest(TextureID ID)
1087 if (!IsTextureValid(ID)) return;
1089 textab[ID].Render = GL_NEAREST;
1092 /*----------------------------------------------------------------------*/
1094 void SetRenderLinear(TextureID ID)
1096 if (!IsTextureValid(ID)) return;
1098 textab[ID].Render = GL_LINEAR;
1101 /*----------------------------------------------------------------------*/
1103 void SetTexturePosition(TextureID ID,
1104 GLfloat scalex, GLfloat scaley,
1105 GLfloat transx, GLfloat transy,
1108 textab[ID].scalex = scalex;
1109 textab[ID].scaley = scaley;
1110 textab[ID].transx = transx;
1111 textab[ID].transy = transy;
1112 textab[ID].angle = angle;
1115 /*----------------------------------------------------------------------*/
1117 void SetTextureDefaultParams(TextureID ID)
1119 if (!IsTextureValid(ID)) return;
1122 printf("SetTextureDefaultParams::set parm par defaut textures\n");
1126 textab[ID].scalex = 1.0;
1127 textab[ID].scaley = 1.0;
1128 textab[ID].transx = 0.0;
1129 textab[ID].transy = 0.0;
1130 textab[ID].angle = 0.0;
1132 textab[ID].Gen = GL_OBJECT_LINEAR;
1133 textab[ID].Light = texdata[textab[ID].data].status == TEXDATA_1D ? GL_DECAL : GL_MODULATE;
1134 textab[ID].Wrap = texdata[textab[ID].data].status == TEXDATA_1D ? GL_CLAMP : GL_REPEAT;
1135 memcpy(textab[ID].Plane1, sgenparams, sizeof(sgenparams));
1136 memcpy(textab[ID].Plane2, tgenparams, sizeof(tgenparams));
1137 textab[ID].Render = texdata[textab[ID].data].status == TEXDATA_1D ? GL_NEAREST : GL_LINEAR;
1140 /*----------------------------------------------------------------------*/
1141 /* Transfere de donnee des donnees internes a la structure TransferData */
1142 void TransferTexture_To_Data(TextureID ID, TextureData *TransfDt)
1145 strcpy(TransfDt->path, texdata[textab[ID].data].imageFileName);
1146 TransfDt->gen = textab[ID].Gen;
1147 TransfDt->wrap = textab[ID].Wrap;
1148 TransfDt->render = textab[ID].Light;
1149 TransfDt->scalex = textab[ID].scalex;
1150 TransfDt->scaley = textab[ID].scaley;
1151 TransfDt->transx = textab[ID].transx;
1152 TransfDt->transy = textab[ID].transy;
1153 TransfDt->angle = textab[ID].angle;
1154 memcpy(TransfDt->plane1, textab[ID].Plane1, sizeof(SizeType));
1155 memcpy(TransfDt->plane2, textab[ID].Plane2, sizeof(SizeType));
1158 /*----------------------------------------------------------------------*/
1159 /* Transfere de donnee de la structure TransferData aux donnees internes */
1160 void TransferData_To_Texture(TextureData *TransfDt, TextureID *newID)
1165 FreeTexture(*newID);
1166 ID = GetTexture2DMipMap(TransfDt->path);
1168 if(IsTextureValid(ID))
1170 /* Affectation de l id courant */
1173 /* Donnees concernant les caracteristiques de la texture */
1174 strcpy(texdata[textab[ID].data].imageFileName, TransfDt->path);
1175 textab[ID].Gen = TransfDt->gen;
1176 textab[ID].Wrap = TransfDt->wrap;
1177 textab[ID].Light = TransfDt->render;
1178 textab[ID].scalex = TransfDt->scalex;
1179 textab[ID].scaley = TransfDt->scaley;
1180 textab[ID].transx = TransfDt->transx;
1181 textab[ID].transy = TransfDt->transy;
1182 textab[ID].angle = TransfDt->angle;
1183 memcpy(textab[ID].Plane1, TransfDt->plane1, sizeof(SizeType));
1184 memcpy(textab[ID].Plane2, TransfDt->plane2, sizeof(SizeType));