I would like to make a 3D viewer which is controllable from mouse input with OpenGL.
The camera moves around the object using a polar coordinate system. I use Euler angle and glm::lookat
, this system works with some limitation. At this case, pitch and yaw should be changed from mouse inputs, however, we have to care about gimbal lock. So I make a limitation to the range of pitch.
This is a part of code
auto V = camera.getViewMatrix();
float xOffset = x - lastX_pos;
float yOffset = lastY_pos - y;
lastX_pos = x;
lastY_pos = y;
float sensitivity = 0.05;
xOffset *= sensitivity;
yOffset *= sensitivity;
yaw += xOffset;
pitch += yOffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 cameraPos;
cameraPos.x = distance * cos(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraPos.y = distance * sin(glm::radians(pitch));
cameraPos.z = distance * sin(glm::radians(yaw)) * cos(glm::radians(pitch));
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(cameraPos);
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));
glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);
V = glm::lookAt(cameraPos + sphereCenter, sphereCenter, cameraUp);
camera.setViewMatrix(V);
I would like to move the camera without any limitations like Meshlab viewer. So I think I have to use not Euler angle but quaternion and calculate view matrix without glm::lookat
.
If I did so, how I would correctly deal with mouse input and calculate camera position and direction from quaternion?