Very basic implementation of 3D preview - install Wx::GLCanvas to get it working

This commit is contained in:
Alessandro Ranellucci 2013-05-17 14:14:33 +02:00
parent 228c84ddc1
commit 5c74fd095b
7 changed files with 221 additions and 149 deletions

View File

@ -89,6 +89,7 @@ utils/post-processing/decimate.pl
utils/post-processing/flowrate.pl
utils/split_stl.pl
utils/stl-to-amf.pl
utils/view-mesh.pl
utils/zsh/functions/_slic3r
utils/zsh/README.markdown
var/add.png

View File

@ -10,11 +10,12 @@ use Slic3r::GUI::Plater;
use Slic3r::GUI::Plater::ObjectDialog;
use Slic3r::GUI::Preferences;
use Slic3r::GUI::OptionsGroup;
use Slic3r::GUI::PreviewCanvas;
use Slic3r::GUI::SkeinPanel;
use Slic3r::GUI::SimpleTab;
use Slic3r::GUI::Tab;
our $have_OpenGL = eval "use Slic3r::GUI::PreviewCanvas; 1";
use Wx 0.9901 qw(:bitmap :dialog :frame :icon :id :misc :systemsettings :toplevelwindow);
use Wx::Event qw(EVT_CLOSE EVT_MENU);
use base 'Wx::App';

View File

