Opengl视图转换矩阵旋转

Opengl视图转换矩阵旋转,opengl,opengl-es,3d,game-engine,coordinate-transformation,Opengl,Opengl Es,3d,Game Engine,Coordinate Transformation,最近我实现了一个简单的Opengl程序,它由一个对象场景组成,我应用了大多数变换和投影矩阵,这样我就能够旋转变换和缩放对象,通过z&x坐标和应用透视投影移动我的相机但是当谈到相机旋转时,事情变得奇怪,我相机的旋转矩阵只是一个均匀旋转世界的旋转矩阵,但是当我旋转世界时,我朝上看+y;当我向前移动时,相机似乎没有朝着它所看到的方向前进与FPS游戏中的情况一样我的相机相对于世界空间移动,我知道我缺少在x,y,z坐标中指定方向的向量,但我无法将这些向量与我的相机(视图变换)矩阵合并,互联网上的大部分教程

最近我实现了一个简单的Opengl程序,它由一个对象场景组成,我应用了大多数变换和投影矩阵,这样我就能够
旋转变换和缩放对象,通过z&x坐标和应用透视投影移动我的相机
但是当谈到相机旋转时,事情变得奇怪,我相机的旋转矩阵只是一个均匀旋转世界的旋转矩阵,但是当我旋转世界时,我朝上看<代码>+y;当我向前移动时,相机似乎没有朝着它所看到的方向前进
与FPS游戏中的情况一样
我的相机相对于世界空间移动,我知道我缺少在
x,y,z
坐标中指定方向的向量,但我无法将这些向量与我的相机(视图变换)矩阵合并,互联网上的大部分教程要么在方框图中描述它,要么使用传统的gluLookAt()函数,我真的需要一个关于视图变换和特别是相机旋转的简要说明,以及我应该如何在矩阵中实现它,我的最终矩阵如下所示:

      for (int x = 0; x< 256; x++)
        {
            if (state[x] == 1 )
            {
                if(x  == 26)
                    tranForward = -0.001;
                if (x == 22)
                    tranForward = 0.001;
                if (x == 4)
                    tranRight = 0.0009;
                if (x == 7)
                    tranRight = -0.0009;

                if (x == 82)
                    lookUp = 0.02;
                if (x == 81)
                    lookUp = -0.02;
                if (x == 80)
                    lookRight = -0.02;
                if (x == 79)
                    lookRight = 0.02;
            }
        }

camTrans   =   Rotation.setRotation(lookUp, lookRight, 0) * Translation.setTranslation(tranRight, 0, tranForward);

        result =   camTrans * result;

        modelTrans = Projection * result;



        tranForward = 0.0;
        tranRight   = 0.0;
        lookUp      = 0.0;
        lookRight   = 0.0;

       glUniformMatrix4fv(uniformloc, 1, GL_TRUE, modelTrans.getMat());
结果转换=透视转换*摄像机转换*模型转换

其中:

perspectiveTrans=仅应用透视投影变换

cameraTrans=是影响场景中所有对象的旋转、平移矩阵的组合

modelTrans=是应用于模型的转换

Matrix4X4.cpp文件:

#include "Matrix4X4.h"

using namespace std;


////////////////////////////////// Constructor Declerations ////////////////////////////////

Matrix4X4::Matrix4X4()
{
setIdentity();
}

Matrix4X4::Matrix4X4(float value)
{
for(int i = 0 ; i < 4; i++)
    for ( int j = 0; j < 4; j++)
        Matrix[i][j] = value;

}

/////////////////////////////////////////////////////////////////////////////////







////////////////////////////// Destructor Decleration //////////////////////////////
Matrix4X4::~Matrix4X4()
{

}

///////////////////////////////////////////////////////////////////////////////////







/////////////////////// Set Identity Matrix /////////////////////////////////////////

void Matrix4X4::setIdentity()
{
Matrix[0][0] =1;   Matrix[0][1] = 0;  Matrix[0][2] = 0;      Matrix[0][3] = 0;
Matrix[1][0] =0;   Matrix[1][1] = 1;  Matrix[1][2] = 0;      Matrix[1][3] = 0;
Matrix[2][0] =0;   Matrix[2][1] = 0;  Matrix[2][2] = 1;      Matrix[2][3] = 0;
Matrix[3][0] =0;   Matrix[3][1] = 0;  Matrix[3][2] = 0;      Matrix[3][3] = 1;


}

