Rotación Opengl de Android con datos de sensores
trato de crear una aplicación Realidad Aumentada con vista opengl en la parte superior de la vista previa de la cámara. Tengo vista previa de cámara y vista opengl, recojo datos de sensores del gestor de sensores y luego crear matriz de rotación para aplicarla a mi vista opengl. Hasta aquí todo bien. Estoy usando sólo la orientación del teléfono del paisaje. Pero aquí está mi problema:
Estoy utilizando la matriz de rotación de los sensores, lo aplican a opengl y luego quiero dibujar mis modelos que están en movimiento constante (0,0,0) punto. Pero cuando hago girar mi teléfono, por supuesto puedo seguir mis movimientos de objetos, pero cuando cambio de dirección (supongo que girar mi teléfono en la dirección correcta y luego cambiarlo a la izquierda opuesta – En este momento mi objeto debe moverse en su camino, pero él permanece en el lugar y se mueve poco hacia atrás.
- ¿Cómo reemplazar adecuadamente Sensor.TYPE_ORIENTATION (que ahora está obsoleto)?
- Sensores para teléfonos móviles
- Cómo usar los datos del sensor onSensorChanged en combinación con OpenGL
- Valor de Android: Sensor.getResolution ()
- Android: Problemas al calcular la orientación del dispositivo
Mi código y mi pantalla de teléfono:
private float[] rotationMatrix = new float[16]; onDrawFrame: moveObjects(smoothedDeltaRealTime_ms); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glMultMatrixf(rotationMatrix, 0); gl.glRotatef(90.0f, 1f, 0f, 0f); drawObjects(gl); @Override public void onSurfaceChanged(GL10 gl, int width, int height) { ArOpenGlLogger.info("onSurfaceChanged " + width + "x" + height); this.screenWidth = width; this.screenHeight = height; gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set color's clear-value gl.glClearDepthf(1.0f); // Set depth's clear-value to farthest gl.glEnable(GL10.GL_DEPTH_TEST); // Enables depth-buffer for hidden surface removal gl.glDepthFunc(GL10.GL_LEQUAL); // The type of depth testing to do gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); gl.glShadeModel(GL10.GL_SMOOTH); // Enable smooth shading of color gl.glDisable(GL10.GL_DITHER); // Disable dithering for better performance gl.glEnable(GL10.GL_TEXTURE_2D); // Enable texture } protected void draw(GL10 gl) { //loading textures if(!isTextureLoaded) loadTexture(gl); gl.glTranslatef(translation.getX(), translation.getY(), translation.getZ()); gl.glScalef(scale.getX(), scale.getY(), scale.getZ()); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); }
Sensores:
rootMeanSquareBuffer(bufferedAccelerometrData, accelerometrData); rootMeanSquareBuffer(bufferedMagneticFieldData, magneticFieldData); SensorManager.getRotationMatrix(rotationMatrix, null, bufferedAccelerometrData, bufferedMagneticFieldData); if(deviceOrientation == ORIENTATION_LANDSCAPE) { float[] result = new float[16]; SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, result); sensorCallback.onNewRotationMatrix(result); } else sensorCallback.onNewRotationMatrix(rotationMatrix); private void rootMeanSquareBuffer(float[] target, float[] values) { final float amplification = 200.0f; float buffer = 20.0f; target[0] += amplification; target[1] += amplification; target[2] += amplification; values[0] += amplification; values[1] += amplification; values[2] += amplification; target[0] = (float) (Math.sqrt((target[0] * target[0] * buffer + values[0] * values[0]) / (1 + buffer))); target[1] = (float) (Math.sqrt((target[1] * target[1] * buffer + values[1] * values[1]) / (1 + buffer))); target[2] = (float) (Math.sqrt((target[2] * target[2] * buffer + values[2] * values[2]) / (1 + buffer))); target[0] -= amplification; target[1] -= amplification; target[2] -= amplification; values[0] -= amplification; values[1] -= amplification; values[2] -= amplification; }
Pantalla del dispositivo:
- ¿Cómo rastrear la posición de un dispositivo Android en un edificio?
- ¿Puede guardar / ver un archivo de vídeo y los datos del sensor simultáneamente?
- Android PhoneGap obtener guiñada, tono, rollo, relativo a las coordenadas de tierra
- Activar y desactivar la pantalla mediante programación en android
- ¿Cómo puedo bloquear el diseño de mi programa Android a una orientación
- Android - SensorManager comportamiento extraño de getOrientation
- Android: ¿Cómo se trata con baromters inexactos?
- Necesidad de calcular el vector de rotación a partir de los datos de Sensor.TYPE_ORIENTATION
- ¿cómo puedo añadir zoom en el zoom fuera programáticamente?
- Android Pad Top / Bottom Padding no funciona en Android 4?