OpenGL gluUnProject 对象部分
我正在使用 gluUnProject 将光线投射到场景中并在那里添加基元。我现在想做的是准确地选择现有的图元,所以如果我有 3 个球体,我可以单击其中一个将其删除。
我认为解决方案会以某种方式检查光线是否与物体相交,并检查它是否最接近投射原点。到目前为止,我的解决方案是原始的,并且用边界立方体包围所有对象,无论如何,是否可以简单地使用以下方法准确地为球体执行此操作:
does the ray intersect with ( object)
或者
returnRayIntersections(ray);
最后一件事,我正在使用 OpenGL 和 GLUT。
谢谢大家, 劳伦斯
I'm using gluUnProject to cast a ray into the scene and adding a primitive there. What I'm trying to do is now accurately pick existing primitives, so if I have 3 spheres I could click on one to delete it.
I think the solution would somehow check if the ray intersected with an object and check if its the closest to the casting origin. My solution so far is primitive and surrounds all objects with a bounding cube, is there anyway to simply to do this accurately for say spheres using:
does the ray intersect with ( object)
or
returnRayIntersections(ray);
Last thing, I'm using OpenGL with GLUT.
Thanks everyone,
Laurence
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 OpenGL 选择模式是实现此目的的最佳方法,因为它可以处理任意复杂的渲染,而不仅仅是球体。您可以看到很多这方面的教程,但粗略地说:您首先在选择模式下设置 GL:
然后,在设置一个选择缓冲区来捕获您所在片段的名称后,绘制场景渲染。这篇文章将帮助您入门。
Nehe 的教程很长,但也非常好。
Using OpenGL selection mode is the best way to do this, since it can handle arbitrarily complex rendering, not just spheres. You can see many tutorials for this, but roughly: you start by setting GL in selection mode:
Then, you draw the scene, after setting up a selection buffer that captures the names of the fragments that you are rendering. This article will get you started.
Nehe's tutorial is long, but very good too.
在 OpenGL 中实现拾取的另一个好方法是这样的:
将场景绘制到后台缓冲区中,为要选择的每个图元使用唯一的颜色。如果您使用 24 位模式,则颜色可以简单地为 #000001、#000002 等。关闭照明、雾等,以便您指定的颜色与像素将要采用的颜色完全相同。
不要将后台缓冲区传输到屏幕(不要使用 glSwapBuffers)。相反,使用 glReadPixels 将 GL 后台缓冲区读入内存缓冲区。之后,您将获得一个内存位图,您可以从中读取与鼠标在屏幕上的位置相对应的像素值。您从中读出的颜色值可以轻松映射到基元(因为它将是#000001、#000002等)。
以下是有关此选择样式的更多信息。这是我喜欢使用的,因为它比 GL_SELECTION 模式有一个优点。如果 3D 场景不变并且鼠标在其上移动,我可以获取后台缓冲区的一份副本,然后通过简单地引用内存位图中的相应像素来快速估计哪个实体位于鼠标下方。在场景发生变化之前我根本不需要使用任何 GL 调用。由于我的应用程序有很多鼠标悬停在 3D 场景上,并且我需要快速知道鼠标悬停在哪个实体上,因此我发现这种方法非常快。
Another good way to implement picking in OpenGL is like this:
Draw your scene into the back buffer, using a unique color for each primitive that you want to select. If you are using a 24 bit mode, the colors can simply be #000001, #000002 etc. Turn off lighting, fog etc so that the colors you specify are the exact colors the pixels are going to take.
Don't blit the back-buffer to the screen (don't use glSwapBuffers). Instead, use glReadPixels to read the GL back-buffer into a memory buffer. After this you have a memory bitmap that you can read a pixel value from, corresponding to the location of the mouse on-screen. The color value you read out from this can easily then be mapped to the primitive (since it's going to be #000001, #000002 etc).
Here's some more information on this style of selection. This is what I like to use, since it has one advantage over the GL_SELECTION mode. If the 3D scene is unchanging and the mouse is being moved over it, I can get one copy of the back-buffer and then quickly estimate which entity is under the mouse, by simply referencing the appropriate pixel in my memory bitmap. I don't have to use any GL calls at all until the scene changes. Since my application has a lot of mouse hovering over a 3D scene and I need to quickly know which entity the mouse is floating over, I found this method really fast.