简体   繁体   English

OpenGL矩阵相机控件,局部旋转无法正常运行

[英]OpenGL Matrix Camera controls, local rotation not functioning properly

So I'm trying to figure out how to mannually create a camera class that creates a local frame for camera transformations. 因此,我试图找出如何手动创建一个相机类的方法,该类创建用于相机转换的本地框架。 I've created a player object based on OpenGL SuperBible's GLFrame class. 我已经基于OpenGL SuperBible的GLFrame类创建了一个播放器对象。

I got keyboard keys mapped to the MoveUp, MoveRight and MoveForward functions and the horizontal and vertical mouse movements are mapped to the xRot variable and rotateLocalY function. 我将键盘键映射到MoveUp,MoveRight和MoveForward函数,将水平和垂直鼠标移动映射到xRot变量和rotateLocalY函数。 This is done to create a FPS style camera. 这样做是为了创建FPS样式的相机。

The problem however is in the RotateLocalY. 但是问题出在RotateLocalY中。 Translation works fine and so does the vertical mouse movement but the horizontal movement scales all my objects down or up in a weird way. 平移效果很好,鼠标的垂直移动也可以,但是水平移动以一种怪异的方式将我的所有对象缩小或放大。 Besides the scaling, the rotation also seems to restrict itself to 180 degrees and rotates around the world origin (0.0) instead of my player's local position. 除了缩放外,旋转似乎还将自身限制为180度,并绕世界原点(0.0)旋转,而不是玩家的本地位置。

I figured that the scaling had something to do with normalizing vectors but the GLframe class (which I used for reference) never normalized any vectors and that class works just fine. 我发现缩放与矢量归一化有关,但是GLframe类(我用作参考)从未对任何矢量进行归一化,并且该类工作得很好。 Normalizing most of my vectors only solved the scaling and all the other problems were still there so I'm figuring one piece of code is causing all these problems? 对我的大多数矢量进行规范化只能解决缩放问题,而所有其他问题仍然存在,因此我想知道一段代码会导致所有这些问题吗?

I can't seem to figure out where the problem lies, I'll post all the appropriate code here and a screenshot to show the scaling. 我似乎无法弄清楚问题出在哪里,我将在此处张贴所有适当的代码和屏幕快照以显示缩放比例。

Player object 玩家对象

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

Player object header 播放器对象标头

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

转换出错

Applying transformations 应用转换

// 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();

and mouse 和鼠标

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

Honestly I think you are taking a way to complicated approach to your problem. 老实说,我认为您正在采取一种解决问题的复杂方法。 There are many ways to create a camera. 有很多创建相机的方法。 My favorite is using a R3-Vector and a Quaternion, but you could also work with a R3-Vector and two floats (pitch and yaw). 我最喜欢的是使用R3-Vector和四元数,但是您也可以使用R3-Vector和两个浮点数(俯仰和偏航)。

The setup with two angles is simple: 具有两个角度的设置很简单:

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

The tricky part now is moving the camera. 现在,棘手的部分是移动相机。 You must do something along the lines of: 您必须执行以下操作:

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

How to do the transforms, I would have to look that up, but you could to it by using a rotation matrix 如何进行转换,我必须查找一下,但是您可以通过使用旋转矩阵来实现

Rotation is trivial, just add or subtract from the pitch and yaw values. 旋转是微不足道的,只需从俯仰和偏航值中添加或减去即可。

I like using a quaternion for the orientation because it is general and thus you have a camera (any entity that is) that independent of any movement scheme. 我喜欢使用四元数作为方向,因为它是通用的,因此您有一个与任何运动方案无关的摄像机(即任何实体)。 In this case you have a camera that looks like so: 在这种情况下,您的相机看起来像这样:

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

Then the setup code uses shamelessly gluLookAt; 然后,安装代码会毫不客气地使用gluLookAt; you could make a transformation matrix out of it, but I never got it to work right. 您可以从中制作一个转换矩阵,但我一直无法正常工作。

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

Moving the camera in local frame is also simple: 在本地框架中移动相机也很简单:

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

The rotation is also straight forward. 旋转也很简单。

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

(Shameless plug): (无耻的插头):

If you are asking what math library I use, it is mathex . 如果您要问我使用的是什么数学库,那就是mathex I wrote it... 我写的...

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

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