OCC22199 OpenGL memory leaks in TKOpenGl
[occt.git] / src / OpenGl / OpenGl_triedron.cxx
1 /***********************************************************************
2
3 FONCTION :
4 ----------
5 File OpenGl_triedron : gestion du triedre non zoomable.
6
7 REMARQUES:
8 ---------- 
9
10 Algorithme :
11 o dimensions et origine du triedre sont recalcules a chaque fois, en
12 fonction de la taille (u,v = largeur-hauteur) de la fenetre =>
13 il est inutile ici de jouer sur le facteur d'echelle.
14 o on n'inhibe que les translations, car on veut conserver les
15 transformations d'orientations.
16
17
18 HISTORIQUE DES MODIFICATIONS   :
19 --------------------------------
20 08-12-98 : BGN ; Creation.
21
22
23 ************************************************************************/
24
25 #define BUC60851  /* GG 15/03/01 Avoid to raise after the second creation
26 of the trihedron
27 */
28
29 #define BUC61045        /* 25/10/01 SAV ; added functionality to control gl lighting 
30 from higher API */
31
32
33 #define QTOCC_PATCH   /* Active QtOPENCASCADE patches */
34
35 /*----------------------------------------------------------------------*/
36 /*
37 * Includes
38 */ 
39
40 #include <OpenGl_tgl_all.hxx>
41 #include <OpenGl_tgl_tox.hxx>
42
43
44 #include <stddef.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <math.h> /* pour M_PI */
48
49 #include <OpenGl_LightBox.hxx>
50 #include <OpenGl_TextureBox.hxx>
51 #include <OpenGl_tgl_tox.hxx>
52
53 #include <OpenGl_tsm_ws.hxx>  /* pour TsmGetWSAttri */
54 #include <OpenGl_telem_view.hxx>  /* pour tel_view_data */
55 #include <InterfaceGraphic_Graphic3d.hxx>  /* pour CALL_DEF_STRUCTURE */
56 #include <InterfaceGraphic_Aspect.hxx>  /* pour CALL_DEF_VIEW  */
57 #include <InterfaceGraphic_Visual3d.hxx>
58
59 #include <OpenGl_transform_persistence.hxx>
60
61 #ifdef HAVE_GL2PS
62 #include <gl2ps.h>
63 #endif
64
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68 #ifdef HAVE_STRING_H
69 #include <string.h>
70 #endif
71
72 #include <OpenGl_TextRender.hxx>
73 /*----------------------------------------------------------------------*/
74 /*
75 * Constantes
76 */ 
77
78 #define NO_DEBUG
79 #define NO_PRINT
80 #define NO_PRINT_MATRIX
81
82 #ifndef M_PI
83 # define M_PI          3.14159265358979323846
84 #endif
85
86 /* on trouve que 10 vues sera suffisant */
87 #define GROW_SIZE_NZ_WKS   10
88
89 /* pour l'instant on ne travaille que sur le triedre */
90 /* on augmentera le define quand on developpera les struc. non zoomables*/
91 #define GROW_SIZE_NZ_STRUC  1 
92
93 /* Identificateur du Triedre Non Zommable dans la liste des structures */
94 # define TRIEDRON_ID -100
95
96 /*----------------------------------------------------------------------*/
97 /*
98 * Definition de types
99 */ 
100 typedef struct
101 {
102   float aXColor[3];
103   float aYColor[3];
104   float aZColor[3];
105   float aRatio;
106   float aDiametr;
107   int   aNbFacettes;
108 } ZBUF_STRUCT;
109
110
111 /* donnees des structures non zoomables liees a une wks (=une vue) */
112 typedef struct
113 {
114   int   NZStrucID; /* = TRIEDRON_ID ou astructure->Id de Graphic3d sinon.*/
115   int   aPos;
116   float aColor[3];
117   float aScale;
118   int   isWireframe;
119   CALL_DEF_STRUCTURE * astructure;
120   ZBUF_STRUCT* aZBufParam;
121 } NZ_STRUC_DATA;
122
123
124
125 /* liste des wks (=vues) concernees par les structures non zoomables */
126 typedef struct
127 {
128   int   nz_wks; /* = id de la wks  */
129   int   triedron_on; /* =0 si le triedre non zoomable n'est pas defini */
130   int   nz_struc_count; /* nb de structures non zoomables de la vue */
131   int   nz_struc_size;
132   NZ_STRUC_DATA *nz_struc;
133 } NZ_WKS;
134
135
136 typedef  struct
137 {
138   TEL_VIEW_REP    vrep;
139 } TEL_VIEW_DATA, *tel_view_data; /* Internal data stored for every view rep */
140
141 /*----------------------------------------------------------------------*/
142 /*
143 * Variables statiques
144 */
145
146 static NZ_WKS *nz_wks = NULL;
147 static int     nz_wks_count = 0;
148 static int     nz_wks_size = 0;
149
150 /* Default parameters for ZBUFFER triheron */
151 static float theXColor[] = { 1.0, 0.0, 0.0 };
152 static float theYColor[] = { 0.0, 1.0, 0.0 };
153 static float theZColor[] = { 0.0, 0.0, 1.0 };
154 static float theRatio = 0.8f;
155 static float theDiametr = 0.05f;
156 static int   theNBFacettes = 12;
157
158 /*----------------------------------------------------------------------*/
159 /*----------------------------------------------------------------------*/
160 /*
161 * Fonctions privees
162 */
163
164
165
166 /*----------------------------------------------------------------------*/
167 /*
168 * recherche de la wks NZWksID concernee par les structures non zoomables;
169 * creation de l'element wks s'il n'existe pas.
170 */
171 static int find_nz_wks(int NZWksID, int alloc)
172 {
173   int i;
174
175   /* recherche la wks dans la liste si elle existe */
176   for (i=0; i<nz_wks_count; i++)
177     if (nz_wks[i].nz_wks == NZWksID)
178       return i;
179
180   if (!alloc) return -1;
181
182   /* la wks n'existe pas => on fait de la place si il n'y en a plus */
183   if (nz_wks_count == nz_wks_size) {      
184 #ifdef BUC60851
185     if( nz_wks_size > 0 ) {
186       nz_wks_size += GROW_SIZE_NZ_WKS;
187       nz_wks =  (NZ_WKS *) realloc(nz_wks, nz_wks_size * sizeof(NZ_WKS)); 
188     } else {
189       nz_wks_size = GROW_SIZE_NZ_WKS;
190       nz_wks = (NZ_WKS *) malloc(nz_wks_size * sizeof(NZ_WKS));
191     }
192 #else
193     nz_wks_size += GROW_SIZE_NZ_WKS;
194     nz_wks =  (NZ_WKS *) realloc(nz_wks, nz_wks_size * sizeof(NZ_WKS));
195 #endif
196     if (nz_wks == NULL) return -1;
197   }
198
199   nz_wks[nz_wks_count].nz_wks = NZWksID;
200   nz_wks[nz_wks_count].triedron_on = 0;
201   nz_wks[nz_wks_count].nz_struc = NULL;
202   nz_wks[nz_wks_count].nz_struc_size = 0;
203   nz_wks[nz_wks_count].nz_struc_count = 0;
204
205   return nz_wks_count++;
206 }
207
208 /*-----------------------------------------------------------------*/
209 /*
210 * recherche de la structure non zoomable NZStrucID dans la liste
211 * de la wks NZWksIdx ;
212 * creation de l'element structure non zoomable s'il n'existe pas.
213 */
214 static int find_nz_struc(int NZWksIdx, int NZStrucID, int alloc)
215 {
216   int i;
217   NZ_STRUC_DATA *nz_struc;
218
219
220   /* recherche la structure non zoomable dans la liste de la wks */
221   nz_struc = nz_wks[NZWksIdx].nz_struc;
222   for (i=0; i<nz_wks[NZWksIdx].nz_struc_count; i++)
223     if (nz_struc[i].NZStrucID == NZStrucID)
224       return i;
225
226   if (!alloc) return -1;
227
228   /* la structure non zoomable n'existe pas => on la cree */
229   if (nz_wks[NZWksIdx].nz_struc_count == nz_wks[NZWksIdx].nz_struc_size) {
230 #ifdef BUC60851
231     if( nz_wks[NZWksIdx].nz_struc_size > 0 ) {
232       nz_wks[NZWksIdx].nz_struc_size += GROW_SIZE_NZ_STRUC;
233       nz_wks[NZWksIdx].nz_struc = 
234         (NZ_STRUC_DATA *)  realloc(nz_wks[NZWksIdx].nz_struc, 
235         nz_wks[NZWksIdx].nz_struc_size * sizeof(NZ_STRUC_DATA));
236     } else {
237       nz_wks[NZWksIdx].nz_struc_size = GROW_SIZE_NZ_STRUC;
238       nz_wks[NZWksIdx].nz_struc = 
239         (NZ_STRUC_DATA *)  malloc( nz_wks[NZWksIdx].nz_struc_size * sizeof(NZ_STRUC_DATA));
240     }
241 #else
242     nz_wks[NZWksIdx].nz_struc_size += GROW_SIZE_NZ_STRUC;
243     nz_wks[NZWksIdx].nz_struc = (NZ_STRUC_DATA *)  realloc(nz_wks[NZWksIdx].nz_struc, nz_wks[NZWksIdx].nz_struc_size * sizeof(NZ_STRUC_DATA));
244 #endif
245     if (nz_wks[NZWksIdx].nz_struc == NULL) return -1;
246
247     nz_wks[NZWksIdx].nz_struc[nz_wks[NZWksIdx].nz_struc_count].aZBufParam = NULL;
248   }
249
250   return nz_wks[NZWksIdx].nz_struc_count++;
251 }
252
253
254 /*----------------------------------------------------------------------*/
255
256 /*
257 * affichage d'un triedre non zoomable a partir des index dans les tables
258 * des structures non zoomables.
259 *
260 * Triedre = Objet non Zoomable :
261 * on recalcule ses dimensions et son origine en fonction de la taille
262 * de la fenetre; on positionne selon le choix de l'init;
263 * et on inhibe seulement les translations.
264 *
265 */
266
267
268
269
270 TStatus call_triedron_redraw (
271                               int      nz_wks_entry,
272                               int      nz_struc_entry,
273                               GLdouble U,
274                               GLdouble V
275                               )
276 {
277   GLdouble modelMatrix[4][4];
278   GLdouble projMatrix[4][4];
279
280   GLdouble TriedronAxeX[3] = { 1.0, 0.0, 0.0 };
281   GLdouble TriedronAxeY[3] = { 0.0, 1.0, 0.0 };
282   GLdouble TriedronAxeZ[3] = { 0.0, 0.0, 1.0 };
283   GLdouble TriedronOrigin[3] = { 0.0, 0.0, 0.0 };
284   GLfloat TriedronColor[3] = { 1.0, 1.0, 1.0 }; /* def = blanc */
285
286   GLfloat TriedronWidth = 1.0;
287   GLfloat TriedronScale = (float)0.1 ;
288   GLint   TriedronPosition = 0; /* def = Aspect_TOTP_CENTER */
289
290   GLdouble L, l, rayon ;
291   GLdouble minUV;
292   int      ii;
293 #ifdef PRINT_MATRIX 
294   int      jj;
295 #endif
296   int      NbFacettes;
297   double   Angle;
298   GLdouble TriedronCoord[3] = { 1.0, 0.0, 0.0 };
299
300   GLuint fontBase = 0;
301   GLint mode;
302   char AxeName[]="X\0";
303
304 #ifdef QTOCC_PATCH /* PCD 10/02/08 */
305   /* Fix to problem with clipping planes chopping off pieces of the triedron */
306   GLint max_plane=0;
307   GLboolean* isPlaneActive;
308   glGetIntegerv( GL_MAX_CLIP_PLANES, &max_plane);
309   isPlaneActive= new GLboolean[max_plane];
310   /* Backup the clip planes.         */
311   for (ii = 0; ii < max_plane ; ii++) {
312     isPlaneActive[ii] = glIsEnabled(GL_CLIP_PLANE0 + ii);
313     glDisable(GL_CLIP_PLANE0 + ii);
314   }
315 #endif
316
317   /* 
318   * Lecture des Init. du Triedre 
319   */
320
321   TriedronColor[0] = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[0];
322   TriedronColor[1] = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[1];
323   TriedronColor[2] = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[2];
324   TriedronScale = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aScale;
325   TriedronPosition = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aPos;
326
327
328   /* 
329   * Calcul des axes => on inhibe le zoom.
330   */
331
332   /* la taille des axes est 1 proportion (fixee a l'init du triedre) */
333   /* de la dimension la plus petite de la window.                    */ 
334   if ( U < V )  minUV = U;
335   else minUV = V;
336   L = minUV * (double) TriedronScale ;
337
338   /* Position de l'origine */
339   TriedronOrigin[0]= 0.0; 
340   TriedronOrigin[1]= 0.0;
341   TriedronOrigin[2]= 0.0; 
342
343   /* Position des Axes */
344   TriedronAxeX[0] = TriedronOrigin[0] + L ;
345   TriedronAxeX[1] = TriedronOrigin[1] + 0.0;
346   TriedronAxeX[2] = TriedronOrigin[2] + 0.0;
347
348   TriedronAxeY[0] = TriedronOrigin[0] + 0.0;
349   TriedronAxeY[1] = TriedronOrigin[1] + L ;
350   TriedronAxeY[2] = TriedronOrigin[2] + 0.0;
351
352   TriedronAxeZ[0] = TriedronOrigin[0] + 0.0;
353   TriedronAxeZ[1] = TriedronOrigin[1] + 0.0;
354   TriedronAxeZ[2] = TriedronOrigin[2] + L ;
355
356   /*
357   * On inhibe les translations; on conserve les autres transformations.
358   */
359
360   /* on lit les matrices de transformation et de projection de la vue */
361   /* pour annuler les translations (dernieres colonnes des matrices). */
362   glGetDoublev( GL_MODELVIEW_MATRIX,  (GLdouble *) modelMatrix );
363   glGetDoublev( GL_PROJECTION_MATRIX, (GLdouble *) projMatrix );
364
365 #ifdef PRINT_MATRIX 
366   printf ("\n--- call_triedron_redraw : avant transfo projMatrix= ");
367   for (ii = 0; ii<4 ; ii++) {
368     printf ("\n ");
369     for (jj = 0; jj<4 ; jj++) {
370       printf ("  %f  ", projMatrix[ii][jj] );
371     }
372   }
373   printf ("\n--- call_triedron_redraw : avant transfo  modelMatrix= ");
374   for (ii = 0; ii<4 ; ii++) {
375     printf ("\n ");
376     for (jj = 0; jj<4 ; jj++) {
377       printf ("  %f  ", modelMatrix[ii][jj] );
378     }
379   }
380 #endif
381
382
383   /* on annule la translation qui peut etre affectee a la vue */
384   modelMatrix[3][0] = 0. ;
385   modelMatrix[3][1] = 0. ;
386   modelMatrix[3][2] = 0. ; 
387   projMatrix[3][0] = 0. ;
388   projMatrix[3][1] = 0. ;
389   projMatrix[3][2] = 0. ; 
390
391
392   /* sauvegarde du contexte des matrices avant chargement */
393   glMatrixMode (GL_MODELVIEW);
394   glPushMatrix ();
395   glLoadIdentity ();
396   glLoadMatrixd( (GLdouble *) modelMatrix);
397   glMatrixMode ( GL_PROJECTION );
398   glPushMatrix ();
399   glLoadIdentity();
400   glLoadMatrixd( (GLdouble *) projMatrix);
401
402
403 #ifdef PRINT_MATRIX 
404   printf ("\n\n\n--- call_triedron_redraw :  APRES transfo projMatrix= ");
405   for (ii = 0; ii<4 ; ii++) {
406     printf ("\n ");
407     for (jj = 0; jj<4 ; jj++) {
408       printf ("  %f  ", projMatrix[ii][jj] );
409     }
410   }
411   printf ("\n--- call_triedron_redraw : APRES transfo  modelMatrix= ");
412   for (ii = 0; ii<4 ; ii++) {
413     printf ("\n ");
414     for (jj = 0; jj<4 ; jj++) {
415       printf ("  %f  ", modelMatrix[ii][jj] );
416     }
417   }
418 #endif
419
420
421   /*
422   * Positionnement de l'origine du triedre selon le choix de l'init
423   */
424
425   /* on fait uniquement une translation de l'origine du Triedre */
426
427   switch (TriedronPosition) {
428
429        case 0 : /* Aspect_TOTP_CENTER */
430          break;
431
432        case 1 : /* Aspect_TOTP_LEFT_LOWER */
433          glTranslated( -U/2. + L , -V/2. + L , 0. );
434          break;
435
436        case 2  : /*Aspect_TOTP_LEFT_UPPER */
437          glTranslated( -U/2. + L , +V/2. - L -L/3. , 0. );
438          break;
439
440        case 3 : /* Aspect_TOTP_RIGHT_LOWER */
441          glTranslated( U/2. - L -L/3. , -V/2. + L , 0. );
442          break;
443
444        case 4  : /* Aspect_TOTP_RIGHT_UPPER */
445          glTranslated( U/2. - L -L/3. , +V/2. - L -L/3. , 0. );
446          break;
447
448        default :
449          break;
450
451   }
452
453 #ifdef PRINT 
454   printf ("\n--- call_triedron_redraw :  TriedronOrigin= %f %f %f\n",
455     TriedronOrigin[0], TriedronOrigin[1], TriedronOrigin[2]);
456   printf ("\n---                      :  TriedronAxeX= %f %f %f \n",
457     TriedronAxeX[0], TriedronAxeX[1], TriedronAxeX[2]);
458   printf ("\n---                      :  TriedronAxeY= %f %f %f \n",
459     TriedronAxeY[0], TriedronAxeY[1], TriedronAxeY[2] );
460   printf ("\n---                      :  TriedronAxeZ= %f %f %f \n",
461     TriedronAxeZ[0], TriedronAxeZ[1], TriedronAxeZ[2]);
462   printf ("\n---                      : TriedronScale= %f \n",TriedronScale);
463   printf ("\n---                      : 1/echelle = ( %f %f %f %f ) \n",
464     modelMatrix[0][0],modelMatrix[1][1],modelMatrix[2][2],modelMatrix[3][3]);
465 #endif
466
467
468   /* 
469   * Creation du triedre 
470   */
471   glColor3fv (TriedronColor);
472
473   glGetIntegerv( GL_RENDER_MODE, &mode );
474
475 #ifdef HAVE_GL2PS
476   if( mode==GL_RENDER )
477     glLineWidth( TriedronWidth );
478   else if( mode==GL_FEEDBACK )
479     gl2psLineWidth( TriedronWidth );
480 #else
481         glLineWidth( TriedronWidth );
482 #endif 
483
484 #ifdef QTOCC_PATCH /* Fotis Sioutis 2007-11-14 15:06 
485   I have also seen in previous posts that the view trihedron in V3d_WIREFRAME mode 
486   changes colors depending on the state of the view. This behaviour can be easily 
487   corrected by altering call_triedron_redraw function in OpenGl_triedron.c of TKOpengl.
488   The only change needed is to erase the LightOff() function that is called before the 
489   Axis name drawing and move this function call just before the initial axis drawing.
490   Below is the code portion with the modification.I don't know if this is considered to 
491   be a bug but anyway i believe it might help some of you out there.*/
492   LightOff();
493 #endif
494
495   /* dessin des axes */
496   glBegin(GL_LINES);
497   glVertex3dv( TriedronOrigin );
498   glVertex3dv( TriedronAxeX );
499
500   glVertex3dv( TriedronOrigin );
501   glVertex3dv( TriedronAxeY );
502
503   glVertex3dv( TriedronOrigin );
504   glVertex3dv( TriedronAxeZ );
505   glEnd();
506
507
508   /* fleches au bout des axes (= cones de la couleur demandee) */
509   l = L - L/4. ; /* distance a l'origine */
510   rayon = L/30. ; /* rayon de la base du cone */
511   NbFacettes = 12; /* le cone sera compose de 12 facettes triangulaires */
512   Angle = 2. * M_PI/ NbFacettes;
513
514   /* solution FILAIRE des cones au bout des axes : une seule ligne */
515   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
516   /* (la couleur est deja initialisee dans TriedronColor) */
517   /* FIN de la solution FILAIRE CHOISIE pour les cones des axes */
518
519   /* fleche en X */
520   glBegin(GL_TRIANGLE_FAN);
521   glVertex3dv( TriedronAxeX );
522   TriedronCoord[0] = TriedronOrigin[0] + l ;
523   ii = NbFacettes;
524   while (ii >= 0 ) {
525     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * sin(ii * Angle) );
526     TriedronCoord[2] = TriedronOrigin[2] + ( rayon * cos(ii * Angle) );
527     glVertex3dv( TriedronCoord );
528     ii--;
529   }
530   glEnd();
531
532   /* fleche en Y */
533   glBegin(GL_TRIANGLE_FAN);
534   glVertex3dv( TriedronAxeY );
535   TriedronCoord[1] = TriedronOrigin[1] + l ;
536   ii = NbFacettes;
537   while (ii >= 0 ) {
538     TriedronCoord[0] = TriedronOrigin[0] + (rayon * cos(ii * Angle) );
539     TriedronCoord[2] = TriedronOrigin[2] + (rayon * sin(ii * Angle) );
540     glVertex3dv( TriedronCoord );
541     ii--;
542   }
543   glEnd();
544
545   /* fleche en Z */
546   glBegin(GL_TRIANGLE_FAN);
547   glVertex3dv( TriedronAxeZ );
548   TriedronCoord[2] = TriedronOrigin[2] + l ;
549   ii = NbFacettes;
550   while (ii >= 0 ) {
551     TriedronCoord[0] = TriedronOrigin[0] + ( rayon * sin(ii * Angle) );
552     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * cos(ii * Angle) );
553     glVertex3dv( TriedronCoord );
554     ii--;
555   }
556   glEnd();
557
558   /* dessin de l'origine */
559   TriedronCoord[0] = TriedronOrigin[0] + rayon ;
560   TriedronCoord[1] = TriedronOrigin[1] + 0.0 ;
561   TriedronCoord[2] = TriedronOrigin[2] + 0.0 ;
562   ii = 24 ;
563   Angle = 2. * M_PI/ii ;
564   glBegin(GL_LINE_LOOP);
565   while (ii >= 0 ) {
566     TriedronCoord[0] = TriedronOrigin[0] + ( rayon * sin(ii * Angle) );
567     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * cos(ii * Angle) );
568     glVertex3dv( TriedronCoord );
569     ii--;
570   }  
571   glEnd();
572
573   LightOff();
574
575   /* 
576   * Noms des axes et de l'origine
577   */
578
579   /* init de la fonte */
580
581   OpenGl_TextRender* textRender=OpenGl_TextRender::instance();
582
583   /* Axe X */
584   strcpy ( AxeName, "X\0" );
585   TriedronCoord[0] = TriedronOrigin[0] + ( L + rayon ) ;
586   TriedronCoord[1] = TriedronOrigin[1] + 0.0;
587   TriedronCoord[2] = TriedronOrigin[2] - rayon ;
588
589   textRender->RenderText(AxeName, fontBase, 0, (float)TriedronCoord[0], (float)TriedronCoord[1], (float)TriedronCoord[2] );
590   /* Axe Y */
591   strcpy ( AxeName, "Y\0" );
592   TriedronCoord[0] = TriedronOrigin[0] + rayon ;
593   TriedronCoord[1] = TriedronOrigin[1] + ( L + 3.0 * rayon ) ;
594   TriedronCoord[2] = TriedronOrigin[2] + ( 2.0 * rayon );
595   textRender->RenderText(AxeName, fontBase, 0, (float)TriedronCoord[0], (float)TriedronCoord[1], (float)TriedronCoord[2]);
596   /* Axe Z */
597   strcpy ( AxeName, "Z\0" );
598   TriedronCoord[0] = TriedronOrigin[0] + ( - 2.0 * rayon ) ;
599   TriedronCoord[1] = TriedronOrigin[1] +  rayon/2. ;
600   TriedronCoord[2] = TriedronOrigin[2] + ( L + 3.0 * rayon ) ;
601
602   textRender->RenderText(AxeName, fontBase, 0, (float)TriedronCoord[0], (float)TriedronCoord[1], (float)TriedronCoord[2]);
603
604 #ifdef QTOCC_PATCH /* PCD 10/02/08 */
605   /* Recover the clip planes */
606   glGetIntegerv( GL_MAX_CLIP_PLANES, &max_plane);
607   for (ii = 0; ii <max_plane ; ii++) {
608     if (isPlaneActive[ii]) { 
609       glEnable(GL_CLIP_PLANE0 + ii);
610     }
611   } 
612   free(isPlaneActive);
613
614 #endif
615
616   /* 
617   * restauration du contexte des matrices
618   */
619   glMatrixMode (GL_PROJECTION);
620   glPopMatrix ();
621   glMatrixMode (GL_MODELVIEW);
622   glPopMatrix ();
623
624
625   return (TSuccess);
626
627 }
628
629
630
631
632 /*******************************************************
633 *  Draws ZBUFFER trihedron mode
634 *******************************************************/
635 TStatus call_zbuffer_triedron_redraw (
636                                       int      nz_wks_entry,
637                                       int      nz_struc_entry,
638                                       GLdouble U,
639                                       GLdouble V
640                                       )
641 {
642   GLdouble modelMatrix[4][4];
643   GLdouble projMatrix[4][4];
644
645   GLdouble TriedronAxeX[3] = { 1.0, 0.0, 0.0 };
646   GLdouble TriedronAxeY[3] = { 0.0, 1.0, 0.0 };
647   GLdouble TriedronAxeZ[3] = { 0.0, 0.0, 1.0 };
648   GLdouble TriedronOrigin[3] = { 0.0, 0.0, 0.0 };
649   GLfloat TriedronColor[3] = { 1.0, 1.0, 1.0 }; /* def = blanc */
650
651   GLfloat TriedronScale = (float)0.1 ;
652   GLint   TriedronPosition = 0; /* def = Aspect_TOTP_CENTER */
653
654   GLdouble L, l, rayon ;
655   GLdouble minUV;
656   int      NbFacettes = 12;
657   double   Angle;
658   GLdouble TriedronCoord[3] = { 1.0, 0.0, 0.0 };
659
660   GLuint fontBase = 0;
661   char AxeName[]="X\0";
662
663   GLuint startList;
664   GLUquadricObj* aQuadric;
665   GLfloat aXColor[] = { 1.0, 0.0, 0.0, 0.6f };
666   GLfloat aYColor[] = { 0.0, 1.0, 0.0, 0.6f };
667   GLfloat aZColor[] = { 0.0, 0.0, 1.0, 0.6f };
668   GLdouble aConeDiametr;
669   GLdouble aConeLength;
670   GLdouble aCylinderDiametr;
671   GLdouble aCylinderLength;
672   GLboolean aIsDepthEnabled;
673 #ifndef BUG
674   GLboolean aIsDepthMaskEnabled;
675 #endif
676   GLint   aViewPort[4];  /* to store view port coordinates */
677   GLdouble aWinCoord[3];
678   GLboolean isWithinView;
679   GLdouble aLengthReduce = 0.8;
680   GLdouble aAxisDiametr = 0.05;
681   ZBUF_STRUCT* aParam;
682
683 #ifdef QTOCC_PATCH 
684   GLint df;                                       /* PCD 17/06/07 */      
685   GLfloat aNULLColor[] = { 0.0, 0.0, 0.0, 0.0f }; /* FS 21/01/08 */
686   /* Fix to problem with clipping planes chopping off pieces of the triedron   */
687   int i;
688   GLint max_plane=0;
689   GLboolean* isPlaneActive;
690   glGetIntegerv( GL_MAX_CLIP_PLANES, &max_plane);
691   isPlaneActive = new GLboolean[max_plane];
692   /* Backup the clip planes. */
693   for (i = 0; i < max_plane ; i++) {
694     isPlaneActive[i] = glIsEnabled(GL_CLIP_PLANE0 + i);
695     glDisable(GL_CLIP_PLANE0 + i);
696   }
697 #endif
698
699   /* 
700   * Lecture des Init. du Triedre 
701   */
702
703   TriedronColor[0] = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[0];
704   TriedronColor[1] = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[1];
705   TriedronColor[2] = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[2];
706   TriedronScale = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aScale;
707   TriedronPosition = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aPos;
708
709   if (nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aZBufParam != NULL) {
710     aParam = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aZBufParam;
711     aXColor[0] = aParam->aXColor[0];
712     aXColor[1] = aParam->aXColor[1];
713     aXColor[2] = aParam->aXColor[2];
714
715     aYColor[0] = aParam->aYColor[0];
716     aYColor[1] = aParam->aYColor[1];
717     aYColor[2] = aParam->aYColor[2];
718
719     aZColor[0] = aParam->aZColor[0];
720     aZColor[1] = aParam->aZColor[1];
721     aZColor[2] = aParam->aZColor[2];
722
723     aLengthReduce = aParam->aRatio;
724     aAxisDiametr = aParam->aDiametr;
725     NbFacettes = aParam->aNbFacettes;
726   }
727
728   /* 
729   * Calcul des axes => on inhibe le zoom.
730   */
731
732   /* la taille des axes est 1 proportion (fixee a l'init du triedre) */
733   /* de la dimension la plus petite de la window.                    */ 
734   if ( U < V )  minUV = U;
735   else minUV = V;
736   L = minUV * (double) TriedronScale ;
737
738   /* Position de l'origine */
739   TriedronOrigin[0]= 0.0; 
740   TriedronOrigin[1]= 0.0;
741   TriedronOrigin[2]= 0.0; 
742
743   /* Position des Axes */
744   TriedronAxeX[0] = TriedronOrigin[0] + L ;
745   TriedronAxeX[1] = TriedronOrigin[1] + 0.0;
746   TriedronAxeX[2] = TriedronOrigin[2] + 0.0;
747
748   TriedronAxeY[0] = TriedronOrigin[0] + 0.0;
749   TriedronAxeY[1] = TriedronOrigin[1] + L ;
750   TriedronAxeY[2] = TriedronOrigin[2] + 0.0;
751
752   TriedronAxeZ[0] = TriedronOrigin[0] + 0.0;
753   TriedronAxeZ[1] = TriedronOrigin[1] + 0.0;
754   TriedronAxeZ[2] = TriedronOrigin[2] + L ;
755
756   /* Check position in the ViewPort */
757   glGetDoublev( GL_MODELVIEW_MATRIX,  (GLdouble *) modelMatrix );
758   glGetDoublev( GL_PROJECTION_MATRIX, (GLdouble *) projMatrix );
759
760   glGetIntegerv(GL_VIEWPORT, aViewPort);
761   gluProject(TriedronOrigin[0], TriedronOrigin[1], TriedronOrigin[2],
762     (GLdouble *)modelMatrix, (GLdouble *)projMatrix, aViewPort,
763     &aWinCoord[0], &aWinCoord[1], &aWinCoord[2]);
764
765 #ifdef QTOCC_PATCH /* PCD 29/09/2008 */
766   /* Simple code modification recommended by Fotis Sioutis and Peter Dolbey  */
767   /* to remove the irritating default behaviour of triedrons using V3d_ZBUFFER   */
768   /* which causes the glyph to jump around the screen when the origin moves offscreen. */
769   isWithinView = GL_FALSE;
770 #else
771   /* Original code */
772   isWithinView = !((aWinCoord[0]<aViewPort[0]) || (aWinCoord[0]>aViewPort[2]) ||
773     (aWinCoord[1]<aViewPort[1]) || (aWinCoord[1]>aViewPort[3]));
774 #endif
775
776   if (!isWithinView) {
777     /* Annulate translation matrix */
778     modelMatrix[3][0] = 0. ;
779     modelMatrix[3][1] = 0. ;
780     modelMatrix[3][2] = 0. ; 
781     projMatrix[3][0] = 0. ;
782     projMatrix[3][1] = 0. ;
783     projMatrix[3][2] = 0. ; 
784
785     /* save matrix */
786     glMatrixMode (GL_MODELVIEW);
787     glPushMatrix ();
788     glLoadIdentity ();
789     glLoadMatrixd( (GLdouble *) modelMatrix);
790     glMatrixMode ( GL_PROJECTION );
791     glPushMatrix ();
792     glLoadIdentity();
793     glLoadMatrixd( (GLdouble *) projMatrix);
794
795
796     /*
797     * Define position in the view
798     */
799     switch (TriedronPosition) {
800       case 0 :  /* Aspect_TOTP_CENTER */
801         break;
802
803       case 1 :  /* Aspect_TOTP_LEFT_LOWER */
804         glTranslated( -U/2. + L , -V/2. + L , 0. );
805         break;
806
807       case 2  : /* Aspect_TOTP_LEFT_UPPER */
808         glTranslated( -U/2. + L , +V/2. - L -L/3. , 0. );
809         break;
810
811       case 3 :  /* Aspect_TOTP_RIGHT_LOWER */
812         glTranslated( U/2. - L -L/3. , -V/2. + L , 0. );
813         break;
814
815       case 4  :  /* Aspect_TOTP_RIGHT_UPPER */
816         glTranslated( U/2. - L -L/3. , +V/2. - L -L/3. , 0. );
817         break;
818
819       default :
820         break;
821
822     }
823     L *= aLengthReduce;
824   }
825
826
827   /* 
828   * Creation the trihedron
829   */
830
831 #define COLOR_REDUCE      0.3f
832 #define CYLINDER_LENGTH   0.75f
833 #ifdef BUG
834 #define ALPHA_REDUCE      0.4f
835 #else
836 #define ALPHA_REDUCE      1.0f
837 #endif
838
839   startList = glGenLists(4);
840   aQuadric = gluNewQuadric();
841
842   aCylinderLength = L * CYLINDER_LENGTH;
843   aCylinderDiametr = L * aAxisDiametr;
844   aConeDiametr = aCylinderDiametr * 2;
845   aConeLength = L * (1 - CYLINDER_LENGTH);
846   /* Correct for owerlapping */
847   /*    aCylinderLength += aConeLength - 1.2*aCylinderDiametr*aConeLength/aConeDiametr;*/
848
849   aIsDepthEnabled = glIsEnabled(GL_DEPTH_TEST);
850 #ifndef BUG
851
852 #ifdef QTOCC_PATCH  /*PCD 02/07/07   */
853   /* GL_DEPTH_WRITEMASK is not a valid argument to glIsEnabled, the  */
854   /* original code is shown to be broken when run under an OpenGL debugger  */
855   /* like GLIntercept. This is the correct way to retrieve the mask value.  */
856   glGetBooleanv(GL_DEPTH_WRITEMASK, &aIsDepthMaskEnabled); 
857 #else
858   aIsDepthMaskEnabled = glIsEnabled(GL_DEPTH_WRITEMASK);
859 #endif
860
861 #endif 
862
863   /* Create cylinder for axis */
864   gluQuadricDrawStyle(aQuadric, GLU_FILL); /* smooth shaded */
865   gluQuadricNormals(aQuadric, GLU_FLAT);
866   /* Axis */
867   glNewList(startList, GL_COMPILE);
868   gluCylinder(aQuadric, aCylinderDiametr, aCylinderDiametr, aCylinderLength, NbFacettes, 1);
869   glEndList();
870   /* Cone */
871   glNewList(startList + 1, GL_COMPILE);
872   gluCylinder(aQuadric, aConeDiametr, 0, aConeLength, NbFacettes, 1);
873   glEndList();
874   /* Central sphere */
875   glNewList(startList + 2, GL_COMPILE);
876 #ifdef QTOCC_PATCH
877   gluSphere(aQuadric, aCylinderDiametr * 2, NbFacettes, NbFacettes);
878 #else
879   gluSphere(aQuadric, aCylinderDiametr, NbFacettes, NbFacettes);
880 #endif
881   glEndList();
882   /* End disk */
883   gluQuadricOrientation(aQuadric,GLU_INSIDE); /*szv*/
884   glNewList(startList + 3, GL_COMPILE);
885   gluDisk(aQuadric, aCylinderDiametr, aConeDiametr, NbFacettes, 1/*szv:2*/);
886   glEndList();
887
888 #ifdef QTOCC_PATCH
889   /* Store previous attributes */
890   glPushAttrib(GL_LIGHTING_BIT | GL_POLYGON_BIT);
891   LightOn();
892 #else
893   LightOn();
894
895   /* Store previous attributes */
896   glPushAttrib(GL_LIGHTING_BIT | GL_POLYGON_BIT);
897 #endif
898
899   glCullFace(GL_BACK);
900   glEnable(GL_CULL_FACE);
901
902 #ifdef QTOCC_PATCH /*Fotis Sioutis | 2008-01-21 10:55 
903   In the function call_zbuffer_triedron_redraw of TKOpengl, 
904   the z buffered trihedron changes colors in case there 
905   is an object in the scene that has an explicit material 
906   attached to it.In the trihedron display loop, 
907   GL_COLOR_MATERIAL is enabled, but only the GL_DIFFUSE 
908   parameter is utilized in glColorMaterial(...).
909   This causes the last ambient,specular and emission values 
910   used, to stay at the stack and applied to the trihedron
911   (which causes the color change).
912   A fix is proposed , to change GL_DIFFUSE to 
913   GL_AMBIENT_AND_DIFFUSE in glColorMaterial call in 
914   line 946.The above of course will leave unchanged 
915   the SPECULAR and EMISSION values.
916   Another proposal which would fix 100% the problem 
917   is to use glMaterial instead of glColor on the trihedron 
918   drawing loop.               */
919   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, aNULLColor);
920   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, aNULLColor);
921   glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, aNULLColor);
922
923   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.);
924 #endif
925
926   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
927   glEnable(GL_COLOR_MATERIAL);
928
929   if (!aIsDepthEnabled)  {
930     glEnable(GL_DEPTH_TEST);
931 #ifndef BUG
932     glClear(GL_DEPTH_BUFFER_BIT);
933 #endif
934   }
935 #ifdef BUG
936   if (!(aIsDepthEnabled && isWithinView))
937     glClear(GL_DEPTH_BUFFER_BIT);
938 #endif
939 #ifndef BUG
940   if (!aIsDepthMaskEnabled)  {
941     /* This is how the depthmask needs to be re-enabled...*/
942     glDepthMask(GL_TRUE);
943     /* ...and not this stuff below */
944   }
945 #endif
946
947   glMatrixMode(GL_MODELVIEW);
948 #ifdef QTOCC_PATCH /* PCD 17/06/07  */
949   glGetIntegerv (GL_DEPTH_FUNC, &df); 
950 #else
951   /*szv:if (isWithinView) {*/
952   glDepthFunc(GL_GREATER);
953   glPushMatrix();
954   glPushMatrix();
955   glPushMatrix();
956
957   glColor4f(TriedronColor[0]*COLOR_REDUCE, 
958     TriedronColor[1]*COLOR_REDUCE, 
959     TriedronColor[2]*COLOR_REDUCE,
960     ALPHA_REDUCE);
961   glCallList(startList+2);
962
963   /* Z axis */
964   glColor4f(aZColor[0]*COLOR_REDUCE, 
965     aZColor[1]*COLOR_REDUCE, 
966     aZColor[2]*COLOR_REDUCE,
967     ALPHA_REDUCE);
968   glCallList(startList);
969   glTranslated(0, 0, L * CYLINDER_LENGTH);
970   glCallList(startList + 3);
971   glCallList(startList + 1);
972   glPopMatrix();    
973
974   /* X axis */
975   glRotated(90.0, TriedronAxeY[0], TriedronAxeY[1], TriedronAxeY[2]);
976   glColor4f(aXColor[0]*COLOR_REDUCE, 
977     aXColor[1]*COLOR_REDUCE, 
978     aXColor[2]*COLOR_REDUCE,
979     ALPHA_REDUCE);
980   glCallList(startList);
981   glTranslated(0, 0, L * CYLINDER_LENGTH);
982   glCallList(startList + 3);
983   glCallList(startList + 1);
984   glPopMatrix();    
985
986   /* Y axis */
987   glRotated(-90.0, TriedronAxeX[0], TriedronAxeX[1], TriedronAxeX[2]);
988   glColor4f(aYColor[0]*COLOR_REDUCE, 
989     aYColor[1]*COLOR_REDUCE, 
990     aYColor[2]*COLOR_REDUCE,
991     ALPHA_REDUCE);
992   glCallList(startList);
993   glTranslated(0, 0, L * CYLINDER_LENGTH);
994   glCallList(startList + 3);
995   glCallList(startList + 1);
996   glPopMatrix();
997
998   glDepthFunc(GL_LESS);
999   /*szv:}*/
1000 #endif
1001
1002 #ifdef QTOCC_PATCH
1003   for (i = 0; i < 2; i++) /* PCD 11/02/08 Two pass method */
1004   {
1005     if (i == 0) /*  First pass  */
1006     {                          
1007       glDepthFunc(GL_ALWAYS); 
1008     }
1009     else
1010     {
1011       glDepthFunc(GL_LEQUAL); 
1012     }
1013 #endif
1014
1015     glPushMatrix();
1016     glPushMatrix();
1017     glPushMatrix();
1018
1019     glColor3fv(TriedronColor);
1020     glCallList(startList+2);
1021
1022     /* Z axis */
1023     glColor4fv(aZColor);
1024     glCallList(startList);
1025     glTranslated(0, 0, L * CYLINDER_LENGTH);
1026     glCallList(startList + 3);
1027     glCallList(startList + 1);
1028     glPopMatrix();    
1029
1030     /* X axis */
1031     glRotated(90.0, TriedronAxeY[0], TriedronAxeY[1], TriedronAxeY[2]);
1032     glColor4fv(aXColor);
1033     glCallList(startList);
1034     glTranslated(0, 0, L * CYLINDER_LENGTH);
1035     glCallList(startList + 3);
1036     glCallList(startList + 1);
1037     glPopMatrix();    
1038
1039     /* Y axis */
1040     glRotated(-90.0, TriedronAxeX[0], TriedronAxeX[1], TriedronAxeX[2]);
1041     glColor4fv(aYColor);
1042     glCallList(startList);
1043     glTranslated(0, 0, L * CYLINDER_LENGTH);
1044     glCallList(startList + 3);
1045     glCallList(startList + 1);
1046     glPopMatrix();
1047
1048 #ifdef QTOCC_PATCH
1049   }
1050 #endif
1051
1052   if (!aIsDepthEnabled) 
1053     glDisable(GL_DEPTH_TEST);
1054 #ifndef BUG
1055   if (!aIsDepthMaskEnabled)
1056
1057 #ifdef QTOCC_PATCH /*PCD 02/07/07   */
1058     glDepthMask(GL_FALSE);
1059 #else
1060     glDisable(GL_DEPTH_WRITEMASK);
1061 #endif
1062
1063 #endif
1064   glDisable(GL_CULL_FACE);
1065   glDisable(GL_COLOR_MATERIAL);
1066
1067   gluDeleteQuadric(aQuadric);
1068   glColor3fv (TriedronColor);
1069
1070 #ifdef QTOCC_PATCH /* PCD 11/02/08 */
1071   /* Always write the text */
1072   glDepthFunc(GL_ALWAYS); 
1073 #endif
1074
1075   glPopAttrib();
1076
1077   /* fleches au bout des axes (= cones de la couleur demandee) */
1078   l = L - L/4. ; /* distance a l'origine */
1079   rayon = L/30. ; /* rayon de la base du cone */
1080   Angle = 2. * M_PI/ NbFacettes;
1081
1082   glDeleteLists(startList, 4); 
1083
1084   LightOff();
1085
1086   /* 
1087   * origine names
1088   */
1089
1090   /* init font */
1091
1092   OpenGl_TextRender* textRender=OpenGl_TextRender::instance();
1093
1094   /* Axe X */
1095   strcpy ( AxeName, "X\0" );
1096   TriedronCoord[0] = TriedronOrigin[0] + ( L + rayon ) ;
1097   TriedronCoord[1] = TriedronOrigin[1] + 0.0;
1098   TriedronCoord[2] = TriedronOrigin[2] - rayon ;
1099
1100   textRender->RenderText(AxeName, fontBase, 0, (float)TriedronCoord[0], (float)TriedronCoord[1], (float)TriedronCoord[2]);
1101
1102   /* Axe Y */
1103   strcpy ( AxeName, "Y\0" );
1104   TriedronCoord[0] = TriedronOrigin[0] + rayon ;
1105   TriedronCoord[1] = TriedronOrigin[1] + ( L + 3.0 * rayon ) ;
1106   TriedronCoord[2] = TriedronOrigin[2] + ( 2.0 * rayon );
1107
1108   textRender->RenderText(AxeName, fontBase, 0, (float)TriedronCoord[0], (float)TriedronCoord[1], (float)TriedronCoord[2]);
1109
1110   /* Axe Z */
1111   strcpy ( AxeName, "Z\0" );
1112   TriedronCoord[0] = TriedronOrigin[0] + ( - 2.0 * rayon ) ;
1113   TriedronCoord[1] = TriedronOrigin[1] +  rayon/2. ;
1114   TriedronCoord[2] = TriedronOrigin[2] + ( L + 3.0 * rayon ) ;
1115   textRender->RenderText(AxeName, fontBase, 0, (float)TriedronCoord[0], (float)TriedronCoord[1], (float)TriedronCoord[2]);
1116
1117 #ifdef QTOCC_PATCH 
1118   /*PCD 17/06/07    */
1119   glDepthFunc(df);
1120
1121   /* PCD 10/02/08  */
1122   /* Recover the clip planes */ 
1123   glGetIntegerv( GL_MAX_CLIP_PLANES, &max_plane);
1124   for (i = 0; i < max_plane ; i++) {
1125     if (isPlaneActive[i]) { 
1126       glEnable(GL_CLIP_PLANE0 + i);
1127     }
1128   }
1129   free(isPlaneActive);
1130 #endif
1131
1132   if (!isWithinView) { /* restore matrix */
1133     glMatrixMode (GL_PROJECTION);
1134     glPopMatrix ();
1135     glMatrixMode (GL_MODELVIEW);
1136     glPopMatrix ();
1137   }
1138
1139   return (TSuccess);
1140
1141 }
1142
1143
1144 /*----------------------------------------------------------------------*/
1145
1146 /*----------------------------------------------------------------------*/
1147 /*
1148 * Fonctions publiques 
1149 */
1150
1151
1152 /*
1153 * initialisation d'un triedre non zoomable dans une vue.
1154 * ou modification des valeurs deja initialisees.
1155 */
1156
1157 TStatus call_triedron_init (
1158                             CALL_DEF_VIEW * aview, 
1159                             int aPosition, 
1160                             float r,
1161                             float g,
1162                             float b, 
1163                             float aScale,
1164                             int asWireframe 
1165                             )
1166
1167 {
1168
1169   int nz_wks_entry;
1170   int nz_struc_entry;
1171   ZBUF_STRUCT* aParam;
1172
1173
1174 #ifdef PRINT
1175   printf("\n----------- call_triedron_init  r = %f, g = %f, b = %f",
1176     r, g, b);
1177   printf(", aScale = %f, aPosition = %d \n", aScale, aPosition );
1178 #endif
1179
1180   if (aview->WsId == -1) return(TFailure);
1181   if (aview->ViewId == -1) return(TFailure);
1182 #ifdef PRINT
1183   printf(", aview->WsId=%d  aview->ViewId=%d \n",aview->WsId,aview->ViewId);
1184 #endif
1185
1186
1187
1188   /* recherche du num de la liste de structures non zoomables de la wks */
1189   /* creation sinon */
1190   nz_wks_entry = find_nz_wks(aview->WsId, 1);
1191   if (nz_wks_entry == -1) return TFailure;
1192
1193   /* recherche du Triedre s'il existe; creation sinon */
1194   nz_struc_entry = find_nz_struc(nz_wks_entry, TRIEDRON_ID, 1);
1195   if (nz_struc_entry == -1) return TFailure;
1196
1197   /* mise a jour du Triedre */
1198   nz_wks[nz_wks_entry].triedron_on = 1;
1199   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].NZStrucID = TRIEDRON_ID;
1200   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].astructure = NULL;
1201   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aPos = aPosition;
1202   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[0] = r;
1203   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[1] = g;
1204   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aColor[2] = b;
1205   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aScale = aScale;
1206   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].isWireframe = asWireframe;
1207
1208   nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aZBufParam = new ZBUF_STRUCT();
1209   if (nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aZBufParam == NULL) return TFailure;
1210
1211   aParam = nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].aZBufParam;
1212   aParam->aXColor[0] = theXColor[0];
1213   aParam->aXColor[1] = theXColor[1];
1214   aParam->aXColor[2] = theXColor[2];
1215
1216   aParam->aYColor[0] = theYColor[0];
1217   aParam->aYColor[1] = theYColor[1];
1218   aParam->aYColor[2] = theYColor[2];
1219
1220   aParam->aZColor[0] = theZColor[0];
1221   aParam->aZColor[1] = theZColor[1];
1222   aParam->aZColor[2] = theZColor[2];
1223
1224   aParam->aRatio = theRatio;
1225   aParam->aDiametr = theDiametr;
1226   aParam->aNbFacettes = theNBFacettes;
1227
1228
1229 #ifdef DEBUG
1230   printf("nz_wks_entry=%d nz_struc_entry=%d \n",nz_wks_entry,nz_struc_entry);
1231   printf("ajout ok\n");
1232 #endif
1233   return (TSuccess);
1234
1235 }
1236
1237 /*----------------------------------------------------------------------*/
1238
1239 /*
1240 * affichage d'un triedre non zoomable dans la wks  awsid 
1241 *
1242 * Triedre = Objet non Zoomable;
1243 * on cree cette fonction pour pouvoir travailler par les structures 
1244 * utilisees par les fonctions Tsm* et TEL_VIEW_REP
1245 *
1246 */
1247
1248 TStatus call_triedron_redraw_from_wsid (
1249                                         Tint awsid
1250                                         )
1251
1252 {
1253
1254   TStatus   status = TSuccess;
1255   int       nz_wks_entry;
1256   int       nz_struc_entry;
1257   int    save_texture_state;
1258   GLdouble  U, V ; /* largeur,hauteur de la fenetre */
1259
1260   CMN_KEY_DATA    key;
1261   tel_view_data   vptr;
1262
1263   if ( awsid == -1) return (TFailure );
1264
1265 #ifdef BUC61045
1266   /* check if GL_LIGHTING should be disabled
1267   no enabling 'cause it will be done (if necessary: kinda Polygon types ) 
1268   during redrawing structures 
1269   */
1270   TsmGetWSAttri (awsid, WSGLLight, &key );
1271   if ( key.ldata == TOff )
1272     glDisable( GL_LIGHTING );
1273 #endif
1274
1275   /* recherche du numero de la liste de structures non zoomables de la wks */
1276 #ifdef PRINT
1277   printf("\n----------- call_triedron_redraw_from_WSID : appel find_nz_wks \n");
1278 #endif
1279   nz_wks_entry = find_nz_wks(awsid, 0);
1280   /* si on ne l'a pas trouve, il n'y a rien a faire */
1281   if (nz_wks_entry == -1) return (TSuccess);
1282
1283   /* recherche du Triedre */
1284 #ifdef PRINT
1285   printf("\n----------- call_triedron_redraw_from_WSID : appel find_nz_struc \n");
1286 #endif
1287   nz_struc_entry = find_nz_struc(nz_wks_entry, TRIEDRON_ID, 0);
1288   /* si on ne l'a pas trouve, il n'y a rien a faire */
1289   if (nz_struc_entry == -1) return (TSuccess);
1290   /* si il est "off" il n'y a rien a faire */
1291   if (nz_wks[nz_wks_entry].triedron_on == 0) return (TSuccess);
1292
1293
1294   /* recherche de la dimension de la fenetre                   */
1295   /* (car la taille des axes du treiedre en sera 1 proportion) */
1296   TsmGetWSAttri (awsid, WSViews, &key );
1297   vptr = (tel_view_data)key.pdata ; /* Obtain defined view data*/
1298   if ( !vptr ) return TFailure; /* no view defined yet */
1299   U = vptr->vrep.extra.map.window.xmax - vptr->vrep.extra.map.window.xmin;
1300   V = vptr->vrep.extra.map.window.ymax - vptr->vrep.extra.map.window.ymin;
1301
1302   /* sauvegarde du contexte (on reste dans le buffer courant) */
1303   save_texture_state = IsTextureEnabled();
1304   DisableTexture();
1305
1306   /* affichage du Triedre Non Zoomable */
1307   transform_persistence_end();
1308   if (nz_wks[nz_wks_entry].nz_struc[nz_struc_entry].isWireframe)
1309     status = call_triedron_redraw (nz_wks_entry, nz_struc_entry, U, V);
1310   else
1311     status = call_zbuffer_triedron_redraw (nz_wks_entry, nz_struc_entry, U, V);
1312
1313   /* restauration du contexte */
1314   if (save_texture_state) EnableTexture();
1315
1316   return status;
1317
1318 }
1319
1320 /*----------------------------------------------------------------------*/
1321
1322 /*
1323 * affichage d'un triedre non zoomable dans la vue aview 
1324 *
1325 * Triedre = Objet non Zoomable;
1326 * on cree cette fonction pour pouvoir utiliser CALL_DEF_VIEW
1327 *
1328 */
1329
1330 TStatus call_triedron_redraw_from_view (
1331                                         CALL_DEF_VIEW * aview
1332                                         )
1333 {
1334
1335   TStatus   status = TSuccess;
1336   int       nz_wks_entry;
1337   int       nz_struc_entry;
1338   int    save_texture_state;
1339   GLdouble  U, V ; /* largeur,hauteur de la fenetre */
1340
1341
1342
1343   if (aview->WsId == -1) return (TFailure );
1344   if (aview->ViewId == -1) return (TFailure);
1345 #ifdef PRINT
1346   printf("\n call_triedron_redraw_from_VIEW aview->WsId=%d  aview->ViewId=%d \n",aview->WsId,aview->ViewId);
1347 #endif
1348
1349
1350   /* recherche du numero de la liste de structures non zoomables de la wks */
1351 #ifdef PRINT
1352   printf("\n----------- call_triedron_redraw_from_VIEW : appel find_nz_wks \n");
1353 #endif
1354   nz_wks_entry = find_nz_wks(aview->WsId, 0);
1355   /* si on ne l'a pas trouve, il n'y a rien a faire */
1356   if (nz_wks_entry == -1) return (TSuccess);
1357
1358   /* recherche du Triedre */
1359 #ifdef PRINT
1360   printf("\n----------- call_triedron_redraw_from_VIEW : appel find_nz_struc \n");
1361 #endif
1362   nz_struc_entry = find_nz_struc(nz_wks_entry, TRIEDRON_ID, 0);
1363   /* si on ne l'a pas trouve, il n'y a rien a faire */
1364   if (nz_struc_entry == -1) return (TSuccess);
1365   /* si il est "off" il n'y a rien a faire */
1366   if (nz_wks[nz_wks_entry].triedron_on == 0) return (TSuccess);
1367
1368
1369   /* recherche de la dimension de la fenetre                   */
1370   /* (car la taille des axes du treiedre en sera 1 proportion) */
1371   U = aview->Mapping.WindowLimit.uM - aview->Mapping.WindowLimit.um ;
1372   V = aview->Mapping.WindowLimit.vM - aview->Mapping.WindowLimit.vm ;
1373
1374   /* sauvegarde du contexte et ecriture en front buffer */
1375   save_texture_state = IsTextureEnabled();
1376   DisableTexture();
1377   glDrawBuffer (GL_FRONT);
1378
1379   /* affichage du Triedre Non Zoomable */
1380   transform_persistence_end();
1381   status = call_triedron_redraw (nz_wks_entry, nz_struc_entry, U, V);
1382
1383   /* necessaire pour WNT */
1384   glFlush();
1385
1386   /* restauration du contexte */
1387   if (save_texture_state) EnableTexture();
1388   glDrawBuffer (GL_BACK); 
1389
1390   return status;
1391
1392 }
1393
1394 /*----------------------------------------------------------------------*/
1395
1396 /*
1397 * destruction du triedre non zoomable d'une vue.
1398 */
1399
1400 TStatus call_triedron_erase (
1401                              CALL_DEF_VIEW * aview
1402                              )
1403
1404 {
1405   int   nz_wks_entry;
1406   int i;
1407
1408
1409 #ifdef PRINT
1410   printf("\n----------- call_triedron_erase  " );
1411 #endif
1412
1413   if (aview->WsId == -1) return(TFailure);
1414   if (aview->ViewId == -1) return(TFailure);
1415 #ifdef PRINT
1416   printf(", aview->WsId=%d  aview->ViewId=%d \n",aview->WsId,aview->ViewId);
1417 #endif
1418
1419
1420   /* recherche du num de la liste de structures non zoomables de la wks */
1421   /* si on ne l'a pas trouve, il n'y a rien a faire                     */
1422   nz_wks_entry = find_nz_wks(aview->WsId, 0);
1423   if (nz_wks_entry == -1) return TSuccess;
1424
1425
1426   /* 
1427   * destruction du Triedre puis de toute la wks
1428   */
1429   /* (aujourd'hui il n'y a pas d'autre structure graphique dans la table */
1430   /* de la wks => on detruit tout ;                                      */
1431   /* si on ajoutait d'autres structures non zoomables, il faudrait       */
1432   /* selectionner et detruire uniquement l'element Triedre en cherchant  */
1433   /* s'il existe via une variable nz_struc_entry ).                      */
1434   for (i = 0; i < nz_wks[nz_wks_entry].nz_struc_count; i++) {
1435     if (nz_wks[nz_wks_entry].nz_struc[i].aZBufParam != NULL) 
1436       free(nz_wks[nz_wks_entry].nz_struc[i].aZBufParam);
1437   }
1438
1439   if (nz_wks[nz_wks_entry].nz_struc != NULL) {
1440     free(nz_wks[nz_wks_entry].nz_struc);
1441   }
1442
1443   if (nz_wks_count == 1) {
1444     free(nz_wks);
1445     nz_wks_count = 0;
1446     nz_wks_size = 0;
1447   }
1448   else { /* il y a au moins 2 wks definies */
1449     memcpy(&nz_wks[nz_wks_entry],
1450       &nz_wks[nz_wks_entry+1],
1451       (nz_wks_count - nz_wks_entry - 1)*sizeof(NZ_WKS));
1452     nz_wks_count--;
1453   }
1454
1455
1456 #ifdef DEBUG
1457   printf("nz_wks_entry=%d   nz_wks_count=%d   nz_wks_size=%d\n",
1458     nz_wks_entry,  nz_wks_count, nz_wks_size );
1459   printf("erase ok\n");
1460 #endif
1461
1462   return (TSuccess);
1463 }
1464
1465 /*----------------------------------------------------------------------*/
1466
1467
1468 /*
1469 * gestion d'un echo de designation du triedre non zoomable d' une vue.
1470 */
1471 TStatus call_triedron_echo (
1472                             CALL_DEF_VIEW * aview,
1473                             int aType
1474                             )
1475
1476 {
1477   return (TSuccess);
1478 }
1479
1480
1481 /*----------------------------------------------------------------------*/
1482
1483 /*
1484 * initialisation of zbuffer trihedron
1485 */
1486 extern TStatus  call_ztriedron_setup (
1487                                       float* xcolor,
1488                                       float* ycolor,
1489                                       float* zcolor,
1490                                       float  sizeratio,
1491                                       float  axisdiameter,
1492                                       int    nbfacettes)
1493 {
1494   theXColor[0] = xcolor[0];
1495   theXColor[1] = xcolor[1];
1496   theXColor[2] = xcolor[2];
1497
1498   theYColor[0] = ycolor[0];
1499   theYColor[1] = ycolor[1];
1500   theYColor[2] = ycolor[2];
1501
1502   theZColor[0] = zcolor[0];
1503   theZColor[1] = zcolor[1];
1504   theZColor[2] = zcolor[2];
1505
1506   theRatio = sizeratio;
1507   theDiametr = axisdiameter;
1508   theNBFacettes = nbfacettes;
1509
1510   return (TSuccess);
1511 }
1512