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