1 // Copyright (C) 2005 OPEN CASCADE
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.
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.
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
17 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19 // Author : OPEN CASCADE
22 // File: GLViewer_Widget.cxx
23 // Created: November, 2004
25 #include "GLViewer_Widget.h"
26 #include "GLViewer_ViewPort2d.h"
27 #include "GLViewer_Viewer2d.h"
28 #include "GLViewer_Compass.h"
29 #include "GLViewer_Grid.h"
30 #include "GLViewer_Object.h"
31 #include "GLViewer_CoordSystem.h"
41 #include <qapplication.h>
43 #include <qpaintdevicemetrics.h>
49 Parameters using for QOGLWidget as is
51 GLViewer_Widget::GLViewer_Widget( QWidget* parent, const char* name ):
52 QGLWidget( parent, 0/*, WRepaintNoErase | WResizeNoErase*/ )
54 myViewPort = ( GLViewer_ViewPort2d* )parent;
62 myRotationAngle = 0.0;
63 myRotationCenterX = 0.0;
64 myRotationCenterY = 0.0;
65 myRotationCenterZ = 1.0;
66 myRotationAnglePrev = 0.0;
73 setMouseTracking( true );
79 GLViewer_Widget::~GLViewer_Widget()
84 \return offset parameters of Window in OpenGL global scene
86 void GLViewer_Widget::getPan( GLfloat& xPan, GLfloat& yPan, GLfloat& zPan )
94 A function for installing the offset parameters of Window in OpenGL global scene
96 void GLViewer_Widget::setPan( GLfloat xPan, GLfloat yPan, GLfloat zPan )
104 \return scales on OpenGL scene along 3 directions in 2d scene zScale = 1.0
106 void GLViewer_Widget::getScale( GLfloat& xScale, GLfloat& yScale, GLfloat& zScale )
114 A function for installing the scales of OpenGL scene
116 void GLViewer_Widget::setScale( GLfloat xScale, GLfloat yScale, GLfloat zScale )
118 if ( xScale > 0 && yScale > 0 && zScale > 0 )
127 \return start point of curren rotation of Window in OpenGL global scene
129 void GLViewer_Widget::getRotationStart( GLfloat& rotationStartX,
130 GLfloat& rotationStartY,
131 GLfloat& rotationStartZ )
133 rotationStartX = myRotationStartX;
134 rotationStartY = myRotationStartY;
135 rotationStartZ = myRotationStartZ;
139 A function for installing the rotation angle of Window in OpenGL global scene in degree (Only in 2D)
141 void GLViewer_Widget::setRotationStart( GLfloat rotationStartX,
142 GLfloat rotationStartY,
143 GLfloat rotationStartZ )
145 myRotationStartX = rotationStartX;
146 myRotationStartY = rotationStartY;
147 myRotationStartZ = rotationStartZ;
151 \return parameters of rotation
152 \param rotationAngle - angle
153 \param rotationCenterX - center x
154 \param rotationCenterY - center y
155 \param rotationCenterZ - center z
157 void GLViewer_Widget::getRotation( GLfloat& rotationAngle,
158 GLfloat& rotationCenterX,
159 GLfloat& rotationCenterY,
160 GLfloat& rotationCenterZ )
162 rotationAngle = myRotationAngle;
163 rotationCenterX = myRotationCenterX;
164 rotationCenterY = myRotationCenterY;
165 rotationCenterZ = myRotationCenterZ;
169 Sets parameters of rotation
170 \param rotationAngle - angle
171 \param rotationCenterX - center x
172 \param rotationCenterY - center y
173 \param rotationCenterZ - center z
175 void GLViewer_Widget::setRotation( GLfloat rotationAngle,
176 GLfloat rotationCenterX,
177 GLfloat rotationCenterY,
178 GLfloat rotationCenterZ )
180 myRotationAngle = rotationAngle;
181 myRotationCenterX = rotationCenterX;
182 myRotationCenterY = rotationCenterY;
183 myRotationCenterZ = rotationCenterZ;
188 Sets image as background
189 \param filename - name of file
191 void GLViewer_Widget::setBackground( QString filename )
196 if ( !filename.isEmpty() && buf.load( filename ) )
197 { // Load first image from file
198 isLoadBackground = true;
199 myBackgroundFile = filename;
204 myBackgroundSize = 64;
205 while( myBackgroundSize < myIW || myBackgroundSize < myIH)
206 myBackgroundSize = myBackgroundSize * 2;
208 GLubyte* pixels = new GLubyte[myBackgroundSize * myBackgroundSize * 4];
210 for( int i = 0; i < myBackgroundSize; i++ )
212 for( int j = 0; j < myBackgroundSize; j++ )
214 if( j < myIW && i < myIH )
216 pixels[i * myBackgroundSize * 4 + j * 4] = (GLubyte)qRed( buf.pixel(j,myIH - i - 1) );
217 pixels[i * myBackgroundSize * 4 + j * 4 + 1]= (GLubyte)qGreen( buf.pixel(j,myIH - i - 1) );
218 pixels[i * myBackgroundSize * 4 + j * 4 + 2] = (GLubyte)qBlue( buf.pixel(j,myIH - i - 1) );
222 pixels[i * myBackgroundSize * 4 + j * 4] = (GLubyte)0;
223 pixels[i * myBackgroundSize * 4 + j * 4 + 1] = (GLubyte)0;
224 pixels[i * myBackgroundSize * 4 + j * 4 + 2] = (GLubyte)0;
226 pixels[i * myBackgroundSize* 4 + j * 4 + 3] = (GLubyte)255;
231 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
232 glGenTextures(1, &texName);
233 glBindTexture(GL_TEXTURE_2D, texName);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
236 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myBackgroundSize , myBackgroundSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
245 \param theString - tooltip text
246 \param theRect - tooltip rectangle
248 void GLViewer_Widget::addToolTip( QString theString, QRect theRect )
250 myToolTipRect = theRect;
251 QToolTip::add( this, myToolTipRect, theString );
257 void GLViewer_Widget::removeToolTip()
259 QToolTip::remove( this, myToolTipRect );
263 Initialization (redefined virtual from QGLWidget)
265 void GLViewer_Widget::initializeGL()
267 setAutoBufferSwap( true );
269 glShadeModel(GL_FLAT);
273 QString aPicturePath = getenv("GLViewer__Background_Picture");
275 if ( !aPicturePath.isEmpty() && buf.load( aPicturePath ) )
276 { // Load first image from file
277 isLoadBackground = true;
278 setBackground( aPicturePath );
283 isLoadBackground = false;
289 void GLViewer_Widget::paintGL()
291 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
293 glMatrixMode( GL_MODELVIEW );
296 glRotatef( myRotationAngle, myRotationCenterX, myRotationCenterY, myRotationCenterZ );
297 glScalef( myXScale, myYScale, myZScale );
298 glTranslatef( myXPan, myYPan, myZPan );
300 if( isLoadBackground )
302 glEnable(GL_TEXTURE_2D);
303 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
304 glBindTexture(GL_TEXTURE_2D, texName);
307 glTexCoord2f( 0.0, 0.0); glVertex3f( -myIW/2, -myIH/2, 0.0);
308 glTexCoord2f( 0.0, (float)myIH/myBackgroundSize ); glVertex3f( -myIW/2, myIH/2, 0.0);
309 glTexCoord2f( (float)myIW/myBackgroundSize, (float)myIH/myBackgroundSize ); glVertex3f( myIW/2, myIH/2, 0.0);
310 glTexCoord2f( (float)myIW/myBackgroundSize, 0.0); glVertex3f( myIW/2, -myIH/2, 0.0);
314 glDisable(GL_TEXTURE_2D);
316 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
320 GLViewer_Grid* grid = myViewPort->getGrid();
324 GLViewer_Viewer2d* v = ( GLViewer_Viewer2d* )getViewPort()->getViewFrame()->getViewer();
326 v->updateDrawers( GL_FALSE, myXScale, myYScale );
328 v->repaintView( getViewPort()->getViewFrame() );
332 Resets OpenGl parameters after resize
334 \param h - new height
336 void GLViewer_Widget::resizeGL( int w, int h )
341 glViewport( 0, 0, w, h);
350 myViewPort->initResize( w, h );
352 glMatrixMode( GL_PROJECTION );
354 GLfloat w_c = w / 2., h_c = h / 2.;
356 gluOrtho2D( -w_c, w_c, -h_c, h_c );
358 glMatrixMode( GL_MODELVIEW );
363 Provides repaint in export mode
365 void GLViewer_Widget::exportRepaint()
371 isExportMode = false;
375 Custom paint event handler
377 void GLViewer_Widget::paintEvent( QPaintEvent* e )
379 QApplication::sendEvent( myViewPort, e );
383 Custom mouse move event handler
385 void GLViewer_Widget::mouseMoveEvent( QMouseEvent* e )
387 QApplication::sendEvent( myViewPort, e );
391 Custom mouse press event handler
393 void GLViewer_Widget::mousePressEvent( QMouseEvent* e )
395 QApplication::sendEvent( myViewPort, e );
399 Custom mouse release event handler
401 void GLViewer_Widget::mouseReleaseEvent( QMouseEvent* e )
403 QApplication::sendEvent( myViewPort, e );
407 Custom enter event handler
409 void GLViewer_Widget::enterEvent( QEvent* e )
415 Custom leave event handler
417 void GLViewer_Widget::leaveEvent( QEvent* e )
424 \return the hex code of digit < 16
427 inline char hex( uchar c )
438 Translates part of image inside rectangle from w1 to w2 and from h2 to h1 to PS format
439 \param hFile - PostScript file
440 \param image - image to be tarnslated
441 \param w1 - x start position
442 \param w2 - x end position
443 \param h1 - y start position
444 \param h2 - y end position
445 \param aViewerCS - viewer co-ordinate system
446 \param aPSCS - paper co-ordinate system
454 void AddImagePart( QFile& hFile, QImage& image, int w1, int w2, int h1, int h2,
455 GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS,
456 double a, double b, double c, double d, double dw, double dh )
458 if( aViewerCS && aPSCS )
460 double width = w2-w1+1, height = h2-h1+1;
461 QString aBuffer = "", temp = "%1 %2 8 [ %3 %4 %5 %6 %7 %8 ]\n";
462 aBuffer += temp.arg( width ).arg( height ).
463 arg( a ).arg( b ).arg( c ).arg( d ).
467 char line[81]; line[80] = '\0'; int cur_index = 0;
469 for( int i=h2; i>=h1; i-- )
471 uchar* theCurLine = image.scanLine( i ), cur;
472 for( int j=w1; j<=w2; j++ )
473 for( int k=0; k<3; k++ )
475 cur = *(theCurLine+4*j+2-k);
476 *(line+cur_index) = hex( cur/16 ); //HI
477 *(line+cur_index+1) = hex( cur%16 ); //LO
489 aBuffer += "> false 3 colorimage\n\n";
491 hFile.writeBlock( aBuffer.ascii(), aBuffer.length() );
496 \return background rectangle in viewer CS
498 void GLViewer_Widget::getBackgroundRectInViewerCS( double& left, double& top, double& right, double& bottom )
500 left = -myIW/2; right = myIW/2;
501 top = myIH/2; bottom = -myIH/2;
505 Translates background to PostScript
506 \param hFile - PostScript file
507 \param aViewerCS - viewer co-ordinate system
508 \param aPSCS - paper co-ordinate system
510 void GLViewer_Widget::translateBackgroundToPS( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS )
514 if( aViewerCS && aPSCS && isLoadBackground && buf.load( myBackgroundFile ) )
516 double a, b, c, d, dx, dy; //The preparation of transformation matrix
518 double width = buf.width(), height = buf.height();
520 double left, top, right, bottom;
521 getBackgroundRectInViewerCS( left, top, right, bottom );
523 double aax = left, aay = bottom,
524 bbx = right, bby = bottom,
525 ccx = left, ccy = top;
527 aViewerCS->transform( *aPSCS, aax, aay );
528 aViewerCS->transform( *aPSCS, bbx, bby );
529 aViewerCS->transform( *aPSCS, ccx, ccy );
531 a = ( bbx - aax ) / width;
532 b = ( ccx - aax ) / height;
533 c = ( bby - aay ) / width;
534 d = ( ccy - aay ) / height;
536 //Now we must find invert matrix
537 double det = a*d-b*c,
543 a = newa; b = newb; c = newc; d = newd;
546 dy = -(b*aax+d*aay); //according to PS specification of coordinate transformation
548 const int max = 133000; //The maximum length of string in PS
549 int dh = int( floor( double( max ) / ( 3.0*2.0*width ) ) );
550 for( int k=buf.height()-1; k>=0; k-=dh )
551 AddImagePart( hFile, buf, 0, buf.width()-1, QMAX( k-dh+1, 0 ), k,
552 aViewerCS, aPSCS, a, b, c, d, dx, dy-(buf.height()-1-k) );
557 Translates image line with one color depth to line with other depth
559 void DecodeScanLine( int width, uchar* dest, int dest_depth, uchar* source, int source_depth )
562 typedef unsigned int WORD;
565 int aSize = width*dest_depth,
571 if( dest_depth==source_depth )
572 memcpy( dest, source, aSize/8 );
575 double r, g, b; WORD color;
576 for( int i=0; i<width; i++ )
579 switch( source_depth )
582 memcpy( &color, source + 2*i, 2 );
583 b = double( color & 0x001F ) / 31.0;
584 g = double( ( color & 0x07E0 ) >> 5 ) / 63.0;
585 r = double( ( color & 0xF800 ) >> 11 ) / 31.0;
588 b = double( *(source + 3*i) ) / 255.0;
589 g = double( *(source + 3*i+1) ) / 255.0;
590 r = double( *(source + 3*i+2) ) / 255.0;
593 b = double( *(source + 4*i) ) / 255.0;
594 g = double( *(source + 4*i+1) ) / 255.0;
595 r = double( *(source + 4*i+2) ) / 255.0;
601 color = WORD(b*31.0);
602 color += (WORD(g*63.0)<<5);
603 color += (WORD(r*31.0)<<11);
604 memcpy( dest + 2*i, &color, 2 );
607 *( dest + 3*i ) = (uchar)(255*b);
608 *( dest + 3*i+1 ) = (uchar)(255*g);
609 *( dest + 3*i+2 ) = (uchar)(255*r);
612 *( dest + 4*i ) = (uchar)(255*b);
613 *( dest + 4*i+1 ) = (uchar)(255*g);
614 *( dest + 4*i+2 ) = (uchar)(255*r);
615 *( dest + 4*i+3 ) = 0;
624 Translates background to EMF
625 \param dc - descriptor of EMF
626 \param aViewerCS - viewer co-ordinate system
627 \param aPSCS - paper co-ordinate system
629 void GLViewer_Widget::translateBackgroundToEMF( HDC dc, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aEMFCS )
633 if( aViewerCS && aEMFCS && isLoadBackground && buf.load( myBackgroundFile ) )
635 double left, top, right, bottom;
636 getBackgroundRectInViewerCS( left, top, right, bottom );
638 double aRot = aViewerCS->getRotation();
640 double lx = left, ly = top;
641 aViewerCS->transform( *aEMFCS, lx, ly );
643 aViewerCS->setRotation( 0.0 ); //we switch off the rotation of CS
645 aViewerCS->transform( *aEMFCS, left, top );
646 aViewerCS->transform( *aEMFCS, right, bottom );
651 HDC aScrDC = GetDC( 0 );
652 HDC aCompDC = CreateCompatibleDC( aScrDC );
653 HBITMAP aBMP = CreateCompatibleBitmap( aScrDC, w, h );
656 GetObject ( aBMP, sizeof(BITMAP), &aBitInfo );
657 int depth = aBitInfo.bmBitsPixel; //how many bits represent a color of one pixel
659 int aLineSize = w*depth;
660 int dw = aLineSize % 32; //scanline word aligning
667 BYTE* bits = new BYTE[aLineSize*h];
668 memset( bits, 0, aLineSize*h );
671 for( int i=0; i<h; i++ )
673 aLine = buf.scanLine( i );
674 DecodeScanLine( w, bits+aLineSize*i, depth, aLine, buf.depth() );
677 SetBitmapBits( aBMP, aLineSize*h, bits );
679 HGDIOBJ old = SelectObject( aCompDC, aBMP );
682 GetWorldTransform( dc, &aTrans );
683 XFORM aRotTrans = aTrans;
684 double a = aRotTrans.eM11,
689 aRotTrans.eM11 = a*cos( aRot )-b*sin( aRot ); //we multiply the current matrix with the rotation matrix
690 aRotTrans.eM12 = a*sin( aRot )+b*cos( aRot );
691 aRotTrans.eM21 = c*cos( aRot )-d*sin( aRot );
692 aRotTrans.eM22 = c*sin( aRot )+d*cos( aRot );
699 double det = a*d-b*c, //now we find the invert matrix
705 a = newa; b = newb; c = newc; d = newd;
707 aRotTrans.eDx = lx -(a*left+c*top); //we find the dx and dy translating (left,top)->(lx,ly) -
708 aRotTrans.eDy = ly -(b*left+d*top); //the real image of left-top corner of picture
710 SetWorldTransform( dc, &aRotTrans );
711 int res = StretchBlt( dc, left, top, right-left, bottom-top, aCompDC, 0, 0, w, h, SRCCOPY );
712 SetWorldTransform( dc, &aTrans );
714 SelectObject( aCompDC, old );
716 ReleaseDC( 0, aScrDC );
718 DeleteObject( aBMP );
721 aViewerCS->setRotation( aRot );