@ -10,11 +10,12 @@ use base 'Wx::Dialog';
sub new {
my $class = shift;
my ($parent, %params) = @_;
my $self = $class->SUPER::new($parent, -1, "Object", wxDefaultPosition, [500,350]);
my $self = $class->SUPER::new($parent, -1, "Object", wxDefaultPosition, [500,350], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
$self->{object} = $params{object};
$self->{tabpanel} = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
$self->{tabpanel}->AddPage($self->{preview} = Slic3r::GUI::Plater::ObjectDialog::PreviewTab->new($self->{tabpanel}, object => $self->{object}), "Preview");
$self->{tabpanel}->AddPage($self->{preview} = Slic3r::GUI::Plater::ObjectDialog::PreviewTab->new($self->{tabpanel}, object => $self->{object}), "Preview")
if $Slic3r::GUI::have_OpenGL;
$self->{tabpanel}->AddPage($self->{info} = Slic3r::GUI::Plater::ObjectDialog::InfoTab->new($self->{tabpanel}, object => $self->{object}), "Info");
$self->{tabpanel}->AddPage($self->{layers} = Slic3r::GUI::Plater::ObjectDialog::LayersTab->new($self->{tabpanel}, object => $self->{object}), "Layers");
@ -34,6 +35,7 @@ sub new {
$sizer->Add($buttons, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
$self->SetSizer($sizer);
$self->SetMinSize($self->GetSize);
return $self;
}
@ -95,7 +97,7 @@ sub new {
$self->{object} = $params{object};
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
$sizer->Add(Slic3r::GUI::PreviewCanvas::Cube->new($self, $self->{object}->get_model_object->mesh), 1, wxEXPAND, 0);
$sizer->Add(Slic3r::GUI::PreviewCanvas->new($self, $self->{object}->get_model_object->mesh), 1, wxEXPAND, 0);
$self->SetSizer($sizer);
$sizer->SetSizeHints($self);

View File

@ -1,90 +1,111 @@
package Slic3r::GUI::PreviewCanvas;
use strict;
use warnings;
use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_ERASE_BACKGROUND EVT_IDLE EVT_TIMER);
use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_ERASE_BACKGROUND EVT_IDLE EVT_TIMER EVT_MOUSEWHEEL);
# must load OpenGL *before* Wx::GLCanvas
use OpenGL qw(:glconstants :glfunctions);
use base qw(Wx::GLCanvas Class::Accessor::Fast);
use base qw(Wx::GLCanvas Class::Accessor);
use Slic3r::Geometry qw(X Y Z MIN MAX triangle_normal normalize deg2rad tan);
use Wx::GLCanvas qw(:all);
__PACKAGE__->mk_accessors( qw(timer x_rot y_rot dirty init mesh) );
__PACKAGE__->mk_accessors( qw(timer x_rot y_rot dirty init mesh_center zoom
verts norms) );
sub new {
my( $class, $parent, $mesh ) = @_;
my ($class, $parent, $mesh) = @_;
my $self = $class->SUPER::new($parent);
$self->mesh($mesh);
my $timer = $self->timer( Wx::Timer->new( $self ) );
$timer->Start( 50 );
$self->x_rot( 0 );
$self->y_rot( 0 );
EVT_PAINT( $self,
sub {
my $dc = Wx::PaintDC->new( $self );
$self->Render( $dc );
} );
EVT_SIZE( $self, sub { $self->dirty( 1 ) } );
EVT_IDLE( $self, sub {
return unless $self->dirty;
$self->Resize( $self->GetSizeWH );
$self->Refresh;
} );
EVT_TIMER( $self, -1, sub {
my( $self, $e ) = @_;
$self->x_rot( $self->x_rot - 1 );
$self->y_rot( $self->y_rot + 2 );
$self->dirty( 1 );
Wx::WakeUpIdle;
} );
# prepare mesh
{
$self->mesh_center($mesh->center);
$self->zoom(0.1);
my @verts = map $self->zoom * $_, map @{ $mesh->vertices->[$_] }, map @$_, @{$mesh->facets};
$self->verts(OpenGL::Array->new_list(GL_FLOAT, @verts));
my @norms = map { @$_, @$_, @$_ } map normalize(triangle_normal(map $mesh->vertices->[$_], @$_)), @{$mesh->facets};
$self->norms(OpenGL::Array->new_list(GL_FLOAT, @norms));
}
my $timer = $self->timer( Wx::Timer->new($self) );
$timer->Start(50);
$self->x_rot(0);
$self->y_rot(0);
EVT_PAINT($self, sub {
my $dc = Wx::PaintDC->new($self);
$self->Render($dc);
});
EVT_SIZE($self, sub { $self->dirty(1) });
EVT_IDLE($self, sub {
return unless $self->dirty;
return if !$self->IsShownOnScreen;
$self->Resize( $self->GetSizeWH );
$self->Refresh;
});
EVT_TIMER($self, -1, sub {
my ($self, $e) = @_;
$self->x_rot( $self->x_rot - 1 );
$self->y_rot( $self->y_rot + 2 );
$self->dirty(1);
Wx::WakeUpIdle;
});
EVT_MOUSEWHEEL($self, sub {
my ($self, $e) = @_;
my $zoom = $self->zoom * (1.0 - $e->GetWheelRotation() / $e->GetWheelDelta() / 10);
$zoom = 0.001 if $zoom < 0.001;
$zoom = 0.1 if $zoom > 0.1;
$self->zoom($zoom);
$self->Refresh;
});
return $self;
}
sub GetContext {
my( $self ) = @_;
if( Wx::wxVERSION >= 2.009 ) {
return $self->{context} ||= Wx::GLContext->new( $self );
my ($self) = @_;
if (Wx::wxVERSION >= 2.009) {
return $self->{context} ||= Wx::GLContext->new($self);
} else {
return $self->SUPER::GetContext;
}
}
sub SetCurrent {
my( $self, $context ) = @_;
if( Wx::wxVERSION >= 2.009 ) {
return $self->SUPER::SetCurrent( $context );
my ($self, $context) = @_;
if (Wx::wxVERSION >= 2.009) {
return $self->SUPER::SetCurrent($context);
} else {
return $self->SUPER::SetCurrent;
}
}
sub Resize {
my( $self, $x, $y ) = @_;
my ($self, $x, $y) = @_;
return unless $self->GetContext;
$self->dirty( 0 );
$self->dirty(0);
$self->SetCurrent( $self->GetContext );
glViewport( 0, 0, $x, $y );
$self->SetCurrent($self->GetContext);
glViewport(0, 0, $x, $y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
my_gluPerspective( 45, $x/$y, .5, 100 );
my_gluPerspective(45, $x/$y, .5, 100);
glMatrixMode(GL_MODELVIEW);
}
use Math::Trig;
sub my_gluPerspective {
my( $fov, $ratio, $near, $far ) = @_;
my ($fov, $ratio, $near, $far) = @_;
my $top = tan(deg2rad($fov)*0.5) * $near;
my $bottom = -$top;
@ -95,73 +116,10 @@ sub my_gluPerspective {
}
sub DESTROY {
my( $self ) = @_;
my $self = shift;
$self->timer->Stop;
$self->timer( undef );
}
package Slic3r::GUI::PreviewCanvas::Cube;
# must load OpenGL *before* Wx::GLCanvas
use OpenGL qw(:glconstants :glfunctions);
use base qw(Slic3r::GUI::PreviewCanvas);
use Slic3r::Geometry qw(X Y Z MIN MAX);
sub cube {
my( @v ) = ( [ 1, 1, 1 ], [ -1, 1, 1 ],
[ -1, -1, 1 ], [ 1, -1, 1 ],
[ 1, 1, -1 ], [ -1, 1, -1 ],
[ -1, -1, -1 ], [ 1, -1, -1 ] );
my( @c ) = ( [ 1, 1, 0 ], [ 1, 0, 1 ],
[ 0, 1, 1 ], [ 1, 1, 1 ],
[ 0, 0, 1 ], [ 0, 1, 0 ],
[ 1, 0, 1 ], [ 1, 1, 0 ] );
my( @s ) = ( [ 0, 1, 2, 3 ], [ 4, 5, 6, 7 ],
[ 0, 1, 5, 4 ], [ 2, 3, 7, 6 ],
[ 1, 2, 6, 5 ], [ 0, 3, 7, 4 ] );
for my $i ( 0 .. 5 ) {
my $s = $s[$i];
glBegin(GL_QUADS);
foreach my $j ( @$s ) {
glColor3f( @{$c[$j]} );
glVertex3f( @{$v[$j]} );
}
glEnd();
}
}
sub draw_mesh {
my $self = shift;
my $mesh = $self->mesh;
#glEnable(GL_CULL_FACE);
glEnableClientState(GL_VERTEX_ARRAY);
#glEnableClientState(GL_NORMAL_ARRAY);
my @verts = map 0.1 * $_, map @{ $mesh->vertices->[$_] }, map @$_, @{$mesh->facets};
my $verts = OpenGL::Array->new_list(GL_FLOAT, @verts);
#my @norms = map @$_, map {my $f = $_; Slic3r::Geometry::triangle_normal(map $mesh->vertices->[$_], @$f) } @{$mesh->facets};
#my $norms = OpenGL::Array->new_list(GL_FLOAT, @norms);
#my @inv_norms = map @$_, map {my $f = $_; Slic3r::Geometry::triangle_normal(reverse map $mesh->vertices->[$_], @$f) } @{$mesh->facets};
#my $inv_norms = OpenGL::Array->new_list(GL_FLOAT, @inv_norms);
glVertexPointer_p(3, $verts);
#glCullFace(GL_BACK);
#glNormalPointer_p($norms);
glDrawArrays(GL_TRIANGLES, 0, scalar @verts);
#glCullFace(GL_FRONT);
#glNormalPointer_p($inv_norms);
#glDrawArrays(GL_TRIANGLES, 0, scalar @verts);
#glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
$self->timer(undef);
}
sub InitGL {
@ -169,51 +127,51 @@ sub InitGL {
return if $self->init;
return unless $self->GetContext;
$self->init( 1 );
$self->mesh->align_to_origin;
glDisable( GL_LIGHTING );
glDepthFunc( GL_LESS );
glEnable( GL_DEPTH_TEST );
$self->init(1);
if (0) {
# Settings for our light.
my @Light_Ambient = ( 0.1, 0.1, 0.1, 1.0 );
my @Light_Diffuse = ( 1.2, 1.2, 1.2, 1.0 );
my @Light_Position = ( 2.0, 2.0, 0.0, 1.0 );
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
# Enables Smooth Color Shading; try GL_FLAT for (lack of) fun.
glShadeModel(GL_SMOOTH);
# Set up a light, turn it on.
glLightfv_p(GL_LIGHT1, GL_POSITION, @Light_Position);
glLightfv_p(GL_LIGHT1, GL_AMBIENT, @Light_Ambient);
glLightfv_p(GL_LIGHT1, GL_DIFFUSE, @Light_Diffuse);
glEnable(GL_LIGHT1);
# A handy trick -- have surface material mirror the color.
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
# Settings for our light.
my @LightPos = (0, 0, 2, 1.0);
my @LightAmbient = (0.1, 0.1, 0.1, 1.0);
my @LightDiffuse = (0.7, 0.5, 0.5, 1.0);
my @LightSpecular = (0.1, 0.1, 0.1, 0.1);
# Enables Smooth Color Shading; try GL_FLAT for (lack of) fun.
glShadeModel(GL_SMOOTH);
# Set up a light, turn it on.
glLightfv_p(GL_LIGHT1, GL_POSITION, @LightPos);
glLightfv_p(GL_LIGHT1, GL_AMBIENT, @LightAmbient);
glLightfv_p(GL_LIGHT1, GL_DIFFUSE, @LightDiffuse);
glLightfv_p(GL_LIGHT1, GL_SPECULAR, @LightSpecular);
glEnable(GL_LIGHT1);
# A handy trick -- have surface material mirror the color.
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
sub Render {
my( $self, $dc ) = @_;
my ($self, $dc) = @_;
return unless $self->GetContext;
$self->SetCurrent( $self->GetContext );
$self->SetCurrent($self->GetContext);
$self->InitGL;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef( 0, 0, -5 );
# this needs to get a lot better...
glRotatef( $self->x_rot, 1, 0, 0 );
glRotatef( $self->y_rot, 0, 0, 1 );
glTranslatef(map -$_ * $self->zoom, @{ $self->mesh_center });
#cube();
$self->draw_mesh;
glPopMatrix();
@ -222,4 +180,21 @@ sub Render {
$self->SwapBuffers();
}
1;
sub draw_mesh {
my $self = shift;
glEnable(GL_CULL_FACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer_p(3, $self->verts);
glCullFace(GL_BACK);
glNormalPointer_p($self->norms);
glDrawArrays(GL_TRIANGLES, 0, $self->verts->elements / 3);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
1;

View File

@ -7,7 +7,7 @@ our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
PI X Y Z A B X1 Y1 X2 Y2 MIN MAX epsilon slope line_atan lines_parallel
line_point_belongs_to_segment points_coincide distance_between_points
chained_path_items chained_path_points
chained_path_items chained_path_points normalize tan
line_length midpoint point_in_polygon point_in_segment segment_in_segment
point_is_on_left_of_segment polyline_lines polygon_lines nearest_point
point_along_segment polygon_segment_having_point polygon_has_subsegment
@ -45,6 +45,11 @@ sub scaled_epsilon () { epsilon / &Slic3r::SCALING_FACTOR }
sub scale ($) { $_[0] / &Slic3r::SCALING_FACTOR }
sub unscale ($) { $_[0] * &Slic3r::SCALING_FACTOR }
sub tan {
my ($angle) = @_;
return (sin $angle) / (cos $angle);
}
sub slope {
my ($line) = @_;
return undef if abs($line->[B][X] - $line->[A][X]) < epsilon; # line is vertical
@ -461,6 +466,14 @@ sub triangle_normal {
return normal($u, $v);
}
sub normalize {
my ($line) = @_;
my $len = sqrt( ($line->[X]**2) + ($line->[Y]**2) + ($line->[Z]**2) )
or return [0, 0, 0]; # to avoid illegal division by zero
return [ map $_ / $len, @$line ];
}
# 2D dot product
sub dot {
my ($u, $v) = @_;

View File

@ -365,6 +365,19 @@ sub align_to_origin {
$self->move(map -$extents[$_][MIN], X,Y,Z);
}
sub center_around_origin {
my $self = shift;
$self->move(map -$_, @{ $self->center });
}
sub center {
my $self = shift;
my @extents = $self->extents;
return [ map +($extents[$_][MAX] + $extents[$_][MIN])/2, X,Y,Z ];
}
sub duplicate {
my $self = shift;
my (@shifts) = @_;

67
utils/view-mesh.pl Normal file
View File

@ -0,0 +1,67 @@
#!/usr/bin/perl
# This script displays 3D preview of a mesh
use strict;
use warnings;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
}
use Getopt::Long qw(:config no_auto_abbrev);
use Slic3r;
use Slic3r::GUI;
$|++;
my %opt = ();
{
my %options = (
'help' => sub { usage() },
);
GetOptions(%options) or usage(1);
$ARGV[0] or usage(1);
}
{
my $model = Slic3r::Model->read_from_file($ARGV[0]);
$Slic3r::ViewMesh::mesh = $model->mesh;
my $app = Slic3r::ViewMesh->new;
$app->MainLoop;
}
sub usage {
my ($exit_code) = @_;
print <<"EOF";
Usage: view-mesh.pl [ OPTIONS ] file.stl
--help Output this usage screen and exit
EOF
exit ($exit_code || 0);
}
package Slic3r::ViewMesh;
use Wx qw(:sizer);
use base qw(Wx::App);
our $mesh;
sub OnInit {
my $self = shift;
my $frame = Wx::Frame->new(undef, -1, 'Mesh Viewer', [-1, -1], [500, 400]);
my $panel = Wx::Panel->new($frame, -1);
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
$sizer->Add(Slic3r::GUI::PreviewCanvas->new($panel, $mesh), 1, wxEXPAND, 0);
$panel->SetSizer($sizer);
$sizer->SetSizeHints($panel);
$frame->Show(1);
}
__END__