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