在我使用 QGLWidget 创建自己的图像查看器的项目中,我试图在显示大图像时添加缩放和滚动功能,但我遇到了图像被剪切并且不能比原始尺寸更宽的问题,或者面板尺寸。
这里我设置了视口(viewport)和 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();
在下图中,我向下滚动图像,但显示的图像不能高于面板的高度
最佳答案
您不应滥用 glViewport 进行缩放。视口(viewport)用于设置投影后映射到(可见)窗口 NDC 坐标的哪一部分。通常你将 glViewport 设置为你正在绘制的窗口的大小。
任何缩放和滚动都应该通过调整投影矩阵来完成,通过调整要显示的内容的左、右、下和上限制。
关于c++ - 如何在QGLWidget中缩放和滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19356258/