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