opengl鼠标获取三维坐标

lingyikong 2020-07-03 11:32:50

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都是相同的呀
...全文
295 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-06
  • 打赏
  • 举报
回复
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10898660.html
lingyikong 2020-07-06
  • 打赏
  • 举报
回复
可以教一下我应该怎么改吗?
smwhotjay 2020-07-03
  • 打赏
  • 举报
回复
以前专门研究了2d->3d鼠标拾取。 3d->2d 公告板之类的显示

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