-
Notifications
You must be signed in to change notification settings - Fork 0
/
transforms.rs
156 lines (132 loc) · 5.67 KB
/
transforms.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![allow(dead_code)]
use std::f32::consts::PI;
use winit::window::Window;
use cgmath::*;
#[rustfmt::skip]
#[allow(unused)]
pub const OPENGL_TO_WGPU_MATRIX: Matrix4<f32> = Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.5, 1.0,
);
pub struct InitWgpu {
pub surface: wgpu::Surface,
pub device: wgpu::Device,
pub queue: wgpu::Queue,
pub config: wgpu::SurfaceConfiguration,
pub size: winit::dpi::PhysicalSize<u32>,
}
impl InitWgpu {
pub async fn init_wgpu(window: &Window) -> Self {
let size = window.inner_size();
//let instance = wgpu::Instance::new(wgpu::Backends::all());
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::VULKAN,
..Default::default()
});
let surface = unsafe { instance.create_surface(window).unwrap() };
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: Some(&surface),
force_fallback_adapter:false,
})
.await
.unwrap();
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
},
None, // Trace path
)
.await
.unwrap();
let surface_cap = surface.get_capabilities(&adapter);
let surface_format = surface_cap.formats
.iter()
.copied()
.find(|f| f.is_srgb())
.unwrap_or(surface_cap.formats[0]);
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface_format,
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: surface_cap.alpha_modes[0],
view_formats: vec![]
};
surface.configure(&device, &config);
Self{
surface,
device,
queue,
config,
size,
}
}
}
pub fn create_view(camera_position: Point3<f32>, look_direction: Point3<f32>, up_direction: Vector3<f32>) -> Matrix4<f32> {
Matrix4::look_at_rh(camera_position, look_direction, up_direction)
}
pub fn create_projection(aspect:f32, is_perspective:bool) -> Matrix4<f32> {
let project_mat:Matrix4<f32>;
if is_perspective {
project_mat = OPENGL_TO_WGPU_MATRIX * perspective(Rad(2.0*PI/5.0), aspect, 0.1, 100.0);
} else {
project_mat = OPENGL_TO_WGPU_MATRIX * ortho(-4.0, 4.0, -3.0, 3.0, -1.0, 6.0);
}
project_mat
}
pub fn create_perspective_projection(fovy:Rad<f32>, aspect:f32, near: f32, far:f32) -> Matrix4<f32> {
OPENGL_TO_WGPU_MATRIX * perspective(fovy, aspect, near, far)
}
pub fn create_projection_ortho(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Matrix4<f32> {
OPENGL_TO_WGPU_MATRIX * ortho(left, right, bottom, top, near, far)
}
pub fn create_view_projection_ortho(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, camera_position: Point3<f32>,
look_direction: Point3<f32>, up_direction: Vector3<f32>) -> (Matrix4<f32>, Matrix4<f32>, Matrix4<f32>) {
// construct view matrix
let view_mat = Matrix4::look_at_rh(camera_position, look_direction, up_direction);
// construct projection matrix
let project_mat = OPENGL_TO_WGPU_MATRIX * ortho(left, right, bottom, top, near, far);
// contruct view-projection matrix
let view_project_mat = project_mat * view_mat;
// return various matrices
(view_mat, project_mat, view_project_mat)
}
pub fn create_view_projection(camera_position: Point3<f32>, look_direction: Point3<f32>, up_direction: Vector3<f32>,
aspect:f32, is_perspective:bool) -> (Matrix4<f32>, Matrix4<f32>, Matrix4<f32>) {
// construct view matrix
let view_mat = Matrix4::look_at_rh(camera_position, look_direction, up_direction);
// construct projection matrix
let project_mat:Matrix4<f32>;
if is_perspective {
project_mat = OPENGL_TO_WGPU_MATRIX * perspective(Rad(2.0*PI/5.0), aspect, 0.1, 100.0);
} else {
project_mat = OPENGL_TO_WGPU_MATRIX * ortho(-4.0, 4.0, -3.0, 3.0, -1.0, 6.0);
}
// contruct view-projection matrix
let view_project_mat = project_mat * view_mat;
// return various matrices
(view_mat, project_mat, view_project_mat)
}
pub fn create_transforms(translation:[f32; 3], rotation:[f32; 3], scaling:[f32; 3]) -> Matrix4<f32> {
// create transformation matrices
let trans_mat = Matrix4::from_translation(Vector3::new(translation[0], translation[1], translation[2]));
let rotate_mat_x = Matrix4::from_angle_x(Rad(rotation[0]));
let rotate_mat_y = Matrix4::from_angle_y(Rad(rotation[1]));
let rotate_mat_z = Matrix4::from_angle_z(Rad(rotation[2]));
let scale_mat = Matrix4::from_nonuniform_scale(scaling[0], scaling[1], scaling[2]);
// combine all transformation matrices together to form a final transform matrix: model matrix
let model_mat = trans_mat * rotate_mat_z * rotate_mat_y * rotate_mat_x * scale_mat;
// return final model matrix
model_mat
}
pub fn rotation_mat(rotation: [f32; 3]) -> cgmath::Quaternion<f32> {
cgmath::Quaternion::from_sv(1.0, rotation.into())
}