代码之家  ›  专栏  ›  技术社区  ›  Mohamed

Android openGL ES2 rotateMM翻转角度

  •  1
  • Mohamed  · 技术社区  · 8 年前

    我有以下相机绘图功能,可以旋转、缩放和平移视图矩阵,但我在某些位置有一个问题,例如角度改变 if (event.getAction() == MotionEvent.ACTION_DOWN) 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;
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   user8581488 user8581488    7 年前

    尝试以下代码:

    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);
    }