Android openGL ES2 rotate flip 旋转角度

Android openGL ES2 rotateMM flip rtation angle

我有以下相机绘制功能,我可以在其中旋转、缩放和平移视图矩阵,但我在某些位置遇到了一个问题,例如角度发生变化 if (event.getAction() == MotionEvent.ACTION_DOWN) 偏航角为 120,俯仰角为 -40在 if (event.getAction() == MotionEvent.ACTION_UP) 上,偏航变为 165,俯仰变为 -85,没有任何旋转。

    Vector3 nv = Vector3.multiplyScalar(Vector3.normalize(Vector3.cross(cameraUp, cameraRight)), zoom);
    cameraPosition = Vector3.add(cameraPosition, nv);

    Vector3 vY = Vector3.multiplyScalar(cameraUp, -mDeltaY);
    cameraPosition = Vector3.add(cameraPosition, vY);
    Vector3 vZ = Vector3.multiplyScalar(Vector3.normalize(Vector3.cross(cameraDirection, cameraRight)), mDeltaY);
    cameraPosition = Vector3.sub(cameraPosition, vZ);
    Vector3 vX = Vector3.multiplyScalar(Vector3.normalize(Vector3.cross(cameraDirection, cameraUp)), -mDeltaX);
    cameraPosition = Vector3.sub(cameraPosition, vX);
    setLookAtM(viewMatrix, 0, cameraPosition.getX(), cameraPosition.getY(), cameraPosition.getZ(), cameraPosition.getX() + cameraDirection.getX(), cameraPosition.getY() + cameraDirection.getY(),
            cameraPosition.getZ() + cameraDirection.getZ(), 0, 1, 0);
    rotateM(viewMatrix, 0, pitch, 1, 0, 0);
    rotateM(viewMatrix, 0, yaw, 0, 1, 0);

    mDeltaX = 0;
    mDeltaY = 0;
    zoom = 0;

相机设置代码为:

public void initCamera(Vector3 position, Vector3 target) {
    yaw = 0f;
    pitch = 0;
    Vector3 upV = new Vector3(0, 0, 1);
    this.cameraPosition = position;
    this.cameraTarget = target;
    this.cameraDirection = Vector3.normalize(Vector3.sub(target, position));
    this.cameraRight = Vector3.normalize(Vector3.cross(upV, cameraDirection));
    this.cameraUp = Vector3.cross(cameraDirection, cameraRight);
}

这是 onTouchEvent 代码

if (event.getPointerCount() == 1) {
    float yaw = (x - mPreviousX) / mDensity / 2f;
    float pitch = (y - mPreviousY) / mDensity / 2f;
                    mainRenderer.getCamera().yaw += yaw;
                    mainRenderer.getCamera().pitch += pitch;
}
if (event.getPointerCount() == 2) {
    mCurrDis = getDistance(event);
    if (mLastDis == 0)
        mLastDis = mCurrDis;
    mainRenderer.getCamera().zoom(mLastDis - mCurrDis);
    mLastDis = getDistance(event);
}
if (event.getPointerCount() == 3) {
    float deltaX = (x - mPreviousX) / mDensity / 2f;
    float deltaY = (y - mPreviousY) / mDensity / 2f;
    mainRenderer.getCamera().mDeltaX += deltaX;
    mainRenderer.getCamera().mDeltaY += deltaY;
}

试试这个代码:

void Camera::rotate(GLfloat xoffset, GLfloat yoffset, glm::vec3& c, GLboolean constrainpitch) {
    xoffset *= this->touchSensitivity;
    yoffset *= this->touchSensitivity;
    pitch += yoffset;
    yaw += xoffset;
    if (constrainpitch) {
    if (pitch >= maxPitch) {
        pitch = maxPitch;
        yoffset = 0;
    }
    if (pitch <= minPitch) {
        pitch = minPitch;
        yoffset = 0;
    }
    }
    glm::quat Qx(glm::angleAxis(glm::radians(yoffset), glm::vec3(1.0f, 0.0f, 0.0f)));
    glm::quat Qy(glm::angleAxis(glm::radians(xoffset), glm::vec3(0.0f, 1.0f, 0.0f)));
    glm::mat4 rotX = glm::mat4_cast(Qx);
    glm::mat4 rotY = glm::mat4_cast(Qy);
    view = glm::translate(view, c);
    view = rotX * view;
    view = view * rotY;
    view = glm::translate(view, -c);
}