0023312: Suspicious for loop in BiTgte_Blend.cxx
[occt.git] / src / OpenGl / OpenGl_TextureBox.cxx
CommitLineData
b311480e 1// Copyright (c) 1999-2012 OPEN CASCADE SAS
2//
3// The content of this file is subject to the Open CASCADE Technology Public
4// License Version 6.5 (the "License"). You may not use the content of this file
5// except in compliance with the License. Please obtain a copy of the License
6// at http://www.opencascade.org and read it completely before using this file.
7//
8// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10//
11// The Original Code and all software distributed under the License is
12// distributed on an "AS IS" basis, without warranty of any kind, and the
13// Initial Developer hereby disclaims all such warranties, including without
14// limitation, any warranties of merchantability, fitness for a particular
15// purpose or non-infringement. Please see the License for the specific terms
16// and conditions governing the rights and limitations under the License.
17
7fd59977 18/*
19* Fonction
20* ~~~~~~~~
21* Gestion des textures sous OpenGL
22*
23*
24* Notes
25* ~~~~~
26* Les textures sont toujours initialisee avec des parametres par defaut
27* texture 1D: WRAP_S = CLAMP
28* MAG_FILTER = NEAREST
29* generation de texture automatique en OBJECT_LINEAR
30* rendu avec DECAL
31*
32* texture 2D: WRAP_S/T = REPEAT
33* MAG/MIN_FILTER = LINEAR
34* generation de texture automatique en OBJECT_LINEAR
35* rendu avec MODULATE
36*
37* texture 2D MipMap: WRAP_S/T = REPEAT
38* MAG_FILTER = LINEAR
39* MIN_FILTER = LINEAR_MIPMAP_NEAREST
40* generation de texture automatique en OBJECT_LINEAR
41* rendu avec MODULATE
42*
7fd59977 43* Historique des modifications
44* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45* 22-05-97: PCT ; Creation
46* 18-06-97: FMN ; Ajout entete
47* 20-06-97: PCT ; Correction bug parametres par defaut texture 1D
48* 30-06-97: PCT ; Correction bug rechargement de la texture courante
49* 04-07-97: PCT ; suppression de l'utilisation de libimage.a de SGI
50* 01-08-97: PCT ; suppression InitializeTextureBox()
51* 04-08-97: FMN,PCT ; Portage WNT
52* 05-08-97: FMN ; ajout GetTextureData...
53* 10-09-97: PCT ; ajout commentaires. GetTexture() ne doit pas
54* etre utilisee dans Cas.Cade ( le chargement est
55* fait par Graphic3d )
56* 06-10-97: FMN ; Portage HP
57* 14-10-97: FMN ; Ajout OpenGl_Extension
58* 22-10-97: FMN ; Meilleure gestion de l'extension glXGetCurrentDisplayEXT
59* 04-11-97: FMN ; Gestion des differentes versions GLX
60* 19-11-97: FMN ; Ajout GetCurrentDisplay plus simple que glXGetCurrentDisplayEXT
61* 04-12-97: FMN ; On suppose que l'on travaille en OpenGL1.1 (cf OpenGl_Extension)
62* 17-12-97: FMN ; Probleme compilation SGI
63* 17-12-97: FMN ; Probleme sur Optimisation sur MyBindTextureEXT()
64* Le test sur la texture courante doit tenir compte du contexte.
65* 22-07-98: FGU ; Ajout fonctions TransferTexture_To_Data() et TransferData_To_Texture()
66*
67*/
7fd59977 68
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72
5f8b738e 73#include <OpenGl_GlCore11.hxx>
2166f0fa 74#include <OpenGl_Display.hxx>
7fd59977 75#include <OpenGl_TextureBox.hxx>
2166f0fa 76#include <OpenGl_ImageBox.hxx>
161c4476 77#include <OpenGl_ResourceTexture.hxx>
5e27df78 78#include <OpenGl_Context.hxx>
7fd59977 79
5f8b738e 80#include <GL/glu.h> // gluBuild2DMipmaps()
81
9d35f668 82#include <NCollection_Vector.hxx>
7fd59977 83
7fd59977 84typedef enum {TEXDATA_NONE, TEXDATA_1D, TEXDATA_2D, TEXDATA_2DMM} texDataStatus;
85typedef enum {TEX_NONE, TEX_ALLOCATED} texStatus;
86
87typedef GLfloat SizeType[4];
88
89typedef int TextureDataID;
90#define TEXTUREDATA_ERROR -1
91
92struct texData
93{
94 char imageFileName[128];
95 int imageWidth, imageHeight;
96 GLubyte *image;
97 texDataStatus status;
98 GLint type;
99 int share_count;
1c35b92f 100 DEFINE_STANDARD_ALLOC
7fd59977 101};
102
9d35f668 103struct contextData
104{
105 GLuint number;
106 GLDRAWABLE drawable;
107 GLCONTEXT context;
9d35f668 108};
7fd59977 109
110struct texDraw
111{
112 TextureDataID data;
9d35f668 113 NCollection_Vector<contextData> contextdata;
7fd59977 114 texStatus status;
115
116 GLint Gen;
117 GLint Light;
118 GLint Wrap;
119 GLfloat Plane1[4];
120 GLfloat Plane2[4];
121 GLint Render;
122 GLfloat scalex, scaley;
123 GLfloat transx, transy;
124 GLfloat angle;
125
1c35b92f 126 DEFINE_STANDARD_ALLOC
7fd59977 127};
128
129
130/*----------------------------------------------------------------------*/
131/*
132* Variables statiques
133*/
134
9d35f668 135static NCollection_Vector<texDraw> textab;
7fd59977 136
9d35f668 137static NCollection_Vector<texData> texdata;
7fd59977 138
139static TextureDataID current_texture_data = TEXTUREDATA_ERROR;
140static TextureID current_texture = TEXTUREBOX_ERROR;
141
142static GLfloat sgenparams[] = { 1.0 ,0.0 ,0.0 ,0.0};
143static GLfloat tgenparams[] = { 0.0 ,1.0 ,0.0 ,0.0};
144
145static GLenum status2type[] = { GL_NONE, GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_2D };
146
7fd59977 147/*----------------------------------------------------------------------*/
148/*
149* Fonctions privees
150*/
151
7fd59977 152/*----------------------------------------------------------------------*/
153/*
154* recherche l'existence de datas de texture par son nom
155*/
156static TextureDataID FindTextureData(char *FileName)
157{
9d35f668 158 for (int i = 0; i < texdata.Length(); i++)
159 {
160 if ( texdata(i).status != TEXDATA_NONE && strcmp(FileName, texdata(i).imageFileName) == 0 )
161 {
7fd59977 162 return i;
9d35f668 163 }
164 }
7fd59977 165
166 return TEXTUREDATA_ERROR;
167}
168
169/*----------------------------------------------------------------------*/
170/*
171* recherche un emplacement de data texture libre
172*/
173static TextureDataID FindFreeTextureData(void)
174{
9d35f668 175 for (int i = 0; i < texdata.Length(); i++)
7fd59977 176 {
9d35f668 177 if (texdata(i).status == TEXDATA_NONE)
7fd59977 178 {
7fd59977 179 return i;
180 }
9d35f668 181 }
7fd59977 182
9d35f668 183 texData aTexData;
184 texdata.Append(aTexData);
185 return texdata.Length() - 1;
7fd59977 186}
187
188/*----------------------------------------------------------------------*/
189/*
190* recherche un emplacement de texture libre
191*/
192static TextureID FindFreeTexture(void)
193{
9d35f668 194 for (int i = 0; i < textab.Length(); i++)
7fd59977 195 {
9d35f668 196 if (textab(i).status == TEX_NONE)
7fd59977 197 {
7fd59977 198 return i;
199 }
9d35f668 200 }
7fd59977 201
9d35f668 202 texDraw aTexDraw;
203 textab.Append(aTexDraw);
204 return textab.Length() - 1;
7fd59977 205}
206
207/*----------------------------------------------------------------------*/
208/*
209* regarde si la texture a ete definie pour le contexte courant
210*/
211static int FindTextureContext(TextureID ID)
212{
213 int i;
214
215 GLCONTEXT cur = GET_GL_CONTEXT();
9d35f668 216 for (i=0; i<textab(ID).contextdata.Length(); i++)
217 if (textab(ID).contextdata(i).context == cur)
7fd59977 218 return i;
219
220 return TEXTUREBOX_ERROR;
221}
222
223/*----------------------------------------------------------------------*/
224/*
225* chargement d'une texture suivant son type
226*/
227static void LoadTexture(TextureID ID)
228{
229 TextureDataID data;
230
9d35f668 231 data = textab(ID).data;
232 switch (texdata(data).status)
7fd59977 233 {
234 case TEXDATA_1D:
235 glTexImage1D(GL_TEXTURE_1D, 0, 4,
9d35f668 236 texdata(data).imageWidth, 0,
237 GL_RGBA, GL_UNSIGNED_BYTE, texdata(data).image);
7fd59977 238 break;
239
240 case TEXDATA_2D:
241 glTexImage2D(GL_TEXTURE_2D, 0, 4,
9d35f668 242 texdata(data).imageWidth, texdata(data).imageHeight, 0,
243 GL_RGBA, GL_UNSIGNED_BYTE, texdata(data).image);
7fd59977 244 break;
245
246 case TEXDATA_2DMM:
247 gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
9d35f668 248 texdata(data).imageWidth,
249 texdata(data).imageHeight,
250 GL_RGBA, GL_UNSIGNED_BYTE, texdata(data).image);
7fd59977 251 break;
252 default:
253 break;
254 }
255}
256
257/*----------------------------------------------------------------------*/
258/*
259* les parametres d'initialisation d'une texture
260* NE PAS METTRE DANS UNE DISPLAY LIST POUR L'INSTANT ( pb avec les matrices )
261*/
262static void SetTextureParam(TextureID ID)
263{
264 GLint cur_matrix;
265 TextureDataID data;
266
267
9d35f668 268 data = textab(ID).data;
7fd59977 269 glGetIntegerv(GL_MATRIX_MODE, &cur_matrix);
270
271 /*
272 * MISE EN PLACE DE LA MATRICE DE TEXTURE
273 */
274 glMatrixMode(GL_TEXTURE);
275 glLoadIdentity();
9d35f668 276 /* if (textab(ID).Gen != GL_SPHERE_MAP)
7fd59977 277 {*/
9d35f668 278 glScalef(textab(ID).scalex, textab(ID).scaley, 1.0);
279 glTranslatef(-textab(ID).transx, -textab(ID).transy, 0.0);
280 glRotatef(-textab(ID).angle, 0.0, 0.0, 1.0);
7fd59977 281 /*}*/
282
283
284 /*
285 * GENERATION AUTOMATIQUE DE TEXTURE
286 */
9d35f668 287 switch (textab(ID).Gen)
7fd59977 288 {
289 case GL_OBJECT_LINEAR:
290 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
9d35f668 291 glTexGenfv(GL_S, GL_OBJECT_PLANE, textab(ID).Plane1);
292 if (texdata(data).status != TEXDATA_1D)
7fd59977 293 {
294 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
9d35f668 295 glTexGenfv(GL_T, GL_OBJECT_PLANE, textab(ID).Plane2);
7fd59977 296 }
297 break;
298
299 case GL_SPHERE_MAP:
300 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
9d35f668 301 if (texdata(data).status != TEXDATA_1D)
7fd59977 302 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
303 break;
304
305 case GL_EYE_LINEAR:
306 glMatrixMode(GL_MODELVIEW);
307 glPushMatrix();
308 glLoadIdentity();
309
310 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
9d35f668 311 glTexGenfv(GL_S, GL_EYE_PLANE, textab(ID).Plane1);
7fd59977 312
9d35f668 313 if (texdata(data).status != TEXDATA_1D)
7fd59977 314 {
315 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
9d35f668 316 glTexGenfv(GL_T, GL_EYE_PLANE, textab(ID).Plane2);
7fd59977 317 }
318
319 glPopMatrix();
320 break;
321 }
322
323
324 /*
325 * RENDU DE LA TEXTURE AVEC LES LUMIERES
326 */
9d35f668 327 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textab(ID).Light);
7fd59977 328
329
330 /*
331 * LISSAGE DE LA TEXTURE
332 */
9d35f668 333 switch (texdata(data).status)
7fd59977 334 {
335 case TEXDATA_1D:
336 case TEXDATA_2D:
9d35f668 337 glTexParameteri(texdata(data).type, GL_TEXTURE_MAG_FILTER, textab(ID).Render);
338 glTexParameteri(texdata(data).type, GL_TEXTURE_MIN_FILTER, textab(ID).Render);
7fd59977 339 break;
340
341 case TEXDATA_2DMM:
9d35f668 342 if (textab(ID).Render == GL_NEAREST)
7fd59977 343 {
344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
345 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
346 }
347 else
348 {
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
351 }
352 break;
353 default:
354 break;
355 }
356
357
358 /*
359 * WRAP DE LA TEXTURE
360 */
9d35f668 361 switch (texdata(data).status)
7fd59977 362 {
363 case TEXDATA_1D:
9d35f668 364 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, textab(ID).Wrap);
7fd59977 365 break;
366
367 case TEXDATA_2D:
368 case TEXDATA_2DMM:
9d35f668 369 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textab(ID).Wrap);
370 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textab(ID).Wrap);
7fd59977 371 break;
372 default:
373 break;
374 }
375
376 glMatrixMode(cur_matrix);
377}
378
379/*----------------------------------------------------------------------*/
380/*
381* simulation du glGenTexturesEXT pour un context
382*/
2166f0fa 383static void MyGenTextureEXT (TextureID ID)
7fd59977 384{
9d35f668 385 TextureDataID data = textab(ID).data;
386 contextData aContextData;
387
388 aContextData.context = GET_GL_CONTEXT();
389 aContextData.drawable = GET_GLDEV_CONTEXT();
9d35f668 390 glGenTextures (1, &aContextData.number);
391 textab(ID).contextdata.Append(aContextData);
392 glBindTexture (texdata(data).type, aContextData.number);
2166f0fa 393 LoadTexture (ID);
7fd59977 394}
395
396/*----------------------------------------------------------------------*/
397/*
398* simulation du glBindTextureEXT
399*/
2166f0fa 400static void MyBindTextureEXT (TextureID ID, int Context)
7fd59977 401{
9d35f668 402 TextureDataID data = textab(ID).data;
403 if (texdata(data).status == TEXDATA_NONE)
7fd59977 404 return;
405
9d35f668 406 GLenum aParamName = texdata(data).status == TEXDATA_1D ?
407 GL_TEXTURE_BINDING_1D : GL_TEXTURE_BINDING_2D;
c320e557 408
2166f0fa
SK
409 GLint aCurrTex = -1;
410 glGetIntegerv (aParamName, &aCurrTex);
9d35f668 411 if (textab(ID).contextdata(Context).number != aCurrTex)
7fd59977 412 {
9d35f668 413 glBindTexture (texdata(data).type, textab(ID).contextdata(Context).number);
7fd59977 414 }
415}
416
417/*----------------------------------------------------------------------*/
418/*
419* installation de la texture pour le dernier contexte
420*/
421static int InstallTextureInContext(TextureID ID)
422{
423#ifdef PRINT
424 printf("InstallTextureInContext::installation de la texture dans le context\n");
425#endif
7fd59977 426 MyGenTextureEXT(ID);
427 SetTextureParam(ID);
428#ifdef PRINT
429 printf("InstallTextureInContext::context ok\n");
430#endif
431 return 0;
432}
433
434/*----------------------------------------------------------------------*/
435static TextureID GetTexture(char *FileName, texDataStatus status)
436{
437 TextureDataID i;
438 TextureID j;
439 int dummy;
440
441 /* essait de trouver la texture */
442 i = FindTextureData(FileName);
443 if (i == TEXTUREDATA_ERROR)
444 {
445#ifdef PRINT
446 printf("GetTexture::la texture %s n'existe pas => chargement\n", FileName);
447#endif
448 /* creation d'une texture */
449 i = FindFreeTextureData();
450 if (i == TEXTUREDATA_ERROR) return TEXTUREBOX_ERROR;
451
9d35f668 452 texdata(i).share_count = 0;
453 strcpy(texdata(i).imageFileName, FileName);
454 texdata(i).image = (GLubyte *)read_texture(FileName,
455 &texdata(i).imageWidth,
456 &texdata(i).imageHeight,
7fd59977 457 &dummy);
9d35f668 458 if (texdata(i).image == NULL) return TEXTUREBOX_ERROR;
7fd59977 459
9d35f668 460 texdata(i).status = status;
461 texdata(i).type = status2type[status];
7fd59977 462 }
463
464 j = FindFreeTexture();
465 if (j != TEXTUREBOX_ERROR)
466 {
467#ifdef PRINT
468 printf("GetTexture::installation texture pour obj %d\n", j);
469#endif
9d35f668 470 textab(j).contextdata.Clear();
471 textab(j).data = i;
472 textab(j).status = TEX_ALLOCATED;
473 texdata(i).share_count++;
7fd59977 474
475 SetTextureDefaultParams(j);
476
477#ifdef PRINT
9d35f668 478 printf("GetTexture::texture %s(%d) texture %d count=%d\n", texdata(i).imageFileName, i, j, texdata(i).share_count);
7fd59977 479#endif
480 }
481 else
9d35f668 482 if (texdata(i).share_count != 0)
483 delete [] texdata(i).image;
7fd59977 484
485 return j;
486}
487
488
489/*----------------------------------------------------------------------*/
490static TextureID GetTextureData(char *FileName, texDataStatus status, const GLint width, const GLint height, const void *data)
491{
492 TextureDataID i;
493 TextureID j;
494
495 /* essait de trouver la texture */
496 i = FindTextureData(FileName);
497 if (i == TEXTUREDATA_ERROR)
498 {
499#ifdef PRINT
500 printf("GetTextureData::la texture %s n'existe pas => chargement\n", FileName);
501#endif
502 /* creation d'une texture */
503 i = FindFreeTextureData();
504 if (i == TEXTUREDATA_ERROR) return TEXTUREBOX_ERROR;
505
9d35f668 506 texdata(i).share_count = 0;
507 strcpy(texdata(i).imageFileName, FileName);
508 texdata(i).image = new GLubyte[width*height*4];
509 memcpy(texdata(i).image, data, (width*height*4));
510 texdata(i).imageWidth = width;
511 texdata(i).imageHeight = height;
7fd59977 512
9d35f668 513 if (texdata(i).image == NULL) return TEXTUREBOX_ERROR;
7fd59977 514
9d35f668 515 texdata(i).status = status;
516 texdata(i).type = status2type[status];
7fd59977 517 }
518
519 j = FindFreeTexture();
520 if (j != TEXTUREBOX_ERROR)
521 {
522#ifdef PRINT
523 printf("GetTextureData::installation texture pour obj %d\n", j);
524#endif
9d35f668 525 textab(j).contextdata.Clear();
526 textab(j).data = i;
527 textab(j).status = TEX_ALLOCATED;
528 texdata(i).share_count++;
7fd59977 529
530 SetTextureDefaultParams(j);
531
532#ifdef PRINT
9d35f668 533 printf("GetTextureData::texture %s(%d) texture %d count=%d\n", texdata(i).imageFileName, i, j, texdata(i).share_count);
7fd59977 534#endif
535 }
536 else
9d35f668 537 if (texdata(i).share_count != 0)
538 delete [] texdata(i).image;
7fd59977 539
540 return j;
541}
542
543/*----------------------------------------------------------------------*/
544/*
545* Fonctions publiques
546*/
547
548
549/*----------------------------------------------------------------------*/
550
551GLboolean IsTextureValid(TextureID ID)
552{
9d35f668 553 if ( (ID < 0) || (ID >= textab.Length()) )
7fd59977 554 return GL_FALSE;
555
9d35f668 556 if (textab.Length() > 0)
557 {
558 return textab(ID).status == TEX_ALLOCATED;
559 }
7fd59977 560 else
9d35f668 561 {
562 return GL_FALSE;
563 }
7fd59977 564}
565
566/*----------------------------------------------------------------------*/
567
568TextureID GetTexture1D(char *FileName)
569{
570#ifdef PRINT
571 printf("GetTexture1D::loading 1d %s \n", FileName);
572#endif
573 return GetTexture(FileName, TEXDATA_1D);
574}
575
576/*----------------------------------------------------------------------*/
577
578TextureID GetTexture2D(char *FileName)
579{
580#ifdef PRINT
581 printf("GetTexture2D::loading 2d %s \n", FileName);
582#endif
583 return GetTexture(FileName, TEXDATA_2D);
584}
585
586/*----------------------------------------------------------------------*/
587
588TextureID GetTexture2DMipMap(char *FileName)
589{
590#ifdef PRINT
591 printf("GetTexture2DMipMap::loading 2dmm %s \n", FileName);
592#endif
593 return GetTexture(FileName, TEXDATA_2DMM);
594}
595
596/*----------------------------------------------------------------------*/
597
598TextureID GetTextureData1D(char *FileName, const GLint width, const GLint height, const void *data)
599{
600#ifdef PRINT
601 printf("GetTextureData1D::loading 1d %s \n", FileName);
602#endif
603 return GetTextureData(FileName, TEXDATA_1D, width, height, data);
604}
605
606/*----------------------------------------------------------------------*/
607
608TextureID GetTextureData2D(char *FileName, const GLint width, const GLint height, const void *data)
609{
610#ifdef PRINT
611 printf("GetTextureData2D::loading 2d %s \n", FileName);
612#endif
613 return GetTextureData(FileName, TEXDATA_2D, width, height, data);
614}
615
616/*----------------------------------------------------------------------*/
617
618TextureID GetTextureData2DMipMap(char *FileName, const GLint width, const GLint height, const void *data)
619{
620#ifdef PRINT
621 printf("GetTextureData2DMipMap::loading 2dmm %s \n", FileName);
622#endif
623 return GetTextureData(FileName, TEXDATA_2DMM, width, height, data);
624}
625
626
627/*----------------------------------------------------------------------*/
628
629void SetCurrentTexture(TextureID ID)
630{
631 int context;
632
633 if (!IsTextureValid(ID)) return;
634
635 context = FindTextureContext(ID);
636
637 /* la texture n'existe pas dans ce contexte */
638 if (context == TEXTUREBOX_ERROR)
639 {
640#ifdef PRINT
641 printf("SetCurrentTexture::installation texture %d dans context\n", ID);
642#endif
643 /* si on a une erreur pendant l'installation dans le context
644 * alors on installe la texture sans bind */
645 if (InstallTextureInContext(ID) == TEXTUREBOX_ERROR)
646 {
647 LoadTexture(ID);
648 SetTextureParam(ID);
649 }
650 }
651
652 /*oui, alors on bind directement */
653 else
654 {
655#ifdef PRINT
656 printf("SetCurrentTexture: utilisation du bind %d\n", ID);
657#endif
658 MyBindTextureEXT(ID, context);
659 SetTextureParam(ID);
660 }
661
662 current_texture = ID;
9d35f668 663 current_texture_data = textab(ID).data;
7fd59977 664}
665
666/*----------------------------------------------------------------------*/
667
5e27df78 668void FreeTexture (const Handle(OpenGl_Context)& theContext,
669 TextureID ID)
7fd59977 670{
5e27df78 671 if (!IsTextureValid (ID))
672 {
673 return;
674 }
7fd59977 675
5e27df78 676 TextureDataID data = textab(ID).data;
9d35f668 677 texdata(data).share_count--;
678 if (texdata(data).share_count == 0)
5e27df78 679 {
680 // release texture data
681 delete [] texdata(data).image;
7fd59977 682
2166f0fa 683 // liberation de la texture dans tous les contextes
5e27df78 684 for (int i = 0; i < textab(ID).contextdata.Length(); ++i)
7fd59977 685 {
5e27df78 686 Handle(OpenGl_ResourceTexture) aResource = new OpenGl_ResourceTexture (textab(ID).contextdata(i).number);
1c4ff5c6 687
5853bf66 688 if (!theContext.IsNull())
689 {
1c4ff5c6 690 theContext->DelayedRelease (aResource);
691 }
2166f0fa 692 }
7fd59977 693
9d35f668 694 texdata(data).status = TEXDATA_NONE;
7fd59977 695
9d35f668 696 textab(ID).contextdata.Clear();
7fd59977 697 }
698
9d35f668 699 textab(ID).status = TEX_NONE;
7fd59977 700
701 current_texture_data = TEXTUREDATA_ERROR;
702}
703
704/*----------------------------------------------------------------------*/
705
706void EnableTexture(void)
707{
708 if (!IsTextureValid(current_texture)) return;
709
9d35f668 710 switch (texdata(current_texture_data).status)
7fd59977 711 {
712 case TEXDATA_1D:
9d35f668 713 if (textab(current_texture).Gen != GL_NONE)
7fd59977 714 glEnable(GL_TEXTURE_GEN_S);
715 glEnable(GL_TEXTURE_1D);
716 break;
717
718 case TEXDATA_2D:
719 case TEXDATA_2DMM:
9d35f668 720 if (textab(current_texture).Gen != GL_NONE)
7fd59977 721 {
722 glEnable(GL_TEXTURE_GEN_S);
723 glEnable(GL_TEXTURE_GEN_T);
724 }
725 glEnable(GL_TEXTURE_2D);
726 break;
727 default:
728 break;
729 }
730}
731
732/*----------------------------------------------------------------------*/
733
734void DisableTexture(void)
735{
736 if ( !IsTextureEnabled() )
737 return;
738 if ( !IsTextureValid( current_texture ) )
739 return;
740
9d35f668 741 switch (texdata(current_texture_data).status)
7fd59977 742 {
743 case TEXDATA_1D:
9d35f668 744 if (textab(current_texture).Gen != GL_NONE)
7fd59977 745 glDisable(GL_TEXTURE_GEN_S);
746 glDisable(GL_TEXTURE_1D);
747 break;
748
749 case TEXDATA_2D:
750 case TEXDATA_2DMM:
9d35f668 751 if (textab(current_texture).Gen != GL_NONE)
7fd59977 752 {
753 glDisable(GL_TEXTURE_GEN_S);
754 glDisable(GL_TEXTURE_GEN_T);
755 }
756 glDisable(GL_TEXTURE_2D);
757 break;
758 default:
759 break;
760 }
761}
762
763/*----------------------------------------------------------------------*/
764
765GLboolean IsTextureEnabled(void)
766{
767 GLboolean isEnabled1D= GL_FALSE, isEnabled2D= GL_FALSE;
768 glGetBooleanv( GL_TEXTURE_1D, &isEnabled1D );
769 glGetBooleanv( GL_TEXTURE_2D, &isEnabled2D );
770 return isEnabled1D || isEnabled2D;
771}
772
773/*----------------------------------------------------------------------*/
774
775void SetTextureModulate(TextureID ID)
776{
777 if (!IsTextureValid(ID)) return;
778
9d35f668 779 textab(ID).Light = GL_MODULATE;
7fd59977 780}
781
782/*----------------------------------------------------------------------*/
783
784void SetTextureDecal(TextureID ID)
785{
786 if (!IsTextureValid(ID)) return;
787
9d35f668 788 textab(ID).Light = GL_DECAL;
7fd59977 789}
790
791/*----------------------------------------------------------------------*/
792
793void SetTextureClamp(TextureID ID)
794{
795 if (!IsTextureValid(ID)) return;
796
9d35f668 797 textab(ID).Wrap = GL_CLAMP;
7fd59977 798}
799
800/*----------------------------------------------------------------------*/
801
802void SetTextureRepeat(TextureID ID)
803{
804 if (!IsTextureValid(ID)) return;
805
9d35f668 806 textab(ID).Wrap = GL_REPEAT;
7fd59977 807}
808
809/*----------------------------------------------------------------------*/
810
811/* gestion de la facon d'appliquer la texture */
5f8b738e 812void SetModeObject(TextureID ID, const GLfloat sparams[4], const GLfloat tparams[4])
7fd59977 813{
814 if (!IsTextureValid(ID)) return;
815
9d35f668 816 textab(ID).Gen = GL_OBJECT_LINEAR;
817 if (sparams != NULL) memcpy(textab(ID).Plane1, sparams, sizeof(sgenparams));
818 else memcpy(textab(ID).Plane1, sgenparams, sizeof(sgenparams));
7fd59977 819
9d35f668 820 if (texdata(textab(ID).data).status != TEXDATA_1D) {
821 if (tparams != NULL) memcpy(textab(ID).Plane2, tparams, sizeof(tgenparams));
822 else memcpy(textab(ID).Plane2, tgenparams, sizeof(tgenparams));
7fd59977 823 }
824}
825
826/*----------------------------------------------------------------------*/
827
828void SetModeSphere(TextureID ID)
829{
830 if (!IsTextureValid(ID)) return;
831
9d35f668 832 textab(ID).Gen = GL_SPHERE_MAP;
7fd59977 833}
834
835/*----------------------------------------------------------------------*/
836
5f8b738e 837void SetModeEye(TextureID ID, const GLfloat sparams[4], const GLfloat tparams[4])
7fd59977 838{
839 if (!IsTextureValid(ID)) return;
840
9d35f668 841 textab(ID).Gen = GL_EYE_LINEAR;
842 if (sparams != NULL) memcpy(textab(ID).Plane1, sparams, sizeof(sgenparams));
843 else memcpy(textab(ID).Plane1, sgenparams, sizeof(sgenparams));
7fd59977 844
9d35f668 845 if (texdata(textab(ID).data).status != TEXDATA_1D) {
846 if (tparams != NULL) memcpy(textab(ID).Plane2, tparams, sizeof(tgenparams));
847 else memcpy(textab(ID).Plane2, tgenparams, sizeof(tgenparams));
7fd59977 848 }
849}
850
851/*----------------------------------------------------------------------*/
852
853void SetModeManual(TextureID ID)
854{
855 if (!IsTextureValid(ID)) return;
856
9d35f668 857 textab(ID).Gen = GL_NONE;
7fd59977 858}
859
860/*----------------------------------------------------------------------*/
861
862void SetRenderNearest(TextureID ID)
863{
864 if (!IsTextureValid(ID)) return;
865
9d35f668 866 textab(ID).Render = GL_NEAREST;
7fd59977 867}
868
869/*----------------------------------------------------------------------*/
870
871void SetRenderLinear(TextureID ID)
872{
873 if (!IsTextureValid(ID)) return;
874
9d35f668 875 textab(ID).Render = GL_LINEAR;
7fd59977 876}
877
878/*----------------------------------------------------------------------*/
879
880void SetTexturePosition(TextureID ID,
881 GLfloat scalex, GLfloat scaley,
882 GLfloat transx, GLfloat transy,
883 GLfloat angle)
884{
9d35f668 885 textab(ID).scalex = scalex;
886 textab(ID).scaley = scaley;
887 textab(ID).transx = transx;
888 textab(ID).transy = transy;
889 textab(ID).angle = angle;
7fd59977 890}
891
892/*----------------------------------------------------------------------*/
893
894void SetTextureDefaultParams(TextureID ID)
895{
896 if (!IsTextureValid(ID)) return;
897
898#ifdef PRINT
899 printf("SetTextureDefaultParams::set parm par defaut textures\n");
900#endif
901
902
9d35f668 903 textab(ID).scalex = 1.0;
904 textab(ID).scaley = 1.0;
905 textab(ID).transx = 0.0;
906 textab(ID).transy = 0.0;
907 textab(ID).angle = 0.0;
7fd59977 908
9d35f668 909 textab(ID).Gen = GL_OBJECT_LINEAR;
910 textab(ID).Light = texdata(textab(ID).data).status == TEXDATA_1D ? GL_DECAL : GL_MODULATE;
911 textab(ID).Wrap = texdata(textab(ID).data).status == TEXDATA_1D ? GL_CLAMP : GL_REPEAT;
912 memcpy(textab(ID).Plane1, sgenparams, sizeof(sgenparams));
913 memcpy(textab(ID).Plane2, tgenparams, sizeof(tgenparams));
914 textab(ID).Render = texdata(textab(ID).data).status == TEXDATA_1D ? GL_NEAREST : GL_LINEAR;
7fd59977 915}
916
917/*----------------------------------------------------------------------*/
918/* Transfere de donnee des donnees internes a la structure TransferData */
919void TransferTexture_To_Data(TextureID ID, TextureData *TransfDt)
920{
921 /* affectations */
9d35f668 922 strcpy(TransfDt->path, texdata(textab(ID).data).imageFileName);
923 TransfDt->gen = textab(ID).Gen;
924 TransfDt->wrap = textab(ID).Wrap;
925 TransfDt->render = textab(ID).Light;
926 TransfDt->scalex = textab(ID).scalex;
927 TransfDt->scaley = textab(ID).scaley;
928 TransfDt->transx = textab(ID).transx;
929 TransfDt->transy = textab(ID).transy;
930 TransfDt->angle = textab(ID).angle;
931 memcpy(TransfDt->plane1, textab(ID).Plane1, sizeof(SizeType));
932 memcpy(TransfDt->plane2, textab(ID).Plane2, sizeof(SizeType));
7fd59977 933}