从鼠标坐标到 3d 的点-三角形相交?

发布于 2024-07-21 21:20:58 字数 183 浏览 4 评论 0原文

我知道如何测试点和三角形之间的交点。

...但是我不明白,如何使用鼠标坐标将点的起始位置精确地移动到屏幕平面上,因此点角度应该根据鼠标光标在屏幕上的位置而变化,这也应该起作用完美的是,无论我在 OpenGL 应用程序中使用哪个透视角度,因此不同透视角度上的点角度都会不同... gluPerspective() 是我正在谈论的函数。

I know how to test intersection between a point and a triangle.

...But i dont get it, how i can move the starting position of the point onto the screen plane precisely by using my mouse coordinates, so the point angle should change depending on where mouse cursor is on the screen, also this should work perfectly no matter which perspective angle i am using in my OpenGL application, so the point angle would be different on different perspective angles... gluPerspective() is the function im talking about.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我鬧 2024-07-28 21:20:58

好吧,先试一下,猜猜你的意思。 猜测是您想用鼠标拾取对象。 查看:

glUnProject

这会将屏幕坐标转换回 3D 世界坐标。

Google可以提供更多信息。

干杯!

Well, gonna take a shot and guess what you mean. The guess is that you would like to pick objects with your mouse. Check out:

glUnProject.

This transforms the screen coordinates back into 3d world coordinates.

Google has more information if you run into problems.

Cheers !

子栖 2024-07-28 21:20:58

是的,我想移动屏幕平面上的点,所以例如我可以使用 3d 坐标在鼠标当前所在的点上渲染一个立方体,然后我从该位置到鼠标所在的位置射一条线是指向的,所以它会击中我的 3D 世界中的三角形,以及我如何用鼠标选择该对象。

很抱歉不清楚:/

--

编辑:是的,我在 nehe 教程中得到了它! 谢谢,我不知道会这么容易!

这是我现在使用的代码,效果很好:

void GetOGLPos(int x, int y, GLdouble &posX, GLdouble &posY, GLdouble &posZ){
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);

    winX = (float)x;
    winY = (float)viewport[3]-(float)y;
    glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
}

yes, i want to move the point on the screen plane, so for example i could render a cube on that point where my mouse is currently, by using 3d coordinates, and then i shoot a line from that position to the place where my mouse is pointing, so it would hit the triangle in my 3d world, and that how i could select that object with mouse.

sorry for being unclear :/

--

Edit: yay i got it working with that nehe tutorial! thanks, i didnt know it would be that easy!

This is the code im using now and it works great:

void GetOGLPos(int x, int y, GLdouble &posX, GLdouble &posY, GLdouble &posZ){
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);

    winX = (float)x;
    winY = (float)viewport[3]-(float)y;
    glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
}
撩动你心 2024-07-28 21:20:58

您需要生成一条垂直于屏幕穿过鼠标位置的射线(线)。

我建议您在进一步了解之前先了解一些有关 3D 几何和 2D 投影的基本信息。

查看维基百科

图书搜索Google 上已经推出了相当多的标题。

弗利与 Van Dam 是一本权威的书 - 位于 亚马逊。 co.ukAmazon.com

You need to generate a ray (line) passing through the mouse location perpendicular to the screen.

I would recommend getting some basic information on 3d geometry and 2d projections before you go much further.

Check out Wikipedia

A book search on Google has come up with quite a few titles.

Foley & Van Dam though is the definitive book - here on Amazon.co.uk or here on Amazon.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文