如何检测我是否点击了GL10对象

时间:2016-03-11 21:35:13

标签: android opengl-es augmented-reality andar

我正在绘制一个使用AndAR在屏幕上显示的简单三角形。现在我想做一个简单的互动。我的意思是我想在点击绘制的对象时显示Toast。我已经实现了onTouchEvent,因此我可以获得点击屏幕的地方的XY坐标。现在我需要检查这个点是否在我的2D三角形区域。如何获得三角形点的坐标?三角是#34;坚持"到标记时,在识别标记时绘制标记,因此基于视角实时更改坐标。这是最大的问题。有什么想法吗?

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) {

    }
}

0 个答案:

没有答案