Advertisement
Guest User

rotation once

a guest
Sep 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.70 KB | None | 0 0
  1. %Notes: R=v*v'+cos(alpha)*(identity-v*v')+sin(alpha)*cross(v,v);
  2. %Where v is the vector we want to rotate around, not the camera axis itself
  3.  
  4. %World coordinate system.
  5. ey = [0 0 0]'; %Camera is at world origin.
  6. look_at = [0 0 -1]'; %Bunny is centered around 0,0,-1
  7. up = [0 1 0]'; %1 on the y axis is up. Makes sense
  8.  
  9. %Get the camera coordinate system.
  10. n = normc(ey-look_at); %z-axis
  11. u = cross(up', n)'; %x-axis
  12. v = cross(n,u); % y-axis
  13.  
  14. %convert degrees to radians
  15. alpha_deg = 25;
  16. alpha = (alpha_deg*pi)/180;
  17.  
  18. %Develop the view matrix to go from world to camera coordinate.
  19. %trans = [-(e'*u),-(e'*v),-(e'*n),1]';
  20. trans = [0,-1,-2,1]';
  21. left = vertcat(u',v',n',[0 0 0]);
  22. V = horzcat(left,trans);
  23.  
  24. %Find the new e aka camera position
  25. new_e = V*vertcat(ey,1);
  26. %Find the new look_at
  27. new_look_at = V*vertcat(look_at,1);
  28. %Find the new up
  29. new_up = V*vertcat(up,1);
  30.  
  31. %Trim off the fourth for now
  32. new_e = new_e(1:3);
  33. new_look_at = new_look_at(1:3);
  34. new_up = new_up(1:3);
  35.  
  36. %We now have our rotation matrix, rotating about v, our y-axis
  37. R=v*v'+cos(alpha)*(eye(3)-v*v')+sin(alpha)*cross(v,v);
  38.  
  39. %Calculate the new u,v,n.
  40. new_n = n'*R;
  41. new_u = u'*R; %Our new x-axis about y.
  42. new_v = v'*R;
  43.  
  44. %Calculate the rotated e,look_at, and up
  45. new_e = vertcat((new_e'*R)',0);
  46. new_look_at = vertcat((new_look_at'*R)',0);
  47. new_up = vertcat((new_up'*R)',0);
  48. %We need to translate back to the world for new_e,new_look_at, and new_up
  49. %Get the transpose of our V (same as inverse)
  50. V = inv(V);
  51.  
  52. %Convert back to world coordinates
  53. new_e = V*new_e;
  54. new_look_at = V*new_look_at;
  55. new_up = V*new_up;
  56.  
  57. %Chop off the fourth for the final results
  58. new_e = new_e(1:3)'
  59. new_look_at = new_look_at(1:3)'
  60. new_up = new_up(1:3)'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement