我是OpenGL新手。我在glSurfaceView上渲染了一些3d形状。现在,我想查找用户触摸屏幕时用户单击了哪个对象。我有触摸坐标。如何检查穿过屏幕坐标的垂直射线是否与我渲染的形状之一相交以及如何找到那一个?
((附加:我正在Maxst ar SDK中,即时跟踪器中进行此操作)
我具有渲染对象的下面的数据(Maxst SDK保持下面的数据),并且我具有触摸坐标。
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];
作为变体。不是直接的答案。您可以尝试将对象的顶点坐标转换为屏幕的像素:
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]
然后将关键点的位置与二维空间中的触摸坐标进行比较。
或获取对象的坐标中心:
GLU.gluProject(0.0f, 0.0f, 0.0f, modelViewMatrix, 0, projectionMatrix, 0, view, 0,
windowCoordinates, 0)
并使比较接触相对对象的中心。在这种情况下,需要考虑物体的距离。