我在天空盒内编码相机,相机应该围绕模型旋转(模型在天空盒的中间向下(0;0;-1),尽管它可能会在未来发生变化)
我有 2 个角度 + 1 个距离:
float theta = m_ry; //m_ry & m_rz are variables incrementing themselves when user
float phi = m_rz; //click a button to rotate the camera
float dist = m_tra //m_tra is the distance from camera to the model
3 个向量:
glm::vec3 eye, center, up;
up = glm::vec3(0.0f, 1.0f, 0.0f);
center = glm::vec3(0.0f, 0.0f, -1.0f);
这是我设置相机坐标的方法:
eye.x = dist * cosf(theta) * cosf(phi);
eye.y = dist * cosf(theta) * sinf(phi);
eye.z = dist * sinf(theta) - 1; // -1 so the camera is in front of my model
然后我只做一个:
modelviewMatrix = glm::lookAt(eye, center, up);
glLoadMatrixf(glm::value_ptr(modelviewMatrix);
开始时旋转效果很好,但是当我达到 m_rz 角度 > PI/2 时,x 变为负数,并且视图完全相反:
|======| |======|
| | | |
|--* | becomes => | *--|
| | "--*" is my model | |
|______| |______|
有什么想法可以使 360° 旋转没有反向效果很好吗?