如何确定 OpenGL 中何时在屏幕上绘制对象?
我对 OpenGL 非常陌生。我正在编写一个在屏幕上显示飞行 3D 文本的程序。我需要知道某些文本字符串何时出现(绘制)到屏幕上并且对用户可见。程序需要识别显示哪些文本字符串。 (注意:虽然我的问题涉及文本,但它可以推广到任何 OpenGL 对象)。
起初,我开始认为我可以使用 OpenGL 的拾取机制,但到目前为止我只看到了选择区域专注于某种用户交互的示例。我想知道整个窗口区域显示了哪些对象。这让我觉得我走错了路……我错过了什么吗?
欢迎任何建议。
I'm extremely new to OpenGL. I'm writing a program that displays flying 3D text on screen. I need to know when certain text string appears (drawn) onto the screen and are visible to the user. The program needs to identify which text strings are displayed. (Note: although my problem deals with text, it could be generalized to any OpenGL object).
At first, I started to think that I could use OpenGL's picking mechanism, but so far I've only seen examples where the selection area is focused on some sort of user interaction. I want to know what objects are displayed on the entire window area. This leads me to think I'm on the wrong track... Am I missing something?
Any suggestions are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用查询对象(特别是使用 GL_ARB_occlusion_query 扩展创建的对象规范)。这些对象用于查询使用一系列 OpenGL 操作(开始/结束等)渲染了多少片段。
另一种方案(仅限软件)是确定渲染文本的边界框,然后以数学方式计算边界框是否位于视锥体内部(源自用于渲染的当前视角。
注意:使用OpenGL拾取并不一定意味着使用gluPickMatrix。您可以“按原样”渲染场景,并且查询渲染的名称(尽管从OpenGL 3开始不推荐拾取)
。对于大多数硬件来说,挑选是另一个很好的解决方案,但比查询对象更示意性。
You can use the query objects (specifically those object created using GL_ARB_occlusion_query extension Specification). Those object are used to query how many fragments are rendered using a sequence of OpenGL operations (begin/end, etc...).
Another scheme (software only), is to determine a bounding box for your rendered text, then compute mathematically whether the bounding box is inside the view frustrum (derived from the current perspective used for rendering.
A note: using OpenGL picking doesn't necessary imply the use of gluPickMatrix. You can render you scene "as is", and the query the rendered names (altought picking is deprecated from OpenGL 3).
Query objects are easy to use, and they are lightweight. Picking is another good solution for most hardware, but more schematic than query objects.
嗯,它实际上是 3D 的吗?或者它只是 2D 空间中屏幕上的 2D 文本?在这种情况下,我只会手动跟踪它。你到底是如何绘制你的文字的?
通常,您执行此操作的方法是使用“视锥体检查”,您基本上只需为相机制作一个体积并测试 3D 对象是否在其中。
hmm, is it actually in 3D? or is it just 2D text on the screen in 2D space? in that case I would just keep track of it manually. how exactly are you drawing your text?
generally the way you do this is with a "frustum check" where you basically just make a volume for the camera and test whether you're 3d objects are inside it or not.
您可以尝试OpenGL的反馈机制。在此模式下,OpenGL 生成片段并将它们传递到反馈缓冲区。如果某些内容被剪切,则不会生成任何片段。当文本变得可见时,您将在片段缓冲区中找到相应的片段。
这个链接应该可以帮助您入门。
这是另一个链接,问题 10.010 似乎与您的内容特别相关想。
You can try OpenGL's feedback mechanism. In this mode, OpenGL generates fragments and passes them to a feedback buffer. If something is clipped, no fragments will be generated. When the text becomes visible, you will find the corresponding fragment in the fragment buffer.
This link should get you started.
Here is another link, the Question 10.010 seems particularly relevant to what you want.
通过投影和模型视图矩阵运行对象坐标以获得屏幕空间坐标。将 X/Y 输出与屏幕范围进行比较,以确定文本是否在屏幕上。
Run your object coordinates through your projection and modelview matrices to get screen-space coordinates. Compare the X/Y output against your screen extents to figure out if the text is on-screen.