且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎样才能让在我的方向移动相机我面对

更新时间:2022-06-26 00:19:31

您可以从模型视图矩阵的第一,第二和第三列获得左侧,摄像头并将其向前载体 - 它的配置,当然之后。

You can obtain the left, up and forward vectors of the camera from the first, second and third column of the modelview matrix - after it's been configured of course.

float mview[16];
float front[4], up[4], left[4];

glGetFloatv(GL_MODELVIEW_MATRIX, mview);
left[0] = mview[0]; left[1] = mview[1]; left[2] = mview[2]; left[3] = 1.0;
up[0] = mview[4]; up[1] = mview[5]; up[2] = mview[6]; up[3] = 1.0;
front[0] = mview[8]; front[1] = mview[9]; front[2] = mview[10]; front[3] = 1.0;

从此,你只需要合适的载体加入到运动的位置。

From then, you just need to add the appropriate vector to the position for movement.

if(myInput.Keys[VK_A]) //Left
{
    curPos->x += left[0] * myInput.Sensitivity;
    curPos->z += left[z] * myInput.Sensitivity;
}
else if(myInput.Keys[VK_D]) //Right
{
    curPos->x -= left[0] * myInput.Sensitivity;
    curPos->z -= left[2] * myInput.Sensitivity;
}
// etc.

请参阅进一步的细节此页面
在同一网站上,这个其他页面介绍了如何使用旋转角度,以获得模型视图矩阵,因此列向量,如上所示。相同的技术(但具有稍微不同的坐标系)也被用来例如在quake游戏引擎意甲。

See this page for further details. On the same site, this other page describes how to use rotation angles to obtain the model view matrix, hence the column vectors as shown above. The same technique (but with a slightly different coordinate system) is also used for example in the quake game engines serie.