我目前正在尝试在 Android 中可视化 OpenGL ES 2 中的建筑物。当我向左滑动时,整个场景应该向左移动(与右、下和上相同)。现在,当我将模型旋转 90° 时,轴被交换。当我向右滑动时,模型将移动到顶部而不是右侧(与另一个轴相同)。
如何在不影响当前场景旋转的情况下平移相机?当我使用手机时,我总是想将对象移动到我要滑动的方向。当我先调用 translate 时,翻译工作正常,但旋转有一些错误。
更新:我创建了大部分相机类(建议在答案中):
private Vector3D c = new Vector3D(eye); // copy of eye
private Vector3D l = Vector3D.getNormalized
(Vector3D.subtract(center, eye)); // normalized center-eye
private Vector3D u = new Vector3D(up); // up (normalized)
public void rotateX(float angle) {
// Now you can rotate L vector left and
// right by rotating it around U by any angle. ??
}
public void rotateY(float angle) {
// If you also need to look up and down
// (rotate) you need to rotate both L and U around S ??
}
public void rotateZ(float angle) {
// to tilt left and right you need to rotate U around L. ??
}
// move left and right
// C = C+S * inputfactor
public void translateX(float inputFactor) {
Vector3D s = Vector3D.cross(l, u);
Vector3D sTimesInput = mul(s, inputFactor);
c = new Vector3D(c).add(sTimesInput);
}
// move forward and backward
// C = C+L*inputfactor
public void translateY(float inputFactor) {
Vector3D lTimesInput = mul(l, inputFactor);
c = new Vector3D(c).add(lTimesInput);
}
// move up and down
// C = C+U * inputfactor
public void translateZ(float inputFactor) {
Vector3D uTimesInput = mul(u, inputFactor);
c = new Vector3D(c).add(uTimesInput);
}
// reconstruct lookAt - eye,center,up
public Vector3D getEye() {
return new Vector3D(c);
}
public Vector3D getCenter() {
return new Vector3D(c).add(new Vector3D(l));
}
public Vector3D getUp() {
return new Vector3D(u);
}
public float[] createLookAt() {
Vector3D eye = getEye();
Vector3D center = getCenter();
Vector3D up = getUp();
return new float[] { eye.getX(), eye.getY(), eye.getZ(), center.getX(),
center.getY(), center.getZ(), up.getX(), up.getY(), up.getZ() };
}
// helper-function
public Vector3D mul(Vector3D vec, float factor) {
return new Vector3D(vec.getX() * factor, vec.getY() * factor,
vec.getZ() * factor);
}
如何将一个向量围绕另一个向量旋转一个角度?