Salome HOME
ZFitAll leads to black screen of OCC viewer during rotation if gradient background...
[modules/gui.git] / src / OCCViewer / OCCViewer_Trihedron.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifdef WIN32
21 #include <windows.h>
22 #endif
23
24 #include <GL/gl.h>
25
26 #include "OCCViewer_Trihedron.h"
27
28
29 #include <SUIT_ResourceMgr.h>
30 #include <SUIT_Session.h>
31
32 #include <Graphic3d_AspectFillArea3d.hxx>
33 #include <Graphic3d_Group.hxx>
34 #include <Graphic3d_MaterialAspect.hxx>
35 #include <Prs3d_Presentation.hxx>
36 #include <Prs3d_Root.hxx>
37 #include <PrsMgr_PresentationManager3d.hxx>
38
39
40 #ifndef WIN32
41 # include <GL/glx.h>
42 #endif
43
44
45
46 #define PI   3.14159265359
47
48 OCCT_IMPLEMENT_STANDARD_RTTIEXT( OCCViewer_Trihedron, AIS_InteractiveObject )
49
50 /*!
51  * Constructor
52  */
53 OCCViewer_Trihedron::OCCViewer_Trihedron( const PrsMgr_TypeOfPresentation3d t )
54   : AIS_InteractiveObject( t ),
55     myTextList( 0 )
56 {
57 }
58
59 /*!
60  * Destructor
61  */
62 OCCViewer_Trihedron::~OCCViewer_Trihedron()
63 {
64   if( myTextList )
65     glDeleteLists( myTextList, 256 );
66 }
67
68 /*!
69  * Sets the bounding box (MinMax values).
70  */
71 #if OCC_VERSION_LARGE > 0x06070100 // for OCC-6.7.2 and higher version
72 void OCCViewer_Trihedron::bounds( Graphic3d_BndBox4f& theMinMax ) const
73 {
74   Graphic3d_Vec4 aMinPt (-1.f, -1.f, -1.f, 1.f);
75   Graphic3d_Vec4 aMaxPt (1.f, 1.f, 1.f, 1.f);
76
77   theMinMax.Add (aMinPt);
78   theMinMax.Add (aMaxPt);
79 }
80 #else
81 void OCCViewer_Trihedron::bounds( Graphic3d_CBounds& aMinMax ) const
82 {
83   Standard_Real aXMin = -1, aYMin = -1, aZMin = -1;
84   Standard_Real aXMax =  1, aYMax =  1, aZMax =  1;
85
86   if( aMinMax.XMin > aXMin ) aMinMax.XMin = aXMin;
87   if( aMinMax.YMin > aYMin ) aMinMax.YMin = aYMin;
88   if( aMinMax.ZMin > aZMin ) aMinMax.ZMin = aZMin;
89   if( aMinMax.XMax < aXMax ) aMinMax.XMax = aXMax;
90   if( aMinMax.YMax < aYMax ) aMinMax.YMax = aYMax;
91   if( aMinMax.ZMax < aZMax ) aMinMax.ZMax = aZMax;
92 }
93 #endif
94
95 /*!
96  * Redefined method. Calculates the object presentation.
97  */
98 void OCCViewer_Trihedron::Compute( const Handle( PrsMgr_PresentationManager3d )&,
99                                    const Handle( Prs3d_Presentation )& aPrs,
100                                    const Standard_Integer aMode )
101 {
102   if( aPrs.IsNull() )
103     return;
104
105   Handle( Graphic3d_Group ) aGroup = Prs3d_Root::CurrentGroup( aPrs );
106   aGroup->UserDraw( this );
107 }
108
109 /*!
110  * Redefined method. Does nothing.
111  */
112 void OCCViewer_Trihedron::ComputeSelection( const Handle( SelectMgr_Selection )& theSelection,
113                                             const Standard_Integer theMode )
114 {
115 }
116
117 /*!
118  * Generates text list.
119  */
120 GLuint generateTextList()
121 {
122   bool ok = false;
123   GLuint aList = glGenLists( 256 );
124
125 #ifdef WIN32
126   HGLRC hglrc = wglGetCurrentContext();
127   if( hglrc )
128   {
129     HDC hdc = ::wglGetCurrentDC();
130     HFONT font = CreateFont( -12, 0, 0, 0, 
131                              FW_BOLD,
132                              0, 0, 0,
133                              ANSI_CHARSET,
134                              OUT_TT_PRECIS,
135                              CLIP_DEFAULT_PRECIS,
136                              ANTIALIASED_QUALITY,
137                              FF_DONTCARE | DEFAULT_PITCH,
138                              "Courier New" );
139     HFONT oldFont = (HFONT)SelectObject( hdc, font );
140
141     ok = ::wglUseFontBitmaps( hdc, 0, 256, aList );
142     SelectObject( hdc, oldFont );
143     DeleteObject( font );
144   }
145 #else // X Window
146   Display* dpy = glXGetCurrentDisplay();
147   if( dpy )
148   {
149     int aFontCount = 0;
150     char** aFontList = XListFonts( dpy, "*-courier-*", 1, &aFontCount  );
151     if( aFontCount > 0 )
152     {
153       //XFontStruct* fontInfo = XLoadQueryFont( dpy, "-*-courier-*-r-*-*-14-*-*-*-m-*-*-*" );
154       Font aFont = XLoadFont( dpy, aFontList[0] );
155       glXUseXFont( aFont, 0, 256, aList );
156       XUnloadFont( dpy, aFont );
157       ok = true;
158     }
159     XFreeFontNames( aFontList );
160   }
161 #endif
162
163   if( !ok )
164     glDeleteLists( aList, 256 );
165
166   return aList;
167 }
168
169 /*!
170  * Draws text string.
171  */
172 void drawText( GLuint theTextList, const char* theText, GLdouble thePosition[3], GLfloat theColor[3] )
173 {
174   glColor3fv( theColor );
175   glRasterPos3dv( thePosition );
176   glListBase( theTextList );
177   glCallLists( strlen( theText ), GL_UNSIGNED_BYTE, (GLubyte*)theText );
178 }
179
180 /*!
181  * Displays trihedron.
182  */
183 void OCCViewer_Trihedron::display()
184 {
185   GLdouble TriedronOrigin[3] = { 0.0, 0.0, 0.0 };
186
187   GLdouble TriedronAxeX[3] = { 1.0, 0.0, 0.0 };
188   GLdouble TriedronAxeY[3] = { 0.0, 1.0, 0.0 };
189   GLdouble TriedronAxeZ[3] = { 0.0, 0.0, 1.0 };
190
191   GLfloat TriedronColorX[3] = { 1.0, 0.0, 0.0 };
192   GLfloat TriedronColorY[3] = { 0.0, 1.0, 0.0 };
193   GLfloat TriedronColorZ[3] = { 0.0, 0.0, 1.0 };
194
195   GLfloat TriedronLetterColorX[3] = { 1.0, 1.0, 1.0 };
196   GLfloat TriedronLetterColorY[3] = { 1.0, 1.0, 1.0 };
197   GLfloat TriedronLetterColorZ[3] = { 1.0, 1.0, 1.0 };
198
199   GLfloat TriedronLineWidth = 2.0;
200   GLdouble TriedronScale = 0.15;
201
202   GLdouble U, V, minUV;
203   GLint aViewPort[4];
204   glGetIntegerv(GL_VIEWPORT, aViewPort);
205   U = aViewPort[2];
206   V = aViewPort[3];
207
208   if( U < V )
209     minUV = U;
210   else
211     minUV = V;
212
213   GLdouble L = minUV * TriedronScale;
214
215   TriedronOrigin[0]= 0.0; 
216   TriedronOrigin[1]= 0.0;
217   TriedronOrigin[2]= 0.0; 
218
219   TriedronAxeX[0] = TriedronOrigin[0] + L;
220   TriedronAxeX[1] = TriedronOrigin[1] + 0.0;
221   TriedronAxeX[2] = TriedronOrigin[2] + 0.0;
222
223   TriedronAxeY[0] = TriedronOrigin[0] + 0.0;
224   TriedronAxeY[1] = TriedronOrigin[1] + L;
225   TriedronAxeY[2] = TriedronOrigin[2] + 0.0;
226
227   TriedronAxeZ[0] = TriedronOrigin[0] + 0.0;
228   TriedronAxeZ[1] = TriedronOrigin[1] + 0.0;
229   TriedronAxeZ[2] = TriedronOrigin[2] + L;
230
231   glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_LINE_BIT | GL_VIEWPORT_BIT );
232
233   glDepthRange( 0, 0 );
234   glDisable( GL_LIGHTING );
235   glDisable( GL_COLOR_MATERIAL );
236   for( int i = 0; i < GL_MAX_CLIP_PLANES; i++ )
237     glDisable( GL_CLIP_PLANE0 + i  );
238
239   glLineWidth( TriedronLineWidth );
240
241   glColor3fv( TriedronColorX );
242   glBegin( GL_LINES );
243   glVertex3dv( TriedronOrigin );
244   glVertex3dv( TriedronAxeX );
245   glEnd();
246
247   glColor3fv( TriedronColorY );
248   glBegin( GL_LINES );
249   glVertex3dv( TriedronOrigin );
250   glVertex3dv( TriedronAxeY );
251   glEnd();
252
253   glColor3fv( TriedronColorZ );
254   glBegin( GL_LINES );
255   glVertex3dv( TriedronOrigin );
256   glVertex3dv( TriedronAxeZ );
257   glEnd();
258
259   GLdouble l = L - L/4.;
260   GLdouble rayon = L/20.;
261   GLint ii, NbFacettes = 12;
262   GLdouble Angle = 2. * PI / NbFacettes;
263   GLdouble TriedronCoord[3] = { 1.0, 0.0, 0.0 };
264
265   if( myTextList == 0 )
266     myTextList = generateTextList();
267
268   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
269
270   glColor3fv( TriedronColorX );
271   glBegin( GL_TRIANGLE_FAN );
272   glVertex3dv( TriedronAxeX );
273   TriedronCoord[0] = TriedronOrigin[0] + l ;
274   ii = NbFacettes;
275   while (ii >= 0 ) {
276     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * sin(ii * Angle) );
277     TriedronCoord[2] = TriedronOrigin[2] + ( rayon * cos(ii * Angle) );
278     glVertex3dv( TriedronCoord );
279     ii--;
280   }
281   glEnd();
282
283   drawText( myTextList, "X", TriedronAxeX, TriedronLetterColorX );
284
285   glColor3fv( TriedronColorY );
286   glBegin( GL_TRIANGLE_FAN );
287   glVertex3dv( TriedronAxeY );
288   TriedronCoord[1] = TriedronOrigin[1] + l ;
289   ii = NbFacettes;
290   while (ii >= 0 ) {
291     TriedronCoord[0] = TriedronOrigin[0] + (rayon * cos(ii * Angle) );
292     TriedronCoord[2] = TriedronOrigin[2] + (rayon * sin(ii * Angle) );
293     glVertex3dv( TriedronCoord );
294     ii--;
295   }
296   glEnd();
297
298   drawText( myTextList, "Y", TriedronAxeY, TriedronLetterColorY );
299
300   glColor3fv( TriedronColorZ );
301   glBegin( GL_TRIANGLE_FAN );
302   glVertex3dv( TriedronAxeZ );
303   TriedronCoord[2] = TriedronOrigin[2] + l ;
304   ii = NbFacettes;
305   while (ii >= 0 ) {
306     TriedronCoord[0] = TriedronOrigin[0] + ( rayon * sin(ii * Angle) );
307     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * cos(ii * Angle) );
308     glVertex3dv( TriedronCoord );
309     ii--;
310   }
311   glEnd();
312
313   drawText( myTextList, "Z", TriedronAxeZ, TriedronLetterColorZ );
314
315   glPopAttrib();
316 }