Tagged: 

Viewing 2 reply threads
  • Author
    Posts
    • #6721 Score: 0
      hyt589
      Participant
      2 pts

      我能够正确的画出三角形并且可以旋转,但是视角似乎不对,三角形起初只在窗口左下角显示出了一半,我看代码感觉应该是在中间啊

      我的代码如下:

      
      Eigen::Matrix4f get_model_matrix(float rotation_angle)
      {
          Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
      
          // TODO: Implement this function
          // Create the model matrix for rotating the triangle around the Z axis.
          // Then return it.
          float rad = radian(rotation_angle);
          model(0,0) = std::cos(rad);
          model(0,1) = -std::sin(rad);
          model(1,0) = std::sin(rad);
          model(1,1) = std::cos(rad);
      
          return model;
      }
      
      Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
                                            float zNear, float zFar)
      {
          // Students will implement this function
      
          Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
      
          // TODO: Implement this function
          // Create the projection matrix for the given parameters.
          // Then return it.
      
          float n = -zNear;
          float f = -zFar;
          float t = std::abs(n) * std::tan(.5f * radian(eye_fov));
          float b = -t;
          float r = aspect_ratio * t;
          float l = -r;
          
      
          Eigen::Matrix4f perspective_to_orthogonal = Eigen::Matrix4f::Identity();
          perspective_to_orthogonal(0,0) = n;
          perspective_to_orthogonal(1,1) = n;
          perspective_to_orthogonal(2,2) = n + f;
          perspective_to_orthogonal(2,3) = -f * n;
          perspective_to_orthogonal(3,2) = 1;
          perspective_to_orthogonal(3,3) = 0;
      
          Eigen::Matrix4f move_to_center = Eigen::Matrix4f::Identity();
          move_to_center(0,3) = -(r-l)/2.f;
          move_to_center(1,3) = -(t-b)/2.f;
          move_to_center(2,3) = -(n-f)/2.f;
      
          Eigen::Matrix4f scale_to_canonical = Eigen::Matrix4f::Identity();
          scale_to_canonical(0,0) = 2.f/(r-l);
          scale_to_canonical(1,1) = 2.f/(t-b);
          scale_to_canonical(2,2) = 2.f/(n-f);
      
          Eigen::Matrix4f orthogonal_projection = scale_to_canonical * move_to_center;
      
          projection = orthogonal_projection * perspective_to_orthogonal;
      
          return projection;
      }
      
      • This topic was modified 3 years, 9 months ago by hyt589.
      Attachments:
      You must be logged in to view attached files.
    • #6790 Score: 0
      youthdou
      Participant

      move_to_center不需要

    • #7059 Score: 0
      zero
      Participant

      move_to_center的参数不正确,应该是

      move_to_center(0,3) = -(r+l)/2.f;
       move_to_center(1,3) = -(t+b)/2.f;
       move_to_center(2,3) = -(n+f)/2.f;
      • This reply was modified 3 years, 7 months ago by zero.
      • This reply was modified 3 years, 7 months ago by zero.
Viewing 2 reply threads
  • You must be logged in to reply to this topic.