关于Android的3D采摘(带的OpenGL ES 2)问题问题、Android、OpenGL、ES

2023-09-07 12:12:09 作者:咽泪^

我需要3D采摘一些帮助。

I need some help on 3D picking.

我使用它的工作原理这里的路。总之,我拥有的是:

I am using the way it works here. In short, what I have is:

normalizedPoint[0] = (x * 2 / screenW) -1;
normalizedPoint[1] = 1 - (y * 2 / screenH);
normalizedPoint[2] = ?
normalizedPoint[3] = ?

2和3,我不知道它应该是什么(我把1,-1就像引用,它不工作)

for 2 and 3, I have no idea what it should be (I put 1, -1 just like the reference, and it doesn't work)

然后,我的根对象(以下只是伪code):

Then, for my root object (following just psuedo code):

matrix = perspective_matrix x model_matrix
inv_matrix = inverse(matrix)
outpoint = inv_matrix x normalizedPoint

这就是我有,但它不工作,我收到外点甚至还没有接近到如此地步,我假设点击。我在网上搜索了一个多星期。但不知道如何解决它。帮助!

That's what I have, but it doesn't work, the outPoint I receive is not even close to the point I am suppose clicking. I've been searching in web for more than a week. but no idea how to solve it. HELP!

推荐答案

哦。其实,我解决了这个问题,排序的。

Oh. I actually solved the problem, sort of.

我第一次,从glUnproject源$ C ​​$ C修改有以下几点:

I first, modify from the source code of glUnproject to have the following:

public static Vec3 unProject(
        float winx, float winy, float winz,
        Matrix44 resultantMatrix,
        int width, int height){
    float[] m = new float[16],
    in = new float[4],
    out = new float[4];

    m = Matrix44.invert(resultantMatrix.get());

    in[0] = (winx / (float)width) * 2 - 1;
    in[1] = (winy / (float)height) * 2 - 1;
    in[2] = 2 * winz - 1;
    in[3] = 1;

    Matrix.multiplyMV(out, 0, m, 0, in, 0);

    if (out[3]==0)
        return null;

    out[3] = 1/out[3];
    return new Vec3(out[0] * out[3], out[1] * out[3], out[2] * out[3]);
}

输入到上面会在投影视域坐标(即,屏输入)的点。例如:

Input to the above would be the point in the Projected View Frustum Coordinates (i.e., screen input). For example:

unProject(30, 50, 0, mvpMatrix, 800, 480) 

将转化在(30,50)的画面输入(点击)与世界坐标,其中该对象坐在。第三个参数, winz 实际上是在其投影面点击的发生,在这里,0表示投影面的nearZ。

will translate the screen input (click) at (30,50) to the world coordinate where the object is sitting at. The third parameter, winz is actually on which projected plane the click is occured, here, 0 means the nearZ of the projection plane.

我做出选择的功能,该方法是通过unprojecting两分,使用上述功能,在远近裁剪平面,所以:

The way I make picking functions, is by unprojecting two points, using the above function, on the far and near clipping plane, so:

Vec3 near = unProject(30, 50, 0, mvpMatrix, 800, 480);
Vec3 far = unProject(30, 50, 1, mvpMatrix, 800, 480);   // 1 for winz means projected on the far plane
Vec3 pickingRay = Subtract(far, near); // Vector subtraction

一旦我们有了捡光,我做的只是测试采摘射线与那些可拾取对象的中心之间的距离。 (当然,你可以有一些更复杂的测试算法)。

Once we have the picking ray, what I am doing is simply testing the distance between the picking ray and the "center" of those "pickable" objects. (of course, you can have some more sophisticated testing algorithm).