summaryrefslogtreecommitdiffstats
path: root/examples/custom_shader/src/scene/camera.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-19 20:41:52 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-19 20:41:52 +0100
commit1781068e1c3a65551db1e832fdbaddba99124051 (patch)
tree60e0b3854cc0541712572fbb0e56f14435951ea9 /examples/custom_shader/src/scene/camera.rs
parent41dec5bd203ff5b1574a33a17d5f7358ae1beea2 (diff)
parent7ae7fcb89855002519bab752fd3686106ce448db (diff)
downloadiced-1781068e1c3a65551db1e832fdbaddba99124051.tar.gz
iced-1781068e1c3a65551db1e832fdbaddba99124051.tar.bz2
iced-1781068e1c3a65551db1e832fdbaddba99124051.zip
Merge branch 'master' into remove-vertex-indexing
Diffstat (limited to 'examples/custom_shader/src/scene/camera.rs')
-rw-r--r--examples/custom_shader/src/scene/camera.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/examples/custom_shader/src/scene/camera.rs b/examples/custom_shader/src/scene/camera.rs
new file mode 100644
index 00000000..2a49c102
--- /dev/null
+++ b/examples/custom_shader/src/scene/camera.rs
@@ -0,0 +1,53 @@
+use glam::{mat4, vec3, vec4};
+use iced::Rectangle;
+
+#[derive(Copy, Clone)]
+pub struct Camera {
+ eye: glam::Vec3,
+ target: glam::Vec3,
+ up: glam::Vec3,
+ fov_y: f32,
+ near: f32,
+ far: f32,
+}
+
+impl Default for Camera {
+ fn default() -> Self {
+ Self {
+ eye: vec3(0.0, 2.0, 3.0),
+ target: glam::Vec3::ZERO,
+ up: glam::Vec3::Y,
+ fov_y: 45.0,
+ near: 0.1,
+ far: 100.0,
+ }
+ }
+}
+
+pub const OPENGL_TO_WGPU_MATRIX: glam::Mat4 = mat4(
+ vec4(1.0, 0.0, 0.0, 0.0),
+ vec4(0.0, 1.0, 0.0, 0.0),
+ vec4(0.0, 0.0, 0.5, 0.0),
+ vec4(0.0, 0.0, 0.5, 1.0),
+);
+
+impl Camera {
+ pub fn build_view_proj_matrix(&self, bounds: Rectangle) -> glam::Mat4 {
+ //TODO looks distorted without padding; base on surface texture size instead?
+ let aspect_ratio = bounds.width / (bounds.height + 150.0);
+
+ let view = glam::Mat4::look_at_rh(self.eye, self.target, self.up);
+ let proj = glam::Mat4::perspective_rh(
+ self.fov_y,
+ aspect_ratio,
+ self.near,
+ self.far,
+ );
+
+ OPENGL_TO_WGPU_MATRIX * proj * view
+ }
+
+ pub fn position(&self) -> glam::Vec4 {
+ glam::Vec4::from((self.eye, 0.0))
+ }
+}