3

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Masahiro
  • 155
  • 9
  • 1
    I once read an article about Rolling Ball navigation using Quaternions found in the Graphics Gems. I tried to implement it. In debugging, this algorithm was numerically unstable and I discarded this eventually. Instead, I turn 2D movement of mouse (on screen) about 90° and transform that vector in the resp. 3D world to determine the rotation axis. The rotation angle is the length of mouse movement (in 2D) multiplied with a factor. (A move over complete view results in e.g. 360°.) With that params, I do a rotation about center using 4x4 matrix. – Scheff's Cat Sep 19 '19 at 05:21
  • A sample implementation of that can be found here: [NoGL3dDemo - RenderWidget.cc](https://github.com/scheff173/NoGL3dDemo/blob/master/RenderWidget.cc). – Scheff's Cat Sep 19 '19 at 05:26
  • Thanks for the comment. You mean we can calculate camera direction from mouse movement and it has no gimbal lock limitation? – Masahiro Sep 19 '19 at 05:41
  • There is no gimbal lock. The mouse movement rolls the 3d scene about defined orbit center and axis upright to mouse motion vector. Gimbal lock is ... not possible. – Scheff's Cat Sep 19 '19 at 05:43
  • The linked sample uses Qt for 2D GUI and output of rendered image, only. The rendering / navigation itself hasn't any other dependencies than `std` C++. The matrix math is included but very similar to `glm`. – Scheff's Cat Sep 19 '19 at 05:47
  • I see. It seems that using your suggestion way is better than quaternion. I will try it, thanks – Masahiro Sep 19 '19 at 06:01
  • You can make any type of camera you want using Euler Angles and glm::lookAt. This code comes pretty much unchanged from https://learnopengl.com. I have the impression you don't really understand it and just copied it. – Eric Sep 19 '19 at 07:26
  • 2
    @Eric Euller angles will always have singularities and related nasty bugs (most new games suffer's from)... Transform matrices have no such limitations if used properly... – Spektre Sep 19 '19 at 07:48
  • @Eric Yes, I refer to this website [learnopengl.com](https://learnopengl.com/). I thought it is difficult and troublesome when I implement viewer with Euler angles because it needs a lot of division into cases. So I thought quaternion is better. Is this wrong? – Masahiro Sep 19 '19 at 11:20
  • 1
    @Masahiro cumulative [Transform matrices](https://stackoverflow.com/a/28084380/2521214) are simplest ... you just need to [ensure orthogonality/normality once in a while](https://stackoverflow.com/a/42293434/2521214) exploiting cross product. Just look at the last links in the first linked QA for examples of camera and player control ... Quaternions are just more optimized way of computing rotation but has not much relevance on nowadays HW ... just like Bresenham vs. DDA in past was Bresenham a thing now its obsolete ... – Spektre Sep 19 '19 at 15:13

0 Answers0