Fixus
Fixus

Reputation: 4641

How to detect if I tapped on GL10 object

I'm drawing a simple Triangle that is displayed on screen using AndAR. Now I want to make a simple interaction. By that I mean that I want to display a Toast when the drawn object is tapped. I've already implemented onTouchEvent so I can get XY coordinates of the place where I tapped the screen. Now I need to check if this point is in area of my 2D triangle. How can I get coordinates of points of my triangle ? Triangle is "sticked" to the marker and it is drawn when marker is recognied, so the coordinates are changed in real time based on view angle. This is the biggest problem. Any idea ?

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

    }
}

Upvotes: 1

Views: 56

Answers (0)

Related Questions