我正在尝试旋转位于我的 OpenGL 世界的 (X,Y,Z)=(0,0,0)
处的模型(地形)。 +X
是东,-Z
是北,+Y
是海拔。 View 正在寻找 -Y
,因为设备欧拉角是 (0°,0°,0°)
,而设备位于指向北方的 table 上。
问题:
我需要切换设备的轴以将测量的角度 device -y
更改为 device z
。
当前围绕设备 x 轴
旋转有效(导致围绕 World X 轴
旋转)。
但围绕 device y
的旋转会导致围绕 World Y
而不是 World -Z
的旋转以及围绕 device z< 的旋转
围绕 World Z
而不是 World Y
旋转。
到现在为止,我已经没有办法解决这个问题了。请问有人可以给我提示吗?
(OpenGL) (Device)
^ +Y-axis ^ +z-axis
* *
* *
* * ^ +y-axis (North)
* * *
* * *
* * *
************> + X-axis ************> +x-axis
*
*
v +Z-axis (Minus North)
到目前为止我尝试了什么:
- 使用
SensorManager.getOrientation
中的欧拉角 并切换角度效果很好,但我在俯仰角接近 90 度时进入万向节锁定状态。所以我正在寻找另一种解决方案(SensorManager
旋转矩阵或四元数)。 SensorManager.remapCoordinateSystem
在几乎所有可能的星座中 -> 没有帮助- 从
SensorManager.getRotationMatrix
更改我的旋转矩阵的列/行 -> 没有帮助
最佳答案
好吧,我找到了一个使用四元数的非常简单的解决方案。 轴在最后被切换。
另见:
- 基于 this解决方案
- SensorEvent.values (--> Sensor.TYPE_ROTATION_VECTOR)
- SensorManager.getQuaternionFromVector
现在我仍然想知道如何启用 90 度以上的俯仰角(在纵向模式下)。有什么想法吗?
Activity :
private SensorManager sensorManager;
private Sensor rotationSensor;
public void onCreate(Bundle savedInstanceState) {
...
rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
...
}
// Register/unregister SensorManager Listener at onResume()/onPause() !
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
// Set rotation vector
MyRenderer.setOrientationVector(event.values);
}
}
我的渲染器:
float[] orientationVector = new float[3];
public void setOrientationVector (float[] vector) {
// Rotation vector to quaternion
float[] quat = new float[4];
SensorManager.getQuaternionFromVector(quat, vector);
// Switch quaternion from [w,x,y,z] to [x,y,z,w]
float[] switchedQuat = new float[] {quat[1], quat[2], quat[3], quat[0]};
// Quaternion to rotation matrix
float[] rotationMatrix = new float[16];
SensorManager.getRotationMatrixFromVector(rotationMatrix, switchedQuat);
// Rotation matrix to orientation vector
SensorManager.getOrientation(rotationMatrix, orientationVector);
}
public void onDrawFrame(GL10 unused) {
...
// Rotate model matrix (note the axes beeing switched!)
Matrix.rotateM(modelMatrix, 0,
(float) (orientationVector[1] * 180/Math.PI), 1, 0, 0);
Matrix.rotateM(modelMatrix, 0,
(float) (orientationVector[0] * 180/Math.PI), 0, 1, 0);
Matrix.rotateM(modelMatrix, 0,
(float) (orientationVector[2] * 180/Math.PI), 0, 0, 1);
...
}
关于android - 在 OpenGL ES2 模型旋转的旋转矩阵中切换设备轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19295018/