如何计算具有多个模型视图矩阵的拾取射线?

发布于 2024-09-19 01:14:34 字数 436 浏览 6 评论 0原文

我想计算光线与用户在我的应用程序屏幕上看到的模型之间的交集。我正在遵循 OpenGL 指南,但我在我在推送-弹出上下文中独立动态绘制的场景,将翻译应用于当前模型视图矩阵(即,对于每个模型,我执行

glPushMatrix

glTranslatef

绘制模型

glPopMatrix )。

我是否还需要计算不同的光线,每个对象各一条(以便稍后测试它是否与模型的边界球体相交)?

也许我还不太清楚有关光线拾取的一些概念。任何链接、指南等将不胜感激^^

I'd like to compute the intersection between a ray and the models the user sees on the screen of my app. I'm following the OpenGL guide but I have many models in the scene that I draw independently and dynamically in a push-pop context applying a translate to the current modelview matrix (ie for each model I do a

glPushMatrix,

glTranslatef,

draw the model,

glPopMatrix
).

Do I need to compute also different rays, one for each object (for testing later if it intersects with the model's bounding sphere)?

Maybe some concept about ray picking is not so clear to me yet. Any link, guide, etc will be very appreciated ^^

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

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

发布评论

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

评论(1

﹏半生如梦愿梦如真 2024-09-26 01:14:35

拾取射线与模型矩阵无关。如果场景中的对象移动(即模型矩阵发生变化),光线本身不会移动。

您计划在链接中建议的方法中使用哪种方法? GL_SELECTION,用特殊颜色渲染,自制交叉点?

我将相应地编辑这个答案

编辑:让我们选择自制的,然后

首先,您需要生成一条射线。你通常想在世界空间中表达它(即“我的光线与我的相机在相同的位置开始,它的目录是 (xyz) )。你有两种方法可以做到这一点,在 openGL Faq 中有很好的解释(生成拾取射线的一种方法...另一种方法...)只需复制并粘贴代码即可。

如果鼠标位于屏幕中间,则您的射线应该指向与相机相同的方向。您可以使用 gluLookAt 的参数来检查这一点。

现在,您想要做的是将光线与网格体的三角形相交,但是您遇到了问题,因为三角形与光线不在同一系统中表示,

所以您有 。三角形

系统:模型系统;光线系统:世界系统。幸运的是,您有一个从模型到世界的矩阵(模型矩阵,您在 glPushMatrix 之后设置的矩阵)。 inverse(ModelMatrix) 是从世界到矩阵的矩阵。

ray = Ray(cam_pos, dir);
foreach (mesh)
    ModelMatrix = whatever you want
    InverseModelMatrix = inverse(ModelMatrix)
    rayInModelSpace = Ray(InverseModelMatrix x ray.pos, InverseModelMatrix x ray.dir)
    Intersect(rayInModelSpace, mesh)

您的 Intersect 方法可能如下所示:

foreach (triangle in mesh )
    if ray.intersect(triangle) return true

三角形射线相交代码可以在 Geometrictools.com 上找到,

这会很慢,但它会起作用。有多种方法可以提高性能,但最简单的方法是计算每个网格的 AABB,然后首先将射线与 AABB 相交。与 AABB 没有交集 =>与网格没有交集

代码也可以在几何工具上找到。

我要离开几天,祝你好运:)

The picking ray has nothing to do with the model matrix. If the objects in your scene move (i.e. the model matrices change), the ray itself won't move.

Which method do you plan to use between those proposed in the link ? GL_SELECTION, rendering with a special color, home-made intersections ?

I'll edit this answer accordingly

EDIT : let's go for the home-made one then

First, you need to generate a ray. You usually want to express it in world space (i.e. "my ray starts at the same pos than my camera, and its dir is (xyz) ). You have two methods to do that, which are pretty well explained in the openGL Faq (One way to generate a pick ray... Another method...). Just copy 'n paste the code.

If the mouse is in the middle of the screen, you should have your ray pointing towards the same direction than your camera. You can check that with your arguments to gluLookAt.

Now, what you want to do is intersect your ray with your meshes' triangles, but you have a problem, because the triangles are not expressed in the same system than the ray.

So you have to express the ray in the triangle's system.

triangles system : the model system ; ray system : the world system. Fortunately, you have a matrix to go from model to world (the Model matrix, the one you set after your glPushMatrix). So inverse(ModelMatrix) is the matrix from world to matrix.

ray = Ray(cam_pos, dir);
foreach (mesh)
    ModelMatrix = whatever you want
    InverseModelMatrix = inverse(ModelMatrix)
    rayInModelSpace = Ray(InverseModelMatrix x ray.pos, InverseModelMatrix x ray.dir)
    Intersect(rayInModelSpace, mesh)

Your Intersect method could look like this :

foreach (triangle in mesh )
    if ray.intersect(triangle) return true

triangle-ray intersection code can be found at geometrictools.com

This will be quite slow, but it will work. There are several ways to improve the preformance, but the easiest is to compute the AABB of each mesh, and first intersect the ray with the AABB. No intersection with the AABB => no intersection with the mesh

Code can be found on geometrictools too.

I'll be away for a few days, so good luck :)

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