Saitei - 1 year ago 209
C++ Question

# Orbit camera (C++, GLM)

I'm trying to write the orbital camera (based on glm::quat) for my OpenGL application.
I have a few questions:

1. Сan I make ViewMatrix from RotationMatrix + position of camera?

camera_quat = glm::quat(glm::vec3(tmp_pitch, tmp_yaw, 0)) * camera_quat;

``````float pitch = camera_quat.pitch();
float yaw = camera_quat.yaw();

glm::mat4 rotate = glm::mat4_cast(camera_quat);

glm::vec3 view_direction(cos(yaw) * cos(pitch), sin(pitch), -sin(yaw) * cos(pitch));
camera_position = target - view_direction * radius;

glm::mat4 translate = glm::translate(camera_position);

glm::mat4 view_matrix = **???**;
``````

2. Is this line correct?:
`glm::vec3 view_direction(cos(yaw) * cos(pitch), sin(pitch), -sin(yaw) * cos(pitch));`

P.S. Sorry if my english is bad. It is not my native language, I am russian.
I hope you can help me. Thank you in advance!

If you change the `translate` matrix to

``````glm::mat4 translate = glm::translate(-camera_position);
``````

, it should be simply

``````glm::mat4 view_matrix = rotation * translation;
``````

However, there is an easier way to go there. What you basically want to do is the following: Move the camera to the target, rotate the camera there, move it a bit back. This can be expressed in matrix form with (note that the view matrix is the inverse model transform for the camera):

``````view_matrix = glm::translate(0, 0, -radius) * rotate * glm::translate(-target);
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download