1

我正在使用 AndAR 绘制一个显示在屏幕上的简单三角形。现在我想做一个简单的交互。我的意思是我想Toast在点击绘制的对象时显示 a 。我已经实现onTouchEvent了,所以我可以获得点击屏幕位置的 XY 坐标。现在我需要检查这个点是否在我的二维三角形区域内。如何获得三角形点的坐标?三角形“粘”在标记上,在识别标记时绘制,因此根据视角实时更改坐标。这是最大的问题。任何想法 ?

public class Triangle extends ARObject implements BasicShape {

    private final FloatBuffer vertexBuffer;

    // number of coordinates per vertex in this array
    static final int COORDS_PER_VERTEX = 3;
    static float triangleCoords[] = {
            // in counterclockwise order:
            25.0f,  25.622008459f, 25.0f,// top
            -25.5f, -25.311004243f, 25.0f,// bottom left
            25.5f, -25.311004243f, 25.0f // bottom right
    };

    float color[] = { 1f, 0f, 0f, 0.0f };

    /**
     * Sets up the drawing object data for use in an OpenGL ES context.
     */
    public Triangle(String name, String patternName, double markerWidth, double[] markerCenter) {
        super(name, patternName, markerWidth, markerCenter);
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (number of coordinate values * 4 bytes per float)
                triangleCoords.length * 4);
        // use the device hardware's native byte order
        bb.order(ByteOrder.nativeOrder());

        // create a floating point buffer from the ByteBuffer
        vertexBuffer = bb.asFloatBuffer();
        // add the coordinates to the FloatBuffer
        vertexBuffer.put(triangleCoords);
        // set the buffer to read the first coordinate
        vertexBuffer.position(0);
    }

    /**
     * Encapsulates the OpenGL ES instructions for drawing this shape.
     *
     * @param gl - The OpenGL ES context in which to draw this shape.
     */
    @Override
    public void draw(GL10 gl) {
        super.draw(gl);
        // Since this shape uses vertex arrays, enable them
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        // draw the shape
        gl.glColor4f(       // set color:
                color[0], color[1],
                color[2], color[3]);
        gl.glVertexPointer( // point to vertex data:
                COORDS_PER_VERTEX,
                GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glDrawArrays(    // draw shape:
                GL10.GL_TRIANGLES, 0,
                triangleCoords.length / COORDS_PER_VERTEX);
        gl.glTranslatef(0.0f, 0.0f, 12.5f);

        // Disable vertex array drawing to avoid
        // conflicts with shapes that don't use it
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

    @Override
    public void init(GL10 gl10) {

    }
}
4

0 回答 0