Opengl:保持Arcball相机向上与y轴对齐

时间:2013-05-14 06:22:56

标签: c++ opengl qt5 arcball

我基本上试图模仿相机在Maya中旋转的方式。 Maya中的弧形球始终与y轴对齐。因此,无论向上指向的位置是什么,它都会沿着y轴旋转或向上注册向上矢量。

我已经能够使用C ++和Qt在OpenGL中实现arcball。但我无法弄清楚如何保持它向上对齐。我可以通过以下代码随时保持对齐:

void ArcCamera::setPos (Vector3 np)
{
    Vector3 up(0, 1, 0);

    Position = np;
    ViewDir = (ViewPoint - Position); ViewDir.normalize();

    RightVector =  ViewDir ^ up; RightVector.normalize();

    UpVector = RightVector ^ ViewDir;  UpVector.normalize();
}

直到位置为90度,然后右矢量发生变化,一切都反转。

因此,我一直保持总旋转(四元数)并旋转原始位置(向上,向右,位置)。这最有效地保持一切连贯,但现在我无法将向上矢量与y轴对齐。以下是轮换的功能。

void CCamera::setRot (QQuaternion q)
{
    tot = tot * q;

    Position  = tot.rotatedVector(PositionOriginal);

    UpVector = tot.rotatedVector(UpVectorOriginal);
    UpVector.normalize();

    RightVector  = tot.rotatedVector(RightVectorOriginal);
    RightVector.normalize();
}

QQuaternion q是从鼠标拖动得到的轴角对生成的。我确信这是正确的。旋转本身很好,它只是不保持方向对齐。

我已经注意到在我选择的实现中,在角落中拖动可以围绕我的视图方向旋转,并且我总是可以重新调整向上矢量以理顺世界的y轴方向。因此,如果我能计算出滚动多少,我可能每次都要进行两次旋转,以确保它完全正确。但是,我不知道该怎么做。

2 个答案:

答案 0 :(得分:3)

这不起作用的原因是因为Maya在视口中的相机操作不使用弧形界面。你想要做的是Maya's tumble command。我发现解释此问题的最佳资源是this document from Professor Orr's Computer Graphics class

左右移动鼠标对应于方位角,并指定围绕世界空间Y轴的旋转。上下移动鼠标对应于仰角,并指定围绕视图空间X轴的旋转。我们的目标是生成新的世界到视图矩阵,然后根据您对相机进行参数化,从该矩阵中提取新的相机方向和眼睛位置。

从当前的世界到视图矩阵开始。接下来,我们需要在世界空间中定义枢轴点。任何支点都可以开始使用,并且最简单的方法是使用世界原点。

回想一下,纯旋转矩阵会产生以原点为中心的旋转。这意味着要围绕任意枢轴点旋转,首先要转换为原点,执行旋转并转换回来。还要记住转换组合是从右到左发生的,所以到达原点的负面翻译出现在最右边:

translate(pivotPosition) * rotate(angleX, angleY, angleZ) * translate(-pivotPosition)

我们可以用它来计算方位角旋转分量,它是绕世界Y轴的旋转:

azimuthRotation = translate(pivotPosition) * rotateY(angleY) * translate(-pivotPosition)

我们必须为高程旋转组件做一些额外的工作,因为它发生在视图空间中,围绕视图空间X轴:

elevationRotation = translate(worldToViewMatrix * pivotPosition) * rotateX(angleX) * translate(worldToViewMatrix * -pivotPosition)

然后我们可以使用以下内容获取新的视图矩阵:

newWorldToViewMatrix = elevationRotation * worldToViewMatrix * azimuthRotation

现在我们有了新的worldToView矩阵,我们不得不从视图矩阵中提取新的世界空间位置和方向。为此,我们需要viewToWorld矩阵,它是worldToView矩阵的反转。

newOrientation = transpose(mat3(newWorldToViewMatrix))
newPosition = -((newOrientation * newWorldToViewMatrix).column(3))

此时,我们将元素分开。如果您的相机已参数化,以便您只为方向存储四元数,则只需要执行旋转矩阵 - >四元数转换。当然,Maya将转换为Euler角度以在通道盒中显示,这将取决于相机的旋转顺序(请注意,当旋转顺序改变时,翻滚的数学不会改变,只是旋转矩阵 - >欧拉角转换完成的方式。)

以下是Python中的示例实现:

#!/usr/bin/env python

import numpy as np
from math import *

def translate(amount):
    'Make a translation matrix, to move by `amount`'
    t = np.matrix(np.eye(4))
    t[3] = amount.T
    t[3, 3] = 1
    return t.T

def rotateX(amount):
    'Make a rotation matrix, that rotates around the X axis by `amount` rads'
    c = cos(amount)
    s = sin(amount)

    return np.matrix([
        [1, 0, 0, 0],
        [0, c,-s, 0],
        [0, s, c, 0],
        [0, 0, 0, 1],
    ])

