简体   繁体   English

转换后获取OpenGL 3D对象的屏幕坐标和大小

[英]Get screen coordinates and size of OpenGL 3D Object after transformation

I have a couple of 3D objects in OpenGL in a processing sketch and I need to find out if the mouse is hovering over those objects. 在处理草图中,我在OpenGL中有几个3D对象,我需要确定鼠标是否悬停在这些对象上。 Since there is constant transformation I can't compare the original coordinates and size to the mouse position. 由于存在常量转换,因此无法将原始坐标和大小与鼠标位置进行比较。 I already found the screenX() and screenY() methods which return the translated screen coordinates after transformation and translation but I would still need to get the displayed size after rotation. 我已经找到了screenX()和screenY()方法,它们在转换和转换后返回转换后的屏幕坐标,但是旋转后我仍然需要获取显示的大小。

Determining which object the mouse is over is called picking and there are 2 main approaches: 确定鼠标悬停在哪个对象上称为拾取,主要有两种方法:

  1. Color picking. 选色。 Draw each object using a different color into the back buffer (this is only done when picking, the colored objects are never displayed on screen). 使用不同的颜色将每个对象绘制到后缓冲区中(这仅在拾取时完成,有色对象永远不会显示在屏幕上)。 Then use glReadPixels to read the pixel under the cursor and check its color to determine which object it is. 然后使用glReadPixels读取光标下方的像素并检查其颜色以确定它是哪个对象。 If the mouse isn't over an object you'll get the background color. 如果鼠标不在对象上,则将获得背景色。 More details here: Lighthouse 3D Picking Tutorial, Color Coding 此处有更多详细信息: Lighthouse 3D Picking教程,颜色编码
  2. Ray casting. 射线铸造。 You cast a ray through the cursor location into the scene and check if it intersects any objects. 您将光线通过光标位置投射到场景中,并检查它是否与任何对象相交。 More details here: Mouse picking with ray casting 此处有更多详细信息: 使用射线投射进行鼠标拾取

From reading your description option 1 would probably be simpler and do what you need. 阅读您的描述选项1可能会更简单,并且可以完成您需要的操作。

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

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