简体   繁体   English

OpenTK:用鼠标旋转对象,有时旋转是相反的

[英]OpenTK: rotate object with mouse, sometimes the rotation is reversed

I try to rotate object with mouse on a virtual sphere like MeshLab.我尝试在像 MeshLab 这样的虚拟球体上用鼠标旋转对象。

private void RotateCamera(Vector3 newDragPoint)
        {
            var rotAxis = Vector3.Normalize(Vector3.Cross(oldDragPoint, newDragPoint));
            var angle = Vector3.Dot(Vector3.Normalize(oldDragPoint), Vector3.Normalize(newDragPoint));
            double angleVal = Math.Acos(angle);
            Matrix4 rotateMatrix = Matrix4.CreateFromAxisAngle(rotAxis, (float)(angleVal));
            var modelViewMatrixTemp = rotateMatrix * modelViewMatrix;
        }

At the beginning it works good, the model view matrix is:一开始效果很好,模型视图矩阵是:

Row0: {(0.99888134, -0.044642903, -0.0154874, 0)}
    Row1: {(0.04587658, 0.9947593, 0.0913476, 0)}
    Row2: {(0.011327561, -0.0919559, 0.9956961, 0)}
    Row3: {(0, 0, -1, 1)}

but after a while if the rotation on xy plane is big enough, the object start to rotate against the movement of mouse,但过了一会儿,如果在 xy 平面上的旋转足够大,对象开始旋转反对鼠标的移动,

Row0: {(-0.9778508, -0.19920659, -0.06419495, 0)}
    Row1: {(0.18042804, -0.95778984, 0.22378196, 0)}
    Row2: {(-0.10606451, 0.20724255, 0.97251993, 0)}
    Row3: {(0, 0, -1, 1)} 

how to determine the angle under this kind of situation?在这种情况下如何确定角度?

update ______更新______

now I figured out the rotation is applied to the object with initial pose, not the rotated object, but still have no idea of how to fix it.现在我发现旋转应用于具有初始姿势的对象,而不是旋转的对象,但仍然不知道如何修复它。

split the new rotation with old rotation will solve this problem.用旧旋转拆分新旋转将解决这个问题。 keep the current rotation as oldRotation, then replace the line var modelViewMatrixTemp = rotateMatrix * modelViewMatrix;保持当前旋转为 oldRotation,然后替换行var modelViewMatrixTemp = rotateMatrix * modelViewMatrix; with

var modelViewMatrixTemp = oldRotation * rotateMatrix * modelViewMatrix;
oldRotation = oldRotation * rotateMatrix;

that will apply rotation on rotated object.这将对旋转的对象应用旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM