简体   繁体   English

向GL空间内的对象添加鼠标侦听器

[英]Adding a mouse listener to an object within a GL space

As part of a game I am writing, I have a textured ground plane that is currently implemented as a quad. 作为我正在编写的游戏的一部分,我有一个纹理化的地平面,目前已实现为四边形。

I need to be able to listen for mouse clicks and pin point their coordinates on the ground plane. 我需要能够听到鼠标的点击并将其坐标指向地面。

I can easily implement a listener that returns the coordinates within the viewspace. 我可以轻松实现一个侦听器,该侦听器返回视图空间内的坐标。 However, the viewpoint is moveable, so a pixel in the viewspace does not always correspond to the same coordinates on the ground plane. 但是,视点是可移动的,因此视图空间中的像素并不总是对应于地平面上的相同坐标。

Is it possible to implement a mouse listener for a quad? 是否可以为Quad实现鼠标侦听器?

Otherwise, is there an alternative plane implementation that will allow me to texture the plane and listen for mouse events. 否则,是否有替代的平面实现,使我可以构造平面纹理并侦听鼠标事件。

Any advice greatly appreciated! 任何建议,不胜感激!

The first thing I thought of reading your question was ( Picking ). 我想到阅读您的问题的第一件事是( Picking )。 But since you've only a single quad you won't be able to determine positions within the quad. 但是由于您只有一个四边形,所以您将无法确定该四边形内的位置。

Another solution would be to solve this problem outside the scope of OpenGL and calculate the intersection of the ray and the quad. 另一个解决方案是在OpenGL范围之外解决此问题,并计算射线与四边形的交点。 To determine the ray that goes from the point of view to the scene you would still need the OpenGL context though. 为了确定从视点到场景的光线,您仍然需要OpenGL上下文。

OpenGL just draws stuff to the screen. OpenGL只是将内容绘制到屏幕上。 In particular it draws points, lines, triangles, quads and convex planar polygons. 特别是它绘制点,线,三角形,四边形和凸平面多边形。 Once the pixels have been set, OpenGL has no persistency about what it drew. 设置像素后,OpenGL就不再保留其绘制的内容。

The terms in which you're thinking in is called a scene graph , but OpenGL is not such a thing. 您所考虑的术语称为场景图 ,但OpenGL并非如此。

However you can of course implement a scene graph that used OpenGL for drawing stuff, and this scene graph could implement event management and associate event listeners to stuff it drew. 但是,您当然可以实现使用OpenGL绘制场景的场景图,并且该场景图可以实现事件管理并将事件侦听器与其绘制的内容相关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM