1 // File: GLViewer_Widget.cxx
2 // Created: November, 2004
4 // Copyright (C) CEA 2004
6 //================================================================
7 // Class : GLViewer_Widget
8 // Description : OpenGL QWidget for GLViewer
9 //================================================================
11 #include "GLViewer_Widget.h"
12 #include "GLViewer_ViewPort2d.h"
13 #include "GLViewer_Viewer2d.h"
14 #include "GLViewer_Compass.h"
15 #include "GLViewer_Grid.h"
16 #include "GLViewer_Object.h"
17 #include "GLViewer_CoordSystem.h"
27 #include <qapplication.h>
29 #include <qpaintdevicemetrics.h>
33 //=======================================================================
34 // Function: GLViewer_Widget
36 //=======================================================================
37 GLViewer_Widget::GLViewer_Widget( QWidget* parent, const char* name ):
38 QGLWidget( parent, 0/*, WRepaintNoErase | WResizeNoErase*/ )
40 myViewPort = ( GLViewer_ViewPort2d* )parent;
48 myRotationAngle = 0.0;
49 myRotationCenterX = 0.0;
50 myRotationCenterY = 0.0;
51 myRotationCenterZ = 1.0;
52 myRotationAnglePrev = 0.0;
59 setMouseTracking( true );
62 //=======================================================================
63 // Function: GLViewer_Widget
65 //=======================================================================
66 GLViewer_Widget::~GLViewer_Widget()
70 //=======================================================================
71 // Function: ~GLViewer_Widget
73 //=======================================================================
74 void GLViewer_Widget::getPan( GLfloat& xPan, GLfloat& yPan, GLfloat& zPan )
81 //=======================================================================
84 //=======================================================================
85 void GLViewer_Widget::setPan( GLfloat xPan, GLfloat yPan, GLfloat zPan )
92 //=======================================================================
95 //=======================================================================
96 void GLViewer_Widget::getScale( GLfloat& xScale, GLfloat& yScale, GLfloat& zScale )
103 //=======================================================================
104 // Function: setScale
106 //=======================================================================
107 void GLViewer_Widget::setScale( GLfloat xScale, GLfloat yScale, GLfloat zScale )
109 if ( xScale > 0 && yScale > 0 && zScale > 0 )
117 //=======================================================================
118 // Function: getRotationStart
120 //=======================================================================
121 void GLViewer_Widget::getRotationStart( GLfloat& rotationStartX,
122 GLfloat& rotationStartY,
123 GLfloat& rotationStartZ )
125 rotationStartX = myRotationStartX;
126 rotationStartY = myRotationStartY;
127 rotationStartZ = myRotationStartZ;
130 //=======================================================================
131 // Function: setRotationStart
133 //=======================================================================
134 void GLViewer_Widget::setRotationStart( GLfloat rotationStartX,
135 GLfloat rotationStartY,
136 GLfloat rotationStartZ )
138 myRotationStartX = rotationStartX;
139 myRotationStartY = rotationStartY;
140 myRotationStartZ = rotationStartZ;
143 //=======================================================================
144 // Function: getRotation
146 //=======================================================================
147 void GLViewer_Widget::getRotation( GLfloat& rotationAngle,
148 GLfloat& rotationCenterX,
149 GLfloat& rotationCenterY,
150 GLfloat& rotationCenterZ )
152 rotationAngle = myRotationAngle;
153 rotationCenterX = myRotationCenterX;
154 rotationCenterY = myRotationCenterY;
155 rotationCenterZ = myRotationCenterZ;
158 //=======================================================================
159 // Function: setRotation
161 //=======================================================================
162 void GLViewer_Widget::setRotation( GLfloat rotationAngle,
163 GLfloat rotationCenterX,
164 GLfloat rotationCenterY,
165 GLfloat rotationCenterZ )
167 myRotationAngle = rotationAngle;
168 myRotationCenterX = rotationCenterX;
169 myRotationCenterY = rotationCenterY;
170 myRotationCenterZ = rotationCenterZ;
173 //=======================================================================
174 // Function: setBackground
176 //=======================================================================
177 void GLViewer_Widget::setBackground( QString filename )
182 if ( !filename.isEmpty() && buf.load( filename ) )
183 { // Load first image from file
184 isLoadBackground = true;
185 myBackgroundFile = filename;
190 myBackgroundSize = 64;
191 while( myBackgroundSize < myIW || myBackgroundSize < myIH)
192 myBackgroundSize = myBackgroundSize * 2;
194 GLubyte* pixels = new GLubyte[myBackgroundSize * myBackgroundSize * 4];
196 for( int i = 0; i < myBackgroundSize; i++ )
198 for( int j = 0; j < myBackgroundSize; j++ )
200 if( j < myIW && i < myIH )
202 pixels[i * myBackgroundSize * 4 + j * 4] = (GLubyte)qRed( buf.pixel(j,myIH - i - 1) );
203 pixels[i * myBackgroundSize * 4 + j * 4 + 1]= (GLubyte)qGreen( buf.pixel(j,myIH - i - 1) );
204 pixels[i * myBackgroundSize * 4 + j * 4 + 2] = (GLubyte)qBlue( buf.pixel(j,myIH - i - 1) );
208 pixels[i * myBackgroundSize * 4 + j * 4] = (GLubyte)0;
209 pixels[i * myBackgroundSize * 4 + j * 4 + 1] = (GLubyte)0;
210 pixels[i * myBackgroundSize * 4 + j * 4 + 2] = (GLubyte)0;
212 pixels[i * myBackgroundSize* 4 + j * 4 + 3] = (GLubyte)255;
217 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
218 glGenTextures(1, &texName);
219 glBindTexture(GL_TEXTURE_2D, texName);
220 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
221 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
222 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myBackgroundSize , myBackgroundSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
229 //=======================================================================
230 // Function: addToolTip
232 //=======================================================================
233 void GLViewer_Widget::addToolTip( QString theString, QRect theRect )
235 myToolTipRect = theRect;
236 QToolTip::add( this, myToolTipRect, theString );
239 //=======================================================================
240 // Function: removeToolTip
242 //=======================================================================
243 void GLViewer_Widget::removeToolTip()
245 QToolTip::remove( this, myToolTipRect );
248 //=======================================================================
249 // Function: initializeGL
251 //=======================================================================
252 void GLViewer_Widget::initializeGL()
254 setAutoBufferSwap( true );
256 glShadeModel(GL_FLAT);
260 QString aPicturePath = getenv("GLViewer__Background_Picture");
262 if ( !aPicturePath.isEmpty() && buf.load( aPicturePath ) )
263 { // Load first image from file
264 isLoadBackground = true;
265 setBackground( aPicturePath );
270 isLoadBackground = false;
273 //=======================================================================
276 //=======================================================================
277 void GLViewer_Widget::paintGL()
279 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
281 glMatrixMode( GL_MODELVIEW );
284 glRotatef( myRotationAngle, myRotationCenterX, myRotationCenterY, myRotationCenterZ );
285 glScalef( myXScale, myYScale, myZScale );
286 glTranslatef( myXPan, myYPan, myZPan );
288 if( isLoadBackground )
290 glEnable(GL_TEXTURE_2D);
291 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
292 glBindTexture(GL_TEXTURE_2D, texName);
295 glTexCoord2f( 0.0, 0.0); glVertex3f( -myIW/2, -myIH/2, 0.0);
296 glTexCoord2f( 0.0, (float)myIH/myBackgroundSize ); glVertex3f( -myIW/2, myIH/2, 0.0);
297 glTexCoord2f( (float)myIW/myBackgroundSize, (float)myIH/myBackgroundSize ); glVertex3f( myIW/2, myIH/2, 0.0);
298 glTexCoord2f( (float)myIW/myBackgroundSize, 0.0); glVertex3f( myIW/2, -myIH/2, 0.0);
302 glDisable(GL_TEXTURE_2D);
304 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
308 GLViewer_Grid* grid = myViewPort->getGrid();
312 GLViewer_Viewer2d* v = ( GLViewer_Viewer2d* )getViewPort()->getViewFrame()->getViewer();
314 v->updateDrawers( GL_FALSE, myXScale, myYScale );
316 v->repaintView( getViewPort()->getViewFrame() );
319 //=======================================================================
320 // Function: resizeGL
322 //=======================================================================
323 void GLViewer_Widget::resizeGL( int w, int h )
328 glViewport( 0, 0, w, h);
337 myViewPort->initResize( w, h );
339 glMatrixMode( GL_PROJECTION );
341 GLfloat w_c = w / 2., h_c = h / 2.;
343 gluOrtho2D( -w_c, w_c, -h_c, h_c );
345 glMatrixMode( GL_MODELVIEW );
349 //=======================================================================
350 // Function: exportRepaint
352 //=======================================================================
353 void GLViewer_Widget::exportRepaint()
359 isExportMode = false;
362 //=======================================================================
363 // Function: paintEvent
365 //=======================================================================
366 void GLViewer_Widget::paintEvent( QPaintEvent* e )
368 QApplication::sendEvent( myViewPort, e );
371 //=======================================================================
372 // Function: mouseMoveEvent
374 //=======================================================================
375 void GLViewer_Widget::mouseMoveEvent( QMouseEvent* e )
377 QApplication::sendEvent( myViewPort, e );
380 //=======================================================================
381 // Function: mousePressEvent
383 //=======================================================================
384 void GLViewer_Widget::mousePressEvent( QMouseEvent* e )
386 QApplication::sendEvent( myViewPort, e );
389 //=======================================================================
390 // Function: mouseReleaseEvent
392 //=======================================================================
393 void GLViewer_Widget::mouseReleaseEvent( QMouseEvent* e )
395 QApplication::sendEvent( myViewPort, e );
398 //=======================================================================
399 // Function: enterEvent
401 //=======================================================================
402 void GLViewer_Widget::enterEvent( QEvent* e )
407 //=======================================================================
408 // Function: leaveEvent
410 //=======================================================================
411 void GLViewer_Widget::leaveEvent( QEvent* e )
416 inline char hex( uchar c )
424 //=======================================================================
425 // Function: AddImagePart
427 //=======================================================================
428 void AddImagePart( QFile& hFile, QImage& image, int w1, int w2, int h1, int h2,
429 GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS,
430 double a, double b, double c, double d, double dw, double dh )
432 if( aViewerCS && aPSCS )
434 double width = w2-w1+1, height = h2-h1+1;
435 QString aBuffer = "", temp = "%1 %2 8 [ %3 %4 %5 %6 %7 %8 ]\n";
436 aBuffer += temp.arg( width ).arg( height ).
437 arg( a ).arg( b ).arg( c ).arg( d ).
441 char line[81]; line[80] = '\0'; int cur_index = 0;
443 for( int i=h2; i>=h1; i-- )
445 uchar* theCurLine = image.scanLine( i ), cur;
446 for( int j=w1; j<=w2; j++ )
447 for( int k=0; k<3; k++ )
449 cur = *(theCurLine+4*j+2-k);
450 *(line+cur_index) = hex( cur/16 ); //HI
451 *(line+cur_index+1) = hex( cur%16 ); //LO
463 aBuffer += "> false 3 colorimage\n\n";
465 hFile.writeBlock( aBuffer.ascii(), aBuffer.length() );
469 //=======================================================================
470 // Function: getBackgroundRectInViewerCS
472 //=======================================================================
473 void GLViewer_Widget::getBackgroundRectInViewerCS( double& left, double& top, double& right, double& bottom )
475 left = -myIW/2; right = myIW/2;
476 top = myIH/2; bottom = -myIH/2;
479 //=======================================================================
480 // Function: translateBackgroundToPS
482 //=======================================================================
483 void GLViewer_Widget::translateBackgroundToPS( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS )
487 if( aViewerCS && aPSCS && isLoadBackground && buf.load( myBackgroundFile ) )
489 double a, b, c, d, dx, dy; //The preparation of transformation matrix
491 double width = buf.width(), height = buf.height();
493 double left, top, right, bottom;
494 getBackgroundRectInViewerCS( left, top, right, bottom );
496 double aax = left, aay = bottom,
497 bbx = right, bby = bottom,
498 ccx = left, ccy = top;
500 aViewerCS->transform( *aPSCS, aax, aay );
501 aViewerCS->transform( *aPSCS, bbx, bby );
502 aViewerCS->transform( *aPSCS, ccx, ccy );
504 a = ( bbx - aax ) / width;
505 b = ( ccx - aax ) / height;
506 c = ( bby - aay ) / width;
507 d = ( ccy - aay ) / height;
509 //Now we must find invert matrix
510 double det = a*d-b*c,
516 a = newa; b = newb; c = newc; d = newd;
519 dy = -(b*aax+d*aay); //according to PS specification of coordinate transformation
521 const int max = 133000; //The maximum length of string in PS
522 int dh = int( floor( double( max ) / ( 3.0*2.0*width ) ) );
523 for( int k=buf.height()-1; k>=0; k-=dh )
524 AddImagePart( hFile, buf, 0, buf.width()-1, QMAX( k-dh+1, 0 ), k,
525 aViewerCS, aPSCS, a, b, c, d, dx, dy-(buf.height()-1-k) );
529 //=======================================================================
530 // Function: DecodeScanLine
532 //=======================================================================
533 void DecodeScanLine( int width, uchar* dest, int dest_depth, uchar* source, int source_depth )
536 typedef unsigned int WORD;
539 int aSize = width*dest_depth,
545 if( dest_depth==source_depth )
546 memcpy( dest, source, aSize/8 );
549 double r, g, b; WORD color;
550 for( int i=0; i<width; i++ )
553 switch( source_depth )
556 memcpy( &color, source + 2*i, 2 );
557 b = double( color & 0x001F ) / 31.0;
558 g = double( ( color & 0x07E0 ) >> 5 ) / 63.0;
559 r = double( ( color & 0xF800 ) >> 11 ) / 31.0;
562 b = double( *(source + 3*i) ) / 255.0;
563 g = double( *(source + 3*i+1) ) / 255.0;
564 r = double( *(source + 3*i+2) ) / 255.0;
567 b = double( *(source + 4*i) ) / 255.0;
568 g = double( *(source + 4*i+1) ) / 255.0;
569 r = double( *(source + 4*i+2) ) / 255.0;
575 color = WORD(b*31.0);
576 color += (WORD(g*63.0)<<5);
577 color += (WORD(r*31.0)<<11);
578 memcpy( dest + 2*i, &color, 2 );
581 *( dest + 3*i ) = (uchar)(255*b);
582 *( dest + 3*i+1 ) = (uchar)(255*g);
583 *( dest + 3*i+2 ) = (uchar)(255*r);
586 *( dest + 4*i ) = (uchar)(255*b);
587 *( dest + 4*i+1 ) = (uchar)(255*g);
588 *( dest + 4*i+2 ) = (uchar)(255*r);
589 *( dest + 4*i+3 ) = 0;
596 //=======================================================================
597 // Function: translateBackgroundToEMF
599 //=======================================================================
601 void GLViewer_Widget::translateBackgroundToEMF( HDC dc, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aEMFCS )
605 if( aViewerCS && aEMFCS && isLoadBackground && buf.load( myBackgroundFile ) )
607 double left, top, right, bottom;
608 getBackgroundRectInViewerCS( left, top, right, bottom );
610 double aRot = aViewerCS->getRotation();
612 double lx = left, ly = top;
613 aViewerCS->transform( *aEMFCS, lx, ly );
615 aViewerCS->setRotation( 0.0 ); //we switch off the rotation of CS
617 aViewerCS->transform( *aEMFCS, left, top );
618 aViewerCS->transform( *aEMFCS, right, bottom );
623 HDC aScrDC = GetDC( 0 );
624 HDC aCompDC = CreateCompatibleDC( aScrDC );
625 HBITMAP aBMP = CreateCompatibleBitmap( aScrDC, w, h );
628 GetObject ( aBMP, sizeof(BITMAP), &aBitInfo );
629 int depth = aBitInfo.bmBitsPixel; //how many bits represent a color of one pixel
631 int aLineSize = w*depth;
632 int dw = aLineSize % 32; //scanline word aligning
639 BYTE* bits = new BYTE[aLineSize*h];
640 memset( bits, 0, aLineSize*h );
643 for( int i=0; i<h; i++ )
645 aLine = buf.scanLine( i );
646 DecodeScanLine( w, bits+aLineSize*i, depth, aLine, buf.depth() );
649 SetBitmapBits( aBMP, aLineSize*h, bits );
651 HGDIOBJ old = SelectObject( aCompDC, aBMP );
654 GetWorldTransform( dc, &aTrans );
655 XFORM aRotTrans = aTrans;
656 double a = aRotTrans.eM11,
661 aRotTrans.eM11 = a*cos( aRot )-b*sin( aRot ); //we multiply the current matrix with the rotation matrix
662 aRotTrans.eM12 = a*sin( aRot )+b*cos( aRot );
663 aRotTrans.eM21 = c*cos( aRot )-d*sin( aRot );
664 aRotTrans.eM22 = c*sin( aRot )+d*cos( aRot );
671 double det = a*d-b*c, //now we find the invert matrix
677 a = newa; b = newb; c = newc; d = newd;
679 aRotTrans.eDx = lx -(a*left+c*top); //we find the dx and dy translating (left,top)->(lx,ly) -
680 aRotTrans.eDy = ly -(b*left+d*top); //the real image of left-top corner of picture
682 SetWorldTransform( dc, &aRotTrans );
683 int res = StretchBlt( dc, left, top, right-left, bottom-top, aCompDC, 0, 0, w, h, SRCCOPY );
684 SetWorldTransform( dc, &aTrans );
686 SelectObject( aCompDC, old );
688 ReleaseDC( 0, aScrDC );
690 DeleteObject( aBMP );
693 aViewerCS->setRotation( aRot );