如何在用cdl中的sdl,opengl移动相机的同时修复奇怪的相机旋转

时间:2009-08-11 10:06:48

标签: c++ linux opengl sdl

我有一个相机对象,我把它放在网上阅读,处理向前和向后移动,左右扫视,甚至用鼠标环顾四周。但是当我向任何方向移动时,试着环顾四周它会跳到那个地方,但是当我不移动并环顾它的时候。

我希望有人可以帮我解决为什么我可以同时移动和环顾四周?

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

}

和mousemotion代码

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

2 个答案:

答案 0 :(得分:3)

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

就个人而言,我只想允许一个简单的4x4矩阵。任何实现都可以。要旋转你,简单地说,需要使用鼠标x和y的变化旋转作为euler输入,以围绕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;
}

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

答案 1 :(得分:1)

我同意Goz。如果要表示仿射变换(例如rotate + translate

),则需要使用homegenous 4x4 matrices

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

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

1 0 0 0&lt; --- x单位向量
0 1 0 0&lt; --- y单位矢量
0 0 1 0&lt; --- z单位矢量
0 0 0 1&lt; ---翻译矢量

就大多数游戏相机的工作方式而言,轴的映射方式如下:
x轴&lt; =&gt;相机底盘左/右
y轴&lt; =&gt;相机平移上/下



z轴&lt; =&gt;相机放大/缩小

因此,如果我旋转整个引用框架以查看新点LookAt然后Goz放入他的BaseCamera重载构造函数代码,则构建一个新的局部坐标系并将其设置为矩阵(全部{ {1}}通常设置矩阵的那四行,即VLat将是行0,vUp行1,vDir行2和vPos行3)

然后放大/缩小就会变成第3行=第2行*步骤

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