Saitei - 2 months ago 28

C++ Question

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

I have a few questions:

- С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 = **???**;

- 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!

Answer

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);
```

Source (Stackoverflow)

Comments