在OpenGL ES中将2D鼠标坐标转换为3D空间 [英] converting 2D mouse coordinates to 3D space in OpenGL ES

查看:110
本文介绍了在OpenGL ES中将2D鼠标坐标转换为3D空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将鼠标的当前X和Y坐标转换为我在视口中绘制的3D空间.我需要在OpenGL ES平台上执行此操作.我发现以下在OpenGL中实现的可能解决方案,但都不符合我的需求.

I want to convert mouse's current X and Y coordinates into the 3D space I have drawn in the viewport. I need to do this on the OpenGL ES platform. I found following possible solutions implemented in OpenGL, but none fits what I am looking for.

  1. 我发现NeHe的教程完全按照传统的OpenGL方式进行.它使用gluUnProject. http://nehe.gamedev.net/data/articles/article. asp?article = 13 尽管gluUnProject在OpenGL ES中不可用,但其实现似乎很简单,可以移植回去.但是在调用它之前,我们需要使用GL_DEPTH_COMPONENT调用glReadPixels,而这在OpenGL ES中是不可能的. (我在此线程中找到的原因: http://www .khronos.org/message_boards/viewtopic.php?f = 4& t = 771 )

  1. I found NeHe's tutorial on doing exactly this, but in traditional OpenGL way. It uses gluUnProject. http://nehe.gamedev.net/data/articles/article.asp?article=13 Although gluUnProject is not available in OpenGL ES, its implementation seems simple enough to port back. But before calling it, we need to call glReadPixels with GL_DEPTH_COMPONENT and that is not possible in OpenGL ES. (The reason I found in this thread: http://www.khronos.org/message_boards/viewtopic.php?f=4&t=771)

我要执行的操作与拾取相似,不同之处在于,我不想选择对象,但需要精确的坐标,以便可以识别当前在鼠标光标下的对象的特定部分.我在这个答案中浏览了Picking教程. https://stackoverflow.com/posts/2211312/revisions 但是他们需要glRenderMode,我相信它在OpenGL ES中是不存在的.

What I want to do is similar to picking, except that I don't want to select the object but I want exact coordinates so that I can recognize particular portion of the object that is currently under mouse cursor. I went through the Picking tutorials in this answer. https://stackoverflow.com/posts/2211312/revisions But they need glRenderMode, which I believe is absent in OpenGL ES.

如果您知道如何在OpenGL ES中解决此问题,请告诉我.

If you know how to solve this problem in OpenGL ES, please let me know.

谢谢.

推荐答案

我认为一般的解决方案是,假设屏幕是世界上的某个平面(在摄像头所在的位置),则计算出单击坐标在世界空间中的哪个位置.然后,您将垂直于飞机的光线发射到场景中.

I think the general solution is to figure out where in world space the clicked coordinate falls, assuming the screen is a plane in the world (at the camera's location). Then you shoot a ray perpendicular to the plane, into your scene.

这需要世界空间"代码来找出射线与哪些对象相交;您提到的不适用于OpenGL ES的解决方案似乎是基于图像的,即取决于渲染场景时生成的像素.

This requires "world-space" code to figure out which object(s) the ray intersects with; the solutions you mention as being unsuitable for OpenGL ES seem to be image-based, i.e. depend on the pixels generated when rendering the scene.

这篇关于在OpenGL ES中将2D鼠标坐标转换为3D空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