如何在 C++ 中使用 sdl、opengl 移动相机时修复奇怪的相机旋转

Posted

技术标签:

【中文标题】如何在 C++ 中使用 sdl、opengl 移动相机时修复奇怪的相机旋转【英文标题】:How to fix weird camera rotation while moving camera with sdl, opengl in c++ 【发布时间】:2009-08-11 10:06:48 【问题描述】:

我有一个相机对象,是我在网上阅读后整理的,它可以处理前后移动、左右扫视,甚至用鼠标环顾四周。但是当我向任何方向移动并尝试环顾四周时,它会到处乱跳,但是当我不动并环顾四周时,它就很好。

我希望有人能帮我弄清楚为什么我可以同时移动和环顾四周?

main.h

#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

#include <cmath>


#define CAMERASPEED 0.03f               // The Camera Speed



struct tVector3 // Extended 3D Vector Struct

           

tVector3()  // Struct Constructor

tVector3 (float new_x, float new_y, float new_z) // Init Constructor     

 x = new_x; y = new_y; z = new_z; 

// overload + operator

tVector3 operator+(tVector3 vVector) return tVector3(vVector.x+x, vVector.y+y, vVector.z+z);

// overload - operator

tVector3 operator-(tVector3 vVector) return tVector3(x-vVector.x, y-vVector.y, z-vVector.z);

// overload * operator

tVector3 operator*(float number)     return tVector3(x*number, y*number, z*number);

// overload / operator

tVector3 operator/(float number)     return tVector3(x/number, y/number, z/number);



float x, y, z;                      // 3D vector coordinates

;



class CCamera 



public:



    tVector3 mPos;  

    tVector3 mView;     

    tVector3 mUp;           



    void Strafe_Camera(float speed);



    void Move_Camera(float speed);

    void Rotate_View(float speed);
    void Position_Camera(float pos_x, float pos_y,float pos_z,

                         float view_x, float view_y, float view_z,

                         float up_x,   float up_y,   float up_z);

;



void Draw_Grid();

camera.cpp

#include "main.h"

void CCamera::Position_Camera(float pos_x, float pos_y, float pos_z,
                float view_x, float view_y, float view_z, 
                float up_x, float up_y, float up_z)

mPos = tVector3(pos_x, pos_y, pos_z);
mView = tVector3(view_x, view_y, view_z);
mUp = tVector3(up_x, up_y, up_z);


void CCamera::Move_Camera(float speed)

tVector3 vVector = mView - mPos;

mPos.x  = mPos.x  + vVector.x * speed;

mPos.z  = mPos.z  + vVector.z * speed;

mView.x = mView.x + vVector.x * speed;

mView.z = mView.z + vVector.z * speed;


void CCamera::Strafe_Camera(float speed)

tVector3 vVector = mView - mPos;

tVector3 vOrthoVector;



vOrthoVector.x = -vVector.z;

vOrthoVector.z =  vVector.x;



mPos.x  = mPos.x  + vOrthoVector.x * speed;

mPos.z  = mPos.z  + vOrthoVector.z * speed;

mView.x = mView.x + vOrthoVector.x * speed;

mView.z = mView.z + vOrthoVector.z * speed;


void CCamera::Rotate_View(float speed)

tVector3 vVector = mView - mPos;
tVector3 vOrthoVector;

vOrthoVector.x = -vVector.z;

vOrthoVector.z =  vVector.x;


mView.z = (float)(mPos.z + sin(speed)*vVector.x + cos(speed)*vVector.z);

mView.x = (float)(mPos.x + cos(speed)*vVector.x - sin(speed)*vVector.z);


和鼠标移动代码

void processEvents()

int mid_x = screen_width  >> 1;

int mid_y = screen_height >> 1;
int mpx = event.motion.x;
int mpy = event.motion.y;

float angle_y  = 0.0f;

float angle_z  = 0.0f;