def rotateY(amount):
    'Make a rotation matrix, that rotates around the Y axis by `amount` rads'
    c = cos(amount)
    s = sin(amount)
    return np.matrix([
        [c, 0, s, 0],
        [0, 1, 0, 0],
       [-s, 0, c, 0],
        [0, 0, 0, 1],
    ])

def rotateZ(amount):
    'Make a rotation matrix, that rotates around the Z axis by `amount` rads'
    c = cos(amount)
    s = sin(amount)
    return np.matrix([
        [c,-s, 0, 0],
        [s, c, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
    ])

def rotate(x, y, z, pivot):
    'Make a XYZ rotation matrix, with `pivot` as the center of the rotation'
    m = rotateX(x) * rotateY(y) * rotateZ(z)

    I = np.matrix(np.eye(4))
    t = (I-m) * pivot
    m[0, 3] = t[0, 0]
    m[1, 3] = t[1, 0]
    m[2, 3] = t[2, 0]

    return m

def eulerAnglesZYX(matrix):
    'Extract the Euler angles from an ZYX rotation matrix'
    x = atan2(-matrix[1, 2], matrix[2, 2])
    cy = sqrt(1 - matrix[0, 2]**2)
    y = atan2(matrix[0, 2], cy)
    sx = sin(x)
    cx = cos(x)
    sz = cx * matrix[1, 0] + sx * matrix[2, 0]
    cz = cx * matrix[1, 1] + sx * matrix[2, 1]
    z = atan2(sz, cz)
    return np.array((x, y, z),)

def eulerAnglesXYZ(matrix):
    'Extract the Euler angles from an XYZ rotation matrix'
    z = atan2(matrix[1, 0], matrix[0, 0])
    cy = sqrt(1 - matrix[2, 0]**2)
    y = atan2(-matrix[2, 0], cy)
    sz = sin(z)
    cz = cos(z)
    sx = sz * matrix[0, 2] - cz * matrix[1, 2]
    cx = cz * matrix[1, 1] - sz * matrix[0, 1]
    x = atan2(sx, cx)
    return np.array((x, y, z),)

class Camera(object):
    def __init__(self, worldPos, rx, ry, rz, coi):
        # Initialize the camera orientation.  In this case the original
        # orientation is built from XYZ Euler angles.  orientation is the top
        # 3x3 XYZ rotation matrix for the view-to-world matrix, and can more
        # easily be thought of as the world space orientation.
        self.orientation = \
            (rotateZ(rz) * rotateY(ry) * rotateX(rx))

        # position is a point in world space for the camera.
        self.position = worldPos

        # Construct the world-to-view matrix, which is the inverse of the
        # view-to-world matrix.
        self.view = self.orientation.T * translate(-self.position)

        # coi is the "center of interest".  It defines a point that is coi
        # units in front of the camera, which is the pivot for the tumble
        # operation.
        self.coi = coi

    def tumble(self, azimuth, elevation):
        '''Tumble the camera around the center of interest.

        Azimuth is the number of radians to rotate around the world-space Y axis.
        Elevation is the number of radians to rotate around the view-space X axis.
        '''
        # Find the world space pivot point.  This is the view position in world
        # space minus the view direction vector scaled by the center of
        # interest distance.
        pivotPos = self.position - (self.coi * self.orientation.T[2]).T

        # Construct the azimuth and elevation transformation matrices
        azimuthMatrix = rotate(0, -azimuth, 0, pivotPos) 
        elevationMatrix = rotate(elevation, 0, 0, self.view * pivotPos)

        # Get the new view matrix
        self.view = elevationMatrix * self.view * azimuthMatrix

        # Extract the orientation from the new view matrix
        self.orientation = np.matrix(self.view).T
        self.orientation.T[3] = [0, 0, 0, 1]

        # Now extract the new view position
        negEye = self.orientation * self.view
        self.position = -(negEye.T[3]).T
        self.position[3, 0] = 1

np.set_printoptions(precision=3)

pos = np.matrix([[5.321, 5.866, 4.383, 1]]).T
orientation = radians(-60), radians(40), 0
coi = 1

camera = Camera(pos, *orientation, coi=coi)
print 'Initial attributes:'
print np.round(np.degrees(eulerAnglesXYZ(camera.orientation)), 3)
print np.round(camera.position, 3)
print 'Attributes after tumbling:'
camera.tumble(azimuth=radians(-40), elevation=radians(-60))
print np.round(np.degrees(eulerAnglesXYZ(camera.orientation)), 3)
print np.round(camera.position, 3)

答案 1 :(得分:0)

从头开始跟踪您的视图和右矢量,并使用旋转矩阵更新它们。然后计算你的向上矢量。