0

I'm new to OpenGL. I have rendered a few 3d shapes on a glSurfaceView in Android. Now I want to find which object has clicked by the user when the user touches the screen. I have touch coordinate. How can I check whether the perpendicular ray, that goes through the screen coordinates is intersecting one of the shapes I rendered and how to find which one is that?

(additional: I'm doing this inside the Maxst ar SDK, instant tracker)

I have below data(Maxst SDK keep below data) of rendered objects and I have touch coordinates.

float[] localMvpMatrix = new float[16];
float [] projectionMatrix = new float[16];
float[] modelMatrix = new float[16];
float[] translation = new float[16];
float[] scale = new float[16];
float[] rotation = new float[16];
float[] transform = new float[16];

Edit: How to use GLU.gluUnProject() to achieve this in android? Can anyone please explain what are those parameters and how to use the output of this method (in Android)?

Venz
  • 33
  • 5
  • 1
    Does this answer your question? [Implementing Ray Picking](https://stackoverflow.com/questions/2093096/implementing-ray-picking) – Morrison Chang Apr 21 '20 at 21:41
  • 1
    Also see: [How do you determine which object/surface the user's pointing at with lwjgl?](https://gamedev.stackexchange.com/q/12360/6883) – Morrison Chang Apr 21 '20 at 21:42

1 Answers1

0

As an variant. Not a direct answer. You can try to convert the vertices coordinates of an object to the pixels of the screen:

import android.opengl.GLU
// widthScreen and heightScreen values will be different for port and land orientation
private var view: IntArray = intArrayOf(0, 0, widthScreen, heightScreen)
private var windowCoordinates = FloatArray(3)
...
// modelX, modelY, modelZ - model coordinates of vertex
GLU.gluProject(modelX, modelY, modelZ, modelViewMatrix, 0, 
     projectionMatrix, 0, view, 0, windowCoordinates, 0)

// coordinates (pixels) of the screen
val x = coordinatesWindow[0]
val y = coordinatesWindow[1]

And compare the position of the key vertices with the touch coordinates in two-dimensional space.

Or to get the coordinates center of the object:

GLU.gluProject(0.0f, 0.0f, 0.0f, modelViewMatrix, 0, projectionMatrix, 0, view, 0,
          windowCoordinates, 0)

And make comparison touch relatively the center of object. In this case need account the distance of the object.

alexrnov
  • 2,346
  • 3
  • 18
  • 34