Android 中的 glUnProject 问题
在我的游戏中,我需要找出玩家触摸的位置。 MotionEvent.getX() 和 MotionEvent.getY() 返回窗口坐标。所以我创建了这个函数来测试将窗口坐标转换为 OpenGL 坐标:
public void ConvertCoordinates(GL10 gl) {
float location[] = new float[4];
final MatrixGrabber mg = new MatrixGrabber(); //From the SpriteText demo in the samples.
mg.getCurrentProjection(gl);
mg.getCurrentModelView(gl);
int viewport[] = {0,0,WinWidth,WinHeight};
GLU.gluUnProject((float) WinWidth/2, (float) WinHeight/2, (float) 0, mg.mModelView, 0, mg.mProjection, 0, viewport, 0, location,0);
Log.d("Location",location[1]+", "+location[2]+", "+location[3]+"");
}
X 和 y 从几乎 -33 振荡到几乎 +33。 Z通常是10。我使用MatrixGrabber错误还是什么?
In my game, I need to find out where the player is touching. MotionEvent.getX() and MotionEvent.getY() return window coordinates. So I made this function to test converting window coordinates into OpenGL coordinates:
public void ConvertCoordinates(GL10 gl) {
float location[] = new float[4];
final MatrixGrabber mg = new MatrixGrabber(); //From the SpriteText demo in the samples.
mg.getCurrentProjection(gl);
mg.getCurrentModelView(gl);
int viewport[] = {0,0,WinWidth,WinHeight};
GLU.gluUnProject((float) WinWidth/2, (float) WinHeight/2, (float) 0, mg.mModelView, 0, mg.mProjection, 0, viewport, 0, location,0);
Log.d("Location",location[1]+", "+location[2]+", "+location[3]+"");
}
X and y oscillated from almost -33 to almost +33. Z is usually 10. Did I use MatrixGrabber wrong or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,对我来说最简单的方法是将屏幕上的点击想象成一条从相机位置开始并进入无穷远的射线
为了获得该射线,我需要询问它在至少 2 个 Z 位置的世界坐标(在视图坐标中) 。
这是我寻找光线的方法(我猜是取自同一个 Android 演示应用程序)。
它在我的应用程序中运行良好:
在 OGL10 AFAIK 中,您无法访问 Z 缓冲区,因此您无法在屏幕 x,y 坐标处找到最近对象的 Z。
您需要自行计算射线击中的第一个物体。
Well the easiest way for me to get into this was imagining the onscreen click as a ray that starts in camera position and goes into infinity
To get that ray i needed to ask for it's world coords in at least 2 Z positions (in view coords).
Here's my method for finding the ray(taken from the same android demo app i guess).
It works fine in my app:
In OGL10 AFAIK you don't have access to Z-Buffer so you can't find the Z of the closest object at the screen x,y coords.
You need to calculate the first object hit by your ray on your own.
您的错误来自输出数组的大小。它们需要长度 4,而不是上面代码中的 3。
Your error comes from the size of the output arrays. They need length 4 and not 3 as in the code above.