1

所以我试图弄清楚如何手动创建一个相机类,为相机转换创建一个本地框架。我创建了一个基于 OpenGL SuperBible 的 GLFrame 类的播放器对象。

我将键盘键映射到 MoveUp、MoveRight 和 MoveForward 函数,水平和垂直鼠标移动映射到 xRot 变量和 rotateLocalY 函数。这样做是为了创建一个 FPS 风格的相机。

然而,问题在于 RotateLocalY。翻译工作正常,垂直鼠标移动也是如此,但水平移动以一种奇怪的方式缩小或放大我的所有对象。除了缩放之外,旋转似乎也将自身限制为 180 度,并围绕世界原点 (0.0) 而不是我玩家的本地位置旋转。

我认为缩放与规范化向量有关,但 GLframe 类(我用作参考)从未规范化任何向量,并且该类工作得很好。规范化我的大部分向量只解决了缩放问题,所有其他问题仍然存在,所以我认为一段代码导致了所有这些问题?

我似乎无法弄清楚问题出在哪里,我将在此处发布所有适当的代码以及显示缩放的屏幕截图。

玩家对象

Player::Player()
{
    location[0] = 0.0f; location[1] = 0.0f; location[2] = 0.0f;
    up[0] = 0.0f; up[1] = 1.0f; up[2] = 0.0f;
    forward[0] = 0.0f; forward[1] = 0.0f; forward[2] = -1.0f;
}

// Does all the camera transformation. Should be called before scene rendering!
void Player::ApplyTransform()
{
    M3DMatrix44f cameraMatrix;
    this->getTransformationMatrix(cameraMatrix);

    glRotatef(xAngle, 1.0f, 0.0f, 0.0f);
    glMultMatrixf(cameraMatrix);
}

void Player::MoveForward(GLfloat delta)
{
    location[0] += forward[0] * delta;
    location[1] += forward[1] * delta;
    location[2] += forward[2] * delta;
}

void Player::MoveUp(GLfloat delta)
{
    location[0] += up[0] * delta;
    location[1] += up[1] * delta;
    location[2] += up[2] * delta;
}

void Player::MoveRight(GLfloat delta)
{
    // Get X axis vector first via cross product
    M3DVector3f xAxis;
    m3dCrossProduct(xAxis, up, forward);

    location[0] += xAxis[0] * delta;
    location[1] += xAxis[1] * delta;
    location[2] += xAxis[2] * delta;
}

void Player::RotateLocalY(GLfloat angle)
{
    // Calculate a rotation matrix first
    M3DMatrix44f rotationMatrix;
    // Rotate around the up vector
    m3dRotationMatrix44(rotationMatrix, angle, up[0], up[1], up[2]); // Use up vector to get correct rotations even with multiple rotations used.

    // Get new forward vector out of the rotation matrix
    M3DVector3f newForward;
    newForward[0] = rotationMatrix[0] * forward[0] + rotationMatrix[4] * forward[1] + rotationMatrix[8] * forward[2]; 
    newForward[1] = rotationMatrix[1] * forward[1] + rotationMatrix[5] * forward[1] + rotationMatrix[9] * forward[2];
    newForward[2] = rotationMatrix[2] * forward[2] + rotationMatrix[6] * forward[1] + rotationMatrix[10] * forward[2];

    m3dCopyVector3(forward, newForward);
}

void Player::getTransformationMatrix(M3DMatrix44f matrix)
{
    // Get Z axis (Z axis is reversed with camera transformations)
    M3DVector3f zAxis;
    zAxis[0] = -forward[0];
    zAxis[1] = -forward[1];
    zAxis[2] = -forward[2];

    // Get X axis
    M3DVector3f xAxis;
    m3dCrossProduct(xAxis, up, zAxis);

    // Fill in X column in transformation matrix
    m3dSetMatrixColumn44(matrix, xAxis, 0); // first column
    matrix[3] = 0.0f; // Set 4th value to 0

    // Fill in the Y column
    m3dSetMatrixColumn44(matrix, up, 1); // 2nd column
    matrix[7] = 0.0f;

    // Fill in the Z column
    m3dSetMatrixColumn44(matrix, zAxis, 2); // 3rd column
    matrix[11] = 0.0f;

    // Do the translation
    M3DVector3f negativeLocation; // Required for camera transform (right handed OpenGL system. Looking down negative Z axis)
    negativeLocation[0] = -location[0];
    negativeLocation[1] = -location[1];
    negativeLocation[2] = -location[2];
    m3dSetMatrixColumn44(matrix, negativeLocation, 3); // 4th column
    matrix[15] = 1.0f;
}

