0022879: Possible bug in Opengl_togl_begin_layer_mode.cxx
[occt.git] / src / OpenGl / OpenGl_Text.cxx
1 // File:      OpenGl_Text.cxx
2 // Created:   13 July 2011
3 // Author:    Sergey ZERCHANINOV
4 // Copyright: OPEN CASCADE 2011
5
6 #include <OpenGl_Text.hxx>
7
8 #if (!defined(_WIN32) && !defined(__WIN32__))
9   #include <X11/Xlib.h>
10 #endif
11
12 #include <OpenGl_tgl_all.hxx>
13 #include <GL/gl.h>
14
15 #include <OpenGl_Memory.hxx>
16
17 #include <OpenGl_AspectText.hxx>
18 #include <OpenGl_Structure.hxx>
19
20 /*----------------------------------------------------------------------*/
21
22 OpenGl_Text::OpenGl_Text (const TCollection_ExtendedString& AText,
23                         const Graphic3d_Vertex& APoint,
24                         const Standard_Real AHeight,
25                         const Graphic3d_HorizontalTextAlignment AHta,
26                         const Graphic3d_VerticalTextAlignment AVta)
27 : myString(NULL)
28 {
29   const Techar *str = (const Techar *) AText.ToExtString();
30
31   //szv: instead of strlen + 1
32   int i = 0; while (str[i++]);
33
34   wchar_t *wstr = new wchar_t[i];
35
36   //szv: instead of memcpy
37   i = 0; while (wstr[i++] = (wchar_t)(*str++));
38   if (myString) delete[] myString;
39   myString = wstr;
40
41   Standard_Real X, Y, Z;
42   APoint.Coord(X, Y, Z);
43   myAttachPnt.xyz[0] = float (X);
44   myAttachPnt.xyz[1] = float (Y);
45   myAttachPnt.xyz[2] = float (Z);
46
47   myParam.Height = int (AHeight);
48
49   myParam.HAlign = AHta;
50   myParam.VAlign = AVta;
51 }
52
53 /*----------------------------------------------------------------------*/
54
55 OpenGl_Text::~OpenGl_Text ()
56 {
57   if (myString)
58     delete[] myString;
59 }
60
61 /*----------------------------------------------------------------------*/
62
63 void OpenGl_Text::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
64 {
65   if ( AWorkspace->DegenerateModel > 0 && AWorkspace->SkipRatio >= 1.f )
66     return;
67
68   const OpenGl_AspectText *aspect_text = AWorkspace->AspectText( Standard_True );
69
70   const TEL_COLOUR *tcolor, *scolor;
71
72   // Use highlight colours
73   if( AWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT )
74   {                         
75     tcolor = scolor = AWorkspace->HighlightColor;
76   }
77   else
78   {
79     tcolor = &aspect_text->Color();
80     scolor = &aspect_text->SubtitleColor();
81   }
82
83   // Handle annotation style
84   GLboolean flag_zbuffer = GL_FALSE;
85   if (aspect_text->StyleType() == Aspect_TOST_ANNOTATION)
86   {
87     flag_zbuffer = glIsEnabled(GL_DEPTH_TEST);
88     if (flag_zbuffer) glDisable(GL_DEPTH_TEST);
89   }
90
91   AWorkspace->SetTextParam(&myParam);
92
93   GLdouble        modelMatrix[16], projMatrix[16];
94   GLint           viewport[4];
95   GLdouble        objrefX, objrefY, objrefZ;
96   GLdouble        objX, objY, objZ;
97   GLdouble        obj1X, obj1Y, obj1Z;
98   GLdouble        obj2X, obj2Y, obj2Z;
99   GLdouble        obj3X, obj3Y, obj3Z;
100   GLdouble        winx1, winy1, winz1;
101   GLdouble        winx, winy, winz;
102   GLint           status;
103
104   /* display type of text */
105   if (aspect_text->DisplayType() != Aspect_TODT_NORMAL)
106   {
107     /* Optimisation: il faudrait ne faire le Get qu'une fois par Redraw */
108     glGetIntegerv (GL_VIEWPORT, viewport);
109     glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
110     glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
111
112     switch (aspect_text->DisplayType())
113     {
114     case Aspect_TODT_BLEND:
115       glEnable(GL_COLOR_LOGIC_OP);
116       glLogicOp(GL_XOR);
117       break;
118     case Aspect_TODT_SUBTITLE:
119     {
120       int sWidth, sAscent, sDescent;
121       AWorkspace->StringSize(myString, sWidth, sAscent, sDescent);
122
123       objrefX = (float)myAttachPnt.xyz[0];   
124       objrefY = (float)myAttachPnt.xyz[1];   
125       objrefZ = (float)myAttachPnt.xyz[2];
126       status = gluProject (objrefX, objrefY, objrefZ, modelMatrix, projMatrix, viewport,
127         &winx1, &winy1, &winz1);
128
129       winx = winx1;
130       winy = winy1-sDescent;
131       winz = winz1+0.00001;     
132       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
133         &objX, &objY, &objZ);
134
135       winx = winx1 + sWidth;
136       winy = winy1-sDescent;
137       winz = winz1+0.00001; /* il vaut mieux F+B / 1000000 ? */     
138       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
139         &obj1X, &obj1Y, &obj1Z);
140
141       winx = winx1 + sWidth;
142       winy = winy1 + sAscent;
143       winz = winz1+0.00001;     
144       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
145         &obj2X, &obj2Y, &obj2Z);
146
147       winx = winx1;
148       winy = winy1+ sAscent;
149       winz = winz1+0.00001;   
150       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
151         &obj3X, &obj3Y, &obj3Z);
152
153       glColor3fv( scolor->rgb );
154       glBegin(GL_POLYGON);
155       glVertex3d(objX, objY, objZ);
156       glVertex3d(obj1X, obj1Y, obj1Z);
157       glVertex3d(obj2X, obj2Y, obj2Z);
158       glVertex3d(obj3X, obj3Y, obj3Z);
159       glEnd();
160       break;
161     }
162
163     case Aspect_TODT_DEKALE:
164       objrefX = (float)myAttachPnt.xyz[0];   
165       objrefY = (float)myAttachPnt.xyz[1];   
166       objrefZ = (float)myAttachPnt.xyz[2];
167       status = gluProject (objrefX, objrefY, objrefZ, modelMatrix, projMatrix, viewport,
168         &winx1, &winy1, &winz1);
169
170       winx = winx1+1;
171       winy = winy1+1;
172       winz = winz1+0.00001;     
173       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
174         &objX, &objY, &objZ);
175
176       glColor3fv( scolor->rgb );
177       AWorkspace->RenderText( myString, 0, (float)objX, (float)objY,(float)objZ );
178       winx = winx1-1;
179       winy = winy1-1;
180       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
181         &objX, &objY, &objZ);
182
183       AWorkspace->RenderText( myString, 0, (float)objX, (float)objY,(float)objZ );
184       winx = winx1-1;
185       winy = winy1+1;
186       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
187         &objX, &objY, &objZ); 
188
189       AWorkspace->RenderText( myString, 0, (float)objX, (float)objY,(float)objZ );
190       winx = winx1+1;
191       winy = winy1-1;
192       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
193         &objX, &objY, &objZ);
194       AWorkspace->RenderText( myString, 0, (float)objX, (float)objY,(float)objZ );
195       break;
196     }
197   }
198
199   glColor3fv( tcolor->rgb );
200   AWorkspace->RenderText( myString, 0, (float)myAttachPnt.xyz[0], (float)myAttachPnt.xyz[1],(float)myAttachPnt.xyz[2] );
201   /* maj attributs */
202   if (flag_zbuffer) glEnable(GL_DEPTH_TEST);
203   if (aspect_text->DisplayType() == Aspect_TODT_BLEND)
204   {
205     glDisable(GL_COLOR_LOGIC_OP);
206   }
207 }
208
209 /*----------------------------------------------------------------------*/