android - Android OpenGL ES 上的光线对象拾取

标签 android opengl-es rajawali

我在 Android 平台上使用 OpenGL 绘制一个地球仪,并试图在球体上找到用户点击的点。我是 OpenGL 的新手,我正在使用开源库 Rajawali如示例中所示 Touch & Drag但如果有人知道更好的方法,请建议我,因为 Rajawali 的代码完全没有评论,而且大部分文档都已过时:(

double[] np4 = new double[4]; //near plane
double[] fp4 = new double[4]; // far plane

GLU.gluUnProject(
        x, mViewportHeight - y, 0.0f, 
        mViewMatrix.getDoubleValues(), 0, 
        mProjectionMatrix.getDoubleValues(), 0, 
        mViewport, 0, 
        np4, 0
);

GLU.gluUnProject(
        x, mViewportHeight - y, 1.0f, 
        mViewMatrix.getDoubleValues(), 0, 
        mProjectionMatrix.getDoubleValues(), 0, 
        mViewport, 0, 
        fp4, 0
);

// transform 4D coordinates (x, y, z, w) to 3D (x, y, z) by dividing each coordinate (x, y, z) by w.
return new Ray(
        new Vector3(np4[0] / np4[3], np4[1] / np4[3], np4[2] / np4[3]),
        new Vector3(fp4[0] / fp4[3], fp4[1] / fp4[3], fp4[2] / fp4[3])
    );

然后按所述找到与球体的交点(半径 = 1,中心 (0,0,0))here

    /**
     * Calculate point of intersection of line and sphere.
     * 
     * @param p1 Point the line goes throw
     * @param p2 Point the line goes throw
     * @param center Center of the sphere
     * @param r Radius of the sphere
     * @return <code>Vector3</code> Coordinates of intersection point
     */
    private Vector3 getIntersectionPoint(Vector3 p1, Vector3 p2, Vector3 center, double r){
        if (p1 == null || p2 == null || center == null)
            return null;

        double a = in2(p2.x - p1.x) + in2(p2.y - p1.y) + in2(p2.z - p1.z);
        double b = 2*( (p2.x - p1.x)*(p1.x - center.x) + (p2.y - p1.y)*(p1.y - center.y) + (p2.z - p1.z)*(p1.z - center.z) );
        double c = in2(center.x) + in2(center.y) + in2(center.z) + in2(p1.x) + in2(p1.y) + in2(p1.z) - 2*(center.x*p1.x + center.y*p1.y + center.z*p1.z) - in2(r);

        //simplified equations for Sphere coordinates are (0, 0, 0) r=1 and cam.x = cam.y = 0
//      double a = in2(p2.x) + in2(p2.y) + in2(p2.z - p1.z);
//      double b = 2*( (p2.z - p1.z)*p1.z );
//      double c = in2(p1.z) - 1;

        for (Vector3 poi : solveQuadraticEquation(a, b, c, p1, p2)){
            if (poi.z > 0)
                return poi;
        }

        return null;
    }


    /**
     * Solve quadratic equation.
     * 
     * @param a Parameter A in equation Ax^2 + Bx + C = 0
     * @param b Parameter B in equation Ax^2 + Bx + C = 0
     * @param c Parameter C in equation Ax^2 + Bx + C = 0
     * @param p1 <code>Vector3</code> Start point for the line
     * @param p2 <code>Vector3</code> End point for the line
     * 
     * @return <code>List<Vector3></code> of results. Empty if no results found.
     */
    private List<Vector3> solveQuadraticEquation(double a, double b, double c, Vector3 p1, Vector3 p2){
        double D = in2(b) - 4*a*c;

        List<Vector3> rez = new ArrayList<Vector3>();

        if (D < 0) {

            return rez;

        } else  if (D == 0) {

            double t = (-b) / (2*a);
            rez.add(new Vector3(p1.x + (p2.x - p1.x)*t, p1.y + (p2.y - p1.y)*t, p1.z + (p2.z - p1.z)*t));

            return rez;

        } else {


            double t = (Math.sqrt(D)-b) / (2*a);
            rez.add(new Vector3(p1.x + (p2.x - p1.x)*t, p1.y + (p2.y - p1.y)*t, p1.z + (p2.z - p1.z)*t));

            t = (-Math.sqrt(D)-b) / (2*a);
            rez.add(new Vector3(p1.x + (p2.x - p1.x)*t, p1.y + (p2.y - p1.y)*t, p1.z + (p2.z - p1.z)*t));

            return rez;

        }
    }

结果如下:

enter image description here

这里: 蓝点是 (-1, -1, -1), (1, -1, -1), (1, 1, -1) 和 (-1, 1, -1) 绿点是 (-1, -1, 1), (1, -1, 1), (1, 1, 1) 和 (-1, 1, 1) 黄色点是 (1, 0, 0), (0, 1, 0), (-1, 0, 0) 和 (0, -1, 0)

小红点是我点击顶部黄色圆圈时的交点: x: 405.10202 y: 430.5059 (event.getX(), evet.getY() in touch listener) 这是一个未预测的点: 附近:Vector3:<0.0038529188490706075,0.08910624125329661,4.999999999999999> 远:Vector3:<0.46235026188847467,10.692748950395632,-114.00000000000048> 交叉点:Vector3:<0.019687997272589165,0.45532322467387265,0.8901085011592542>

相机位置是(0,0,-6)

谁能帮我把这个小红点放在手指下,好吗?

附言我需要一个交点,而不仅仅是一个对象拾取,因为稍后我将在此时获取国家/地区代码...

最佳答案

问题是 ViewPort 对应于 View 尺寸(在本例中为 GLSurfaceView),但点具有绝对坐标,因此应将其转换为相对坐标,例如:

final float x = e.getX() - getLeft();
final float y = e.getY() - getTop();

关于android - Android OpenGL ES 上的光线对象拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21528359/

相关文章:

android - 在 OpenGL ES 2.0/Rajawali 中,如何避免用户旋转球体时翻转球体?

android - 使用 min3d/Rajawali 框架(android)在触摸时围绕 3d 对象旋转

android - 带有 rajawali + VR 的 360 度全景查看器如何更改视野

android - 如何在android中的列表适配器中给出布局边距?

java - 将自定义 View 添加到 xml 布局

java - 一个非常简单的塞子

android - 无法在 Unity 中获取 EGL14.eglGetCurrentContext()

android - 编译具有多种风格和播放服务的错误构建项目

android - 检测用户在 OpenGL 正方形上的触摸

iOS OpenGL ES - 模拟器和设备上的不同纹理行为