Skip to content

Commit e9cffe8

Browse files
committed
change mouse control from ArcBall-like to orbiting-camera-like
1 parent 941911c commit e9cffe8

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
- non-trivial tests added
22
- add overloads of ::Quaternion
3+
- change mouse control from ArcBall-like to orbiting-camera-like
34

45
2.100 2025-01-16
56
- adjust MathGraph and Labels so Debian packaging is happy

lib/PDL/Graphics/TriD/ArcBall.pm

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,26 @@ sub get_z {
106106
cos($dist*PI/2);
107107
}
108108

109+
package PDL::Graphics::TriD::Orbiter;
110+
111+
use base qw/PDL::Graphics::TriD::QuaterController/;
112+
BEGIN { *PI = \&PDL::Graphics::TriD::QuaterController::PI; }
113+
114+
# we rotate about our space's "Z" because that's what we made vertical
115+
# this is different from the OpenGL Z axis which is towards the viewer
116+
sub mouse_moved {
117+
my($this,$x0,$y0,$x1,$y1) = @_;
118+
$this->SUPER::mouse_moved($x0,$y0,$x1,$y1);
119+
my ($dx, $dy) = ($x1-$x0, $y1-$y0);
120+
$dx /= $this->{W}/2; $dy /= $this->{H}/2; # scale to whole window not SC
121+
my ($x_rad, $y_rad) = map PI*$_, $dx, $dy;
122+
my $q_horiz = PDL::Graphics::TriD::Quaternion->new(cos $x_rad/2, 0, 0, sin $x_rad/2);
123+
my $q_vert = PDL::Graphics::TriD::Quaternion->new(cos $y_rad/2, sin $y_rad/2, 0, 0);
124+
if ($this->{Inv}) {
125+
$_->invert_rotation_this for $q_horiz, $q_vert;
126+
}
127+
$this->{Quat} .= $q_vert * $this->{Quat} * $q_horiz;
128+
1; # signals a refresh
129+
}
130+
109131
1;

lib/PDL/Graphics/TriD/Window.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ sub new_viewport {
6767
my $cont = $options->{Transformer} // PDL::Graphics::TriD::SimpleController->new;
6868
$vp->transformer($cont);
6969
if (ref($ev)){
70-
$ev->set_button(0,PDL::Graphics::TriD::ArcCone->new( $vp, 0, $cont->{WRotation}));
70+
$ev->set_button(0,PDL::Graphics::TriD::Orbiter->new( $vp, 0, $cont->{WRotation}));
7171
$ev->set_button(2,PDL::Graphics::TriD::SimpleScaler->new( $vp, \$cont->{CDistance}));
7272
$ev->set_button(3,PDL::Graphics::TriD::ScrollButtonScaler->new( $vp, \$cont->{CDistance}, 0.9));
7373
$ev->set_button(4,PDL::Graphics::TriD::ScrollButtonScaler->new( $vp, \$cont->{CDistance}, 1.1));

t/arcball.t

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,12 @@ is_qua $arcbowl->xy2qua(75,75), [0,0.598834,-0.598834,0.531784];
6262
mousemove $arcbowl, 50, 50, 50, 50, [1,0,0,0];
6363
mousemove $arcbowl, 50, 50, 25, 25, [0.531784,-0.598834,-0.598834,0];
6464

65+
my $orbiter = PDL::Graphics::TriD::Orbiter->new($win);
66+
isa_ok $orbiter, 'PDL::Graphics::TriD::Orbiter';
67+
$orbiter->set_wh(100,100);
68+
mousemove $orbiter, 50, 50, 50, 50, [1,0,0,0];
69+
mousemove $orbiter, 25, 25, 25, 25, [1,0,0,0];
70+
mousemove $orbiter, 50, 50, 25, 25, [0.5,-0.5,-0.5,-0.5];
71+
mousemove $orbiter, 25, 25, 0, 0, [0.5,-0.5,-0.5,-0.5];
72+
6573
done_testing;

0 commit comments

Comments
 (0)