Proper way to handle camera rotations

前端 未结 3 1450
谎友^
谎友^ 2020-12-21 06:36

Let\'s start by considering 2 type of camera rotations:

Camera rotating around a point (Orbit):

def rotate_around_target(self, target, delta):
    ri         


        
3条回答
  •  时光取名叫无心
    2020-12-21 07:17

    So many ways to reinvent the wheel are there not? here is a neat option (adapted from the target camera concept in Opengl Development Cookbook, M.M.Movania, Chapter 2):

    1. Create the new orientation (rotation) matrix first (updated to use accumulated mouse deltas)

      # global variables somewhere appropriate (or class variables)
      mouseX = 0.0
      mouseY = 0.0
      def rotate_around_target(self, target, delta):
          global mouseX
          global mouseY
          mouseX += delta.x/5.0
          mouseY += delta.y/5.0
          glm::mat4 M = glm::mat4(1)
          M = glm::rotate(M, delta.z, glm::vec3(0, 0, 1))
          M = glm::rotate(M, mouseX , glm::vec3(0, 1, 0))
          M = glm::rotate(M, mouseY, glm::vec3(1, 0, 0))
      
    2. Use the distance to get a vector and then translate this vector by the current rotation matrix

          self.target = target
          float distance = glm::distance(self.target, self.eye)
          glm::vec3 T = glm::vec3(0, 0, distance)
          T = glm::vec3(M*glm::vec4(T, 0.0f))
      
    3. Get the new camera eye position by adding the translation vector to the target position

          self.eye = self.target + T
      
    4. Recalculate the orthonormal basis (of which you have just the UP vector to be done)

          # assuming self.original_up = glm::vec3(0, 1, 0)
          self.up = glm::vec3(M*glm::vec4(self.original_up, 0.0f))
          # or
          self.up = glm::vec3(M*glm::vec4(glm::vec3(0, 1, 0), 0.0f))
      

    5...and then you can try it out by updating a view matrix with a lookAt function

        self.view = glm.lookAt( self.eye, self.target, self.up)
    

    It's the simplest of concepts for these kinds of transform problems/solutions I have found to date. I tested it in C/C++ and just modified it to pyopengl syntax for you (faithfully I hope). Let us know how it goes (or not).

提交回复
热议问题