播放器对象头

class Player
{
public:
    //////////////////////////////////////
    // Variables
    M3DVector3f location;
    M3DVector3f up;
    M3DVector3f forward;
    GLfloat xAngle; // Used for FPS divided X angle rotation (can't combine yaw and pitch since we'll also get a Roll which we don't want for FPS)
    /////////////////////////////////////
    // Functions
    Player();
    void ApplyTransform();
    void MoveForward(GLfloat delta);
    void MoveUp(GLfloat delta);
    void MoveRight(GLfloat delta);
    void RotateLocalY(GLfloat angle); // Only need rotation on local axis for FPS camera style. Then a translation on world X axis. (done in apply transform)

private:
    void getTransformationMatrix(M3DMatrix44f matrix);
};

转换出错

应用转换

// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Apply camera transforms
player.ApplyTransform();


// Set up lights
...

// Use shaders
...

// Render the scene
RenderScene();

// Do post rendering operations
glutSwapBuffers();

和鼠标

float mouseSensitivity = 500.0f;

float horizontal = (width / 2) - mouseX;
float vertical = (height / 2) - mouseY;

horizontal /= mouseSensitivity;
vertical /= (mouseSensitivity / 25);

player.xAngle += -vertical;
player.RotateLocalY(horizontal);

glutWarpPointer((width / 2), (height / 2));
4

1 回答 1

1

老实说,我认为您正在采取一种复杂的方法来解决您的问题。有很多方法可以创建相机。我最喜欢的是使用 R3-Vector 和四元数,但您也可以使用 R3-Vector 和两个浮点数(俯仰和偏航)。

两个角度的设置很简单:

glLoadIdentity();
glTranslatef(-pos[0], -pos[1], -pos[2]);
glRotatef(-yaw, 0.0f, 0.0f, 1.0f);
glRotatef(-pitch, 0.0f, 1.0f, 0.0f);

现在棘手的部分是移动相机。您必须按照以下方式做一些事情:

flaot ds = speed * dt;
position += tranform_y(pich, tranform_z(yaw, Vector3(ds, 0, 0)));

如何进行转换,我必须查找它,但您可以使用旋转矩阵来查找

旋转是微不足道的,只需添加或减去俯仰和偏航值。

我喜欢使用四元数作为方向,因为它是通用的,因此你有一个独立于任何运动方案的相机(任何实体)。在这种情况下,您有一个看起来像这样的相机:

class Camera
{
public:
   // lots of stuff omitted

   void setup();

   void move_local(Vector3f value);

   void rotate(float dy, float dz);

private:
    mx::Vector3f position;
    mx::Quaternionf orientation;
};

然后设置代码无耻地使用gluLookAt;你可以用它制作一个转换矩阵,但我从来没有让它正常工作。

void Camera::setup()
{
    // projection related stuff

    mx::Vector3f eye     = position;
    mx::Vector3f forward = mx::transform(orientation, mx::Vector3f(1, 0, 0));
    mx::Vector3f center  = eye + forward;
    mx::Vector3f up      = mx::transform(orientation, mx::Vector3f(0, 0, 1));
    gluLookAt(eye(0), eye(1), eye(2), center(0), center(1), center(2), up(0), up(1), up(2));
}

在本地框架中移动相机也很简单:

void Camera::move_local(Vector3f value) 
{
    position += mx::transform(orientation, value);
}

旋转也是直截了当的。

void Camera::rotate(float dy, float dz)
{
    mx::Quaternionf o = orientation;
    o = mx::axis_angle_to_quaternion(horizontal, mx::Vector3f(0, 0, 1)) * o;
    o = o * mx::axis_angle_to_quaternion(vertical, mx::Vector3f(0, 1, 0));   
    orientation = o;
}

(无耻插件):

如果你问我使用什么数学库,它是mathex。我写的...

于 2013-02-06T13:02:04.127 回答