0022962: Invalid realization of reading and writing material in STEP.
[occt.git] / src / OpenGl / OpenGl_text.cxx
CommitLineData
7fd59977 1/***********************************************************************
2
3FONCTION :
4----------
5File OpenGl_text :
6
7
8REMARQUES:
9----------
10
11
12HISTORIQUE DES MODIFICATIONS :
13--------------------------------
14xx-xx-xx : xxx ; Creation.
1505-03-96 : FMN ; Suppression code inutile:
1601-04-96 : CAL ; Integration MINSK portage WNT
1729-04-96 : FMN ; correction warning de compilation.
1821-10-96 : FMN ; Suppression LMC_COLOR fait dans OpenGl_execstruct.c
1930-11-98 : FMN ; S4069 : Textes toujours visibles
2002.14.100 : JR : Warnings on WNT truncations from double to float
2121.06.03 : SAN : Suppress text display while in animation mode (TEXT_DEGENER)
2222.01.04 : SAN : Implement texture mapped fonts on WNT only (OCC2934)
23
24************************************************************************/
25
26/*----------------------------------------------------------------------*/
27/*
28* Includes
29*/
30
31#ifndef WNT
32#include <X11/Xlib.h>
33#endif
34#include <OpenGl_tgl_all.hxx>
35
36#include <stddef.h>
37#include <string.h>
38#include <stdio.h>
39#include <GL/gl.h>
40#include <GL/glu.h>
41
42#include <OpenGl_cmn_varargs.hxx>
43#include <OpenGl_telem_attri.hxx>
44#include <OpenGl_tsm.hxx>
45#include <OpenGl_telem.hxx>
46#include <OpenGl_telem_highlight.hxx>
47#include <OpenGl_telem_inquire.hxx>
48#include <OpenGl_tgl_funcs.hxx>
49#include <OpenGl_Extension.hxx>
50#include <OpenGl_Memory.hxx>
51
7fd59977 52#include <OpenGl_TextRender.hxx>
53
13a22457
S
54struct TEL_TEXT_DATA
55{
56 TEL_POINT attach_pt;
57 Tint length;
58 const wchar_t *sdata;
59 IMPLEMENT_MEMORY_OPERATORS
60};
61typedef TEL_TEXT_DATA* tel_text_data;
62
7fd59977 63/*----------------------------------------------------------------------*/
64/*
65* Fonctions statiques
66*/
67
68static TStatus TextDisplay( TSM_ELEM_DATA, Tint, cmn_key* );
69static TStatus TextAdd( TSM_ELEM_DATA, Tint, cmn_key* );
70static TStatus TextDelete( TSM_ELEM_DATA, Tint, cmn_key* );
71static TStatus TextPrint( TSM_ELEM_DATA, Tint, cmn_key* );
72static TStatus TextInquire( TSM_ELEM_DATA, Tint, cmn_key* );
73
74#if defined(__sun) || defined (__osf__) || defined (__hp) || defined (__sgi)
75#else
76PFNGLBLENDEQUATIONEXTPROC glBlendEquationOp = (PFNGLBLENDEQUATIONEXTPROC) INVALID_EXT_FUNCTION_PTR;
77#endif
78/*----------------------------------------------------------------------*/
79/*
80* Constantes
81*/
82
83#if defined(__osf__) || defined (__sun) || defined (__sgi) || defined(__hp9000s700) || defined(WNT)
84#undef GL_EXT_blend_logic_op
85#endif
86
87/*----------------------------------------------------------------------*/
88/*
89* Variables statiques
90*/
91
92/*static GLboolean lighting_mode;*/
93
94static TStatus (*MtdTbl[])( TSM_ELEM_DATA, Tint, cmn_key* ) =
95{
96 TextDisplay,
97 TextDisplay,
98 TextAdd,
99 TextDelete,
100 TextPrint,
101 TextInquire
102};
103
104extern GLuint fontBase;
105extern GLuint printerFontBase;
106
107#define TEXT_DEGENER
108
109#ifdef TEXT_DEGENER
110extern int g_nDegenerateModel;
111extern float g_fSkipRatio;
112#endif
113
7fd59977 114/*----------------------------------------------------------------------*/
115
116MtblPtr
117TelTextInitClass( TelType* el )
118{
119 *el = TelText;
120 return MtdTbl;
121}
122
123/*----------------------------------------------------------------------*/
124
125static TStatus
126TextAdd( TSM_ELEM_DATA d, Tint n, cmn_key *k )
127{
128 Tint i;
129 TEL_POINT def_pt = {{ ( float )0.0, ( float )0.0, ( float )0.0 }};
130 tel_point pt = &def_pt;
131 tel_text_data data;
13a22457 132 Techar *str= 0;
7fd59977 133
134 for( i = 0; i < n; i++ )
135 {
136 switch( k[i]->id )
137 {
138 case TEXT_ATTACH_PT_ID:
139 pt = (tel_point)k[i]->data.pdata;
140 break;
141 case TEXT_STRING_ID:
13a22457 142 str = (Techar*)k[i]->data.pdata;
7fd59977 143 break;
144 }
145 }
146
7fd59977 147 data = new TEL_TEXT_DATA();
148 if( !data )
149 return TFailure;
13a22457
S
150
151 //szv: instead of strlen + 1
152 i = 0; while (str[i++]);
153
154 wchar_t *wstr = new wchar_t[i];
155 if( !wstr )
7fd59977 156 return TFailure;
157
158 data->attach_pt = *pt;
159 data->length = i;
13a22457
S
160 //szv: instead of memcpy
161 i = 0; while (wstr[i++] = (wchar_t)(*str++));
162 data->sdata = wstr;
7fd59977 163
164 ((tsm_elem_data)(d.pdata))->pdata = data;
165
166 return TSuccess;
167}
168
169/*----------------------------------------------------------------------*/
170
171static TStatus
172TextDisplay( TSM_ELEM_DATA data, Tint n, cmn_key *k )
173{
174 tel_text_data d;
175 CMN_KEY key, key1;
176 TEL_COLOUR colour, colours;
177 int style, display_type;
178 GLboolean flag_zbuffer = GL_FALSE;
179 GLboolean blend_state = GL_FALSE;
180 GLint sWidth, sAscent, sDescent;
181 GLdouble modelMatrix[16], projMatrix[16];
182 GLint viewport[4];
183 GLdouble objrefX, objrefY, objrefZ;
184 GLdouble objX, objY, objZ;
185 GLdouble obj1X, obj1Y, obj1Z;
186 GLdouble obj2X, obj2Y, obj2Z;
187 GLdouble obj3X, obj3Y, obj3Z;
188 GLdouble winx1, winy1, winz1;
189 GLdouble winx, winy, winz;
190 GLint status;
191
192#ifdef TEXT_DEGENER
193 if ( g_nDegenerateModel > 0 && g_fSkipRatio == 1. )
194 return TSuccess;
195#endif
196
197 d = (tel_text_data)data.pdata;
198
199 if( k[0]->id == TOn )
200 { /* Use highlight colours */
201 TEL_HIGHLIGHT hrep;
202
203 key.id = TelHighlightIndex;
204 TsmGetAttri( 1, &key );
205 if( TelGetHighlightRep( TglActiveWs, key.data.ldata, &hrep )
206 == TSuccess )
207 colour = hrep.col;
208 else
209 {
210 TelGetHighlightRep( TglActiveWs, 0, &hrep );
211 colour = hrep.col;
212 }
213 }
214 else
215 {
216 key.id = TelTextColour;
217 key.data.pdata = &colour;
218 TsmGetAttri( 1, &key );
219 }
220
221 key.id = TelTextStyle;
222 key1.id = TelTextDisplayType;
223 TsmGetAttri( 2, &key, &key1 );
224 style = key.data.ldata;
225 display_type = key1.data.ldata;
226
227 /* style annotation */
228 if (style == ASPECT_TOST_ANNOTATION)
229 {
230 flag_zbuffer = glIsEnabled(GL_DEPTH_TEST);
231 if (flag_zbuffer) glDisable(GL_DEPTH_TEST);
232 }
233
13a22457
S
234 OpenGl_TextRender* textRender=OpenGl_TextRender::instance();
235
7fd59977 236 /* display type of text */
237 if (display_type != ASPECT_TODT_NORMAL)
238 {
239 key.id = TelTextColourSubTitle;
240 key.data.pdata = &colours;
241 TsmGetAttri( 1, &key );
242
243 /* Optimisation: il faudrait ne faire le Get qu'une fois par Redraw */
244 glGetIntegerv (GL_VIEWPORT, viewport);
245 glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
246 glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
247
7fd59977 248 switch (display_type)
249 {
250 case ASPECT_TODT_BLEND:
251
252#if defined(GL_EXT_blend_logic_op)
253 if ( (PFNGLBLENDEQUATIONEXTPROC)glBlendEquationOp == (PFNGLBLENDEQUATIONEXTPROC)INVALID_EXT_FUNCTION_PTR )
254 {
255 if(QueryExtension("GL_EXT_blend_logic_op")) {
256#ifdef WNT
257 glBlendEquationOp = (PFNGLBLENDEQUATIONEXTPROC)wglGetProcAddress("glBlendEquationOp");
258#else
259 glBlendEquationOp = (PFNGLBLENDEQUATIONEXTPROC)glXGetProcAddress((GLubyte*)"glBlendEquationOp");
260#endif
261 } else
262 glBlendEquationOp = NULL;
263 }
264
265 if ( glBlendEquationOp )
266 {
267 blend_state = glIsEnabled(GL_BLEND);
268 if (!blend_state) glEnable(GL_BLEND);
269 glBlendEquationOp(GL_LOGIC_OP);
270 glLogicOp(GL_XOR);
271 }
272#else
273
274 blend_state = glIsEnabled(GL_BLEND);
275 if (!blend_state) glEnable(GL_BLEND);
276 glEnable(GL_COLOR_LOGIC_OP);
277 glLogicOp(GL_XOR);
278
279#endif
280 break;
281 case ASPECT_TODT_SUBTITLE:
13a22457 282 textRender->StringSize(d->sdata, &sWidth, &sAscent, &sDescent);
7fd59977 283 objrefX = (float)d->attach_pt.xyz[0];
284 objrefY = (float)d->attach_pt.xyz[1];
285 objrefZ = (float)d->attach_pt.xyz[2];
286 status = gluProject (objrefX, objrefY, objrefZ, modelMatrix, projMatrix, viewport,
287 &winx1, &winy1, &winz1);
288
289 winx = winx1;
290 winy = winy1-sDescent;
291 winz = winz1+0.00001;
292 status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
293 &objX, &objY, &objZ);
294
295 winx = winx1 + sWidth;
296 winy = winy1-sDescent;
297 winz = winz1+0.00001; /* il vaut mieux F+B / 1000000 ? */
298 status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
299 &obj1X, &obj1Y, &obj1Z);
300
301 winx = winx1 + sWidth;
302 winy = winy1 + sAscent;
303 winz = winz1+0.00001;
304 status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
305 &obj2X, &obj2Y, &obj2Z);
306
307 winx = winx1;
308 winy = winy1+ sAscent;
309 winz = winz1+0.00001;
310 status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
311 &obj3X, &obj3Y, &obj3Z);
312
313 glColor3fv( colours.rgb );
314 glBegin(GL_POLYGON);
315 glVertex3d(objX, objY, objZ);
316 glVertex3d(obj1X, obj1Y, obj1Z);
317 glVertex3d(obj2X, obj2Y, obj2Z);
318 glVertex3d(obj3X, obj3Y, obj3Z);
319 glEnd();
320 break;
321
322 case ASPECT_TODT_DEKALE:
323 objrefX = (float)d->attach_pt.xyz[0];
324 objrefY = (float)d->attach_pt.xyz[1];
325 objrefZ = (float)d->attach_pt.xyz[2];
326 status = gluProject (objrefX, objrefY, objrefZ, modelMatrix, projMatrix, viewport,
327 &winx1, &winy1, &winz1);
328
329 winx = winx1+1;
330 winy = winy1+1;
331 winz = winz1+0.00001;
332 status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
333 &objX, &objY, &objZ);
334
335 glColor3fv( colours.rgb );
13a22457 336 textRender->RenderText( d->sdata, fontBase, 0, (float)objX, (float)objY,(float)objZ );
7fd59977 337 winx = winx1-1;
338 winy = winy1-1;
339 status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
340 &objX, &objY, &objZ);
341
13a22457 342 textRender->RenderText( d->sdata, fontBase, 0, (float)objX, (float)objY,(float)objZ );
7fd59977 343 winx = winx1-1;
344 winy = winy1+1;
345 status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
346 &objX, &objY, &objZ);
347
13a22457 348 textRender->RenderText( d->sdata, fontBase, 0, (float)objX, (float)objY,(float)objZ );
7fd59977 349 winx = winx1+1;
350 winy = winy1-1;
351 status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
352 &objX, &objY, &objZ);
13a22457 353 textRender->RenderText( d->sdata, fontBase, 0, (float)objX, (float)objY,(float)objZ );
7fd59977 354 break;
355 }
356 }
357
358 glColor3fv( colour.rgb );
13a22457 359 textRender->RenderText( d->sdata, fontBase, 0, (float)d->attach_pt.xyz[0], (float)d->attach_pt.xyz[1],(float)d->attach_pt.xyz[2] );
7fd59977 360 /* maj attributs */
361 if (flag_zbuffer) glEnable(GL_DEPTH_TEST);
362 if (display_type == ASPECT_TODT_BLEND)
363 {
364#if defined(GL_EXT_blend_logic_op)
365 if ((!blend_state) && (QueryExtension("GL_EXT_blend_logic_op"))) glDisable(GL_BLEND);
366#else
367 if (!blend_state) glDisable(GL_BLEND);
368 glDisable(GL_COLOR_LOGIC_OP);
369#endif
370 }
371
372 return TSuccess;
373}
374
375/*----------------------------------------------------------------------*/
376
377static TStatus
378TextDelete( TSM_ELEM_DATA data, Tint n, cmn_key *k )
379{
13a22457
S
380 tel_text_data d = (tel_text_data)data.pdata;
381 if (d)
382 {
383 delete[] d->sdata;
384 delete d;
385 }
7fd59977 386 return TSuccess;
387}
388
389/*----------------------------------------------------------------------*/
390
391static TStatus
392TextPrint( TSM_ELEM_DATA data, Tint n, cmn_key *k )
393{
394 tel_text_data p;
395
396 p = (tel_text_data)data.pdata;
397
398 fprintf( stdout, "TelText.\n" );
13a22457 399 fprintf( stdout, "\t\tString : %S\n", p->sdata );
7fd59977 400 fprintf( stdout, "\t\tAttach Point : %f %f %f\n", p->attach_pt.xyz[0],
401 p->attach_pt.xyz[1],
402 p->attach_pt.xyz[2] );
403 fprintf( stdout, "\n" );
404
405 return TSuccess;
406}
407
408/*----------------------------------------------------------------------*/
409
410static TStatus
411TextInquire( TSM_ELEM_DATA data, Tint n, cmn_key *k )
412{
413 Tint i;
414 tel_text_data d;
415 Tint size_reqd=0;
416 TStatus status = TSuccess;
417
418 d = (tel_text_data)data.pdata;
419
13a22457 420 size_reqd = sizeof(Techar)*d->length;
7fd59977 421
422 for( i = 0; i < n; i++ )
423 {
424 switch( k[i]->id )
425 {
426 case INQ_GET_SIZE_ID:
427 {
428 k[i]->data.ldata = size_reqd;
429 break;
430 }
431
432 case INQ_GET_CONTENT_ID:
433 {
434 TEL_INQ_CONTENT *c;
435 Teldata *w;
436
437 c = (tel_inq_content)k[i]->data.pdata;
438 c->act_size = size_reqd;
439 w = c->data;
440
441 if( c->size >= size_reqd )
442 {
13a22457 443 w->atext3.string = (Techar*)c->buf;
7fd59977 444 w->atext3.ref_pt = d->attach_pt;
445 w->atext3.anno.xyz[0] = ( float )0.0;
446 w->atext3.anno.xyz[1] = ( float )0.0;
447 w->atext3.anno.xyz[2] = ( float )0.0;
13a22457
S
448 //szv: instead of strcpy
449 Techar *ptr1 = w->atext3.string;
450 const wchar_t *ptr2 = d->sdata;
451 while (*ptr1++ = (Techar)(*ptr2++));
7fd59977 452 status = TSuccess;
453 }
454 else
455 status = TFailure;
456 break;
457 }
458 }
459 }
460 return status;
461}
462
463/*----------------------------------------------------------------------*/