0023246: TKOpenGl viewer created without depth buffer on some X11 implementations
[occt.git] / src / OpenGl / OpenGl_Text.cxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <OpenGl_GlCore11.hxx>
22 #include <OpenGl_Text.hxx>
23 #include <OpenGl_AspectText.hxx>
24 #include <OpenGl_Structure.hxx>
25
26 #include <GL/glu.h> // gluUnProject()
27
28 /*----------------------------------------------------------------------*/
29
30 OpenGl_Text::OpenGl_Text (const TCollection_ExtendedString& AText,
31                         const Graphic3d_Vertex& APoint,
32                         const Standard_Real AHeight,
33                         const Graphic3d_HorizontalTextAlignment AHta,
34                         const Graphic3d_VerticalTextAlignment AVta)
35 : myString(NULL)
36 {
37   const Techar *str = (const Techar *) AText.ToExtString();
38
39   //szv: instead of strlen + 1
40   int i = 0; while (str[i++]);
41
42   wchar_t *wstr = new wchar_t[i];
43
44   //szv: instead of memcpy
45   i = 0; while (wstr[i++] = (wchar_t)(*str++));
46   if (myString) delete[] myString;
47   myString = wstr;
48
49   Standard_Real X, Y, Z;
50   APoint.Coord(X, Y, Z);
51   myAttachPnt.xyz[0] = float (X);
52   myAttachPnt.xyz[1] = float (Y);
53   myAttachPnt.xyz[2] = float (Z);
54
55   myParam.Height = int (AHeight);
56
57   myParam.HAlign = AHta;
58   myParam.VAlign = AVta;
59 }
60
61 /*----------------------------------------------------------------------*/
62
63 OpenGl_Text::~OpenGl_Text ()
64 {
65   if (myString)
66     delete[] myString;
67 }
68
69 /*----------------------------------------------------------------------*/
70
71 void OpenGl_Text::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
72 {
73   if ( AWorkspace->DegenerateModel > 0 && AWorkspace->SkipRatio >= 1.f )
74     return;
75
76   const OpenGl_AspectText *aspect_text = AWorkspace->AspectText( Standard_True );
77
78   const TEL_COLOUR *tcolor, *scolor;
79
80   // Use highlight colours
81   if( AWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT )
82   {                         
83     tcolor = scolor = AWorkspace->HighlightColor;
84   }
85   else
86   {
87     tcolor = &aspect_text->Color();
88     scolor = &aspect_text->SubtitleColor();
89   }
90
91   // Handle annotation style
92   GLboolean flag_zbuffer = GL_FALSE;
93   if (aspect_text->StyleType() == Aspect_TOST_ANNOTATION)
94   {
95     flag_zbuffer = glIsEnabled(GL_DEPTH_TEST);
96     if (flag_zbuffer) glDisable(GL_DEPTH_TEST);
97   }
98
99   AWorkspace->SetTextParam(&myParam);
100
101   GLdouble        modelMatrix[16], projMatrix[16];
102   GLint           viewport[4];
103   GLdouble        objrefX, objrefY, objrefZ;
104   GLdouble        objX, objY, objZ;
105   GLdouble        obj1X, obj1Y, obj1Z;
106   GLdouble        obj2X, obj2Y, obj2Z;
107   GLdouble        obj3X, obj3Y, obj3Z;
108   GLdouble        winx1, winy1, winz1;
109   GLdouble        winx, winy, winz;
110   GLint           status;
111
112   /* display type of text */
113   if (aspect_text->DisplayType() != Aspect_TODT_NORMAL)
114   {
115     /* Optimisation: il faudrait ne faire le Get qu'une fois par Redraw */
116     glGetIntegerv (GL_VIEWPORT, viewport);
117     glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
118     glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
119
120     switch (aspect_text->DisplayType())
121     {
122     case Aspect_TODT_BLEND:
123       glEnable(GL_COLOR_LOGIC_OP);
124       glLogicOp(GL_XOR);
125       break;
126     case Aspect_TODT_SUBTITLE:
127     {
128       int sWidth, sAscent, sDescent;
129       AWorkspace->StringSize(myString, sWidth, sAscent, sDescent);
130
131       objrefX = (float)myAttachPnt.xyz[0];   
132       objrefY = (float)myAttachPnt.xyz[1];   
133       objrefZ = (float)myAttachPnt.xyz[2];
134       status = gluProject (objrefX, objrefY, objrefZ, modelMatrix, projMatrix, viewport,
135         &winx1, &winy1, &winz1);
136
137       winx = winx1;
138       winy = winy1-sDescent;
139       winz = winz1+0.00001;     
140       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
141         &objX, &objY, &objZ);
142
143       winx = winx1 + sWidth;
144       winy = winy1-sDescent;
145       winz = winz1+0.00001; /* il vaut mieux F+B / 1000000 ? */     
146       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
147         &obj1X, &obj1Y, &obj1Z);
148
149       winx = winx1 + sWidth;
150       winy = winy1 + sAscent;
151       winz = winz1+0.00001;     
152       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
153         &obj2X, &obj2Y, &obj2Z);
154
155       winx = winx1;
156       winy = winy1+ sAscent;
157       winz = winz1+0.00001;   
158       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
159         &obj3X, &obj3Y, &obj3Z);
160
161       glColor3fv( scolor->rgb );
162       glBegin(GL_POLYGON);
163       glVertex3d(objX, objY, objZ);
164       glVertex3d(obj1X, obj1Y, obj1Z);
165       glVertex3d(obj2X, obj2Y, obj2Z);
166       glVertex3d(obj3X, obj3Y, obj3Z);
167       glEnd();
168       break;
169     }
170
171     case Aspect_TODT_DEKALE:
172       objrefX = (float)myAttachPnt.xyz[0];   
173       objrefY = (float)myAttachPnt.xyz[1];   
174       objrefZ = (float)myAttachPnt.xyz[2];
175       status = gluProject (objrefX, objrefY, objrefZ, modelMatrix, projMatrix, viewport,
176         &winx1, &winy1, &winz1);
177
178       winx = winx1+1;
179       winy = winy1+1;
180       winz = winz1+0.00001;     
181       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
182         &objX, &objY, &objZ);
183
184       glColor3fv( scolor->rgb );
185       AWorkspace->RenderText( myString, 0, (float)objX, (float)objY,(float)objZ );
186       winx = winx1-1;
187       winy = winy1-1;
188       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
189         &objX, &objY, &objZ);
190
191       AWorkspace->RenderText( myString, 0, (float)objX, (float)objY,(float)objZ );
192       winx = winx1-1;
193       winy = winy1+1;
194       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
195         &objX, &objY, &objZ); 
196
197       AWorkspace->RenderText( myString, 0, (float)objX, (float)objY,(float)objZ );
198       winx = winx1+1;
199       winy = winy1-1;
200       status = gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport,
201         &objX, &objY, &objZ);
202       AWorkspace->RenderText( myString, 0, (float)objX, (float)objY,(float)objZ );
203       break;
204     }
205   }
206
207   glColor3fv( tcolor->rgb );
208   AWorkspace->RenderText( myString, 0, (float)myAttachPnt.xyz[0], (float)myAttachPnt.xyz[1],(float)myAttachPnt.xyz[2] );
209   /* maj attributs */
210   if (flag_zbuffer) glEnable(GL_DEPTH_TEST);
211   if (aspect_text->DisplayType() == Aspect_TODT_BLEND)
212   {
213     glDisable(GL_COLOR_LOGIC_OP);
214   }
215 }
216
217 void OpenGl_Text::Release (const Handle(OpenGl_Context)& theContext)
218 {
219   //
220 }