一、FPS摄像机
题目:能否修改摄像机类,使得其能够变成一个真正的FPS摄像机(不能随意飞行,只能待在xz平面上);
在camera.h中的ProcessKeyboard()函数中添加一行代码:Position.y = 0.0f;使y轴一直为0
void ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
Position.y = 0.0f;
}
二、创建观察矩阵
题目:手动创建一个观察矩阵,替换LookAt()函数;
QMatrix4x4 myLookAt(QVector3D position, QVector3D target, QVector3D worldUp)
{
//z轴向量(摄像机反方向)
QVector3D zaxis = QVector3D(position - target); //摄像机的位置 - 原点
zaxis.normalize();
worldUp.normalize();
//x轴向量(右轴)
QVector3D xaxis = QVector3D::crossProduct(worldUp, zaxis);
xaxis.normalize();
//y轴向量(上轴)
QVector3D yaxis = QVector3D::crossProduct(zaxis, xaxis);
QMatrix4x4 translation;
QVector4D theVector4D;
theVector4D.setX(-position.x()); // Third column, first row
theVector4D.setY(-position.y());
theVector4D.setZ(-position.z());
theVector4D.setW(1.0);
translation.setColumn(3,theVector4D);
QMatrix4x4 rotation;
theVector4D.setX(xaxis.x()); // First column, first row
theVector4D.setY(xaxis.y());
theVector4D.setZ(xaxis.z());
theVector4D.setW(0.0);
rotation.setColumn(0,theVector4D);
theVector4D.setX(yaxis.x());
theVector4D.setY(yaxis.y());
theVector4D.setZ(yaxis.z());
theVector4D.setW(0.0);
rotation.setColumn(1,theVector4D);
theVector4D.setX(zaxis.x());
theVector4D.setY(zaxis.y());
theVector4D.setZ(zaxis.z());
theVector4D.setW(0.0);
rotation.setColumn(2,theVector4D);
return rotation * translation;
}
注:观看OpenGL中文官网(https://learnopengl-cn.github.io/)和阿西拜的现代OpenGL入门(https://ke.qq.com/course/3999604#term_id=104150693)学习OpenGL