gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:03 28 4
gpt4 key购买 nike

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

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

主要.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();

相机.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);

}

和鼠标 Action 代码

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

最佳答案

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

就我个人而言,我只允许一个简单的 4x4 矩阵。任何实现都可以。要旋转你,简单地说,需要使用鼠标 x 和 y 的变化作为围绕 y 和 x 轴旋转的欧拉输入进行旋转。 Internet 上有很多代码可以为您执行此操作。

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

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

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

这是我非常简单的相机类。它不处理您描述的功能,但希望能给您一些关于如何设置类的想法(警告,我使用主要行,即 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;
}

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

关于c++ - 如何在使用 c++ 中的 sdl、opengl 移动相机时修复奇怪的相机旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1259461/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com