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>
84 #include <OpenGl_ResourceCleaner.hxx>
85 #include <OpenGl_ResourceTexture.hxx>
88 /*----------------------------------------------------------------------*/
96 #define GROW_TEXTURES 8
97 #define GROW_TEXTURES_DATA 8
98 #define GROW_CONTEXT 8
101 #define glGenTextures glGenTexturesEXT
102 #define glDeleteTextures glDeleteTexturesEXT
103 #define glBindTexture glBindTextureEXT
107 #define max(a,b) ((a) > (b)) ? (a) : (b);
110 /*----------------------------------------------------------------------*/
115 typedef enum {TEXDATA_NONE, TEXDATA_1D, TEXDATA_2D, TEXDATA_2DMM} texDataStatus;
116 typedef enum {TEX_NONE, TEX_ALLOCATED} texStatus;
118 typedef GLfloat SizeType[4];
120 typedef int TextureDataID;
121 #define TEXTUREDATA_ERROR -1
125 char imageFileName[128];
126 int imageWidth, imageHeight;
128 texDataStatus status;
131 IMPLEMENT_MEMORY_OPERATORS
139 GLDRAWABLE *drawable;
141 char *use_bind_texture;
152 GLfloat scalex, scaley;
153 GLfloat transx, transy;
156 IMPLEMENT_MEMORY_OPERATORS
160 /*----------------------------------------------------------------------*/
162 * Variables statiques
165 static texDraw *textab = NULL;
166 static int textures_count = 0;
167 static int textures_size = 0;
169 static texData *texdata = NULL;
170 static int textures_data_count = 0;
171 static int textures_data_size = 0;
173 static TextureDataID current_texture_data = TEXTUREDATA_ERROR;
174 static TextureID current_texture = TEXTUREBOX_ERROR;
176 static GLfloat sgenparams[] = { 1.0 ,0.0 ,0.0 ,0.0};
177 static GLfloat tgenparams[] = { 0.0 ,1.0 ,0.0 ,0.0};
179 static GLenum status2type[] = { GL_NONE, GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_2D };
181 static GLboolean init_extension = GL_FALSE;
182 static GLboolean use_bind_texture = GL_FALSE;
184 /*----------------------------------------------------------------------*/
189 /*----------------------------------------------------------------------*/
191 * recherche l'existence de datas de texture par son nom
193 static TextureDataID FindTextureData(char *FileName)
197 for (i=0; i<textures_data_size; i++)
198 if ( texdata[i].status!=TEXDATA_NONE && strcmp(FileName, texdata[i].imageFileName)==0 )
201 return TEXTUREDATA_ERROR;
204 /*----------------------------------------------------------------------*/
206 * recherche un emplacement de data texture libre
208 static TextureDataID FindFreeTextureData(void)
212 /* ya encore de la place ? */
213 if (textures_data_count == textures_data_size)
215 textures_data_size += GROW_TEXTURES_DATA;
216 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
217 texdata = (texData*)realloc(texdata, textures_data_size*sizeof(texData));
219 texdata = cmn_resizemem<texData>(texdata, textures_data_size);
221 if (texdata == NULL) return TEXTUREDATA_ERROR;
223 for (i=textures_data_count; i<textures_data_size; i++)
224 texdata[i].status = TEXDATA_NONE;
226 /* on a deja assez perdu de temps comme ca => retourne un ID rapidement... */
227 return textures_data_count++;
230 /* recherche d'un ID libre */
231 for (i=0; i<textures_data_size; i++)
232 if (texdata[i].status == TEXDATA_NONE)
235 textures_data_count++;
237 textures_data_count = max(textures_data_count,i+1);
242 return TEXTUREDATA_ERROR;
245 /*----------------------------------------------------------------------*/
247 * recherche un emplacement de texture libre
249 static TextureID FindFreeTexture(void)
253 /* ya encore de la place ? */
254 if (textures_count == textures_size)
256 textures_size += GROW_TEXTURES;
257 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
258 textab = (texDraw*)realloc(textab, textures_size*sizeof(texDraw));
260 textab = cmn_resizemem<texDraw>(textab, textures_size);
262 if (textab == NULL) return TEXTUREBOX_ERROR;
264 for (i=textures_count; i<textures_size; i++)
265 textab[i].status = TEX_NONE;
267 /* on a deja assez perdu de temps comme ca => retourne un ID rapidement... */
268 return textures_count++;
271 for (i=0; i<textures_size; i++)
272 if (textab[i].status == TEX_NONE)
277 textures_count = max(textures_count,i+1);
282 return TEXTUREBOX_ERROR;
285 /*----------------------------------------------------------------------*/
287 * regarde si la texture a ete definie pour le contexte courant
289 static int FindTextureContext(TextureID ID)
293 GLCONTEXT cur = GET_GL_CONTEXT();
294 for (i=0; i<textab[ID].context_count; i++)
295 if (textab[ID].context[i] == cur)
298 return TEXTUREBOX_ERROR;
301 /*----------------------------------------------------------------------*/
303 * chargement d'une texture suivant son type
305 static void LoadTexture(TextureID ID)
309 data = textab[ID].data;
310 switch (texdata[data].status)
313 glTexImage1D(GL_TEXTURE_1D, 0, 4,
314 texdata[data].imageWidth, 0,
315 GL_RGBA, GL_UNSIGNED_BYTE, texdata[data].image);
319 glTexImage2D(GL_TEXTURE_2D, 0, 4,
320 texdata[data].imageWidth, texdata[data].imageHeight, 0,
321 GL_RGBA, GL_UNSIGNED_BYTE, texdata[data].image);
325 gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
326 texdata[data].imageWidth,
327 texdata[data].imageHeight,
328 GL_RGBA, GL_UNSIGNED_BYTE, texdata[data].image);
335 /*----------------------------------------------------------------------*/
337 * les parametres d'initialisation d'une texture
338 * NE PAS METTRE DANS UNE DISPLAY LIST POUR L'INSTANT ( pb avec les matrices )
340 static void SetTextureParam(TextureID ID)
346 data = textab[ID].data;
347 glGetIntegerv(GL_MATRIX_MODE, &cur_matrix);
350 * MISE EN PLACE DE LA MATRICE DE TEXTURE
352 glMatrixMode(GL_TEXTURE);
354 /* if (textab[ID].Gen != GL_SPHERE_MAP)
356 glScalef(textab[ID].scalex, textab[ID].scaley, 1.0);
357 glTranslatef(-textab[ID].transx, -textab[ID].transy, 0.0);
358 glRotatef(-textab[ID].angle, 0.0, 0.0, 1.0);
363 * GENERATION AUTOMATIQUE DE TEXTURE
365 switch (textab[ID].Gen)
367 case GL_OBJECT_LINEAR:
368 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
369 glTexGenfv(GL_S, GL_OBJECT_PLANE, textab[ID].Plane1);
370 if (texdata[data].status != TEXDATA_1D)
372 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
373 glTexGenfv(GL_T, GL_OBJECT_PLANE, textab[ID].Plane2);
378 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
379 if (texdata[data].status != TEXDATA_1D)
380 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
384 glMatrixMode(GL_MODELVIEW);
388 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
389 glTexGenfv(GL_S, GL_EYE_PLANE, textab[ID].Plane1);
391 if (texdata[data].status != TEXDATA_1D)
393 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
394 glTexGenfv(GL_T, GL_EYE_PLANE, textab[ID].Plane2);
403 * RENDU DE LA TEXTURE AVEC LES LUMIERES
405 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textab[ID].Light);
409 * LISSAGE DE LA TEXTURE
411 switch (texdata[data].status)
415 glTexParameteri(texdata[data].type, GL_TEXTURE_MAG_FILTER, textab[ID].Render);
416 glTexParameteri(texdata[data].type, GL_TEXTURE_MIN_FILTER, textab[ID].Render);
420 if (textab[ID].Render == GL_NEAREST)
422 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
423 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
427 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
428 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
439 switch (texdata[data].status)
442 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, textab[ID].Wrap);
447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textab[ID].Wrap);
448 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textab[ID].Wrap);
454 glMatrixMode(cur_matrix);
457 /*----------------------------------------------------------------------*/
459 * simulation du glGenTexturesEXT pour un context
461 static void MyGenTextureEXT(TextureID ID)
463 #if defined(GL_EXT_texture_object)
468 Context = textab[ID].context_count;
469 data = textab[ID].data;
473 use_bind_texture = QueryExtension("GL_EXT_texture_object");
475 init_extension = GL_TRUE;
477 printf("GL_EXT_texture_object %d\n", use_bind_texture);
481 if (use_bind_texture)
484 printf("MyGenTextureEXT::gen texture\n");
487 textab[ID].context[Context] = GET_GL_CONTEXT();
488 textab[ID].drawable[Context] = GET_GLDEV_CONTEXT();
489 textab[ID].use_bind_texture[Context] = (char)use_bind_texture;
490 glGenTextures(1, &textab[ID].number[Context]);
491 glBindTexture(texdata[data].type, textab[ID].number[Context]);
493 textab[ID].context_count++;
496 #endif /* GL_EXT_texture_object */
497 if (current_texture_data != textab[ID].data)
501 /*----------------------------------------------------------------------*/
503 * simulation du glBindTextureEXT
505 static void MyBindTextureEXT(TextureID ID, int Context)
507 /* si l'extension est presente, c'est facile */
508 #if defined(GL_EXT_texture_object)
510 data = textab[ID].data;
512 if (textab[ID].status == TEXDATA_NONE)
516 printf("MyBindTextureEXT::ID %d data %d context %d Current %d\n", ID, textab[ID].data, Context, current_texture_data);
519 if (textab[ID].use_bind_texture[Context])
521 /* OCC11904 - make sure that the correct texture is bound before drawing */
522 GLenum aParamName = textab[ID].status == TEXDATA_1D ?
523 GL_TEXTURE_BINDING_1D : GL_TEXTURE_BINDING_2D;
525 glGetIntegerv( aParamName, &aCurrTex );
526 if ( textab[ID].number[Context] != aCurrTex )
529 printf("MyBindTextureEXT::bind texture\n");
531 glBindTexture( texdata[data].type, textab[ID].number[Context] );
534 /* sinon on reset tout */
536 #endif /* GL_EXT_texture_object */
538 /* OCC11904 - make sure that the correct texture is bound before drawing */
539 GLenum aParamName = textab[ID].status == TEXDATA_1D ?
540 GL_TEXTURE_BINDING_1D : GL_TEXTURE_BINDING_2D;
542 glGetIntegerv( aParamName, &aCurrTex );
543 if ( textab[ID].number[Context] != aCurrTex )
546 printf("MyBindTextureEXT::chargement sans bind de la texture\n");
553 /*----------------------------------------------------------------------*/
555 * installation de la texture pour le dernier contexte
557 static int InstallTextureInContext(TextureID ID)
560 printf("InstallTextureInContext::installation de la texture dans le context\n");
564 /* ya encore de la place dans le tableau de context ? */
565 if (textab[ID].context_count == textab[ID].context_size)
568 printf("InstallTextureInContext::allocation dans le context\n");
570 textab[ID].context_size += GROW_CONTEXT;
571 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
573 (GLuint*)realloc(textab[ID].number, textab[ID].context_size*sizeof(GLuint));
575 (GLCONTEXT*)realloc(textab[ID].context, textab[ID].context_size*sizeof(GLCONTEXT));
576 textab[ID].drawable =
577 (GLDRAWABLE*)realloc(textab[ID].drawable, textab[ID].context_size*sizeof(GLDRAWABLE));
578 textab[ID].use_bind_texture =
579 (char*)realloc(textab[ID].use_bind_texture, textab[ID].context_size);
582 cmn_resizemem<GLuint>(textab[ID].number, textab[ID].context_size);
584 cmn_resizemem<GLCONTEXT>(textab[ID].context, textab[ID].context_size);
585 textab[ID].drawable =
586 cmn_resizemem<GLDRAWABLE>(textab[ID].drawable, textab[ID].context_size);
587 textab[ID].use_bind_texture =
588 cmn_resizemem<char>(textab[ID].use_bind_texture, textab[ID].context_size);
590 if ( (textab[ID].number == NULL) ||
591 (textab[ID].context == NULL) ||
592 (textab[ID].drawable == NULL) ||
593 (textab[ID].use_bind_texture == NULL) )
595 /* erreur => libere tout */
596 free(textab[ID].number);
597 free(textab[ID].context);
598 free(textab[ID].drawable);
599 free(textab[ID].use_bind_texture);
600 textab[ID].context_size = 0;
602 return TEXTUREBOX_ERROR;
609 printf("InstallTextureInContext::context ok\n");
614 /*----------------------------------------------------------------------*/
615 static TextureID GetTexture(char *FileName, texDataStatus status)
621 /* essait de trouver la texture */
622 i = FindTextureData(FileName);
623 if (i == TEXTUREDATA_ERROR)
626 printf("GetTexture::la texture %s n'existe pas => chargement\n", FileName);
628 /* creation d'une texture */
629 i = FindFreeTextureData();
630 if (i == TEXTUREDATA_ERROR) return TEXTUREBOX_ERROR;
632 texdata[i].share_count = 0;
633 strcpy(texdata[i].imageFileName, FileName);
634 texdata[i].image = (GLubyte *)read_texture(FileName,
635 &texdata[i].imageWidth,
636 &texdata[i].imageHeight,
638 if (texdata[i].image == NULL) return TEXTUREBOX_ERROR;
640 texdata[i].status = status;
641 texdata[i].type = status2type[status];
644 j = FindFreeTexture();
645 if (j != TEXTUREBOX_ERROR)
648 printf("GetTexture::installation texture pour obj %d\n", j);
650 textab[j].context_count = 0;
651 textab[j].context_size = 0;
652 textab[j].number = NULL;
653 textab[j].drawable = NULL;
654 textab[j].context = NULL;
655 textab[j].use_bind_texture = NULL;
657 textab[j].status = TEX_ALLOCATED;
658 texdata[i].share_count++;
660 SetTextureDefaultParams(j);
663 printf("GetTexture::texture %s(%d) texture %d count=%d\n", texdata[i].imageFileName, i, j, texdata[i].share_count);
667 if (texdata[i].share_count != 0)
668 free(texdata[i].image);
674 /*----------------------------------------------------------------------*/
675 static TextureID GetTextureData(char *FileName, texDataStatus status, const GLint width, const GLint height, const void *data)
680 /* essait de trouver la texture */
681 i = FindTextureData(FileName);
682 if (i == TEXTUREDATA_ERROR)
685 printf("GetTextureData::la texture %s n'existe pas => chargement\n", FileName);
687 /* creation d'une texture */
688 i = FindFreeTextureData();
689 if (i == TEXTUREDATA_ERROR) return TEXTUREBOX_ERROR;
691 texdata[i].share_count = 0;
692 strcpy(texdata[i].imageFileName, FileName);
693 texdata[i].image = (GLubyte *)malloc(width*height*4*sizeof(GLubyte));
694 memcpy(texdata[i].image, data, (width*height*4));
695 texdata[i].imageWidth = width;
696 texdata[i].imageHeight = height;
698 if (texdata[i].image == NULL) return TEXTUREBOX_ERROR;
700 texdata[i].status = status;
701 texdata[i].type = status2type[status];
704 j = FindFreeTexture();
705 if (j != TEXTUREBOX_ERROR)
708 printf("GetTextureData::installation texture pour obj %d\n", j);
710 textab[j].context_count = 0;
711 textab[j].context_size = 0;
712 textab[j].number = NULL;
713 textab[j].drawable = NULL;
714 textab[j].context = NULL;
715 textab[j].use_bind_texture = NULL;
717 textab[j].status = TEX_ALLOCATED;
718 texdata[i].share_count++;
720 SetTextureDefaultParams(j);
723 printf("GetTextureData::texture %s(%d) texture %d count=%d\n", texdata[i].imageFileName, i, j, texdata[i].share_count);
727 if (texdata[i].share_count != 0)
728 free(texdata[i].image);
733 /*----------------------------------------------------------------------*/
735 * Fonctions publiques
739 /*----------------------------------------------------------------------*/
741 GLboolean IsTextureValid(TextureID ID)
743 if ((ID<0) | (ID>=textures_count))
747 return textab[ID].status == TEX_ALLOCATED;
752 /*----------------------------------------------------------------------*/
754 TextureID GetTexture1D(char *FileName)
757 printf("GetTexture1D::loading 1d %s \n", FileName);
759 return GetTexture(FileName, TEXDATA_1D);
762 /*----------------------------------------------------------------------*/
764 TextureID GetTexture2D(char *FileName)
767 printf("GetTexture2D::loading 2d %s \n", FileName);
769 return GetTexture(FileName, TEXDATA_2D);
772 /*----------------------------------------------------------------------*/
774 TextureID GetTexture2DMipMap(char *FileName)
777 printf("GetTexture2DMipMap::loading 2dmm %s \n", FileName);
779 return GetTexture(FileName, TEXDATA_2DMM);
782 /*----------------------------------------------------------------------*/
784 TextureID GetTextureData1D(char *FileName, const GLint width, const GLint height, const void *data)
787 printf("GetTextureData1D::loading 1d %s \n", FileName);
789 return GetTextureData(FileName, TEXDATA_1D, width, height, data);
792 /*----------------------------------------------------------------------*/
794 TextureID GetTextureData2D(char *FileName, const GLint width, const GLint height, const void *data)
797 printf("GetTextureData2D::loading 2d %s \n", FileName);
799 return GetTextureData(FileName, TEXDATA_2D, width, height, data);
802 /*----------------------------------------------------------------------*/
804 TextureID GetTextureData2DMipMap(char *FileName, const GLint width, const GLint height, const void *data)
807 printf("GetTextureData2DMipMap::loading 2dmm %s \n", FileName);
809 return GetTextureData(FileName, TEXDATA_2DMM, width, height, data);
813 /*----------------------------------------------------------------------*/
815 void SetCurrentTexture(TextureID ID)
819 if (!IsTextureValid(ID)) return;
821 context = FindTextureContext(ID);
823 /* la texture n'existe pas dans ce contexte */
824 if (context == TEXTUREBOX_ERROR)
827 printf("SetCurrentTexture::installation texture %d dans context\n", ID);
829 /* si on a une erreur pendant l'installation dans le context
830 * alors on installe la texture sans bind */
831 if (InstallTextureInContext(ID) == TEXTUREBOX_ERROR)
838 /*oui, alors on bind directement */
842 printf("SetCurrentTexture: utilisation du bind %d\n", ID);
844 MyBindTextureEXT(ID, context);
848 current_texture = ID;
849 current_texture_data = textab[ID].data;
852 /*----------------------------------------------------------------------*/
854 void FreeTexture(TextureID ID)
857 bool notResource = false; // if there old-style texture deletion
859 #if defined(GL_EXT_texture_object)
860 GLCONTEXT cur_context;
861 GLDRAWABLE cur_drawable;
863 #endif /* GL_EXT_texture_object */
865 if (!IsTextureValid(ID)) return;
867 data = textab[ID].data;
868 texdata[data].share_count--;
869 if (texdata[data].share_count == 0)
871 /* liberation des datas de la textures */
872 free(texdata[data].image);
874 #if defined(GL_EXT_texture_object)
877 /* liberation de la texture dans tous les contextes */
878 cur_drawable = GET_GLDEV_CONTEXT();
879 for (i=0; i<textab[ID].context_count; i++)
882 printf("FreeTexture::liberation de %d\n", ID);
885 bool isResource = false;
887 if (textab[ID].use_bind_texture[i])
889 if( !OpenGl_ResourceCleaner::GetInstance()->AddResource(textab[ID].context[i],
890 new OpenGl_ResourceTexture(textab[ID].number[i])) )
892 GL_MAKE_CURRENT(GetCurrentDisplay(),
893 textab[ID].drawable[i],
894 textab[ID].context[i]);
896 /*This check has been added to avoid exception,
897 which is raised when trying to delete textures when no rendering context
899 cur_context = GET_GL_CONTEXT();
901 glDeleteTextures(1, &textab[ID].number[i]);
908 if( !isResource && cur_context )
913 GL_MAKE_CURRENT(GetCurrentDisplay(),cur_drawable,cur_context);
915 texdata[data].status = TEXDATA_NONE;
917 textures_data_count--;
919 if( data+1 == textures_data_count ) textures_data_count--;
921 free(textab[ID].context);
922 free(textab[ID].drawable);
923 free(textab[ID].use_bind_texture);
924 free(textab[ID].number);
926 } /* use_bind_texture */
927 #endif /* GL_EXT_texture_object */
930 textab[ID].status = TEX_NONE;
934 if( ID+1 == textures_count ) textures_count--;
937 current_texture_data = TEXTUREDATA_ERROR;
940 /*----------------------------------------------------------------------*/
942 void EnableTexture(void)
944 if (!IsTextureValid(current_texture)) return;
946 switch (texdata[current_texture_data].status)
949 if (textab[current_texture].Gen != GL_NONE)
950 glEnable(GL_TEXTURE_GEN_S);
951 glEnable(GL_TEXTURE_1D);
956 if (textab[current_texture].Gen != GL_NONE)
958 glEnable(GL_TEXTURE_GEN_S);
959 glEnable(GL_TEXTURE_GEN_T);
961 glEnable(GL_TEXTURE_2D);
968 /*----------------------------------------------------------------------*/
970 void DisableTexture(void)
972 if ( !IsTextureEnabled() )
974 if ( !IsTextureValid( current_texture ) )
977 switch (texdata[current_texture_data].status)
980 if (textab[current_texture].Gen != GL_NONE)
981 glDisable(GL_TEXTURE_GEN_S);
982 glDisable(GL_TEXTURE_1D);
987 if (textab[current_texture].Gen != GL_NONE)
989 glDisable(GL_TEXTURE_GEN_S);
990 glDisable(GL_TEXTURE_GEN_T);
992 glDisable(GL_TEXTURE_2D);
999 /*----------------------------------------------------------------------*/
1001 GLboolean IsTextureEnabled(void)
1003 GLboolean isEnabled1D= GL_FALSE, isEnabled2D= GL_FALSE;
1004 glGetBooleanv( GL_TEXTURE_1D, &isEnabled1D );
1005 glGetBooleanv( GL_TEXTURE_2D, &isEnabled2D );
1006 return isEnabled1D || isEnabled2D;
1009 /*----------------------------------------------------------------------*/
1011 void SetTextureModulate(TextureID ID)
1013 if (!IsTextureValid(ID)) return;
1015 textab[ID].Light = GL_MODULATE;
1018 /*----------------------------------------------------------------------*/
1020 void SetTextureDecal(TextureID ID)
1022 if (!IsTextureValid(ID)) return;
1024 textab[ID].Light = GL_DECAL;
1027 /*----------------------------------------------------------------------*/
1029 void SetTextureClamp(TextureID ID)
1031 if (!IsTextureValid(ID)) return;
1033 textab[ID].Wrap = GL_CLAMP;
1036 /*----------------------------------------------------------------------*/
1038 void SetTextureRepeat(TextureID ID)
1040 if (!IsTextureValid(ID)) return;
1042 textab[ID].Wrap = GL_REPEAT;
1045 /*----------------------------------------------------------------------*/
1047 /* gestion de la facon d'appliquer la texture */
1048 void SetModeObject(TextureID ID, GLfloat sparams[4], GLfloat tparams[4])
1050 if (!IsTextureValid(ID)) return;
1052 textab[ID].Gen = GL_OBJECT_LINEAR;
1053 if (sparams != NULL) memcpy(textab[ID].Plane1, sparams, sizeof(sgenparams));
1054 else memcpy(textab[ID].Plane1, sgenparams, sizeof(sgenparams));
1056 if (texdata[textab[ID].data].status != TEXDATA_1D) {
1057 if (tparams != NULL) memcpy(textab[ID].Plane2, tparams, sizeof(tgenparams));
1058 else memcpy(textab[ID].Plane2, tgenparams, sizeof(tgenparams));
1062 /*----------------------------------------------------------------------*/
1064 void SetModeSphere(TextureID ID)
1066 if (!IsTextureValid(ID)) return;
1068 textab[ID].Gen = GL_SPHERE_MAP;
1071 /*----------------------------------------------------------------------*/
1073 void SetModeEye(TextureID ID, GLfloat sparams[4], GLfloat tparams[4])
1075 if (!IsTextureValid(ID)) return;
1077 textab[ID].Gen = GL_EYE_LINEAR;
1078 if (sparams != NULL) memcpy(textab[ID].Plane1, sparams, sizeof(sgenparams));
1079 else memcpy(textab[ID].Plane1, sgenparams, sizeof(sgenparams));
1081 if (texdata[textab[ID].data].status != TEXDATA_1D) {
1082 if (tparams != NULL) memcpy(textab[ID].Plane2, tparams, sizeof(tgenparams));
1083 else memcpy(textab[ID].Plane2, tgenparams, sizeof(tgenparams));
1087 /*----------------------------------------------------------------------*/
1089 void SetModeManual(TextureID ID)
1091 if (!IsTextureValid(ID)) return;
1093 textab[ID].Gen = GL_NONE;
1096 /*----------------------------------------------------------------------*/
1098 void SetRenderNearest(TextureID ID)
1100 if (!IsTextureValid(ID)) return;
1102 textab[ID].Render = GL_NEAREST;
1105 /*----------------------------------------------------------------------*/
1107 void SetRenderLinear(TextureID ID)
1109 if (!IsTextureValid(ID)) return;
1111 textab[ID].Render = GL_LINEAR;
1114 /*----------------------------------------------------------------------*/
1116 void SetTexturePosition(TextureID ID,
1117 GLfloat scalex, GLfloat scaley,
1118 GLfloat transx, GLfloat transy,
1121 textab[ID].scalex = scalex;
1122 textab[ID].scaley = scaley;
1123 textab[ID].transx = transx;
1124 textab[ID].transy = transy;
1125 textab[ID].angle = angle;
1128 /*----------------------------------------------------------------------*/
1130 void SetTextureDefaultParams(TextureID ID)
1132 if (!IsTextureValid(ID)) return;
1135 printf("SetTextureDefaultParams::set parm par defaut textures\n");
1139 textab[ID].scalex = 1.0;
1140 textab[ID].scaley = 1.0;
1141 textab[ID].transx = 0.0;
1142 textab[ID].transy = 0.0;
1143 textab[ID].angle = 0.0;
1145 textab[ID].Gen = GL_OBJECT_LINEAR;
1146 textab[ID].Light = texdata[textab[ID].data].status == TEXDATA_1D ? GL_DECAL : GL_MODULATE;
1147 textab[ID].Wrap = texdata[textab[ID].data].status == TEXDATA_1D ? GL_CLAMP : GL_REPEAT;
1148 memcpy(textab[ID].Plane1, sgenparams, sizeof(sgenparams));
1149 memcpy(textab[ID].Plane2, tgenparams, sizeof(tgenparams));
1150 textab[ID].Render = texdata[textab[ID].data].status == TEXDATA_1D ? GL_NEAREST : GL_LINEAR;
1153 /*----------------------------------------------------------------------*/
1154 /* Transfere de donnee des donnees internes a la structure TransferData */
1155 void TransferTexture_To_Data(TextureID ID, TextureData *TransfDt)
1158 strcpy(TransfDt->path, texdata[textab[ID].data].imageFileName);
1159 TransfDt->gen = textab[ID].Gen;
1160 TransfDt->wrap = textab[ID].Wrap;
1161 TransfDt->render = textab[ID].Light;
1162 TransfDt->scalex = textab[ID].scalex;
1163 TransfDt->scaley = textab[ID].scaley;
1164 TransfDt->transx = textab[ID].transx;
1165 TransfDt->transy = textab[ID].transy;
1166 TransfDt->angle = textab[ID].angle;
1167 memcpy(TransfDt->plane1, textab[ID].Plane1, sizeof(SizeType));
1168 memcpy(TransfDt->plane2, textab[ID].Plane2, sizeof(SizeType));
1171 /*----------------------------------------------------------------------*/
1172 /* Transfere de donnee de la structure TransferData aux donnees internes */
1173 void TransferData_To_Texture(TextureData *TransfDt, TextureID *newID)
1178 FreeTexture(*newID);
1179 ID = GetTexture2DMipMap(TransfDt->path);
1181 if(IsTextureValid(ID))
1183 /* Affectation de l id courant */
1186 /* Donnees concernant les caracteristiques de la texture */
1187 strcpy(texdata[textab[ID].data].imageFileName, TransfDt->path);
1188 textab[ID].Gen = TransfDt->gen;
1189 textab[ID].Wrap = TransfDt->wrap;
1190 textab[ID].Light = TransfDt->render;
1191 textab[ID].scalex = TransfDt->scalex;
1192 textab[ID].scaley = TransfDt->scaley;
1193 textab[ID].transx = TransfDt->transx;
1194 textab[ID].transy = TransfDt->transy;
1195 textab[ID].angle = TransfDt->angle;
1196 memcpy(textab[ID].Plane1, TransfDt->plane1, sizeof(SizeType));
1197 memcpy(textab[ID].Plane2, TransfDt->plane2, sizeof(SizeType));