From 6668a81c644f00ec469d21dbd3668583fc7f5cd2 Mon Sep 17 00:00:00 2001 From: Guillaume Seguin Date: Sat, 29 Jun 2013 18:31:06 +0200 Subject: [PATCH] Start work on rotation handling --- lib/Slic3r/GUI/PreviewCanvas.pm | 42 +++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/Slic3r/GUI/PreviewCanvas.pm b/lib/Slic3r/GUI/PreviewCanvas.pm index 92a3770b..73eb6513 100644 --- a/lib/Slic3r/GUI/PreviewCanvas.pm +++ b/lib/Slic3r/GUI/PreviewCanvas.pm @@ -15,7 +15,9 @@ __PACKAGE__->mk_accessors( qw(quat dirty init mesh_center sub new { my ($class, $parent, $mesh) = @_; my $self = $class->SUPER::new($parent); - + + $self->quat((0, 0, 0, 1)); + # prepare mesh { $self->mesh_center($mesh->center); @@ -27,9 +29,6 @@ sub new { $self->norms(OpenGL::Array->new_list(GL_FLOAT, @norms)); } - $self->x_rot(0); - $self->y_rot(0); - EVT_PAINT($self, sub { my $dc = Wx::PaintDC->new($self); $self->Render($dc); @@ -68,6 +67,38 @@ sub new { return $self; } +sub trackball { + return (0, 0, 0, 1); +} + +sub mulquat { + my ($self, @q1, @rq) = @_; + return ($q1[3] * $rq[0] + $q1[0] * $rq[3] + $q1[1] * $rq[2] - $q1[2] * $rq[1], + $q1[3] * $rq[1] + $q1[1] * $rq[3] + $q1[2] * $rq[0] - $q1[0] * $rq[2], + $q1[3] * $rq[2] + $q1[2] * $rq[3] + $q1[0] * $rq[1] - $q1[1] * $rq[0], + $q1[3] * $rq[3] - $q1[0] * $rq[0] - $q1[1] * $rq[1] - $q1[2] * $rq[2]) +} + +sub handle_rotation { + my ($self, $e) = @_; + + if (not defined $self->initpos) { + $self->initpos($e->GetPosition()); + } else { + my ($orig, $new, $size, @quat); + $orig = $self->initpos; + $new = $e->GetPosition(); + $size = $self->GetClientSize(); + @quat = $self->trackball($orig->x / ($size->width / 2) - 1, + 1 - $orig->y / ($size->height / 2), + $new->x / ($size->width / 2) - 1, + 1 - $new->y / ($size->height / 2)); + $self->quat($self->mulquat($self->quat, @quat)); + $self->initpos($new); + $self->Refresh; + } +} + sub handle_translation { my ($self, $e) = @_; @@ -203,9 +234,6 @@ sub Render { glPushMatrix(); - # this needs to get a lot better... - glRotatef( $self->x_rot, 1, 0, 0 ); - glRotatef( $self->y_rot, 0, 0, 1 ); glTranslatef(map -$_, @{ $self->mesh_center }); $self->draw_mesh;