在我使用 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();
在下图中,我向下滚动图像,但显示的图像不能高于面板的高度