Estoy en Android OpenGL-ES 2.0 y después de todas las limitaciones que vienen con él, no puedo imaginar cómo tomar toques de pantalla en 2D a los puntos 3D que tengo. No puedo obtener los resultados correctos.Android OpenGL selección 3D
Estoy tratando de implementar el tiro de un rayo en la nube de puntos, que luego puedo comparar las distancias de mis puntos al rayo, encontrando el punto más cercano.
public class OpenGLRenderer extends Activity implements GLSurfaceView.Renderer {
public PointCloud ptCloud;
MatrixGrabber mg = new MatrixGrabber();
...
public void onDrawFrame(GL10 gl) {
gl.glDisable(GL10.GL_COLOR_MATERIAL);
gl.glDisable(GL10.GL_BLEND);
gl.glDisable(GL10.GL_LIGHTING);
//Background drawing
if(customBackground)
gl.glClearColor(backgroundRed, backgroundGreen, backgroundBlue, 1.0f);
else
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
if (PointCloud.doneParsing == true) {
if (envDone == false)
setupEnvironment();
// Clears the screen and depth buffer.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 55.0f, (float) screenWidth/(float) screenHeight, 10.0f ,10000.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
upX, upY, upZ);
if(pickPointTrigger)
pickPoint(gl);
gl.glPushMatrix();
gl.glTranslatef(_xTranslate, _yTranslate, _zTranslate);
gl.glTranslatef(centerX, centerY, centerZ);
gl.glRotatef(_xAngle, 1f, 0f, 0f);
gl.glRotatef(_yAngle, 0f, 1f, 0f);
gl.glRotatef(_zAngle, 0f, 0f, 1f);
gl.glTranslatef(-centerX, -centerY, -centerZ);
ptCloud.draw(gl);
gl.glPopMatrix();
}
}
}
Aquí está mi función de selección. He establecido la ubicación para el centro de la pantalla sólo para propósitos de depuración:
public void pickPoint(GL10 gl){
mg.getCurrentState(gl);
double mvmatrix[] = new double[16];
double projmatrix[] = new double[16];
int viewport[] = {0,0,screenWidth, screenHeight};
for(int i=0 ; i<16; i++){
mvmatrix[i] = mg.mModelView[i];
projmatrix[i] = mg.mProjection[i];
}
mg.getCurrentState(gl);
float realY = ((float) (screenHeight) - pickY);
float nearCoords[] = { 0.0f, 0.0f, 0.0f, 0.0f };
float farCoords[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLU.gluUnProject(screenWidth/2, screenHeight/2, 0.0f, mg.mModelView, 0, mg.mProjection, 0,
viewport, 0, nearCoords, 0);
GLU.gluUnProject(screenWidth/2, screenHeight/2, 1.0f, mg.mModelView, 0, mg.mProjection, 0,
viewport, 0, farCoords, 0);
System.out.println("Near: " + nearCoords[0] + "," + nearCoords[1] + "," + nearCoords[2]);
System.out.println("Far: " + farCoords[0] + "," + farCoords[1] + "," + farCoords[2]);
//Plot the points in the scene
nearMarker.set(nearCoords);
farMarker.set(farCoords);
markerOn = true;
double diffX = nearCoords[0] - farCoords[0];
double diffY = nearCoords[1] - farCoords[1];
double diffZ = nearCoords[2] - farCoords[2];
double rayLength = Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2) + Math.pow(diffZ, 2));
System.out.println("rayLength: " + rayLength);
pickPointTrigger = false;
}
Cambio de la persepctive zNear y Lejano no tiene los resultados esperados, ¿cómo podría el punto de una perspectiva 1,0-1000,0 nunca tal 11 unidades de distancia?
GLU.gluPerspective(gl, 55.0f, (float) screenWidth/(float) screenHeight, 1.0f ,100.0f);
.....
07-18 11:23:50.430: INFO/System.out(31795): Near: 57.574852,-88.60514,37.272636
07-18 11:23:50.430: INFO/System.out(31795): Far: 0.57574844,0.098602295,0.2700405
07-18 11:23:50.430: INFO/System.out(31795): rayLength: 111.74275719790872
GLU.gluPerspective(gl, 55.0f, (float) width/(float) height, 10.0f , 1000.0f);
...
07-18 11:25:12.420: INFO/System.out(31847): Near: 5.7575016,-7.965394,3.6339219
07-18 11:25:12.420: INFO/System.out(31847): Far: 0.057574987,0.90500546,-0.06634784
07-18 11:25:12.420: INFO/System.out(31847): rayLength: 11.174307289026638
Buscando cualquier sugerencia o bugs con suerte que vea en mi código. Muy apreciado. Estoy haciendo un Bountying tanto como puedo (esto ha sido un problema por un tiempo).
He progresado un poco. Puedo tomar el resultado ** nearCoords ** e iterar desde allí en la ** dirección ** de ** farCoords ** (simplemente voy por la magnitud de Z_FAR), y el rayo es algo cercano a lo que realmente selecciono. Vea cómo esto funciona para usted. – RedLeader
No he intentado iterar en la dirección de los nearCoords, sin embargo, en parte porque me pareció que ni los nearCoords ni los farCoords tenían mucha relación con el lugar donde hice clic en primer lugar. Me he dado cuenta de que gluUnProject no parece funcionar (de una manera nueva) - Corrí algunas coordenadas a través de gluProject, luego tomé el resultado y lo ejecuté a través de gluUnProject inmediatamente después, y obtuve algo totalmente diferente. Actualmente estoy tratando de hacer una transformación inversa. Noté que usas lookAt para elegir, pero una serie de transformaciones para dibujar. ¿Porqué es eso? – Erhannis
Era solo mi diseño, y funciona de alguna manera, no es exactamente el punto (está cerca si tomo a la izquierda, no tan cerca a la derecha), pero es decentemente preciso ... – RedLeader