]> SALOME platform Git repositories - modules/gui.git/blob - src/OCCViewer/OCCViewer_Trihedron.cxx
Salome HOME
Merge remote branch 'origin/V7_dev'
[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 IMPLEMENT_STANDARD_HANDLE( OCCViewer_Trihedron, AIS_InteractiveObject )
49 IMPLEMENT_STANDARD_RTTIEXT( OCCViewer_Trihedron, AIS_InteractiveObject )
50
51 /*!
52  * Constructor
53  */
54 OCCViewer_Trihedron::OCCViewer_Trihedron( const PrsMgr_TypeOfPresentation3d t )
55   : AIS_InteractiveObject( t ),
56     myTextList( 0 )
57 {
58 }
59
60 /*!
61  * Destructor
62  */
63 OCCViewer_Trihedron::~OCCViewer_Trihedron()
64 {
65   if( myTextList )
66     glDeleteLists( myTextList, 256 );
67 }
68
69 /*!
70  * Sets the bounding box (MinMax values).
71  */
72 #if OCC_VERSION_LARGE > 0x06070100 // for OCC-6.7.2 and higher version
73 void OCCViewer_Trihedron::bounds( Graphic3d_BndBox4f& theMinMax ) const
74 {
75   Graphic3d_Vec4 aMinPt (-1.f, -1.f, -1.f, 1.f);
76   Graphic3d_Vec4 aMaxPt (1.f, 1.f, 1.f, 1.f);
77
78   theMinMax.Add (aMinPt);
79   theMinMax.Add (aMaxPt);
80 }
81 #else
82 void OCCViewer_Trihedron::bounds( Graphic3d_CBounds& aMinMax ) const
83 {
84   Standard_Real aXMin = -1, aYMin = -1, aZMin = -1;
85   Standard_Real aXMax =  1, aYMax =  1, aZMax =  1;
86
87   if( aMinMax.XMin > aXMin ) aMinMax.XMin = aXMin;
88   if( aMinMax.YMin > aYMin ) aMinMax.YMin = aYMin;
89   if( aMinMax.ZMin > aZMin ) aMinMax.ZMin = aZMin;
90   if( aMinMax.XMax < aXMax ) aMinMax.XMax = aXMax;
91   if( aMinMax.YMax < aYMax ) aMinMax.YMax = aYMax;
92   if( aMinMax.ZMax < aZMax ) aMinMax.ZMax = aZMax;
93 }
94 #endif
95
96 /*!
97  * Redefined method. Calculates the object presentation.
98  */
99 void OCCViewer_Trihedron::Compute( const Handle( PrsMgr_PresentationManager3d )&,
100                                    const Handle( Prs3d_Presentation )& aPrs,
101                                    const Standard_Integer aMode )
102 {
103   if( aPrs.IsNull() )
104     return;
105
106   Handle( Graphic3d_Group ) aGroup = Prs3d_Root::CurrentGroup( aPrs );
107   aGroup->UserDraw( this );
108 }
109
110 /*!
111  * Redefined method. Does nothing.
112  */
113 void OCCViewer_Trihedron::ComputeSelection( const Handle( SelectMgr_Selection )& theSelection,
114                                             const Standard_Integer theMode )
115 {
116 }
117
118 /*!
119  * Generates text list.
120  */
121 GLuint generateTextList()
122 {
123   bool ok = false;
124   GLuint aList = glGenLists( 256 );
125
126 #ifdef WIN32
127   HGLRC hglrc = wglGetCurrentContext();
128   if( hglrc )
129   {
130     HDC hdc = ::wglGetCurrentDC();
131     HFONT font = CreateFont( -12, 0, 0, 0, 
132                              FW_BOLD,
133                              0, 0, 0,
134                              ANSI_CHARSET,
135                              OUT_TT_PRECIS,
136                              CLIP_DEFAULT_PRECIS,
137                              ANTIALIASED_QUALITY,
138                              FF_DONTCARE | DEFAULT_PITCH,
139                              "Courier New" );
140     HFONT oldFont = (HFONT)SelectObject( hdc, font );
141
142     ok = ::wglUseFontBitmaps( hdc, 0, 256, aList );
143     SelectObject( hdc, oldFont );
144     DeleteObject( font );
145   }
146 #else // X Window
147   Display* dpy = glXGetCurrentDisplay();
148   if( dpy )
149   {
150     int aFontCount = 0;
151     char** aFontList = XListFonts( dpy, "*-courier-*", 1, &aFontCount  );
152     if( aFontCount > 0 )
153     {
154       //XFontStruct* fontInfo = XLoadQueryFont( dpy, "-*-courier-*-r-*-*-14-*-*-*-m-*-*-*" );
155       Font aFont = XLoadFont( dpy, aFontList[0] );
156       glXUseXFont( aFont, 0, 256, aList );
157       XUnloadFont( dpy, aFont );
158       ok = true;
159     }
160     XFreeFontNames( aFontList );
161   }
162 #endif
163
164   if( !ok )
165     glDeleteLists( aList, 256 );
166
167   return aList;
168 }
169
170 /*!
171  * Draws text string.
172  */
173 void drawText( GLuint theTextList, const char* theText, GLdouble thePosition[3], GLfloat theColor[3] )
174 {
175   glColor3fv( theColor );
176   glRasterPos3dv( thePosition );
177   glListBase( theTextList );
178   glCallLists( strlen( theText ), GL_UNSIGNED_BYTE, (GLubyte*)theText );
179 }
180
181 /*!
182  * Displays trihedron.
183  */
184 void OCCViewer_Trihedron::display()
185 {
186   GLdouble TriedronOrigin[3] = { 0.0, 0.0, 0.0 };
187
188   GLdouble TriedronAxeX[3] = { 1.0, 0.0, 0.0 };
189   GLdouble TriedronAxeY[3] = { 0.0, 1.0, 0.0 };
190   GLdouble TriedronAxeZ[3] = { 0.0, 0.0, 1.0 };
191
192   GLfloat TriedronColorX[3] = { 1.0, 0.0, 0.0 };
193   GLfloat TriedronColorY[3] = { 0.0, 1.0, 0.0 };
194   GLfloat TriedronColorZ[3] = { 0.0, 0.0, 1.0 };
195
196   GLfloat TriedronLetterColorX[3] = { 1.0, 1.0, 1.0 };
197   GLfloat TriedronLetterColorY[3] = { 1.0, 1.0, 1.0 };
198   GLfloat TriedronLetterColorZ[3] = { 1.0, 1.0, 1.0 };
199
200   GLfloat TriedronLineWidth = 2.0;
201   GLdouble TriedronScale = 0.15;
202
203   GLdouble U, V, minUV;
204   GLint aViewPort[4];
205   glGetIntegerv(GL_VIEWPORT, aViewPort);
206   U = aViewPort[2];
207   V = aViewPort[3];
208
209   if( U < V )
210     minUV = U;
211   else
212     minUV = V;
213
214   GLdouble L = minUV * TriedronScale;
215
216   TriedronOrigin[0]= 0.0; 
217   TriedronOrigin[1]= 0.0;
218   TriedronOrigin[2]= 0.0; 
219
220   TriedronAxeX[0] = TriedronOrigin[0] + L;
221   TriedronAxeX[1] = TriedronOrigin[1] + 0.0;
222   TriedronAxeX[2] = TriedronOrigin[2] + 0.0;
223
224   TriedronAxeY[0] = TriedronOrigin[0] + 0.0;
225   TriedronAxeY[1] = TriedronOrigin[1] + L;
226   TriedronAxeY[2] = TriedronOrigin[2] + 0.0;
227
228   TriedronAxeZ[0] = TriedronOrigin[0] + 0.0;
229   TriedronAxeZ[1] = TriedronOrigin[1] + 0.0;
230   TriedronAxeZ[2] = TriedronOrigin[2] + L;
231
232   glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_LINE_BIT | GL_VIEWPORT_BIT );
233
234   glDepthRange( 0, 0 );
235   glDisable( GL_LIGHTING );
236   glDisable( GL_COLOR_MATERIAL );
237   for( int i = 0; i < GL_MAX_CLIP_PLANES; i++ )
238     glDisable( GL_CLIP_PLANE0 + i  );
239
240   glLineWidth( TriedronLineWidth );
241
242   glColor3fv( TriedronColorX );
243   glBegin( GL_LINES );
244   glVertex3dv( TriedronOrigin );
245   glVertex3dv( TriedronAxeX );
246   glEnd();
247
248   glColor3fv( TriedronColorY );
249   glBegin( GL_LINES );
250   glVertex3dv( TriedronOrigin );
251   glVertex3dv( TriedronAxeY );
252   glEnd();
253
254   glColor3fv( TriedronColorZ );
255   glBegin( GL_LINES );
256   glVertex3dv( TriedronOrigin );
257   glVertex3dv( TriedronAxeZ );
258   glEnd();
259
260   GLdouble l = L - L/4.;
261   GLdouble rayon = L/20.;
262   GLint ii, NbFacettes = 12;
263   GLdouble Angle = 2. * PI / NbFacettes;
264   GLdouble TriedronCoord[3] = { 1.0, 0.0, 0.0 };
265
266   if( myTextList == 0 )
267     myTextList = generateTextList();
268
269   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
270
271   glColor3fv( TriedronColorX );
272   glBegin( GL_TRIANGLE_FAN );
273   glVertex3dv( TriedronAxeX );
274   TriedronCoord[0] = TriedronOrigin[0] + l ;
275   ii = NbFacettes;
276   while (ii >= 0 ) {
277     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * sin(ii * Angle) );
278     TriedronCoord[2] = TriedronOrigin[2] + ( rayon * cos(ii * Angle) );
279     glVertex3dv( TriedronCoord );
280     ii--;
281   }
282   glEnd();
283
284   drawText( myTextList, "X", TriedronAxeX, TriedronLetterColorX );
285
286   glColor3fv( TriedronColorY );
287   glBegin( GL_TRIANGLE_FAN );
288   glVertex3dv( TriedronAxeY );
289   TriedronCoord[1] = TriedronOrigin[1] + l ;
290   ii = NbFacettes;
291   while (ii >= 0 ) {
292     TriedronCoord[0] = TriedronOrigin[0] + (rayon * cos(ii * Angle) );
293     TriedronCoord[2] = TriedronOrigin[2] + (rayon * sin(ii * Angle) );
294     glVertex3dv( TriedronCoord );
295     ii--;
296   }
297   glEnd();
298
299   drawText( myTextList, "Y", TriedronAxeY, TriedronLetterColorY );
300
301   glColor3fv( TriedronColorZ );
302   glBegin( GL_TRIANGLE_FAN );
303   glVertex3dv( TriedronAxeZ );
304   TriedronCoord[2] = TriedronOrigin[2] + l ;
305   ii = NbFacettes;
306   while (ii >= 0 ) {
307     TriedronCoord[0] = TriedronOrigin[0] + ( rayon * sin(ii * Angle) );
308     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * cos(ii * Angle) );
309     glVertex3dv( TriedronCoord );
310     ii--;
311   }
312   glEnd();
313
314   drawText( myTextList, "Z", TriedronAxeZ, TriedronLetterColorZ );
315
316   glPopAttrib();
317 }