Guest User

Untitled

a guest
Nov 21st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. use std::ops;
  2.  
  3. struct Transform(Box<[f32;16]>);
  4.  
  5. impl ops::Mul for Transform {
  6. type Output = Self;
  7.  
  8. fn mul(&self, rhs: &Self) -> Self {
  9. let t = Transform(Box::new([0f32; 16]));
  10.  
  11. for row in 0..4 {
  12. for col in 0..4 {
  13. t.0[row * 4 + col] =
  14. self.0[row << 2 + col] * rhs.0[col * 4 + row] +
  15. self.0[row << 2 + col] * rhs.0[col * 4 + row] +
  16. self.0[row << 2 + col] * rhs.0[col * 4 + row] +
  17. self.0[row << 2 + col] * rhs.0[col * 4 + row];
  18. }
  19. }
  20.  
  21. t
  22. }
  23. }
  24.  
  25. impl Transform {
  26. fn rotation_x(deg: f32) -> Self {
  27. deg = deg.to_radians();
  28. let cos = deg.cos();
  29. let sin = deg.sin();
  30.  
  31. Transform(Box::new([
  32. 1, 0, 0, 0,
  33. 0, cos, sin, 0,
  34. 0, -sin, cos, 0,
  35. 0, 0, 0, 1
  36. ]))
  37. }
  38.  
  39. fn rotation_y(deg: f32) -> Transform {
  40. deg = deg.to_radians();
  41. let cos = deg.cos();
  42. let sin = deg.sin();
  43.  
  44. Transform(Box::new([
  45. cos, 0, -sin, 0,
  46. 0, 1, 0, 0,
  47. sin, 0, cos, 0,
  48. 0, 0, 0, 1
  49. ]))
  50. }
  51.  
  52. fn rotation_z(deg: f32) -> Transform {
  53. deg = deg.to_radians();
  54. let cos = deg.cos();
  55. let sin = deg.sin();
  56.  
  57. Transform(Box::new([
  58. cos, sin, 0, 0,
  59. -sin, cos, 0, 0,
  60. 0, 0, 1, 0,
  61. 0, 0, 0, 1
  62. ]))
  63. }
  64.  
  65. fn scale(x: f32, y: f32, z:f32) -> Transform {
  66. Transform(Box::new([
  67. x, 0, 0, 0,
  68. 0, y, 0, 0,
  69. 0, 0, z, 0,
  70. 0, 0, 0, 1
  71. ]))
  72. }
  73.  
  74. fn translate(x: f32, y: f32, z:f32) -> Transform {
  75. Transform(Box::new([
  76. 1, 0, 0, 0,
  77. 0, 1, 0, 0,
  78. 0, 0, 1, 0,
  79. x, y, z, 1
  80. ]))
  81. }
  82. }
  83.  
  84. fn main() {
  85. println!("{:?}", (Transform::scale(2, 2, 2) * Transform::translate(15, 62, 32)).0);
  86. }
Add Comment
Please, Sign In to add comment