سؤال

I want to move a matrix according to it's own space (direction) and not world space.

Specifically I want to move the view matrix/the "camera".

glm::mat4x4 view = glm::lookAt(glm::vec3(1.1f, 1.3f, 1.2f),
                                   glm::vec3(0.0f, 0.0f, 0.0f),
                                   glm::vec3(0.0f, 1.0f, 0.0f));

view=glm::rotate(view, r, glm::vec3(0,1,0));
view=glm::translate(view, glm::vec3(x,y,z));

The translation is equal disregarding it's rotation, but I'd like to translate according to where the camera is facing, e.g. when the camera is facing the x axis, and I translate z, it should translate along world axis x. How can this be done?

هل كانت مفيدة؟

المحلول

If you want to manipulate the camera position, you should store the camera position and only put it into view space when you need to.

glm::mat4 camera =  glm::inverse(glm::lookAt(
                               glm::vec3(1.1f, 1.3f, 1.2f),
                               glm::vec3(0.0f, 0.0f, 0.0f),
                               glm::vec3(0.0f, 1.0f, 0.0f)));
camera = glm::rotate(camera, r, glm::vec3(0,1,0));
camera = glm::translate(camera, glm::vec3(x,y,z));
glm::mat4 view = glm::inverse(camera);

It's also possible that doing pre-multiplcation of your view matrix is what you want, but I'm uncertain, since I try to avoid having to deal with pre vs post multiplication as if my life depended on it.

glm::mat4 view =  glm::lookAt(
                               glm::vec3(1.1f, 1.3f, 1.2f),
                               glm::vec3(0.0f, 0.0f, 0.0f),
                               glm::vec3(0.0f, 1.0f, 0.0f)));
view = glm::rotate(glm::mat4(), r, glm::vec3(0,1,0)) * view;
view = glm::translate(glm::mat4(), glm::vec3(x,y,z)) * view;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top