相对于gluLookAt中的查看方向移动相机

时间:2013-05-26 11:07:24

标签: java opengl jogl

将相机放在display内的3D立方体中

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

相机看着这一点 - (centerX, centerY, centerZ)

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

根据eye(对于上述每一方)移动相机,我应该在centercenter上进行的操作是什么?< / p>

修改

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

1 个答案:

答案 0 :(得分: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是摄像机位置(在x y z坐标中描述),mView是你正在看的点.ObjCamera是在initlializeGL()中创建的;

希望它有所帮助。