如果我理解你的问题,首先定义一个 Vector3D 点
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; }
tVector3 operator+(tVector3 vVector) {return tVector3(vVector.x+x, vVector.y+y, vVector.z+z);}
tVector3 operator-(tVector3 vVector) {return tVector3(x-vVector.x, y-vVector.y, z-vVector.z);}
tVector3 operator*(float number) {return tVector3(x*number, y*number, z*number);}
tVector3 operator/(float number) {return tVector3(x/number, y/number, z/number);}
float x, y, z; // 3D vector coordinates
};
然后定义相机必须做什么
void CCamera::Move_Camera(float speed) //UP AND DOWN
{
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;
mPos.y = mPos.y + vVector.y * speed;
mView.y = mView.y + vVector.y * speed;
}
void CCamera::Rotate_View(float speed)
{
tVector3 vVector = mView - mPos; // Get the view vector
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);
}
void CCamera::Strafe_Camera(float speed,bool YAxis) // MOVE LEFT AND RIGHT
{
tVector3 vVector = mView - mPos; // Get the view vector
tVector3 vOrthoVector; // Orthogonal vector for the view vector
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;
}
在 Display()
gluLookAt(objCamera->mPos.x, objCamera->mPos.y, objCamera->mPos.z,
objCamera->mView.x, objCamera->mView.y, objCamera->mView.z,
objCamera->mUp.x, objCamera->mUp.y, objCamera->mUp.z);
其中 mPos 是相机位置(在 xyz 坐标中描述),mView 是您正在查看的点。ObjCamera 在 initlializeGL() 中创建;
希望能帮助到你。