///////////////////////////////////////////////////////////////////////////////////






///////////////////////// Set Translation Matrix //////////////////////////////////

Matrix4X4 Matrix4X4::setTranslation(float x,float y,float z)
{


Matrix[0][0] =1;   Matrix[0][1] = 0;  Matrix[0][2] = 0;      Matrix[0][3] = x;
Matrix[1][0] =0;   Matrix[1][1] = 1;  Matrix[1][2] = 0;      Matrix[1][3] = y;
Matrix[2][0] =0;   Matrix[2][1] = 0;  Matrix[2][2] = 1;      Matrix[2][3] = z;
Matrix[3][0] =0;   Matrix[3][1] = 0;  Matrix[3][2] = 0;      Matrix[3][3] = 1;

return  *this;

}
/////////////////////////////////////////////////////////////////////////////////





////////////////////////////////////// Set Rotation Matrix     ///////////////////////////////////////////

Matrix4X4 Matrix4X4::setRotation(float x,float y,float z)
{
Matrix4X4 xRot;
Matrix4X4 yRot;
Matrix4X4 zRot;

x = (float)x * 3.14/ 180.0;
y = (float)y * 3.14/ 180.0;
z = (float)z * 3.14/ 180.0;



xRot.Matrix[0][0] =1;         xRot.Matrix[0][1] = 0;        xRot.Matrix[0][2] = 0;            xRot.Matrix[0][3] = 0;
xRot.Matrix[1][0] =0;         xRot.Matrix[1][1] = cosf(x);  xRot.Matrix[1][2] = -sinf(x);   xRot.Matrix[1][3] = 0;
xRot.Matrix[2][0] =0;         xRot.Matrix[2][1] = sinf(x);  xRot.Matrix[2][2] = cosf(x);    xRot.Matrix[2][3] = 0;
xRot.Matrix[3][0] =0;         xRot.Matrix[3][1] = 0;        xRot.Matrix[3][2] = 0;          xRot.Matrix[3][3] = 1;

yRot.Matrix[0][0] = cosf(y);  yRot.Matrix[0][1] = 0;        yRot.Matrix[0][2] = -sinf(y);   yRot.Matrix[0][3] = 0;
yRot.Matrix[1][0] =0;         yRot.Matrix[1][1] = 1;        yRot.Matrix[1][2] = 0;          yRot.Matrix[1][3] = 0;
yRot.Matrix[2][0] = sinf(y);  yRot.Matrix[2][1] = 0;        yRot.Matrix[2][2] = cosf(y);    yRot.Matrix[2][3] = 0;
yRot.Matrix[3][0] =0;         yRot.Matrix[3][1] = 0;        yRot.Matrix[3][2] = 0;          yRot.Matrix[3][3] = 1;

zRot.Matrix[0][0] = cosf(z);  zRot.Matrix[0][1] = -sinf(z); zRot.Matrix[0][2] = 0;          zRot.Matrix[0][3] = 0;
zRot.Matrix[1][0] = sinf(z);  zRot.Matrix[1][1] = cosf(z);  zRot.Matrix[1][2] = 0;          zRot.Matrix[1][3] = 0;
zRot.Matrix[2][0] =0;         zRot.Matrix[2][1] = 0;        zRot.Matrix[2][2] = 1;          zRot.Matrix[2][3] = 0;
zRot.Matrix[3][0] =0;         zRot.Matrix[3][1] = 0;        zRot.Matrix[3][2] = 0;          zRot.Matrix[3][3] = 1;


return (zRot * yRot * xRot) ;

}

////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////// Set Scale Matrix //////////////////////////////////////////

