2013-10-14 59 views
1

在我使用QGLWidget創建自己的圖像查看器的項目中,我試圖在顯示大圖像時添加縮放和滾動功能,但是我遇到了圖像被剪切掉的問題,比原始尺寸或面板尺寸更寬。如何在QGLWidget中縮放和滾動

在這裏我設置了視口和glScalef。在實現滾動時,我將QAbstractScrollArea分類並將滾動條的座標傳遞給一個變量。

// scrollOffset has the coordinates of horizontal and vertical scrollbars 
// this->width() and this->height() are panel size 
glViewport(0 - scrollOffset.x(), 0 + scrollOffset.y(), this->width(), this->height()); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluOrtho2D(0, this->width(), this->height(), 0); // flip the y axis 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

// if zoomFactor value is 1.0 means no zooming 
glScalef(zoomFactor, zoomFactor, 1.0); 

glClear(GL_COLOR_BUFFER_BIT); 

渲染圖像:

glBindTexture(GL_TEXTURE_2D, texId); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());  
glBegin(GL_QUADS);  
// text coords are flipped in y axis 
// width and height are image's original size 
glTexCoord2d(0,1); glVertex3d(0,  0,  0); 
glTexCoord2d(1,1); glVertex3d(width, 0,  0); 
glTexCoord2d(1,0); glVertex3d(width, height, 0); 
glTexCoord2d(0,0); glVertex3d(0,  height, 0);  
glEnd(); 
在下面的圖片

,我向下滾動圖像,但顯示的圖像不能高於面板的高度更高

enter image description here

回答

0

你不應該濫用glViewport進行縮放。該視口用於設置投影后映射(可見)窗口NDC座標的哪部分。通常你將glViewport設置爲你正在繪製的窗口的大小。

任何縮放和滾動都應通過調整投影矩陣來完成,方法是調整左側,右側,底部和頂部限制以顯示內容。

+0

感謝您的建議,即使我的滾動代碼不是那麼優雅,它工作得很好,實際上我只使用glviewport進行滾動,縮放功能使用glScalef。現在問題glScalef不能按預期工作 – azer89