0

有一个位于 3D 立方体内的相机display

public void display(GLAutoDrawable drawable) {
    ...
    glu.gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
    ...
}

相机看点—— (centerX, centerY, centerZ)

我想相对于它的观察方向(向前、向后、向右、向左)移动它。

为了根据(对于上述每个方面)移动相机,我应该在eye's 和's 上进行什么操作?centercenter

编辑:

我不需要实现,只需要概念。

4

1 回答 1

0

如果我理解你的问题,首先定义一个 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() 中创建;

希望能帮助到你。

于 2013-05-26T11:19:01.910 回答