Back to index

salome-gui  6.5.0
OCCViewer_Trihedron.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #ifdef WIN32
00021 #include <windows.h>
00022 #endif
00023 
00024 #include <GL/gl.h>
00025 
00026 #include "OCCViewer_Trihedron.h"
00027 
00028 
00029 #include <SUIT_ResourceMgr.h>
00030 #include <SUIT_Session.h>
00031 
00032 #include <Graphic3d_AspectFillArea3d.hxx>
00033 #include <Graphic3d_Group.hxx>
00034 #include <Graphic3d_MaterialAspect.hxx>
00035 #include <Prs3d_Presentation.hxx>
00036 #include <Prs3d_Root.hxx>
00037 #include <PrsMgr_PresentationManager3d.hxx>
00038 
00039 
00040 #ifndef WIN32
00041 # include <GL/glx.h>
00042 #endif
00043 
00044 
00045 
00046 #define PI   3.14159265359
00047 
00048 IMPLEMENT_STANDARD_HANDLE( OCCViewer_Trihedron, AIS_InteractiveObject )
00049 IMPLEMENT_STANDARD_RTTIEXT( OCCViewer_Trihedron, AIS_InteractiveObject )
00050 
00054 OCCViewer_Trihedron::OCCViewer_Trihedron( const PrsMgr_TypeOfPresentation3d t )
00055   : AIS_InteractiveObject( t ),
00056     myTextList( 0 )
00057 {
00058 }
00059 
00063 OCCViewer_Trihedron::~OCCViewer_Trihedron()
00064 {
00065   if( myTextList )
00066     glDeleteLists( myTextList, 256 );
00067 }
00068 
00072 void OCCViewer_Trihedron::bounds( Graphic3d_CBounds& aMinMax ) const
00073 {
00074   Standard_Real aXMin = -1, aYMin = -1, aZMin = -1;
00075   Standard_Real aXMax =  1, aYMax =  1, aZMax =  1;
00076   
00077   if( aMinMax.XMin > aXMin ) aMinMax.XMin = aXMin;
00078   if( aMinMax.YMin > aYMin ) aMinMax.YMin = aYMin;
00079   if( aMinMax.ZMin > aZMin ) aMinMax.ZMin = aZMin;
00080   if( aMinMax.XMax < aXMax ) aMinMax.XMax = aXMax;
00081   if( aMinMax.YMax < aYMax ) aMinMax.YMax = aYMax;
00082   if( aMinMax.ZMax < aZMax ) aMinMax.ZMax = aZMax;
00083 }
00084 
00088 void OCCViewer_Trihedron::Compute( const Handle( PrsMgr_PresentationManager3d )&,
00089                                    const Handle( Prs3d_Presentation )& aPrs,
00090                                    const Standard_Integer aMode )
00091 {
00092   if( aPrs.IsNull() )
00093     return;
00094 
00095   Handle( Graphic3d_Group ) aGroup = Prs3d_Root::CurrentGroup( aPrs );
00096   aGroup->UserDraw( this );
00097 }
00098 
00102 void OCCViewer_Trihedron::ComputeSelection( const Handle( SelectMgr_Selection )& theSelection,
00103                                             const Standard_Integer theMode )
00104 {
00105 }
00106 
00110 GLuint generateTextList()
00111 {
00112   bool ok = false;
00113   GLuint aList = glGenLists( 256 );
00114 
00115 #ifdef WIN32
00116   HGLRC hglrc = wglGetCurrentContext();
00117   if( hglrc )
00118   {
00119     HDC hdc = ::wglGetCurrentDC();
00120     HFONT font = CreateFont( -12, 0, 0, 0, 
00121                              FW_BOLD,
00122                              0, 0, 0,
00123                              ANSI_CHARSET,
00124                              OUT_TT_PRECIS,
00125                              CLIP_DEFAULT_PRECIS,
00126                              ANTIALIASED_QUALITY,
00127                              FF_DONTCARE | DEFAULT_PITCH,
00128                              "Courier New" );
00129     HFONT oldFont = (HFONT)SelectObject( hdc, font );
00130 
00131     ok = ::wglUseFontBitmaps( hdc, 0, 256, aList );
00132     SelectObject( hdc, oldFont );
00133     DeleteObject( font );
00134   }
00135 #else // X Window
00136   Display* dpy = glXGetCurrentDisplay();
00137   if( dpy )
00138   {
00139     int aFontCount = 0;
00140     char** aFontList = XListFonts( dpy, "*-courier-*", 1, &aFontCount  );
00141     if( aFontCount > 0 )
00142     {
00143       //XFontStruct* fontInfo = XLoadQueryFont( dpy, "-*-courier-*-r-*-*-14-*-*-*-m-*-*-*" );
00144       Font aFont = XLoadFont( dpy, aFontList[0] );
00145       glXUseXFont( aFont, 0, 256, aList );
00146       XUnloadFont( dpy, aFont );
00147       ok = true;
00148     }
00149     XFreeFontNames( aFontList );
00150   }
00151 #endif
00152 
00153   if( !ok )
00154     glDeleteLists( aList, 256 );
00155 
00156   return aList;
00157 }
00158 
00162 void drawText( GLuint theTextList, const char* theText, GLdouble thePosition[3], GLfloat theColor[3] )
00163 {
00164   glColor3fv( theColor );
00165   glRasterPos3dv( thePosition );
00166   glListBase( theTextList );
00167   glCallLists( strlen( theText ), GL_UNSIGNED_BYTE, (GLubyte*)theText );
00168 }
00169 
00173 void OCCViewer_Trihedron::display()
00174 {
00175   GLdouble TriedronOrigin[3] = { 0.0, 0.0, 0.0 };
00176 
00177   GLdouble TriedronAxeX[3] = { 1.0, 0.0, 0.0 };
00178   GLdouble TriedronAxeY[3] = { 0.0, 1.0, 0.0 };
00179   GLdouble TriedronAxeZ[3] = { 0.0, 0.0, 1.0 };
00180 
00181   GLfloat TriedronColorX[3] = { 1.0, 0.0, 0.0 };
00182   GLfloat TriedronColorY[3] = { 0.0, 1.0, 0.0 };
00183   GLfloat TriedronColorZ[3] = { 0.0, 0.0, 1.0 };
00184 
00185   GLfloat TriedronLetterColorX[3] = { 1.0, 1.0, 1.0 };
00186   GLfloat TriedronLetterColorY[3] = { 1.0, 1.0, 1.0 };
00187   GLfloat TriedronLetterColorZ[3] = { 1.0, 1.0, 1.0 };
00188 
00189   GLfloat TriedronLineWidth = 2.0;
00190   GLdouble TriedronScale = 0.15;
00191 
00192   GLdouble U, V, minUV;
00193   GLint aViewPort[4];
00194   glGetIntegerv(GL_VIEWPORT, aViewPort);
00195   U = aViewPort[2];
00196   V = aViewPort[3];
00197 
00198   if( U < V )
00199     minUV = U;
00200   else
00201     minUV = V;
00202 
00203   GLdouble L = minUV * TriedronScale;
00204 
00205   TriedronOrigin[0]= 0.0; 
00206   TriedronOrigin[1]= 0.0;
00207   TriedronOrigin[2]= 0.0; 
00208 
00209   TriedronAxeX[0] = TriedronOrigin[0] + L;
00210   TriedronAxeX[1] = TriedronOrigin[1] + 0.0;
00211   TriedronAxeX[2] = TriedronOrigin[2] + 0.0;
00212 
00213   TriedronAxeY[0] = TriedronOrigin[0] + 0.0;
00214   TriedronAxeY[1] = TriedronOrigin[1] + L;
00215   TriedronAxeY[2] = TriedronOrigin[2] + 0.0;
00216 
00217   TriedronAxeZ[0] = TriedronOrigin[0] + 0.0;
00218   TriedronAxeZ[1] = TriedronOrigin[1] + 0.0;
00219   TriedronAxeZ[2] = TriedronOrigin[2] + L;
00220 
00221   glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_LINE_BIT | GL_VIEWPORT_BIT );
00222 
00223   glDepthRange( 0, 0 );
00224   glDisable( GL_LIGHTING );
00225   glDisable( GL_COLOR_MATERIAL );
00226   for( int i = 0; i < GL_MAX_CLIP_PLANES; i++ )
00227     glDisable( GL_CLIP_PLANE0 + i  );
00228 
00229   glLineWidth( TriedronLineWidth );
00230 
00231   glColor3fv( TriedronColorX );
00232   glBegin( GL_LINES );
00233   glVertex3dv( TriedronOrigin );
00234   glVertex3dv( TriedronAxeX );
00235   glEnd();
00236 
00237   glColor3fv( TriedronColorY );
00238   glBegin( GL_LINES );
00239   glVertex3dv( TriedronOrigin );
00240   glVertex3dv( TriedronAxeY );
00241   glEnd();
00242 
00243   glColor3fv( TriedronColorZ );
00244   glBegin( GL_LINES );
00245   glVertex3dv( TriedronOrigin );
00246   glVertex3dv( TriedronAxeZ );
00247   glEnd();
00248 
00249   GLdouble l = L - L/4.;
00250   GLdouble rayon = L/20.;
00251   GLint ii, NbFacettes = 12;
00252   GLdouble Angle = 2. * PI / NbFacettes;
00253   GLdouble TriedronCoord[3] = { 1.0, 0.0, 0.0 };
00254 
00255   if( myTextList == 0 )
00256     myTextList = generateTextList();
00257 
00258   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00259 
00260   glColor3fv( TriedronColorX );
00261   glBegin( GL_TRIANGLE_FAN );
00262   glVertex3dv( TriedronAxeX );
00263   TriedronCoord[0] = TriedronOrigin[0] + l ;
00264   ii = NbFacettes;
00265   while (ii >= 0 ) {
00266     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * sin(ii * Angle) );
00267     TriedronCoord[2] = TriedronOrigin[2] + ( rayon * cos(ii * Angle) );
00268     glVertex3dv( TriedronCoord );
00269     ii--;
00270   }
00271   glEnd();
00272 
00273   drawText( myTextList, "X", TriedronAxeX, TriedronLetterColorX );
00274 
00275   glColor3fv( TriedronColorY );
00276   glBegin( GL_TRIANGLE_FAN );
00277   glVertex3dv( TriedronAxeY );
00278   TriedronCoord[1] = TriedronOrigin[1] + l ;
00279   ii = NbFacettes;
00280   while (ii >= 0 ) {
00281     TriedronCoord[0] = TriedronOrigin[0] + (rayon * cos(ii * Angle) );
00282     TriedronCoord[2] = TriedronOrigin[2] + (rayon * sin(ii * Angle) );
00283     glVertex3dv( TriedronCoord );
00284     ii--;
00285   }
00286   glEnd();
00287 
00288   drawText( myTextList, "Y", TriedronAxeY, TriedronLetterColorY );
00289 
00290   glColor3fv( TriedronColorZ );
00291   glBegin( GL_TRIANGLE_FAN );
00292   glVertex3dv( TriedronAxeZ );
00293   TriedronCoord[2] = TriedronOrigin[2] + l ;
00294   ii = NbFacettes;
00295   while (ii >= 0 ) {
00296     TriedronCoord[0] = TriedronOrigin[0] + ( rayon * sin(ii * Angle) );
00297     TriedronCoord[1] = TriedronOrigin[1] + ( rayon * cos(ii * Angle) );
00298     glVertex3dv( TriedronCoord );
00299     ii--;
00300   }
00301   glEnd();
00302 
00303   drawText( myTextList, "Z", TriedronAxeZ, TriedronLetterColorZ );
00304 
00305   glPopAttrib();
00306 }