gpt4 book ai didi

OpenGL:将鼠标点击投影到几何体上

转载 作者:行者123 更新时间:2023-12-02 11:39:31 27 4
gpt4 key购买 nike

我设置了这个 View :

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective

我可以通过单击鼠标获得屏幕位置(sx,sy)。

给定 z 值,如何根据 sx 和 sy 计算 3d 空间中的 x 和 y?

最佳答案

您应该使用gluUnProject:

首先,计算到近平面的“非投影”:

GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLint viewport[4];

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);

GLdouble x, y, z;
gluUnProject(sx, viewport[1] + viewport[3] - sy, 0, modelMatrix, projMatrix, viewport, &x, &y, &z);

然后到远平面:

// replace the above gluUnProject call with
gluUnProject(sx, viewport[1] + viewport[3] - sy, 1, modelMatrix, projMatrix, viewport, &x, &y, &z);

现在您已经在世界坐标中得到了一条线,它可以追踪出您可能点击过的所有可能的点。所以现在你只需要插值:假设你给定了 z 坐标:

GLfloat nearv[3], farv[3];  // already computed as above

if(nearv[2] == farv[2]) // this means we have no solutions
return;

GLfloat t = (nearv[2] - z) / (nearv[2] - farv[2]);

// so here are the desired (x, y) coordinates
GLfloat x = nearv[0] + (farv[0] - nearv[0]) * t,
y = nearv[1] + (farv[1] - nearv[1]) * t;

关于OpenGL:将鼠标点击投影到几何体上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/113352/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com