gluUnproject返回0,似乎与模型视图矩阵有关
gluUnproject returning 0, seems to be related to modelview matrix
我正在开发一个 2D 图像查看器,我想检索纹理上的 openGL 鼠标位置,但如果在模型视图矩阵上进行 glTranslatef() 或 glScalef() 调用,我无法让它工作。我正在使用著名的Qt库的QGLWidget。
以下是重要的电话:调整大小功能:
void ViewerGL::resizeGL(int width, int height){
glViewport (0, 0, width, height);
显示功能:
void ViewerGL::paintGL()
{ int w = width();
int h = height();
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
//transX,transY are for panning around the image in the viewer
float left = (0.f+transX) ;
float right = (w+transX) ;
float bottom = (h-transY);
float top = (0.f-transY) ;
glOrtho(left, right, top, bottom, -1, 1);
。后来在油漆GL中:
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
//padx,pady are used to translate the image from the bottom left corner
// to the center of the viewer
float padx,pady;
padx= ((float)width() - _dw.w()*zoomFactor)/2.f; // _dw.w is the width of the texture
pady =((float)height() - _dw.h()*zoomFactor)/2.f ;// _dw.h is the height of the texture
glTranslatef( padx , pady, 0);
//zoomX,zoomY are the position at which the user required a zoom
glTranslatef(-zoomX,-zoomY, 0.f);
glScalef(zoomFactor, zoomFactor,0.f);
glTranslatef(zoomX ,zoomY, 0.f);
现在这是我检索 openGL 坐标的函数:
QPoint ViewerGL::openGLpos(int x,int y){
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX=0, winY=0, winZ=0;
GLdouble posX=0, posY=0, posZ=0;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)x;
winY = height()- y;
if(winY == 0) winY =1.f;
glReadPixels( x, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
return QPoint(posX,posY);
}
到目前为止,这是我注意到的:像这样的代码总是返回 (0,0),GLU_FALSE从 gluUnproject 返回。我在论坛上的某处读到可能是因为模型视图矩阵,所以我放了单位矩阵,但是,如果我这样做,我会得到窗口中鼠标的坐标......
之前,我使用正交投影处理缩放,但我无法使其完美工作,因此为了更简单,我决定检索 openGL 位置,并使用 glTranslatef/glScalef 代替。
如果我删除 paintGL 函数中的所有平移/缩放内容,一切都在工作......但是缩放不起作用:x)
我请求您帮助使用 gluUnProject 解决方案使这个该死的缩放到点工作;
Aigth , 没关系,我找到了解决方案:我在 glScalef(x,y,z) 中将 z 归零所以它使矩阵不可逆...
相关文章:
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 如何根据模型设置的视图调整模型?
- Qml 列表视图在重置模型时保留视图
- 为 QML 树视图创建模型
- C++模型视图设计
- 如何从模型/视图中获取QImage POINTER
- 更新模型/视图方案中 QXXXView (QTableView, QTreeView,..) 元素的显示工具提示
- MFC在切换视图时如何关闭无模型对话框
- 从角度转换为正交保持相同的模型和缩放的视图大小
- Qt中数据类(模型)和视图/控制器类之间的数据通信的正确方式是什么
- Qt:从视图中删除模型
- Qt:模型/视图框架上的数据转换
- 根据用户的视图更改更新模型
- 多个模型视图矩阵
- 垂直列表视图中的水平列表视图模型
- 具有两种不同视图的Qt数据模型
- Qt 5.2模型视图模式:如何通知模型对象底层数据结构的变化
- 了解Qt视图模型架构:何时创建以及如何在QAbstractItemModel实现中清除索引
- Qt中刷新视图模型
- 现代OpenGL投影视图模型转换不起作用