Android 中的 glUnProject 问题

发布于 2024-11-03 06:23:11 字数 727 浏览 0 评论 0原文

在我的游戏中,我需要找出玩家触摸的位置。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很快妥协 2024-11-10 06:23:11

好吧,对我来说最简单的方法是将屏幕上的点击想象成一条从相机位置开始并进入无穷远的射线

为了获得该射线,我需要询问它在至少 2 个 Z 位置的世界坐标(在视图坐标中) 。

这是我寻找光线的方法(我猜是取自同一个 Android 演示应用程序)。
它在我的应用程序中运行良好:

public void Select(float x, float y) {
    MatrixGrabber mg = new MatrixGrabber();
    int viewport[] = { 0, 0, _renderer.width, _renderer.height };

    _renderer.gl.glMatrixMode(GL10.GL_MODELVIEW);
    _renderer.gl.glLoadIdentity();
    // We need to apply our camera transformations before getting the ray coords 
    // (ModelView matrix should only contain camera transformations)
    mEngine.mCamera.SetView(_renderer.gl);

    mg.getCurrentProjection(_renderer.gl);
    mg.getCurrentModelView(_renderer.gl);

    float realY = ((float) (_renderer.height) - y);
    float nearCoords[] = { 0.0f, 0.0f, 0.0f };
    float farCoords[] = { 0.0f, 0.0f, 0.0f };
    gluUnProject(x, realY, 0.0f, mg.mModelView, 0, mg.mProjection, 0,
            viewport, 0, nearCoords, 0);
    gluUnProject(x, realY, 1.0f, mg.mModelView, 0, mg.mProjection, 0,
            viewport, 0, farCoords, 0);

    mEngine.RespondToClickRay(nearCoords, farCoords);
}

在 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:

public void Select(float x, float y) {
    MatrixGrabber mg = new MatrixGrabber();
    int viewport[] = { 0, 0, _renderer.width, _renderer.height };

    _renderer.gl.glMatrixMode(GL10.GL_MODELVIEW);
    _renderer.gl.glLoadIdentity();
    // We need to apply our camera transformations before getting the ray coords 
    // (ModelView matrix should only contain camera transformations)
    mEngine.mCamera.SetView(_renderer.gl);

    mg.getCurrentProjection(_renderer.gl);
    mg.getCurrentModelView(_renderer.gl);

    float realY = ((float) (_renderer.height) - y);
    float nearCoords[] = { 0.0f, 0.0f, 0.0f };
    float farCoords[] = { 0.0f, 0.0f, 0.0f };
    gluUnProject(x, realY, 0.0f, mg.mModelView, 0, mg.mProjection, 0,
            viewport, 0, nearCoords, 0);
    gluUnProject(x, realY, 1.0f, mg.mModelView, 0, mg.mProjection, 0,
            viewport, 0, farCoords, 0);

    mEngine.RespondToClickRay(nearCoords, farCoords);
}

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.

楠木可依 2024-11-10 06:23:11

您的错误来自输出数组的大小。它们需要长度 4,而不是上面代码中的 3。

Your error comes from the size of the output arrays. They need length 4 and not 3 as in the code above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文