
if (event->buttons()&Qt::RightButton)//滑鼠右鍵
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(0, 0, 0, 0, 0.8, 0.4);
glOrtho(-25, 25, -25, 25, -20, 20);
//glFrustum(-1, 1, -1, 1, 0.2, 35); // near and far match your triangle Z distance
gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
double modelview[16], projection[16];
POINT iScreen;
int viewport[4];
float x = event->pos().rx();
float y = event->pos().ry();
GLfloat z = 0;
double winx, winy, winz;
qDebug("Window coords are (%d, %d)\n", x, y);
/*Read the projection, modelview and viewport matrices using the glGet functions.*/
glGetIntegerv(GL_VIEWPORT, viewport);//獲得視角矩陣
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);//獲得觀察矩陣
glGetDoublev(GL_PROJECTION_MATRIX, projection);//獲得投影矩陣
//::GetCursorPos(&iScreen);
//Read the window z value from the z-buffer
glReadBuffer(GL_FRONT);
glReadPixels(x, viewport[3] - y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
//Use the gluUnProject to get the world co-ordinates of
//the point the user clicked and save in objx, objy, objz.
gluUnProject(x, viewport[3] - y, z, modelview, projection, viewport, &objx, &objy, &objz);
qDebug("World coords at z=%.1f are (%.3f, %.3f, %.3f)\n", z, objx, objy, objz);
update();
為什么我獲取的坐標z都是相同的呀
uj5u.com熱心網友回復:

以前專門研究了2d->3d滑鼠拾取。
3d->2d 公告板之類的顯示
uj5u.com熱心網友回復:
可以教一下我應該怎么改嗎?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/32545.html
標籤:C++ 語言
