Skip to content

Commit

Permalink
Merge pull request #29 from bwasty/panning
Browse files Browse the repository at this point in the history
Fix panning and high DPI display handling
  • Loading branch information
bwasty authored Mar 16, 2018
2 parents f74fb58 + a55f1da commit 198581f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [0.3.1] - 2018-03-16
### Changed
* update `gltf` crate (0.9.3 -> 0.10.1)
- partially fixes sparse accessor handling (-> https://github.com/gltf-rs/gltf/pull/148, https://github.com/gltf-rs/gltf/issues/154)

### Fixed
* Decode images from data URIs (fully support embedded glTF files, #26)
* Fix non-headless screenshots on high DPI displays (#23)
* Fix panning direction (#25)

## [0.3.0] - 2018-02-23
### Added
* re-add keyboard controls - `WASD` and cursor keys
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ collision = "0.14.0"
# futures = "0.1.14"
# futures-cpupool = "0.1.5"
gl = "0.10.0"
glutin = "0.12.0"
glutin = "0.12.0" # NOTE: check macOS workaround in viewer.rs when upgrading
image = "0.18.0"
# reqwest = "0.7.3"
log = "0.4.1"
Expand Down
8 changes: 4 additions & 4 deletions src/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ impl OrbitControls {
target_distance *= (self.camera.fovy / 2.0).tan() * PI / 180.0;

// we actually don't use screen_width, since perspective camera is fixed to screen height
let distance = 2.0 * delta.x * target_distance / self.screen_height;
self.pan_left(distance);
let distance = 2.0 * delta.y * target_distance / self.screen_height;
self.pan_up(distance);
let distance = 50.0 * delta.x * target_distance / self.screen_height;
self.pan_left(-distance);
let distance = 50.0 * delta.y * target_distance / self.screen_height;
self.pan_up(-distance);
} else {
// TODO!: orthographic camera pan
warn!("unimplemented: orthographic camera pan")
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ pub fn main() {
warn!("filename should end with .png");
}
if count > 1 {
viewer.multiscreenshot(filename, width, height, count)
viewer.multiscreenshot(filename, count)
} else {
viewer.screenshot(filename, width, height)
viewer.screenshot(filename)
}
return;
}
Expand Down
24 changes: 15 additions & 9 deletions src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,15 @@ impl GltfViewer {
let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();

// Real dimensions might be much higher on High-DPI displays
let (real_width, real_height) = gl_window.get_inner_size().unwrap();
let (mut real_width, mut real_height) = gl_window.get_inner_size().unwrap();

// TODO!!: workaround for https://github.com/tomaka/winit/issues/399
#[cfg(target_os = "macos")]
{
let factor = gl_window.hidpi_factor();
real_width = (real_width as f32 * factor) as u32;
real_height = (real_height as f32 * factor) as u32;
}

unsafe { gl_window.make_current().unwrap(); }

Expand Down Expand Up @@ -314,14 +322,14 @@ impl GltfViewer {
}
}

pub fn screenshot(&mut self, filename: &str, width: u32, height: u32) {
pub fn screenshot(&mut self, filename: &str) {
self.draw();

let mut img = DynamicImage::new_rgba8(width, height);
let mut img = DynamicImage::new_rgba8(self.width, self.height);
unsafe {
let pixels = img.as_mut_rgba8().unwrap();
gl::PixelStorei(gl::PACK_ALIGNMENT, 1);
gl::ReadPixels(0, 0, width as i32, height as i32, gl::RGBA,
gl::ReadPixels(0, 0, self.width as i32, self.height as i32, gl::RGBA,
gl::UNSIGNED_BYTE, pixels.as_mut_ptr() as *mut c_void);
gl_check_error!();
}
Expand All @@ -333,10 +341,10 @@ impl GltfViewer {
error!("{}", err);
}
else {
println!("Saved {}x{} screenshot to {}", width, height, filename);
println!("Saved {}x{} screenshot to {}", self.width, self.height, filename);
}
}
pub fn multiscreenshot(&mut self, filename: &str, width: u32, height: u32, count: u32) {
pub fn multiscreenshot(&mut self, filename: &str, count: u32) {
let min_angle : f32 = 0.0 ;
let max_angle : f32 = 2.0 * PI ;
let increment_angle : f32 = ((max_angle - min_angle)/(count as f32)) as f32;
Expand All @@ -345,7 +353,7 @@ impl GltfViewer {
let dot = filename.rfind('.').unwrap_or_else(|| filename.len());
let mut actual_name = filename.to_string();
actual_name.insert_str(dot, &format!("_{}", i));
self.screenshot(&actual_name[..], width,height);
self.screenshot(&actual_name[..]);
}
}
}
Expand Down Expand Up @@ -373,8 +381,6 @@ fn process_events(
orbit_controls.camera.update_aspect_ratio(w / h);
orbit_controls.screen_width = w;
orbit_controls.screen_height = h;

trace!("Resized to {}x{}", w, h);
},
WindowEvent::DroppedFile(_path_buf) => (), // TODO: drag file in
WindowEvent::MouseInput { button, state: Pressed, ..} => {
Expand Down

0 comments on commit 198581f

Please sign in to comment.