使用gluUnProject映射润色X,Y线在z = 0平面的Android的OpenGL ES 2.0 [英] Using gluUnProject to map touches to x,y cords on z=0 plane in Android OpenGL ES 2.0
问题描述
我在OpenGL ES 2.0绘制网格在z = 0,只是想触摸输入x / y坐标上飞机转换。看起来这通过光线追踪,其中包括在0和1的运行gluUnProject,然后创建一个线,解决了光线用于z是最好的做法= 0?
I've drawn a grid at z=0 in OpenGL 2.0 ES, and just want to convert touch inputs to x/y coordinates on that plane. It seems like this is best done through ray tracing, which involves running gluUnProject on 0 and 1, then creating a ray, solving that ray for z=0?
我发现这个code,但它的OpenGL ES 1.0: I-schuetz / Android_OpenGL_Picking 一>
I found this code, but it is OpenGL ES 1.0: i-schuetz / Android_OpenGL_Picking
我的code Github上,只有4个文件。该unproject功能我试着写在MyGLRenderer.java:
My code on Github, only 4 files. The unproject function I'm trying to write is in MyGLRenderer.java:
public float[] unproject(float rx, float ry) {
float rz = 1;
float[] xyzw = {0, 0, 0, 0};
int[] viewport = {0, 0, mDisplayWidth, mDisplayHeight};
android.opengl.GLU.gluUnProject(
rx, ry, rz, // window coordinates
mViewMatrix, 0,
mProjectionMatrix, 0,
viewport, 0,
xyzw, 0);
xyzw[0] /= xyzw[3];
xyzw[1] /= xyzw[3];
xyzw[2] /= xyzw[3];
xyzw[3] = 1;
return xyzw;
}
我想这个函数来采取Rx和Ry为屏幕,并返回Rx和Ry为Z = 0的平面。
I would like this function to take an rx and ry for the screen, and return an rx and ry for the z=0 plane.
推荐答案
没有什么特别之处是什么 gluUnProject(...)
一样。如果你把所有的矩阵和视口尺寸<子>( X,Y 和是W <子>IDþ, ^ h 强> <子> 8 )我可以步行通过自己实现它的过程。
There is nothing particularly special about what gluUnProject (...)
does. If you have all of the matrices and the viewport dimensions (x,y and width,height) I can walk you through the process of implementing it yourself.
<分> 注意:我倾向于看涨分别由比你可能被用于不同的名称坐标空间,理解的的的屏幕空间的是另一个名字对于的的窗口空间的 的视图空间的是另一个名字对于的眼位的 的对象空间的是另一个名字对于的模型空间的
NOTE: I tend to call each coordinate space by a different name than you might be used to, understand that screen space is another name for window space, view space is another name for eye space, object space is another name for model space.
第1步:
&NBSP;&NBSP;&NBSP;屏幕空间NDC空间<子>(撤消视口变换)
Step 1:
Screen Space to NDC space (Undo Viewport Transform)
NDCX =(2.0×(屏幕 <子>的 X 的 - 视窗 <子>的 X 的子>)/ 视窗 <子>的是W 的)&NBSP; - 1.0
NDCX = (2.0 × (ScreenX - ViewportX) / ViewportW) - 1.0
NDCY =(2.0×(屏幕 <子>的是的 - 视窗 <子>的是的子>)/ 视窗 <子>的 ^ h 的)&NBSP;&NBSP; - 1.0
NDCY = (2.0 × (ScreenY - ViewportY) / ViewportH) - 1.0
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;通常,在屏幕空间,在深度范围将映射 Z = 0 到< STRONG>的附近的和 Z = 1 为的据的
Typically in screen space, the Depth Range will map z=0 to near and z=1 to far:
NDCZ = 2.0×屏幕 <子>的以Z 的 - 1.0
NDCZ = 2.0 × ScreenZ - 1.0
第2步 † :
&NBSP;&NBSP;&NBSP; NDC空间物体空间<子>(撤消投影,视图和模型变换)
Step 2†:
NDC space to Object space (Undo Projection, View and Model Transforms)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;(投影矩阵) 1 &NBSP;&NBSP;× NDCXYZ1 = ViewXYZW
(Projection Matrix)-1 × NDCXYZ1 = ViewXYZW
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;(模型视图矩阵) 1 &NBSP;× ViewXYZW = ObjectXYZW
(ModelView Matrix)-1 × ViewXYZW = ObjectXYZW
† This实际上可以组合成单个步骤,你会看到下面... 子>
† This can actually be combined into a single step, as you will see below...
对象 <分> XYZ <打击>的是W 的行使> =(投影矩阵× 模型视图矩阵) 1 &NBSP;× NDC <分> XYZ 1 的
ObjectXYZw = (Projection Matrix × ModelView Matrix)-1 × NDCXYZ1
现在,你可能会注意到,我打叉的<击>是W 罢工>的对象<子> XYZ ,我们真的不关心这个,但在所有的数学会产生一个讨厌的是W 的价值不过。在这一点上,你可以返回对象<子> XYZ 作为您的研究<子> X ,R <子>是次的各个组成部分> 和研究<子>以Z
Now, you may notice that I crossed-out
这篇关于使用gluUnProject映射润色X,Y线在z = 0平面的Android的OpenGL ES 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!