Reputation: 31
I have 3D scene and in the scene multiple objects are present. I want to rotate all the objects in the scene around a selected object within the scene. I don't want the selected object to change its position on the screen, it should just rotate around its own axis and all the other objects should rotate around that axis only, like it happens in Revit application. I did by rotating all the objects in the scene about the selected point. Below is the pseudo code for this.
glm::vec3 axis = glm::vec3{0.0f, 1.0f, 0.0f};
float angle = glm::half_pi<float>();
glm::quat rotation = glm::angleAxis(angle, axis);
glm::vec3 rotation_point = glm::vec3{1.0f, 2.0f, 3.0f};
// Precompute common transformation matrix
glm::mat4 translate = glm::translate(glm::mat4{1.0f}, -rotation_point);
glm::mat4 untranslate = glm::translate(glm::mat4{1.0f}, rotation_point);
glm::mat4 rotate_around_center = untranslate * glm::mat4_cast(rotation) * translate;
// For each node
world_from_node_after = rotate_around_center * world_from_node_before;
But applying a rotation matrix to each object in the scene seems to be computationally intensive, so I want to tweak the camera matrix instead of applying rotation to each object. How should I change my camera matrix to achieve the desired results?
Upvotes: 0
Views: 68
Reputation: 31
I figured it out, instead of rotating each object about an axis, we can rotate the camera in opposite direction(negative angle of actual rotation) on the same axis and change it's view matrix only, so now we don't have to update the model matrix of each object.
Upvotes: 0