Salome HOME
Merge Qt5 porting.
[modules/gui.git] / src / GLViewer / GLViewer_Drawer.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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
23 //  Author : OPEN CASCADE
24 // File:      GLViewer_Drawer.cxx
25 // Created:   November, 2004
26 //#include <GLViewerAfx.h>
27 //
28 #include "GLViewer_Drawer.h"
29 #include "GLViewer_Object.h"
30 #include "GLViewer_Text.h"
31 #include "GLViewer_ViewFrame.h"
32 #include "GLViewer_ViewPort2d.h"
33
34 #include <QApplication>
35 #include <QImage>
36 #include <QPainter>
37 #include <QFile>
38
39 #ifndef WIN32
40 #include <GL/glx.h>
41 #endif
42
43 #include <gp_Pnt2d.hxx>
44
45 #define TEXT_GAP    5
46 // Two texture components for texmapped fonts: luminance and alpha
47 #define NB_TEX_COMP 2
48 // A font is split into rows each containing 32 characters
49 #define TEX_ROW_LEN 32
50 // Gap in pixels between two character rows in a font texture
51 #define TEX_ROW_GAP 2
52
53 GLfloat modelMatrix[16];
54
55
56 //! code of first font symbol
57 static int FirstSymbolNumber = 32;
58 //! code of last font symbol
59 static int LastSymbolNumber = 127;
60
61 QMap<GLViewer_TexFindId,GLViewer_TexIdStored> GLViewer_TexFont::TexFontBase;
62 QMap<GLViewer_TexFindId,GLuint>               GLViewer_TexFont::BitmapFontCache; 
63
64 /*!
65   Clears all generated fonts
66 */
67 void GLViewer_TexFont::clearTextBases()
68 {
69   //cout << "Clear font map" << endl;
70   TexFontBase.clear();
71   BitmapFontCache.clear();
72 }
73
74 /*!
75   Default constructor
76 */
77 GLViewer_TexFont::GLViewer_TexFont()
78 : myMaxRowWidth( 0 ), myFontHeight( 0 )
79 {
80     myQFont = QApplication::font();//QFont::defaultFont();
81     mySeparator = 2;
82     myIsResizeable = false;
83     myMinMagFilter = GL_LINEAR;
84
85     init();
86 }
87
88 /*!
89   Constructor
90   \param theFont         - a base font
91   \param theSeparator    - separator between letters
92   \param theIsResizeable - specifies whether text drawn by this object can be scaled along with the scene
93   \param theMinMagFilter - min/mag filter, affects text sharpness
94 */
95 GLViewer_TexFont::GLViewer_TexFont( QFont* theFont, int theSeparator, bool theIsResizeable, GLuint theMinMagFilter )
96 : myMaxRowWidth( 0 ), myFontHeight( 0 )
97 {
98     myQFont = *theFont;
99     mySeparator = theSeparator;
100     myIsResizeable = theIsResizeable;
101     myMinMagFilter = theMinMagFilter;
102
103     init();
104 }
105
106 /*!
107   Destructor
108 */
109 GLViewer_TexFont::~GLViewer_TexFont()
110 {
111     delete[] myWidths;
112     delete[] myPositions;
113
114
115 /*!
116   Initializes font parameters
117 */
118 void GLViewer_TexFont::init()
119 {
120     myNbSymbols = LastSymbolNumber - FirstSymbolNumber + 1;
121
122     // It is unsafe to draw all characters in a single row -
123     // this leads to problems on some graphic cards with small GL_MAX_TEXTURE_SIZE.
124     // So splitting the characters into rows each containing 32 characters (or less).
125     // Assuming contant height of each row (64 pixels) to simplify texture mapping.
126     // However, this can be improved if necessary.
127     QFontMetrics aFM( myQFont ); 
128     myFontHeight = aFM.height();
129     
130     myWidths    = new int[myNbSymbols];
131     myPositions = new int[myNbSymbols];
132
133     for( int i = 0, k = FirstSymbolNumber, aWidth = 0; i < myNbSymbols; i++, k++ )
134     {
135         // is it time to start a new row?
136         if ( !( i % TEX_ROW_LEN ) )
137         {
138           if( aWidth > myMaxRowWidth )
139             myMaxRowWidth = aWidth;
140           aWidth = 0;
141         }
142         myWidths[i]    = aFM.width( k );
143         myPositions[i] = aWidth;
144         aWidth += myWidths[i] + 2;
145     }
146
147     myTexFontWidth  = 0;
148     myTexFontHeight = 0;
149 }
150   
151 /*!
152   Generating font texture
153 */
154 bool GLViewer_TexFont::generateTexture()
155 {
156     GLViewer_TexFindId aFindFont;
157     aFindFont.myFontFamily = myQFont.family();//myQFont.toString();
158     aFindFont.myIsBold = myQFont.bold();
159     aFindFont.myIsItal = myQFont.italic();
160     aFindFont.myIsUndl = myQFont.underline();
161     aFindFont.myPointSize = myQFont.pointSize();
162     aFindFont.myViewPortId = size_t(QGLContext::currentContext());
163         
164     if( TexFontBase.contains( aFindFont ) )
165     {
166         GLViewer_TexIdStored aTexture = TexFontBase[ aFindFont ];
167         myTexFont = aTexture.myTexFontId;
168         myTexFontWidth = aTexture.myTexFontWidth;
169         myTexFontHeight = aTexture.myTexFontHeight;
170     }    
171     else    
172     {
173         // Adding some pixels to have a gap between rows
174         int aRowPixelHeight = myFontHeight + TEX_ROW_GAP;
175         int aDescent = QFontMetrics( myQFont ).descent();
176
177         int aNumRows = myNbSymbols / TEX_ROW_LEN;
178         if ( myNbSymbols % TEX_ROW_LEN ) 
179           aNumRows++;
180         int pixelsHight = aNumRows * aRowPixelHeight;
181
182         myTexFontWidth  = 64;
183         myTexFontHeight = 64;
184
185         while( myTexFontWidth < myMaxRowWidth )
186             myTexFontWidth <<= 1;
187         while( myTexFontHeight < pixelsHight )
188             myTexFontHeight <<= 1;
189         
190         // Checking whether the texture dimensions for the requested font
191         // do not exceed the maximum size supported by the OpenGL implementation
192         int maxSize;
193         glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maxSize );
194         if ( myTexFontWidth > maxSize || myTexFontHeight > maxSize )
195           return false;
196
197         QPixmap aPixmap( myTexFontWidth, myTexFontHeight );
198         aPixmap.fill( QColor( 0, 0, 0) );
199         QPainter aPainter( &aPixmap );
200         aPainter.setFont( myQFont );
201         int row;
202         for( int l = 0; l < myNbSymbols; l++  )
203         {
204             row = l / TEX_ROW_LEN;
205             QString aLetter;
206             aLetter += (char)(FirstSymbolNumber + l);
207             aPainter.setPen( QColor( 255,255,255) );
208             aPainter.drawText( myPositions[l], ( row + 1 ) * aRowPixelHeight - aDescent, aLetter );
209         }
210     
211         QImage aImage = aPixmap.toImage();
212
213         //int qqq = 0;
214         //if (qqq)
215         //  aImage.save("w:\\work\\CATHARE\\texture.png", "PNG");
216
217         char* pixels = new char[myTexFontWidth * myTexFontHeight * NB_TEX_COMP];
218
219         for( int i = 0; i < myTexFontHeight; i++ )
220         {            
221             for( int j = 0; j < myTexFontWidth;  j++ )
222             {
223                 int aRed = qRed( aImage.pixel( j, myTexFontHeight - i - 1 ) );
224                 int aGreen = qGreen( aImage.pixel( j, myTexFontHeight - i - 1 ) );
225                 int aBlue = qBlue( aImage.pixel( j, myTexFontHeight - i - 1 ) );
226           
227                 if( aRed != 0 || aGreen != 0 || aBlue != 0 )
228                 {
229                     pixels[i * myTexFontWidth * NB_TEX_COMP + j * NB_TEX_COMP] = (GLubyte)( (aRed + aGreen + aBlue)/3 );
230                     pixels[i * myTexFontWidth * NB_TEX_COMP + j * NB_TEX_COMP + 1]= (GLubyte) 255;
231                 }
232                 else
233                 {
234                     pixels[i * myTexFontWidth * NB_TEX_COMP + j * NB_TEX_COMP] = (GLubyte) 0;
235                     pixels[i * myTexFontWidth * NB_TEX_COMP + j * NB_TEX_COMP + 1]= (GLubyte) 0;
236                 }                
237             }
238         }
239
240         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
241         glGenTextures(1, &myTexFont);
242         glBindTexture(GL_TEXTURE_2D, myTexFont);  
243         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, myMinMagFilter);
244         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, myMinMagFilter);
245         glTexImage2D(GL_TEXTURE_2D, 
246                      0, 
247                      GL_INTENSITY, 
248                      myTexFontWidth,
249                      myTexFontHeight, 
250                      0, 
251                      GL_LUMINANCE_ALPHA, 
252                      GL_UNSIGNED_BYTE, 
253                      pixels);
254     
255         delete[] pixels;
256         
257         GLViewer_TexIdStored aTexture;
258         aTexture.myTexFontId = myTexFont;
259         aTexture.myTexFontWidth = myTexFontWidth;
260         aTexture.myTexFontHeight = myTexFontHeight;
261
262         TexFontBase.insert( aFindFont, aTexture );
263     }
264     return true;
265 }
266
267 /*!
268   Drawing string in viewer
269   \param theStr - string to be drawn
270   \param theX - X position
271   \param theY - Y position
272   \param theScale - scale coefficient
273 */
274 void GLViewer_TexFont::drawString( QString theStr, GLdouble theX , GLdouble theY, GLfloat theScale )
275 {
276     // Adding some pixels to have a gap between rows
277     int aRowPixelHeight = myFontHeight + TEX_ROW_GAP;
278
279     float aXScale = 1.f, aYScale = 1.f;
280     if ( !myIsResizeable )
281     {
282       glGetFloatv (GL_MODELVIEW_MATRIX, modelMatrix);
283       aXScale = modelMatrix[0];
284       aYScale = modelMatrix[5];     
285     } 
286     else if ( theScale > 0.f )
287     {
288       aXScale = aXScale / theScale;
289       aYScale = aYScale / theScale;
290     }
291
292     // store attributes
293     glPushAttrib( GL_ENABLE_BIT | GL_TEXTURE_BIT );
294
295     glEnable(GL_TEXTURE_2D);
296     glPixelTransferi(GL_MAP_COLOR, 0);
297
298     glAlphaFunc(GL_GEQUAL, 0.05F);
299     glEnable(GL_ALPHA_TEST);
300
301     glEnable(GL_BLEND);
302     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
303
304     glBindTexture(GL_TEXTURE_2D, myTexFont);
305     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
306
307     glBegin(GL_QUADS);
308
309     float aLettBeginS, aLettEndS, aLettBeginT, aLettEndT;
310     float aDY = ( aRowPixelHeight - 1 ) / aYScale, aDX;
311     char aLetter;
312     int aLettIndex, row;
313     for ( int i = 0; i < (int)theStr.length(); i++ )
314     {
315         aLetter    = theStr.data()[i].toLatin1();
316         aLettIndex = (int)aLetter - FirstSymbolNumber;
317         row        = aLettIndex / TEX_ROW_LEN;
318
319         aLettBeginS = (float)myPositions[aLettIndex] / ( (float)myTexFontWidth - 1.f );
320         aLettEndS   = aLettBeginS + ( (float)myWidths[aLettIndex] - 1.f ) / ( (float)myTexFontWidth - 1.f );
321         aLettBeginT = ( myTexFontHeight - ( row + 1 ) * aRowPixelHeight ) / ( (float)myTexFontHeight - 1.f ); 
322         aLettEndT   = aLettBeginT + ( (float)aRowPixelHeight - 1.f ) / ( (float)myTexFontHeight - 1.f );
323
324         aDX = ( (float)myWidths[aLettIndex] - 1.f ) / aXScale;
325
326         glTexCoord2f( aLettBeginS, aLettBeginT ); glVertex3f( theX,       theY,       1.f );
327         glTexCoord2f( aLettBeginS, aLettEndT   ); glVertex3f( theX,       theY + aDY, 1.f );
328         glTexCoord2f( aLettEndS,   aLettEndT   ); glVertex3f( theX + aDX, theY + aDY, 1.f );
329         glTexCoord2f( aLettEndS,   aLettBeginT ); glVertex3f( theX + aDX, theY,       1.f );
330
331         theX += aDX + mySeparator / aXScale;
332     }
333
334     glEnd();
335     // restore attributes
336     glPopAttrib();
337 }
338
339 /*!
340   \return width of string in pixels
341 */
342 int GLViewer_TexFont::getStringWidth( QString theStr )
343 {
344     int aWidth = 0;
345     for ( int i = 0; i < (int)theStr.length(); i ++ )
346     {
347         char aLetter = theStr.data()[i].toLatin1();
348         int aLettIndex = (int)aLetter - FirstSymbolNumber;
349         aWidth += myWidths[aLettIndex] + mySeparator;
350     }
351
352     return aWidth;
353 }
354
355 /*!
356   \return height of string in pixels
357 */
358 int GLViewer_TexFont::getStringHeight()
359 {
360     QFontMetrics aFM( myQFont );
361     return aFM.height();
362 }
363
364 /*!
365   Generates list base for bitmap fonts
366 */
367 static GLuint displayListBase( QFont* theFont )
368 {
369   if ( !theFont )
370     return 0;
371   GLuint aList = 0;
372   //static QMap<GLViewer_TexFindId, GLuint> fontCache;
373   GLViewer_TexFindId aFindFont;
374   aFindFont.myFontFamily = theFont->family();//theFont->toString();
375   aFindFont.myIsBold = theFont->bold();
376   aFindFont.myIsItal = theFont->italic();
377   aFindFont.myIsUndl = theFont->underline();
378   aFindFont.myPointSize = theFont->pointSize();
379
380 #ifdef WIN32
381   HGLRC ctx = ::wglGetCurrentContext();
382   if ( !ctx )
383     return aList;  
384   
385   aFindFont.myViewPortId = (int)ctx;
386
387   if ( GLViewer_TexFont::BitmapFontCache.contains( aFindFont ) )
388     aList = GLViewer_TexFont::BitmapFontCache[aFindFont];
389   else
390   {
391     GLuint listBase = 0;
392     QMap<GLViewer_TexFindId, GLuint>::iterator it = GLViewer_TexFont::BitmapFontCache.begin();
393     for ( ; it != GLViewer_TexFont::BitmapFontCache.end(); ++it )
394     {
395       if ( it.key().myViewPortId == (int)ctx && it.value() > listBase )
396         listBase = it.value();
397     }
398     listBase += 256;
399
400     HDC glHdc = ::wglGetCurrentDC();
401     ::SelectObject( glHdc, theFont->handle() );
402     if ( !::wglUseFontBitmaps( glHdc, 0, 256, listBase ) )
403       listBase = 0;
404     aList = listBase;
405     GLViewer_TexFont::BitmapFontCache[aFindFont] = aList;
406   }
407 #else //X Window
408   Display* aDisp = glXGetCurrentDisplay();
409   if( !aDisp )
410   {
411 #ifdef _DEBUG_
412     printf( "Can't find current dislay\n" );
413 #endif
414     return aList;
415   }
416   
417   GLXContext aCont = glXGetCurrentContext();
418   if( !aCont )
419   {
420 #ifdef _DEBUG_
421     printf( "Can't find current context\n" );
422 #endif
423     return aList;
424   }
425
426   aFindFont.myViewPortId = size_t(aCont);
427
428   if ( GLViewer_TexFont::BitmapFontCache.contains( aFindFont ) )
429     aList = GLViewer_TexFont::BitmapFontCache[aFindFont];
430   else
431   {
432     GLuint listBase = 0;
433     QMap<GLViewer_TexFindId, GLuint>::iterator it = GLViewer_TexFont::BitmapFontCache.begin();
434     for ( ; it != GLViewer_TexFont::BitmapFontCache.end(); ++it )
435     {
436       if ( it.key().myViewPortId == size_t(aCont) && it.value() > listBase )
437         listBase = it.value();
438     }
439     listBase += 256;
440     
441     //glXUseXFont( (Font)(theFont->handle()), 0, 256, listBase );
442     int aFontCont = 0;
443     QString aFontDef = theFont->toString();
444     char** xFontList = XListFonts( aDisp, aFontDef.toLatin1()/*aFindFont.myFontString.data()*/, 1, &aFontCont  );
445 // TODO (QT5 PORTING) Below is a temporary solution, to allow compiling with Qt 5
446 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
447     if( !theFont->handle() )
448     {
449 #endif
450 #ifdef _DEBUG_
451       printf( "Can't load font %s. loading default font....\n", aFontDef.toLatin1().data()/*aFindFont.myFontString.data()*/ );
452 #endif
453       QString aFontMask ("-*-*-*-r-*-*-");
454       aFontMask += aFontDef/*aFindFont.myFontString*/.section( ',', 1, 1 );
455 #ifdef _DEBUG_
456       printf( "Height of Default font: %s\n", aFontDef/*aFindFont.myFontString*/.section( ',', 1, 1 ).data() );
457 #endif
458       aFontMask += "-*-*-*-m-*-*-*";
459       xFontList = XListFonts( aDisp, aFontMask.toLatin1().constData()/*"-*-*-*-r-*-*-12-*-*-*-m-*-*-*"*/, 1, &aFontCont  );
460       if( aFontCont == 0 )
461       {
462 #ifdef _DEBUG_
463         printf( "Can't load default font\n" );
464 #endif
465         return 0;
466       }
467       glXUseXFont( (Font)(XLoadFont( aDisp,xFontList[0] )), 0, 256, listBase );
468 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
469     }
470     else
471       glXUseXFont( (Font)(theFont->handle()), 0, 256, listBase );
472 #endif
473     aList = listBase;
474     GLViewer_TexFont::BitmapFontCache[aFindFont] = aList;
475   }
476
477 #endif
478
479   return aList;
480 }
481
482 /*!
483   Default constructor
484 */
485 GLViewer_Drawer::GLViewer_Drawer()
486 : myFont( "Helvetica", 10, QFont::Bold )
487 {
488   myXScale = myYScale = 0.0;
489   myObjects.clear();
490   myTextList = 0/*-1*/;
491   myObjectType = "GLViewer_Object";
492   myPriority = 0;
493   myTextFormat = DTF_BITMAP;
494   myTextScale = 0.125;
495 }
496
497 /*!
498   Destructor
499 */
500 GLViewer_Drawer::~GLViewer_Drawer()
501 {
502   myObjects.clear();
503   glDeleteLists( myTextList, 1 );
504 }
505
506 /*!
507   Clears all generated textures
508 */
509 void GLViewer_Drawer::destroyAllTextures()
510 {
511     QMap<GLViewer_TexFindId,GLViewer_TexIdStored>::Iterator anIt= GLViewer_TexFont::TexFontBase.begin();
512     QMap<GLViewer_TexFindId,GLViewer_TexIdStored>::Iterator anEndIt= GLViewer_TexFont::TexFontBase.end();
513
514     for( ; anIt != anEndIt; anIt++ )
515         glDeleteTextures( 1, &(anIt.value().myTexFontId) );
516 }
517
518 /*!
519   Enables and disables antialiasing in Open GL (for points, lines and polygons).
520   \param on - if it is true, antialiasing is enabled
521 */
522 void GLViewer_Drawer::setAntialiasing(const bool on)
523 {
524         if (on)
525         {
526     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
527     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
528
529                 glEnable(GL_POINT_SMOOTH);
530                 glEnable(GL_LINE_SMOOTH);
531                 glEnable(GL_POLYGON_SMOOTH);
532                 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
533                 glEnable (GL_BLEND);
534         }
535         else
536         {
537                 glDisable(GL_POINT_SMOOTH);
538                 glDisable(GL_LINE_SMOOTH);
539                 glDisable(GL_POLYGON_SMOOTH);
540                 glBlendFunc (GL_ONE, GL_ZERO);
541                 glDisable (GL_BLEND);
542         }
543 }
544
545 /*! Loads texture from file
546   \param fileName - the name of texture file
547   \param x_size   - the horizontal size of picture ( less or equal texture horizontal size )
548   \param y_size   - the vertical size of picture ( less or equal texture vertical size )
549   \param t_size   - the size of texture ( texture vertical size equals texture horizontal size )
550 */
551 GLuint GLViewer_Drawer::loadTexture( const QString& fileName,
552                                      GLint* x_size,
553                                      GLint* y_size,
554                                      GLint* t_size )
555 {
556     QImage buf;
557     if ( fileName.isEmpty() || !buf.load( fileName ) )
558         return 0;
559
560     int w = buf.width();
561     int h = buf.height();
562
563     int size = 16;
564     while( size < w || size < h )
565         size = size * 2;
566
567     GLuint texture;
568     GLubyte* pixels = new GLubyte[ size * size * 4 ];
569
570     for( int i = 0; i < size; i++ )
571     {            
572         for( int j = 0; j < size; j++ )
573         {
574             GLubyte r, g, b, a;
575             if( j < w && i < h )
576             {
577                 QRgb pixel = buf.pixel( j, h - i - 1 );
578                 r = (GLubyte)qRed( pixel );
579                 g = (GLubyte)qGreen( pixel );
580                 b = (GLubyte)qBlue( pixel );
581                 a = (GLubyte)qAlpha( pixel );
582             }
583             else
584             {
585                 r = (GLubyte)255;
586                 g = (GLubyte)255;
587                 b = (GLubyte)255;
588                 a = (GLubyte)255;
589             }
590
591             int index = 4 * ( i * size + j );
592             pixels[ index ] = r;
593             pixels[ index + 1 ] = g;
594             pixels[ index + 2 ] = b;
595             pixels[ index + 3 ] = a;
596         }
597     }
598
599     //initialize texture
600     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
601     glGenTextures( 1, &texture );
602     glBindTexture( GL_TEXTURE_2D, texture );
603     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
604     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
605     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0,
606                   GL_RGBA, GL_UNSIGNED_BYTE, pixels );
607
608     delete[] pixels;
609
610     if ( x_size )
611       *(x_size) = w;
612
613     if ( y_size )
614       *(y_size) = h;
615
616     if ( t_size )
617       *(t_size) = size;
618
619     return texture;
620 }
621
622 /*! Draw square texture
623    \param texture - the texture ID
624    \param size    - the size of square texture
625    \param x       - x coord
626    \param y       - y coord
627 */
628 void GLViewer_Drawer::drawTexture( GLuint texture, GLint size, GLfloat x, GLfloat y )
629 {
630     /*float xScale = myXScale;
631     float yScale = myYScale;
632
633     glColor4f( 1.0, 1.0, 1.0, 1.0 );
634
635     glEnable( GL_TEXTURE_2D );
636     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
637     glAlphaFunc( GL_GREATER, 0.95F );
638     glEnable( GL_ALPHA_TEST );
639     
640     glBindTexture( GL_TEXTURE_2D, texture );
641     glBegin( GL_QUADS );
642
643     glTexCoord2f( 0.0, 0.0 );
644     glVertex3f( x-size/2./xScale, y-size/2./yScale, 0.0 );
645
646     glTexCoord2f( 0.0, 1.0 );
647     glVertex3f( x-size/2./xScale, y+size/2./yScale, 0.0 );
648
649     glTexCoord2f( 1.0, 1.0 );
650     glVertex3f( x+size/2./xScale, y+size/2./yScale, 0.0 );
651
652     glTexCoord2f( 1.0, 0.0 );
653     glVertex3f( x+size/2./xScale, y-size/2./yScale, 0.0 );
654     
655     glEnd();
656     glFlush();
657
658     glDisable( GL_ALPHA_TEST );
659     glDisable( GL_TEXTURE_2D );*/
660
661   drawTexture( texture, size, size, x, y );
662 }
663
664 /*! Draw texture
665    \param texture - the texture ID
666    \param x_size  - the horizontal size of texture
667    \param y_size  - the vertical size of texture
668    \param x       - x coord
669    \param y       - y coord
670 */
671 void GLViewer_Drawer::drawTexture( GLuint texture, GLint x_size, GLint y_size, GLfloat x, GLfloat y )
672 {
673     /*float xScale = myXScale;
674     float yScale = myYScale;
675
676     glColor4f( 1.0, 1.0, 1.0, 1.0 );
677
678     glEnable( GL_TEXTURE_2D );
679     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
680     glAlphaFunc( GL_GREATER, 0.95F );
681     glEnable( GL_ALPHA_TEST );
682     
683     glBindTexture( GL_TEXTURE_2D, texture );
684     glBegin( GL_QUADS );
685
686     glTexCoord2f( 0.0, 0.0 );
687     glVertex3f( x-x_size/2./xScale, y-y_size/2./yScale, 0.0 );
688
689     glTexCoord2f( 0.0, 1.0 );
690     glVertex3f( x-x_size/2./xScale, y+y_size/2./yScale, 0.0 );
691
692     glTexCoord2f( 1.0, 1.0 );
693     glVertex3f( x+x_size/2./xScale, y+y_size/2./yScale, 0.0 );
694
695     glTexCoord2f( 1.0, 0.0 );
696     glVertex3f( x+x_size/2./xScale, y-y_size/2./yScale, 0.0 );
697     
698     glEnd();
699     glFlush();
700
701     glDisable( GL_ALPHA_TEST );
702     glDisable( GL_TEXTURE_2D );*/
703   drawTexturePart( texture, 1.0, 1.0, x_size, y_size, x, y );
704 }
705
706 /*! Draw texture part
707    \param texture - the texture ID
708    \param x_ratio - the horizontal ratio of texture part
709    \param y_ratio - the vertical ratio of texture part
710    \param x_size  - the horizontal size of texture
711    \param y_size  - the vertical size of texture
712    \param x       - x coord
713    \param y       - y coord
714    \param scale   - common scale factor ( if = 0, use drawer scales )
715 */
716 void GLViewer_Drawer::drawTexturePart( GLuint texture,
717                                        GLfloat x_ratio,
718                                        GLfloat y_ratio,
719                                        GLfloat x_size,
720                                        GLfloat y_size,
721                                        GLfloat x,
722                                        GLfloat y,
723                                        GLfloat scale )
724 {
725   if( !texture )
726     return;
727
728   float xScale = scale > 0. ? 1./scale : myXScale;
729   float yScale = scale > 0. ? 1./scale : myYScale;
730
731   glColor4f( 1.0, 1.0, 1.0, 1.0 );
732
733
734   glEnable( GL_TEXTURE_2D );
735   glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
736   bool hasAlpha = glIsEnabled( GL_ALPHA_TEST );
737   glDisable( GL_ALPHA_TEST );
738
739   glBindTexture( GL_TEXTURE_2D, texture );
740   glBegin( GL_QUADS );
741
742   glTexCoord2f( 0.0, 0.0 );
743   glVertex3f( x-x_size/2./xScale, y-y_size/2./yScale, 0.0 );
744
745   glTexCoord2f( 0.0, y_ratio );
746   glVertex3f( x-x_size/2./xScale, y+y_size/2./yScale, 0.0 );
747
748   glTexCoord2f( x_ratio, y_ratio );
749   glVertex3f( x+x_size/2./xScale, y+y_size/2./yScale, 0.0 );
750
751   glTexCoord2f( x_ratio, 0.0 );
752   glVertex3f( x+x_size/2./xScale, y-y_size/2./yScale, 0.0 );
753   
754   glEnd();
755   glFlush();
756
757   if ( hasAlpha )
758     glEnable( GL_ALPHA_TEST );
759
760   glDisable( GL_TEXTURE_2D );
761 }
762
763 /*!
764   Draw text
765   \param text - text to be drawn
766   \param xPos - x position
767   \param yPos - y position
768   \param color - color of text
769   \param theFont - font of text
770   \param theSeparator - letter separator
771   \param theFormat - text format (by default DTF_BITMAP)
772 */
773 void GLViewer_Drawer::drawText( const QString& text, GLfloat xPos, GLfloat yPos,
774                                 const QColor& color, QFont* theFont, int theSeparator, DisplayTextFormat theFormat )
775 {
776   glColor3f( ( GLfloat )color.red() / 255, 
777              ( GLfloat )color.green() / 255, 
778              ( GLfloat )color.blue() / 255 );
779
780   if( theFormat != DTF_BITMAP )
781   {
782     GLViewer_TexFont aTexFont( theFont, theSeparator, theFormat == DTF_TEXTURE_SCALABLE, GL_LINEAR );
783     // Font texture was not found or generated --> cannot draw text
784     if ( !aTexFont.generateTexture() )
785       return;
786
787     if ( theFormat == DTF_TEXTURE_SCALABLE )
788       aTexFont.drawString( text, xPos, yPos, textScale() );
789     else
790       aTexFont.drawString( text, xPos, yPos );
791   }
792   else
793   {
794     glRasterPos2f( xPos, yPos );
795     glListBase( displayListBase( theFont ) );
796     glCallLists( text.length(), GL_UNSIGNED_BYTE, text.toLocal8Bit().data() );
797   }
798 }
799
800 /*!
801   Draws object-text
802 */
803 void GLViewer_Drawer::drawText( GLViewer_Object* theObject )
804 {
805   if( !theObject )
806     return;
807
808   GLViewer_Text* aText = theObject->getGLText();
809   if( !aText )
810     return;
811
812   GLfloat aPosX, aPosY;
813   aText->getPosition( aPosX, aPosY );
814   // get temporary copy of font
815   QFont aTmpVarFont = aText->getFont();
816   drawText( aText->getText(), aPosX, aPosY, aText->getColor(), &aTmpVarFont, aText->getSeparator(), aText->getDisplayTextFormat() );
817 }
818
819 /*! Draw text
820    \param text      - the text string
821    \param x         - x coord
822    \param y         - y coord
823    \param hPosition - horizontal alignment
824    \param vPosition - vertical alignment
825    \param color     - text color
826    \param smallFont - font format
827 */
828 void GLViewer_Drawer::drawGLText( QString text, float x, float y,
829                                   int hPosition, int vPosition, QColor color, bool smallFont )
830 {
831   QFont aFont( myFont );
832   if( smallFont )
833     aFont.setPointSize( int(aFont.pointSize() * 0.8) );
834
835   GLfloat scale = textScale() > 0. ? textScale() : 1.;
836
837   QFontMetrics aFontMetrics( aFont );
838   float width  = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.width( text ) * scale : aFontMetrics.width( text ) / myXScale;
839   float height = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.height() * scale : aFontMetrics.height() / myYScale;
840   float gap = 5 / myXScale;
841
842   switch( hPosition )
843   {
844       case GLText_Left   : x -= ( gap + width ); break;
845       case GLText_Center : x -= width / 2; break;
846       case GLText_Right  : x += gap; break;
847       default : break;
848   }
849
850   switch( vPosition )
851   {
852       case GLText_Top    : y += height * 0.5; break;
853       case GLText_Center : y -= height * 0.5; break;
854       case GLText_Bottom : y -= height * 1.5; break;
855       default : break;
856   }
857
858   drawText( text, x, y, color, &aFont, 2, myTextFormat );
859 }
860
861 /*!
862   \return a rectangle of text (without viewer scale)
863 */
864 GLViewer_Rect GLViewer_Drawer::textRect( const QString& text ) const
865 {
866   GLfloat scale = textScale() > 0. ? textScale() : 1.;
867
868   QFontMetrics aFontMetrics( myFont );
869   float width  = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.width( text ) * scale : aFontMetrics.width( text );
870   float height = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.height() * scale : aFontMetrics.height();
871
872   return GLViewer_Rect( 0, width, height, 0 );
873 }
874
875 /*!
876   Draws rectangle
877   \param rect - instance of primitive
878   \param color - color of primitive
879 */
880 void GLViewer_Drawer::drawRectangle( GLViewer_Rect* rect, QColor color )
881 {
882   if( !rect )
883     return;
884
885   float x1 = rect->left();
886   float x2 = rect->right();
887   float y1 = rect->bottom();
888   float y2 = rect->top();
889   
890   glColor3f( ( GLfloat )color.red() / 255,
891     ( GLfloat )color.green() / 255,
892     ( GLfloat )color.blue() / 255 );
893   glLineWidth( 1.0 );
894   
895   glBegin( GL_LINE_LOOP );
896   glVertex2f( x1, y1 );
897   glVertex2f( x1, y2 );
898   glVertex2f( x2, y2 );
899   glVertex2f( x2, y1 );
900   glEnd();
901 }
902
903 /*!
904   Saves object to file with format of HPGL
905   \param hFile - file
906   \param aViewerCS - viewer co-ordinate system
907   \param aHPGLCS - paper co-ordinate system
908 */
909 bool GLViewer_Drawer::translateToHPGL( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aHPGLCS )
910 {
911     bool result = true;
912     for( int i=0, n=myObjects.count(); i<n; i++ ) 
913         result &= myObjects[i]->translateToHPGL( hFile, aViewerCS, aHPGLCS );
914     return result;
915 }
916
917 /*!
918   Saves object to file with format of PostScript
919   \param hFile - file
920   \param aViewerCS - viewer co-ordinate system
921   \param aPSCS - paper co-ordinate system
922 */
923 bool GLViewer_Drawer::translateToPS( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS )
924 {
925     bool result = true;
926     for( int i=0, n=myObjects.count(); i<n; i++ ) 
927         result &= myObjects[i]->translateToPS( hFile, aViewerCS, aPSCS );
928     return result;
929 }
930
931 #ifdef WIN32
932 /*!
933   Saves object to file with format of EMF
934   \param hFile - file
935   \param aViewerCS - viewer co-ordinate system
936   \param aEMFCS - paper co-ordinate system
937 */
938 bool GLViewer_Drawer::translateToEMF( HDC hDC, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aEMFCS )
939 {
940     bool result = true;
941     for( int i=0, n=myObjects.count(); i<n; i++ ) 
942         result &= myObjects[i]->translateToEMF( hDC, aViewerCS, aEMFCS );
943     return result;
944 }
945 #endif
946
947 /*!
948   Draws rectangle
949   \param rect - instance of primitive
950   \param lineWidth - width of line
951   \param gap - gap of rectangle
952   \param color - color of primitive
953   \param filled - if it is true, then rectangle will be drawn filled with color "fillingColor"
954   \param fillingColor - color of filling
955 */
956 void GLViewer_Drawer::drawRectangle( GLViewer_Rect* rect, GLfloat lineWidth, GLfloat gap,
957                                      QColor color, bool filled, QColor fillingColor )
958 {
959   if( !rect )
960     return;
961
962   float x1 = rect->left() - gap;
963   float x2 = rect->right() + gap;
964   float y1 = rect->bottom() - gap;
965   float y2 = rect->top() + gap;
966   
967   if( filled )
968   {
969     glColor3f( ( GLfloat )fillingColor.red() / 255,
970       ( GLfloat )fillingColor.green() / 255,
971       ( GLfloat )fillingColor.blue() / 255 );
972     glBegin( GL_POLYGON );
973     glVertex2f( x1, y1 );
974     glVertex2f( x1, y2 );
975     glVertex2f( x2, y2 );
976     glVertex2f( x2, y1 );
977     glEnd();
978   }
979
980   glColor3f( ( GLfloat )color.red() / 255,
981     ( GLfloat )color.green() / 255,
982     ( GLfloat )color.blue() / 255 );
983   glLineWidth( lineWidth );
984   
985   glBegin( GL_LINE_LOOP );
986   glVertex2f( x1, y1 );
987   glVertex2f( x1, y2 );
988   glVertex2f( x2, y2 );
989   glVertex2f( x2, y1 );
990   glEnd();
991 }
992
993 /*!
994   Draws contour
995   \param pntList - list of points
996   \param color - color of contour
997   \param lineWidth - width of line
998 */
999 void GLViewer_Drawer::drawContour( const GLViewer_PntList& pntList, QColor color, GLfloat lineWidth )
1000 {
1001   glColor3f( ( GLfloat )color.red() / 255,
1002     ( GLfloat )color.green() / 255,
1003     ( GLfloat )color.blue() / 255 );
1004   glLineWidth( lineWidth );
1005   
1006   glBegin( GL_LINES );
1007   QList<GLViewer_Pnt>::const_iterator it = pntList.begin();
1008   for( ; it != pntList.end(); ++it )
1009     glVertex2f( (*it).x(), (*it).y() );
1010   glEnd();
1011 }
1012
1013 /*!
1014   Draws rectangular contour
1015   \param rect - instance of rectangle
1016   \param color - color of primitive
1017   \param lineWidth - width of line
1018   \param pattern - pattern of line
1019   \param isStripe - enables line stipple
1020 */
1021 void GLViewer_Drawer::drawContour( GLViewer_Rect* rect, QColor color, GLfloat lineWidth,
1022                                    GLushort pattern, bool isStripe )
1023 {
1024   float x1 = rect->left();
1025   float x2 = rect->right();
1026   float y1 = rect->bottom();
1027   float y2 = rect->top();
1028   
1029   glColor3f( ( GLfloat )color.red() / 255,
1030     ( GLfloat )color.green() / 255,
1031     ( GLfloat )color.blue() / 255 );
1032   glLineWidth( lineWidth );
1033   
1034   if ( isStripe )
1035   {
1036     glEnable( GL_LINE_STIPPLE );
1037     glLineStipple( 1, pattern );
1038   }
1039
1040   glBegin( GL_LINE_LOOP );
1041
1042   glVertex2f( x1, y1 );
1043   glVertex2f( x1, y2 );
1044   glVertex2f( x2, y2 );
1045   glVertex2f( x2, y1 );
1046
1047   glEnd();
1048   glDisable( GL_LINE_STIPPLE );
1049 }
1050
1051 /*!
1052   Draws polygon
1053   \param pntList - list of points
1054   \param color - color of polygon
1055 */
1056 void GLViewer_Drawer::drawPolygon( const GLViewer_PntList& pntList, QColor color )
1057 {
1058   glColor3f( ( GLfloat )color.red() / 255,
1059     ( GLfloat )color.green() / 255,
1060     ( GLfloat )color.blue() / 255 );
1061   glBegin( GL_POLYGON );
1062   QList<GLViewer_Pnt>::const_iterator it = pntList.begin();
1063   for( ; it != pntList.end(); ++it )
1064     glVertex2f( (*it).x(), (*it).y() );
1065   glEnd();
1066 }
1067
1068 /*!
1069   Draws rectangle
1070   \param rect - instance of rectangle
1071   \param color - color of polygon
1072   \param pattern - pattern of line
1073   \param isStripe - enables line stipple
1074 */
1075 void GLViewer_Drawer::drawPolygon( GLViewer_Rect* rect, QColor color,
1076                                       GLushort pattern, bool isStripe )
1077 {
1078   float x1 = rect->left();
1079   float x2 = rect->right();
1080   float y1 = rect->bottom();
1081   float y2 = rect->top();
1082   glColor3f( ( GLfloat )color.red() / 255,
1083     ( GLfloat )color.green() / 255,
1084     ( GLfloat )color.blue() / 255 );
1085
1086   if ( isStripe )
1087   {
1088     glEnable( GL_LINE_STIPPLE );
1089     glLineStipple( 1, pattern );
1090   }
1091   glBegin( GL_POLYGON );
1092
1093   glVertex2f( x1, y1 );
1094   glVertex2f( x1, y2 );
1095   glVertex2f( x2, y2 );
1096   glVertex2f( x2, y1 );
1097
1098   glEnd();
1099   glDisable( GL_LINE_STIPPLE );
1100 }
1101
1102 GLubyte rasterVertex[5] = { 0x70, 0xf8, 0xf8, 0xf8, 0x70 };
1103
1104 /*!
1105   Draws vertex
1106   \param x - x position
1107   \param y - y position
1108   \param color - color of vertex
1109 */
1110 void GLViewer_Drawer::drawVertex( GLfloat x, GLfloat y, QColor color )
1111 {
1112   glColor3f( ( GLfloat )color.red() / 255, ( GLfloat )color.green() / 255, ( GLfloat )color.blue() / 255 );
1113   glRasterPos2f( x, y );
1114   glBitmap( 5, 5, 2, 2, 0, 0, rasterVertex );
1115 }
1116
1117 GLubyte rasterCross[7] =  { 0x82, 0x44, 0x28, 0x10, 0x28, 0x44, 0x82 };
1118
1119 /*!
1120   Draws cross
1121   \param x - x position
1122   \param y - y position
1123   \param color - color of cross
1124 */
1125 void GLViewer_Drawer::drawCross( GLfloat x, GLfloat y, QColor color )
1126 {
1127   glColor3f( ( GLfloat )color.red() / 255, ( GLfloat )color.green() / 255, ( GLfloat )color.blue() / 255 );
1128   glRasterPos2f( x, y );
1129   glBitmap( 7, 7, 3, 3, 0, 0, rasterCross );
1130 }
1131
1132 /*!
1133   Draws arrow
1134   \param red, green, blue - components of color
1135   \param lineWidth - width of line
1136   \param staff - 
1137   \param length - length of arrow
1138   \param width - width of arrow
1139   \param x - x position
1140   \param y - y position
1141   \param angle - angle of arrow
1142   \param filled - drawn as filled
1143 */
1144 void GLViewer_Drawer::drawArrow( const GLfloat red, const GLfloat green, const GLfloat blue,
1145                                  GLfloat lineWidth,
1146                                  GLfloat staff, GLfloat length, GLfloat width,
1147                                  GLfloat x, GLfloat y, GLfloat angle, GLboolean filled )
1148 {
1149   GLfloat vx1 = x;
1150   GLfloat vy1 = y + staff + length;
1151   GLfloat vx2 = vx1 - width / 2;
1152   GLfloat vy2 = vy1 - length;
1153   GLfloat vx3 = vx1 + width / 2;
1154   GLfloat vy3 = vy1 - length;
1155
1156   gp_Pnt2d p0( x, y );
1157   gp_Pnt2d p1( vx1, vy1 );
1158   gp_Pnt2d p2( vx2, vy2 );
1159   gp_Pnt2d p3( vx3, vy3 );
1160
1161   p1.Rotate( p0, angle );
1162   p2.Rotate( p0, angle );
1163   p3.Rotate( p0, angle );
1164   
1165   vx1 = p1.X(); vy1 = p1.Y();
1166   vx2 = p2.X(); vy2 = p2.Y();
1167   vx3 = p3.X(); vy3 = p3.Y();
1168
1169   glColor3f( red, green, blue );
1170   glLineWidth( lineWidth );
1171
1172   glBegin( GL_LINES );
1173   glVertex2f( x, y );
1174   glVertex2f( vx1, vy1 );
1175   glEnd();
1176
1177   filled = true;
1178   if( !filled )
1179   {
1180     glBegin( GL_LINES );
1181     glVertex2f( vx1, vy1 );
1182     glVertex2f( vx2, vy2 );
1183     glVertex2f( vx1, vy1 );
1184     glVertex2f( vx3, vy3 );
1185     glEnd();
1186   }
1187   else
1188   {
1189     glBegin( GL_POLYGON );
1190     glVertex2f( vx1, vy1 );
1191     glVertex2f( vx2, vy2 );
1192     glVertex2f( vx3, vy3 );
1193     glEnd();
1194   }
1195 }