Matrix4X4 Matrix4X4::setScale(float x,float y,float z)
{


Matrix[0][0] =x;   Matrix[0][1] = 0;  Matrix[0][2] = 0;      Matrix[0][3] = 0;
Matrix[1][0] =0;   Matrix[1][1] = y;  Matrix[1][2] = 0;      Matrix[1][3] = 0;
Matrix[2][0] =0;   Matrix[2][1] = 0;  Matrix[2][2] = z;      Matrix[2][3] = 0;
Matrix[3][0] =0;   Matrix[3][1] = 0;  Matrix[3][2] = 0;      Matrix[3][3] = 1;

return *this;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////





///////////////////////////////// Set Perspective Projection ///////////////////////////////////////

void Matrix4X4::setPerspective(float fov,float aRatio,float zNear,float zFar)
{


fov = (fov/2) * 3.14 / 180.0;
float tanHalfFOV = tanf(fov);
float zRange = zNear - zFar;


 Matrix[0][0] =1.0f / (tanHalfFOV * aRatio);   Matrix[0][1] = 0;                  Matrix[0][2] = 0;                         Matrix[0][3] = 0;
 Matrix[1][0] =0;                              Matrix[1][1] = 1.0f / tanHalfFOV;  Matrix[1][2] = 0;                         Matrix[1][3] = 0;
 Matrix[2][0] =0;                              Matrix[2][1] = 0;                  Matrix[2][2] = (-zNear - zFar)/ zRange;   Matrix[2][3] = 2* zFar * zNear / zRange;
 Matrix[3][0] =0;                              Matrix[3][1] = 0;                  Matrix[3][2] = 1;                         Matrix[3][3] = 0;



}
/////////////////////////////////////////////////////////////////////////////////////////////////////////





////////////////////////////////////// Getters & Setters ////////////////////////////////////////////

float * Matrix4X4::getMat()
{
return (float *) Matrix;
}


float Matrix4X4::getMember(int x, int y) const
{
return Matrix[x][y];
}


void Matrix4X4::setMat(int row,int col,float value)
{
Matrix[row][col] = value;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////






/////////////////////////////////////// (*) Operator Overload //////////////////////////////////////

Matrix4X4 operator * (const Matrix4X4 & lhs,const Matrix4X4 & rhs)
{

Matrix4X4 result;

    for(int i = 0 ; i < 4; i++)
        for ( int j = 0; j < 4; j++)
            result.setMat(i, j,  lhs.getMember(i,0) * rhs.getMember(0, j) +
                            lhs.getMember(i,1) * rhs.getMember(1, j) +
                            lhs.getMember(i,2) * rhs.getMember(2, j) +
                            lhs.getMember(i,3) * rhs.getMember(3, j));


        return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
#包括“Matrix4X4.h”
使用名称空间std;
//////////////////////////////////构造器偏差////////////////////////////////
Matrix4X4::Matrix4X4()
{
setIdentity();
}
Matrix4X4::Matrix4X4(浮点值)
{
对于(int i=0;i<4;i++)
对于(int j=0;j<4;j++)
矩阵[i][j]=值;
}
/////////////////////////////////////////////////////////////////////////////////
//////////////////////////////破坏者减容//////////////////////////////
Matrix4X4::~Matrix4X4()
{
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////集合单位矩阵/////////////////////////////////////////
void Matrix4X4::setIdentity()
{
矩阵[0][0]=1;矩阵[0][1]=0;矩阵[0][2]=0;矩阵[0][3]=0;
矩阵[1][0]=0;矩阵[1][1]=1;矩阵[1][2]=0;矩阵[1][3]=0;
矩阵[2][0]=0;矩阵[2][1]=0;矩阵[2][2]=1;矩阵[2][3]=0;
矩阵[3][0]=0;矩阵[3][1]=0;矩阵[3][2]=0;矩阵[3][3]=1;
}
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////集平移矩阵//////////////////////////////////
Matrix4X4 Matrix4X4::setTranslation(浮点x、浮点y、浮点z)
{
矩阵[0][0]=1;矩阵[0][1]=0;矩阵[0][2]=0;矩阵[0][3]=x;
矩阵[1][0]=0;矩阵[1][1]=1;矩阵[1][2]=0;矩阵[1][3]=y;
矩阵[2][0]=0;矩阵[2][1]=0;矩阵[2][2]=1;矩阵[2][3]=z;
矩阵[3][0]=0;矩阵[3][1]=0;矩阵[3][2]=0;矩阵[3][3]=1;
归还*这个;
}
/////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////集合旋转矩阵///////////////////////////////////////////
Matrix4X4 Matrix4X4::设置旋转(浮点x、浮点y、浮点z)
{
Matrix4x4xRot;
Matrix4X4-yRot;
matrix4x4zrot;
x=(浮动)x*3.14/180.0;
y=(浮动)y*3.14/180.0;
z=(浮动)z*3.14/180.0;
xRot.Matrix[0][0]=1;xRot.Matrix[0][1]=0;xRot.Matrix[0][2]=0;xRot.Matrix[0][3]=0;
xRot.Matrix[1][0]=0;xRot.Matrix[1][1]=cosf(x);xRot.Matrix[1][2]=-sinf(x);xRot.Matrix[1][3]=0;
矩阵[2][0]=0;矩阵[2][1]=sinf(x);矩阵[2][2]=cosf(x);矩阵[2][3]=0;
xRot.Matrix[3][0]=0;xRot.Matrix[3][1]=0;xRot.Matrix[3][2]=0;xRot.Matrix[3][3]=1;
旋转矩阵[0][0]=cosf(y);旋转矩阵[0][1]=0;旋转矩阵[0][2]=-sinf(y);旋转矩阵[0][3]=0;
旋转矩阵[1][0]=0;旋转矩阵[1][1]=1;旋转矩阵[1][2]=0;旋转矩阵[1][3]=0;
yRot.矩阵[2][0]=sinf(y);yRot.矩阵[2][1]=0;yRot.矩阵[2][2]=cosf(y);yRot.矩阵[2][3]=0;
旋转矩阵[3][0]=0;旋转矩阵[3][1]=0;旋转矩阵[3][2]=0;旋转矩阵[3][3]=1;
zRot.Matrix[0][0]=cosf(z);zRot.Matrix[0][1]=-sinf(z);zRot.Matrix[0][2]=0;zRot.Matrix[0][3]=0;
zRot.Matrix[1][0]=sinf(z);zRot.Matrix[1][1]=cosf(z);zRot.Matrix[1][2]=0;zRot.Matrix[1][3]=0;
zRot.矩阵[2][0]=0;zRot.矩阵[2][1]=0;zRot.矩阵[2][2]=1;zRot.矩阵[2][3]=0;
zRot.矩阵[3][0]=0;zRot.矩阵[3][1]=0;zRot.矩阵[3][2]=0;zRot.矩阵[3][3]=1;
返回(zRot*yRot*xRot);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////集尺度矩阵//////////////////////////////////////////
Matrix4X4 Matrix4X4::设置刻度(浮点x、浮点y、浮点z)
{
矩阵[0][0]=x;矩阵[0][1]=0;矩阵[0][2]=0;矩阵[0][3]=0;
矩阵[1][0]=0;矩阵[1][1]=y;矩阵[1][2]=0;矩阵[1][3]=0;
矩阵[2][0]=0;矩阵[2][1]=0;矩阵[2][2]=z;矩阵[2][3]=0;
矩阵[3][0]=0;矩阵[3][1]=0;矩阵[3][2]=0;矩阵[3][3]=1;
归还*这个;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////集合透视投影///////////////////////////////////////
无效矩阵x4x4::setPerspective(浮动视野、浮动视野、浮动视野、浮动视野、浮动视野)
{
视野=(视野/2)*3.14/180.0;
浮动tanHalfFOV=tanf(fov);
浮动zRange=zNear-zFar;
矩阵[0][0]=1.0
result =  Projection * camTrans * modelTrans;
result =  Projection * (modelTrans * camTrans);
    Matrix4X4 orbitRotation; //rotation matrix for where in orbit the object is
    Matrix4X4 objectRotation; //object rotation around its own axis
    Matrix4X4 orbitRadius; //object orbit radius

    Matrix4X4 result = (orbitRotation*orbitRadius)*objectRotation;
      for (int x = 0; x< 256; x++)
        {
            if (state[x] == 1 )
            {
                if(x  == 26)
                    tranForward = -0.001;
                if (x == 22)
                    tranForward = 0.001;
                if (x == 4)
                    tranRight = 0.0009;
                if (x == 7)
                    tranRight = -0.0009;

                if (x == 82)
                    lookUp = 0.02;
                if (x == 81)
                    lookUp = -0.02;
                if (x == 80)
                    lookRight = -0.02;
                if (x == 79)
                    lookRight = 0.02;
            }
        }

camTrans   =   Rotation.setRotation(lookUp, lookRight, 0) * Translation.setTranslation(tranRight, 0, tranForward);

        result =   camTrans * result;

        modelTrans = Projection * result;



        tranForward = 0.0;
        tranRight   = 0.0;
        lookUp      = 0.0;
        lookRight   = 0.0;

       glUniformMatrix4fv(uniformloc, 1, GL_TRUE, modelTrans.getMat());