while(SDL_PollEvent(&event))

    switch(event.type)
    
        case SDL_MOUSEMOTION:
            if( (mpx == mid_x) && (mpy == mid_y) ) return;



            // Get the direction from the mouse cursor, set a resonable maneuvering speed

            angle_y = (float)( (mid_x - mpx) ) / 1000; //1000

            angle_z = (float)( (mid_y - mpy) ) / 1000; //1000


            // The higher the value is the faster the camera looks around.

            objCamera.mView.y += angle_z * 2;



            // limit the rotation around the x-axis

            if((objCamera.mView.y - objCamera.mPos.y) > 8)  objCamera.mView.y = objCamera.mPos.y + 8;

            if((objCamera.mView.y - objCamera.mPos.y) <-8)  objCamera.mView.y = objCamera.mPos.y - 8;


            objCamera.Rotate_View(-angle_y);
            SDL_WarpMouse(mid_x, mid_y);
            break;

        case SDL_KEYUP:
            objKeyb.handleKeyboardEvent(event,true);
            break;
        case SDL_KEYDOWN:
            objKeyb.handleKeyboardEvent(event,false);
            break;

        case SDL_QUIT:
            quit = true;
            break;

        case SDL_VIDEORESIZE:
            screen = SDL_SetVideoMode( event.resize.w, event.resize.h, screen_bpp, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE );
            screen_width = event.resize.w;
            screen_height = event.resize.h;
            init_opengl();
            std::cout << "Resized to width: " << event.resize.w << " height: " << event.resize.h << std::endl;
            break;

        default:
            break;
    


【问题讨论】:

只有当你同时看和移动时才会出现这种情况吗? 是的,他们单独工作,一起工作很奇怪。 【参考方案1】:

我不完全确定你在上面做什么。

我个人只允许一个简单的 4x4 矩阵。任何实现都可以。要旋转您,简单地说,需要使用鼠标 x 和 y 的变化作为欧拉输入来旋转,以便围绕 y 和 x 轴旋转。互联网上有很多代码可以为您完成这项工作。

其中一些矩阵库不会为您提供“MoveForward()”函数。如果是这样就可以了,前进很容易。第三列(或行,如果您使用行主矩阵)是您的前向向量。提取它。对其进行规范化(无论如何它确实应该被规范化,因此可能不需要此步骤)。将其乘以您希望向前移动的数量,然后将其添加到位置(第 4 列/行)。

现在是奇怪的部分。视图矩阵是一种特殊类型的矩阵。上面的矩阵定义了视图空间。如果您将当前模型矩阵乘以该矩阵,您将不会得到您期望的答案。因为您希望对其进行转换,以使相机位于原点。因此,您需要有效地撤消相机转换以将事物重新定向到上面定义的视图。为此,您将模型矩阵乘以视图矩阵的

您现在在正确的视图空间中定义了一个对象。

这是我非常简单的相机类。它不处理您描述的功能,但希望能给您一些关于如何设置类的想法(请注意,我使用行专业,即 DirectX 样式、矩阵)。

BaseCamera.h:

#ifndef BASE_CAMERA_H_
#define BASE_CAMERA_H_

/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

#include "Maths/Vector4.h"
#include "Maths/Matrix4x4.h"

/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

class BaseCamera

protected:
    bool                    mDirty;
    MathsLib::Matrix4x4     mCameraMat;
    MathsLib::Matrix4x4     mViewMat;
public:
    BaseCamera();
    BaseCamera( const BaseCamera& camera );
    BaseCamera( const MathsLib::Vector4& vPos, const MathsLib::Vector4& vLookAt );
    BaseCamera( const MathsLib::Matrix4x4& matCamera );

    bool IsDirty() const;
    void SetDirty();

    MathsLib::Matrix4x4&        GetOrientationMatrix();
    const MathsLib::Matrix4x4&  GetOrientationMatrix() const;

    MathsLib::Matrix4x4&        GetViewMatrix();
;

/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

