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