Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops;
- struct Transform(Box<[f32;16]>);
- impl ops::Mul for Transform {
- type Output = Self;
- fn mul(&self, rhs: &Self) -> Self {
- let t = Transform(Box::new([0f32; 16]));
- for row in 0..4 {
- for col in 0..4 {
- t.0[row * 4 + col] =
- self.0[row << 2 + col] * rhs.0[col * 4 + row] +
- self.0[row << 2 + col] * rhs.0[col * 4 + row] +
- self.0[row << 2 + col] * rhs.0[col * 4 + row] +
- self.0[row << 2 + col] * rhs.0[col * 4 + row];
- }
- }
- t
- }
- }
- impl Transform {
- fn rotation_x(deg: f32) -> Self {
- deg = deg.to_radians();
- let cos = deg.cos();
- let sin = deg.sin();
- Transform(Box::new([
- 1, 0, 0, 0,
- 0, cos, sin, 0,
- 0, -sin, cos, 0,
- 0, 0, 0, 1
- ]))
- }
- fn rotation_y(deg: f32) -> Transform {
- deg = deg.to_radians();
- let cos = deg.cos();
- let sin = deg.sin();
- Transform(Box::new([
- cos, 0, -sin, 0,
- 0, 1, 0, 0,
- sin, 0, cos, 0,
- 0, 0, 0, 1
- ]))
- }
- fn rotation_z(deg: f32) -> Transform {
- deg = deg.to_radians();
- let cos = deg.cos();
- let sin = deg.sin();
- Transform(Box::new([
- cos, sin, 0, 0,
- -sin, cos, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1
- ]))
- }
- fn scale(x: f32, y: f32, z:f32) -> Transform {
- Transform(Box::new([
- x, 0, 0, 0,
- 0, y, 0, 0,
- 0, 0, z, 0,
- 0, 0, 0, 1
- ]))
- }
- fn translate(x: f32, y: f32, z:f32) -> Transform {
- Transform(Box::new([
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- x, y, z, 1
- ]))
- }
- }
- fn main() {
- println!("{:?}", (Transform::scale(2, 2, 2) * Transform::translate(15, 62, 32)).0);
- }
Add Comment
Please, Sign In to add comment