inline MathsLib::Matrix4x4& BaseCamera::GetOrientationMatrix()

    return mCameraMat;


/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

inline const MathsLib::Matrix4x4& BaseCamera::GetOrientationMatrix() const

    return mCameraMat;


/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

inline bool BaseCamera::IsDirty() const

    return mDirty;


/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

inline void BaseCamera::SetDirty()

    mDirty = true;


/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

#endif

BaseCamera.cpp:

#include "Render/stdafx.h"

#include "BaseCamera.h"

/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

BaseCamera::BaseCamera() :
    mDirty( true )



/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

BaseCamera::BaseCamera( const BaseCamera& camera ) :
    mDirty( camera.mDirty ),
    mCameraMat( camera.mCameraMat ),
    mViewMat( camera.mViewMat )



/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

BaseCamera::BaseCamera( const MathsLib::Vector4& vPos, const MathsLib::Vector4& vLookAt ) :
    mDirty( true )

    MathsLib::Vector4 vDir  = (vLookAt - vPos).Normalise();
    MathsLib::Vector4 vLat  = MathsLib::CrossProduct( MathsLib::Vector4( 0.0f, 1.0f, 0.0f ), vDir ).Normalise();
    MathsLib::Vector4 vUp   = MathsLib::CrossProduct( vDir, vLat );//.Normalise();

    mCameraMat.Set( vLat, vUp, vDir, vPos );    


/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

BaseCamera::BaseCamera( const MathsLib::Matrix4x4& matCamera ) :
    mDirty( true ),
    mCameraMat( matCamera )



    /*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

MathsLib::Matrix4x4& BaseCamera::GetViewMatrix()

    if ( IsDirty() )
    
        mViewMat    = mCameraMat.Inverse();
        mDirty      = false;
    
    return mViewMat;


/*+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+*/

【讨论】:

【参考方案2】:

我同意戈兹的观点。如果要表示仿射变换,如旋转+平移,则需要使用homegenous 4x4 matrices

假设row major representation,那么如果没有缩放或剪切,您的 4x4 矩阵表示以下内容: 第 0 到 2 行:本地坐标系的三个基向量(即 x、y、z) 第 3 行:来自原点的当前翻译

因此,正如 Goz 所说,沿着你的局部 x 向量移动,因为你可以假设它是一个单位向量 如果没有缩放/剪切,只需将其乘以移动步骤( +ve 或 -ve ),然后将结果向量添加到矩阵中的第 4 行 所以举一个简单的例子,从你的本地框架设置为世界框架的原点开始,那么你的矩阵看起来像这样

1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1

就大多数游戏摄像机的工作方式而言,轴映射如下: x 轴 相机平移左/右 y 轴 相机平移向上/向下 z轴相机放大/缩小

因此,如果我旋转整个参考系以查看一个新点 LookAt,然后当 Goz 放入他的 BaseCamera 重载构造函数代码时,您然后构建一个新的局部坐标系并将其设置到您的矩阵中(所有 @ 987654323@ 通常设置矩阵的这四行,即 VLat 将是第 0 行、vUp 第 1 行、vDir 第 2 行和 vPos 第 3 行)

然后放大/缩小将变为第 3 行 = 第 2 行 * stepval

正如 Goz 正确指出的那样,您需要将其转换回世界空间,这是通过乘以视图矩阵的倒数来完成的

【讨论】:

请记住,上面的代码是针对行主矩阵的。您需要将矩阵转置为列主矩阵:)

以上是关于如何在 C++ 中使用 sdl、opengl 移动相机时修复奇怪的相机旋转的主要内容,如果未能解决你的问题,请参考以下文章

我的 C++ OpenGL SDL 程序没有按预期工作

c++:如何使用 sdl 将位图加载到 opengl 中的多维数据集中?

OpenGL C++ SDL 2D 阴影

如何相对于角度移动?

如何只使用SDL编写的游戏OpenGL代码的一部分?

将 SDL 示例移植到 OpenGL 和 C++