Skip to content

Commit

Permalink
Add auto rotate settings
Browse files Browse the repository at this point in the history
  • Loading branch information
caspark committed Apr 8, 2024
1 parent f9b2456 commit c84bd56
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ pub struct RenderConfig {
width: usize,
height: usize,
model: PathBuf,
auto_rotate_model_speed: f32,
auto_rotate_model_angle: f32,
light_dir: Vec3,
auto_rotate_light_speed: f32,
auto_rotate_light_angle: f32,
camera_distance: f32,
camera_look_from: Vec3,
camera_look_at: Vec3,
Expand All @@ -43,6 +47,11 @@ impl RenderConfig {
self.width * self.height
}

pub(crate) fn always_re_render(&self) -> bool {
self.auto_rotate_light_speed > 0.0
|| self.auto_rotate_model_speed > 0.0
}

pub(crate) fn validate(&self) -> Result<RenderInput> {
if self.width < 200 {
bail!("Width must be 200 or greater");
Expand Down Expand Up @@ -97,7 +106,11 @@ impl Default for RenderConfig {
width: 1000,
height: 1000,
model: PathBuf::from("assets/head.obj"),
auto_rotate_model_speed: 0.1,
auto_rotate_model_angle: 0.0,
light_dir: Vec3::new(0.0, 0.0, 1.0),
auto_rotate_light_speed: 0.1,
auto_rotate_light_angle: 0.0,
camera_distance: 3.0,
camera_look_from: Vec3::new(0.0, 0.0, 3.0),
camera_look_at: Vec3::ZERO,
Expand Down
20 changes: 20 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ impl epi::App for RendererApp {
);
ui.end_row();

ui.add(egui::Slider::new(&mut self.config.auto_rotate_model_speed, 0.0..=3.0).text("Auto-rotate model"));
if self.config.auto_rotate_model_angle > 0.0 {
self.config.auto_rotate_model_angle += self.config.auto_rotate_model_speed * std::f32::consts::FRAC_PI_4 / 60.0;
let rotate = glam::Quat::from_rotation_y(self.config.auto_rotate_model_angle);
self.config.camera_look_from = rotate * Vec3::new(0.0, 0.0, 3.0);
}
ui.end_row();

let light_dir_before = self.config.light_dir;
vec3_editor(ui, "Light Dir", &mut self.config.light_dir);
if light_dir_before != self.config.light_dir {
Expand All @@ -278,6 +286,14 @@ impl epi::App for RendererApp {
}
ui.end_row();

ui.add(egui::Slider::new(&mut self.config.auto_rotate_light_speed, 0.0..=3.0).text("Auto-rotate light"));
if self.config.auto_rotate_light_speed > 0.0 {
self.config.auto_rotate_light_angle += self.config.auto_rotate_light_speed * std::f32::consts::FRAC_PI_4 / 60.0;
let rotate = glam::Quat::from_rotation_z(self.config.auto_rotate_light_angle);
self.config.light_dir = rotate * Vec3::new(0.0, 1.0, 2.0);
}
ui.end_row();

vec3_editor(ui, "Camera look from", &mut self.config.camera_look_from);
ui.end_row();

Expand Down Expand Up @@ -428,6 +444,10 @@ impl epi::App for RendererApp {
});
}
});

if self.config.always_re_render() {
ctx.request_repaint();
}
}
}

Expand Down

0 comments on commit c84bd56

Please sign in to comment.