1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // Author : OPEN CASCADE
24 // File: GLViewer_Drawer.cxx
25 // Created: November, 2004
26 //#include <GLViewerAfx.h>
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"
34 #include <QApplication>
43 #include <gp_Pnt2d.hxx>
46 // Two texture components for texmapped fonts: luminance and alpha
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
53 GLfloat modelMatrix[16];
56 //! code of first font symbol
57 static int FirstSymbolNumber = 32;
58 //! code of last font symbol
59 static int LastSymbolNumber = 127;
61 QMap<GLViewer_TexFindId,GLViewer_TexIdStored> GLViewer_TexFont::TexFontBase;
62 QMap<GLViewer_TexFindId,GLuint> GLViewer_TexFont::BitmapFontCache;
65 Clears all generated fonts
67 void GLViewer_TexFont::clearTextBases()
69 //cout << "Clear font map" << endl;
71 BitmapFontCache.clear();
77 GLViewer_TexFont::GLViewer_TexFont()
78 : myMaxRowWidth( 0 ), myFontHeight( 0 )
80 myQFont = QApplication::font();//QFont::defaultFont();
82 myIsResizeable = false;
83 myMinMagFilter = GL_LINEAR;
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
95 GLViewer_TexFont::GLViewer_TexFont( QFont* theFont, int theSeparator, bool theIsResizeable, GLuint theMinMagFilter )
96 : myMaxRowWidth( 0 ), myFontHeight( 0 )
99 mySeparator = theSeparator;
100 myIsResizeable = theIsResizeable;
101 myMinMagFilter = theMinMagFilter;
109 GLViewer_TexFont::~GLViewer_TexFont()
112 delete[] myPositions;
116 Initializes font parameters
118 void GLViewer_TexFont::init()
120 myNbSymbols = LastSymbolNumber - FirstSymbolNumber + 1;
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();
130 myWidths = new int[myNbSymbols];
131 myPositions = new int[myNbSymbols];
133 for( int i = 0, k = FirstSymbolNumber, aWidth = 0; i < myNbSymbols; i++, k++ )
135 // is it time to start a new row?
136 if ( !( i % TEX_ROW_LEN ) )
138 if( aWidth > myMaxRowWidth )
139 myMaxRowWidth = aWidth;
142 myWidths[i] = aFM.width( k );
143 myPositions[i] = aWidth;
144 aWidth += myWidths[i] + 2;
152 Generating font texture
154 bool GLViewer_TexFont::generateTexture()
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());
164 if( TexFontBase.contains( aFindFont ) )
166 GLViewer_TexIdStored aTexture = TexFontBase[ aFindFont ];
167 myTexFont = aTexture.myTexFontId;
168 myTexFontWidth = aTexture.myTexFontWidth;
169 myTexFontHeight = aTexture.myTexFontHeight;
173 // Adding some pixels to have a gap between rows
174 int aRowPixelHeight = myFontHeight + TEX_ROW_GAP;
175 int aDescent = QFontMetrics( myQFont ).descent();
177 int aNumRows = myNbSymbols / TEX_ROW_LEN;
178 if ( myNbSymbols % TEX_ROW_LEN )
180 int pixelsHight = aNumRows * aRowPixelHeight;
183 myTexFontHeight = 64;
185 while( myTexFontWidth < myMaxRowWidth )
186 myTexFontWidth <<= 1;
187 while( myTexFontHeight < pixelsHight )
188 myTexFontHeight <<= 1;
190 // Checking whether the texture dimensions for the requested font
191 // do not exceed the maximum size supported by the OpenGL implementation
193 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maxSize );
194 if ( myTexFontWidth > maxSize || myTexFontHeight > maxSize )
197 QPixmap aPixmap( myTexFontWidth, myTexFontHeight );
198 aPixmap.fill( QColor( 0, 0, 0) );
199 QPainter aPainter( &aPixmap );
200 aPainter.setFont( myQFont );
202 for( int l = 0; l < myNbSymbols; l++ )
204 row = l / TEX_ROW_LEN;
206 aLetter += (char)(FirstSymbolNumber + l);
207 aPainter.setPen( QColor( 255,255,255) );
208 aPainter.drawText( myPositions[l], ( row + 1 ) * aRowPixelHeight - aDescent, aLetter );
211 QImage aImage = aPixmap.toImage();
215 // aImage.save("w:\\work\\CATHARE\\texture.png", "PNG");
217 char* pixels = new char[myTexFontWidth * myTexFontHeight * NB_TEX_COMP];
219 for( int i = 0; i < myTexFontHeight; i++ )
221 for( int j = 0; j < myTexFontWidth; j++ )
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 ) );
227 if( aRed != 0 || aGreen != 0 || aBlue != 0 )
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;
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;
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,
257 GLViewer_TexIdStored aTexture;
258 aTexture.myTexFontId = myTexFont;
259 aTexture.myTexFontWidth = myTexFontWidth;
260 aTexture.myTexFontHeight = myTexFontHeight;
262 TexFontBase.insert( aFindFont, aTexture );
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
274 void GLViewer_TexFont::drawString( QString theStr, GLdouble theX , GLdouble theY, GLfloat theScale )
276 // Adding some pixels to have a gap between rows
277 int aRowPixelHeight = myFontHeight + TEX_ROW_GAP;
279 float aXScale = 1.f, aYScale = 1.f;
280 if ( !myIsResizeable )
282 glGetFloatv (GL_MODELVIEW_MATRIX, modelMatrix);
283 aXScale = modelMatrix[0];
284 aYScale = modelMatrix[5];
286 else if ( theScale > 0.f )
288 aXScale = aXScale / theScale;
289 aYScale = aYScale / theScale;
293 glPushAttrib( GL_ENABLE_BIT | GL_TEXTURE_BIT );
295 glEnable(GL_TEXTURE_2D);
296 glPixelTransferi(GL_MAP_COLOR, 0);
298 glAlphaFunc(GL_GEQUAL, 0.05F);
299 glEnable(GL_ALPHA_TEST);
302 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
304 glBindTexture(GL_TEXTURE_2D, myTexFont);
305 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
309 float aLettBeginS, aLettEndS, aLettBeginT, aLettEndT;
310 float aDY = ( aRowPixelHeight - 1 ) / aYScale, aDX;
313 for ( int i = 0; i < (int)theStr.length(); i++ )
315 aLetter = theStr.data()[i].toLatin1();
316 aLettIndex = (int)aLetter - FirstSymbolNumber;
317 row = aLettIndex / TEX_ROW_LEN;
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 );
324 aDX = ( (float)myWidths[aLettIndex] - 1.f ) / aXScale;
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 );
331 theX += aDX + mySeparator / aXScale;
335 // restore attributes
340 \return width of string in pixels
342 int GLViewer_TexFont::getStringWidth( QString theStr )
345 for ( int i = 0; i < (int)theStr.length(); i ++ )
347 char aLetter = theStr.data()[i].toLatin1();
348 int aLettIndex = (int)aLetter - FirstSymbolNumber;
349 aWidth += myWidths[aLettIndex] + mySeparator;
356 \return height of string in pixels
358 int GLViewer_TexFont::getStringHeight()
360 QFontMetrics aFM( myQFont );
365 Generates list base for bitmap fonts
367 static GLuint displayListBase( QFont* theFont )
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();
381 HGLRC ctx = ::wglGetCurrentContext();
385 aFindFont.myViewPortId = (int)ctx;
387 if ( GLViewer_TexFont::BitmapFontCache.contains( aFindFont ) )
388 aList = GLViewer_TexFont::BitmapFontCache[aFindFont];
392 QMap<GLViewer_TexFindId, GLuint>::iterator it = GLViewer_TexFont::BitmapFontCache.begin();
393 for ( ; it != GLViewer_TexFont::BitmapFontCache.end(); ++it )
395 if ( it.key().myViewPortId == (int)ctx && it.value() > listBase )
396 listBase = it.value();
400 HDC glHdc = ::wglGetCurrentDC();
401 ::SelectObject( glHdc, theFont->handle() );
402 if ( !::wglUseFontBitmaps( glHdc, 0, 256, listBase ) )
405 GLViewer_TexFont::BitmapFontCache[aFindFont] = aList;
408 Display* aDisp = glXGetCurrentDisplay();
412 printf( "Can't find current dislay\n" );
417 GLXContext aCont = glXGetCurrentContext();
421 printf( "Can't find current context\n" );
426 aFindFont.myViewPortId = size_t(aCont);
428 if ( GLViewer_TexFont::BitmapFontCache.contains( aFindFont ) )
429 aList = GLViewer_TexFont::BitmapFontCache[aFindFont];
433 QMap<GLViewer_TexFindId, GLuint>::iterator it = GLViewer_TexFont::BitmapFontCache.begin();
434 for ( ; it != GLViewer_TexFont::BitmapFontCache.end(); ++it )
436 if ( it.key().myViewPortId == size_t(aCont) && it.value() > listBase )
437 listBase = it.value();
441 //glXUseXFont( (Font)(theFont->handle()), 0, 256, listBase );
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() )
451 printf( "Can't load font %s. loading default font....\n", aFontDef.toLatin1().data()/*aFindFont.myFontString.data()*/ );
453 QString aFontMask ("-*-*-*-r-*-*-");
454 aFontMask += aFontDef/*aFindFont.myFontString*/.section( ',', 1, 1 );
456 printf( "Height of Default font: %s\n", aFontDef/*aFindFont.myFontString*/.section( ',', 1, 1 ).data() );
458 aFontMask += "-*-*-*-m-*-*-*";
459 xFontList = XListFonts( aDisp, aFontMask.toLatin1().constData()/*"-*-*-*-r-*-*-12-*-*-*-m-*-*-*"*/, 1, &aFontCont );
463 printf( "Can't load default font\n" );
467 glXUseXFont( (Font)(XLoadFont( aDisp,xFontList[0] )), 0, 256, listBase );
468 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
471 glXUseXFont( (Font)(theFont->handle()), 0, 256, listBase );
474 GLViewer_TexFont::BitmapFontCache[aFindFont] = aList;
485 GLViewer_Drawer::GLViewer_Drawer()
486 : myFont( "Helvetica", 10, QFont::Bold )
488 myXScale = myYScale = 0.0;
490 myTextList = 0/*-1*/;
491 myObjectType = "GLViewer_Object";
493 myTextFormat = DTF_BITMAP;
500 GLViewer_Drawer::~GLViewer_Drawer()
503 glDeleteLists( myTextList, 1 );
507 Clears all generated textures
509 void GLViewer_Drawer::destroyAllTextures()
511 QMap<GLViewer_TexFindId,GLViewer_TexIdStored>::Iterator anIt= GLViewer_TexFont::TexFontBase.begin();
512 QMap<GLViewer_TexFindId,GLViewer_TexIdStored>::Iterator anEndIt= GLViewer_TexFont::TexFontBase.end();
514 for( ; anIt != anEndIt; anIt++ )
515 glDeleteTextures( 1, &(anIt.value().myTexFontId) );
519 Enables and disables antialiasing in Open GL (for points, lines and polygons).
520 \param on - if it is true, antialiasing is enabled
522 void GLViewer_Drawer::setAntialiasing(const bool on)
526 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
527 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
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);
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);
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 )
551 GLuint GLViewer_Drawer::loadTexture( const QString& fileName,
557 if ( fileName.isEmpty() || !buf.load( fileName ) )
561 int h = buf.height();
564 while( size < w || size < h )
568 GLubyte* pixels = new GLubyte[ size * size * 4 ];
570 for( int i = 0; i < size; i++ )
572 for( int j = 0; j < size; j++ )
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 );
591 int index = 4 * ( i * size + j );
593 pixels[ index + 1 ] = g;
594 pixels[ index + 2 ] = b;
595 pixels[ index + 3 ] = a;
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 );
622 /*! Draw square texture
623 \param texture - the texture ID
624 \param size - the size of square texture
628 void GLViewer_Drawer::drawTexture( GLuint texture, GLint size, GLfloat x, GLfloat y )
630 /*float xScale = myXScale;
631 float yScale = myYScale;
633 glColor4f( 1.0, 1.0, 1.0, 1.0 );
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 );
640 glBindTexture( GL_TEXTURE_2D, texture );
643 glTexCoord2f( 0.0, 0.0 );
644 glVertex3f( x-size/2./xScale, y-size/2./yScale, 0.0 );
646 glTexCoord2f( 0.0, 1.0 );
647 glVertex3f( x-size/2./xScale, y+size/2./yScale, 0.0 );
649 glTexCoord2f( 1.0, 1.0 );
650 glVertex3f( x+size/2./xScale, y+size/2./yScale, 0.0 );
652 glTexCoord2f( 1.0, 0.0 );
653 glVertex3f( x+size/2./xScale, y-size/2./yScale, 0.0 );
658 glDisable( GL_ALPHA_TEST );
659 glDisable( GL_TEXTURE_2D );*/
661 drawTexture( texture, size, size, x, y );
665 \param texture - the texture ID
666 \param x_size - the horizontal size of texture
667 \param y_size - the vertical size of texture
671 void GLViewer_Drawer::drawTexture( GLuint texture, GLint x_size, GLint y_size, GLfloat x, GLfloat y )
673 /*float xScale = myXScale;
674 float yScale = myYScale;
676 glColor4f( 1.0, 1.0, 1.0, 1.0 );
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 );
683 glBindTexture( GL_TEXTURE_2D, texture );
686 glTexCoord2f( 0.0, 0.0 );
687 glVertex3f( x-x_size/2./xScale, y-y_size/2./yScale, 0.0 );
689 glTexCoord2f( 0.0, 1.0 );
690 glVertex3f( x-x_size/2./xScale, y+y_size/2./yScale, 0.0 );
692 glTexCoord2f( 1.0, 1.0 );
693 glVertex3f( x+x_size/2./xScale, y+y_size/2./yScale, 0.0 );
695 glTexCoord2f( 1.0, 0.0 );
696 glVertex3f( x+x_size/2./xScale, y-y_size/2./yScale, 0.0 );
701 glDisable( GL_ALPHA_TEST );
702 glDisable( GL_TEXTURE_2D );*/
703 drawTexturePart( texture, 1.0, 1.0, x_size, y_size, x, y );
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
714 \param scale - common scale factor ( if = 0, use drawer scales )
716 void GLViewer_Drawer::drawTexturePart( GLuint texture,
728 float xScale = scale > 0. ? 1./scale : myXScale;
729 float yScale = scale > 0. ? 1./scale : myYScale;
731 glColor4f( 1.0, 1.0, 1.0, 1.0 );
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 );
739 glBindTexture( GL_TEXTURE_2D, texture );
742 glTexCoord2f( 0.0, 0.0 );
743 glVertex3f( x-x_size/2./xScale, y-y_size/2./yScale, 0.0 );
745 glTexCoord2f( 0.0, y_ratio );
746 glVertex3f( x-x_size/2./xScale, y+y_size/2./yScale, 0.0 );
748 glTexCoord2f( x_ratio, y_ratio );
749 glVertex3f( x+x_size/2./xScale, y+y_size/2./yScale, 0.0 );
751 glTexCoord2f( x_ratio, 0.0 );
752 glVertex3f( x+x_size/2./xScale, y-y_size/2./yScale, 0.0 );
758 glEnable( GL_ALPHA_TEST );
760 glDisable( GL_TEXTURE_2D );
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)
773 void GLViewer_Drawer::drawText( const QString& text, GLfloat xPos, GLfloat yPos,
774 const QColor& color, QFont* theFont, int theSeparator, DisplayTextFormat theFormat )
776 glColor3f( ( GLfloat )color.red() / 255,
777 ( GLfloat )color.green() / 255,
778 ( GLfloat )color.blue() / 255 );
780 if( theFormat != DTF_BITMAP )
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() )
787 if ( theFormat == DTF_TEXTURE_SCALABLE )
788 aTexFont.drawString( text, xPos, yPos, textScale() );
790 aTexFont.drawString( text, xPos, yPos );
794 glRasterPos2f( xPos, yPos );
795 glListBase( displayListBase( theFont ) );
796 glCallLists( text.length(), GL_UNSIGNED_BYTE, text.toLocal8Bit().data() );
803 void GLViewer_Drawer::drawText( GLViewer_Object* theObject )
808 GLViewer_Text* aText = theObject->getGLText();
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() );
820 \param text - the text string
823 \param hPosition - horizontal alignment
824 \param vPosition - vertical alignment
825 \param color - text color
826 \param smallFont - font format
828 void GLViewer_Drawer::drawGLText( QString text, float x, float y,
829 int hPosition, int vPosition, QColor color, bool smallFont )
831 QFont aFont( myFont );
833 aFont.setPointSize( int(aFont.pointSize() * 0.8) );
835 GLfloat scale = textScale() > 0. ? textScale() : 1.;
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;
844 case GLText_Left : x -= ( gap + width ); break;
845 case GLText_Center : x -= width / 2; break;
846 case GLText_Right : x += gap; break;
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;
858 drawText( text, x, y, color, &aFont, 2, myTextFormat );
862 \return a rectangle of text (without viewer scale)
864 GLViewer_Rect GLViewer_Drawer::textRect( const QString& text ) const
866 GLfloat scale = textScale() > 0. ? textScale() : 1.;
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();
872 return GLViewer_Rect( 0, width, height, 0 );
877 \param rect - instance of primitive
878 \param color - color of primitive
880 void GLViewer_Drawer::drawRectangle( GLViewer_Rect* rect, QColor color )
885 float x1 = rect->left();
886 float x2 = rect->right();
887 float y1 = rect->bottom();
888 float y2 = rect->top();
890 glColor3f( ( GLfloat )color.red() / 255,
891 ( GLfloat )color.green() / 255,
892 ( GLfloat )color.blue() / 255 );
895 glBegin( GL_LINE_LOOP );
896 glVertex2f( x1, y1 );
897 glVertex2f( x1, y2 );
898 glVertex2f( x2, y2 );
899 glVertex2f( x2, y1 );
904 Saves object to file with format of HPGL
906 \param aViewerCS - viewer co-ordinate system
907 \param aHPGLCS - paper co-ordinate system
909 bool GLViewer_Drawer::translateToHPGL( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aHPGLCS )
912 for( int i=0, n=myObjects.count(); i<n; i++ )
913 result &= myObjects[i]->translateToHPGL( hFile, aViewerCS, aHPGLCS );
918 Saves object to file with format of PostScript
920 \param aViewerCS - viewer co-ordinate system
921 \param aPSCS - paper co-ordinate system
923 bool GLViewer_Drawer::translateToPS( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS )
926 for( int i=0, n=myObjects.count(); i<n; i++ )
927 result &= myObjects[i]->translateToPS( hFile, aViewerCS, aPSCS );
933 Saves object to file with format of EMF
935 \param aViewerCS - viewer co-ordinate system
936 \param aEMFCS - paper co-ordinate system
938 bool GLViewer_Drawer::translateToEMF( HDC hDC, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aEMFCS )
941 for( int i=0, n=myObjects.count(); i<n; i++ )
942 result &= myObjects[i]->translateToEMF( hDC, aViewerCS, aEMFCS );
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
956 void GLViewer_Drawer::drawRectangle( GLViewer_Rect* rect, GLfloat lineWidth, GLfloat gap,
957 QColor color, bool filled, QColor fillingColor )
962 float x1 = rect->left() - gap;
963 float x2 = rect->right() + gap;
964 float y1 = rect->bottom() - gap;
965 float y2 = rect->top() + gap;
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 );
980 glColor3f( ( GLfloat )color.red() / 255,
981 ( GLfloat )color.green() / 255,
982 ( GLfloat )color.blue() / 255 );
983 glLineWidth( lineWidth );
985 glBegin( GL_LINE_LOOP );
986 glVertex2f( x1, y1 );
987 glVertex2f( x1, y2 );
988 glVertex2f( x2, y2 );
989 glVertex2f( x2, y1 );
995 \param pntList - list of points
996 \param color - color of contour
997 \param lineWidth - width of line
999 void GLViewer_Drawer::drawContour( const GLViewer_PntList& pntList, QColor color, GLfloat lineWidth )
1001 glColor3f( ( GLfloat )color.red() / 255,
1002 ( GLfloat )color.green() / 255,
1003 ( GLfloat )color.blue() / 255 );
1004 glLineWidth( lineWidth );
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() );
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
1021 void GLViewer_Drawer::drawContour( GLViewer_Rect* rect, QColor color, GLfloat lineWidth,
1022 GLushort pattern, bool isStripe )
1024 float x1 = rect->left();
1025 float x2 = rect->right();
1026 float y1 = rect->bottom();
1027 float y2 = rect->top();
1029 glColor3f( ( GLfloat )color.red() / 255,
1030 ( GLfloat )color.green() / 255,
1031 ( GLfloat )color.blue() / 255 );
1032 glLineWidth( lineWidth );
1036 glEnable( GL_LINE_STIPPLE );
1037 glLineStipple( 1, pattern );
1040 glBegin( GL_LINE_LOOP );
1042 glVertex2f( x1, y1 );
1043 glVertex2f( x1, y2 );
1044 glVertex2f( x2, y2 );
1045 glVertex2f( x2, y1 );
1048 glDisable( GL_LINE_STIPPLE );
1053 \param pntList - list of points
1054 \param color - color of polygon
1056 void GLViewer_Drawer::drawPolygon( const GLViewer_PntList& pntList, QColor color )
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() );
1070 \param rect - instance of rectangle
1071 \param color - color of polygon
1072 \param pattern - pattern of line
1073 \param isStripe - enables line stipple
1075 void GLViewer_Drawer::drawPolygon( GLViewer_Rect* rect, QColor color,
1076 GLushort pattern, bool isStripe )
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 );
1088 glEnable( GL_LINE_STIPPLE );
1089 glLineStipple( 1, pattern );
1091 glBegin( GL_POLYGON );
1093 glVertex2f( x1, y1 );
1094 glVertex2f( x1, y2 );
1095 glVertex2f( x2, y2 );
1096 glVertex2f( x2, y1 );
1099 glDisable( GL_LINE_STIPPLE );
1102 GLubyte rasterVertex[5] = { 0x70, 0xf8, 0xf8, 0xf8, 0x70 };
1106 \param x - x position
1107 \param y - y position
1108 \param color - color of vertex
1110 void GLViewer_Drawer::drawVertex( GLfloat x, GLfloat y, QColor color )
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 );
1117 GLubyte rasterCross[7] = { 0x82, 0x44, 0x28, 0x10, 0x28, 0x44, 0x82 };
1121 \param x - x position
1122 \param y - y position
1123 \param color - color of cross
1125 void GLViewer_Drawer::drawCross( GLfloat x, GLfloat y, QColor color )
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 );
1134 \param red, green, blue - components of color
1135 \param lineWidth - width of line
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
1144 void GLViewer_Drawer::drawArrow( const GLfloat red, const GLfloat green, const GLfloat blue,
1146 GLfloat staff, GLfloat length, GLfloat width,
1147 GLfloat x, GLfloat y, GLfloat angle, GLboolean filled )
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;
1156 gp_Pnt2d p0( x, y );
1157 gp_Pnt2d p1( vx1, vy1 );
1158 gp_Pnt2d p2( vx2, vy2 );
1159 gp_Pnt2d p3( vx3, vy3 );
1161 p1.Rotate( p0, angle );
1162 p2.Rotate( p0, angle );
1163 p3.Rotate( p0, angle );
1165 vx1 = p1.X(); vy1 = p1.Y();
1166 vx2 = p2.X(); vy2 = p2.Y();
1167 vx3 = p3.X(); vy3 = p3.Y();
1169 glColor3f( red, green, blue );
1170 glLineWidth( lineWidth );
1172 glBegin( GL_LINES );
1174 glVertex2f( vx1, vy1 );
1180 glBegin( GL_LINES );
1181 glVertex2f( vx1, vy1 );
1182 glVertex2f( vx2, vy2 );
1183 glVertex2f( vx1, vy1 );
1184 glVertex2f( vx3, vy3 );
1189 glBegin( GL_POLYGON );
1190 glVertex2f( vx1, vy1 );
1191 glVertex2f( vx2, vy2 );
1192 glVertex2f( vx3, vy3 );