3DScene.pm 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. package Slic3r::GUI::3DScene::Base;
  2. use strict;
  3. use warnings;
  4. use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_ERASE_BACKGROUND EVT_IDLE EVT_MOUSEWHEEL EVT_MOUSE_EVENTS);
  5. # must load OpenGL *before* Wx::GLCanvas
  6. use OpenGL qw(:glconstants :glfunctions :glufunctions :gluconstants);
  7. use base qw(Wx::GLCanvas Class::Accessor);
  8. use Math::Trig qw(asin);
  9. use List::Util qw(reduce min max first);
  10. use Slic3r::Geometry qw(X Y Z MIN MAX triangle_normal normalize deg2rad tan scale unscale scaled_epsilon);
  11. use Slic3r::Geometry::Clipper qw(offset_ex intersection_pl);
  12. use Wx::GLCanvas qw(:all);
  13. __PACKAGE__->mk_accessors( qw(_quat _dirty init
  14. enable_cutting
  15. enable_picking
  16. enable_moving
  17. on_viewport_changed
  18. on_hover
  19. on_select
  20. on_double_click
  21. on_right_click
  22. on_move
  23. volumes
  24. _sphi _stheta
  25. cutting_plane_z
  26. cut_lines_vertices
  27. bed_shape
  28. bed_triangles
  29. bed_grid_lines
  30. background
  31. origin
  32. _mouse_pos
  33. _hover_volume_idx
  34. _drag_volume_idx
  35. _drag_start_pos
  36. _drag_start_xy
  37. _dragged
  38. _camera_target
  39. _zoom
  40. ) );
  41. use constant TRACKBALLSIZE => 0.8;
  42. use constant TURNTABLE_MODE => 1;
  43. use constant GROUND_Z => -0.02;
  44. use constant DEFAULT_COLOR => [1,1,0];
  45. use constant SELECTED_COLOR => [0,1,0,1];
  46. use constant HOVER_COLOR => [0.4,0.9,0,1];
  47. # make OpenGL::Array thread-safe
  48. {
  49. no warnings 'redefine';
  50. *OpenGL::Array::CLONE_SKIP = sub { 1 };
  51. }
  52. sub new {
  53. my ($class, $parent) = @_;
  54. # we request a depth buffer explicitely because it looks like it's not created by
  55. # default on Linux, causing transparency issues
  56. my $self = $class->SUPER::new($parent, -1, Wx::wxDefaultPosition, Wx::wxDefaultSize, 0, "",
  57. [WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0]);
  58. $self->background(1);
  59. $self->_quat((0, 0, 0, 1));
  60. $self->_stheta(45);
  61. $self->_sphi(45);
  62. $self->_zoom(1);
  63. # 3D point in model space
  64. $self->_camera_target(Slic3r::Pointf3->new(0,0,0));
  65. $self->reset_objects;
  66. EVT_PAINT($self, sub {
  67. my $dc = Wx::PaintDC->new($self);
  68. $self->Render($dc);
  69. });
  70. EVT_SIZE($self, sub { $self->_dirty(1) });
  71. EVT_IDLE($self, sub {
  72. return unless $self->_dirty;
  73. return if !$self->IsShownOnScreen;
  74. $self->Resize( $self->GetSizeWH );
  75. $self->Refresh;
  76. });
  77. EVT_MOUSEWHEEL($self, sub {
  78. my ($self, $e) = @_;
  79. # Calculate the zoom delta and apply it to the current zoom factor
  80. my $zoom = $e->GetWheelRotation() / $e->GetWheelDelta();
  81. $zoom = max(min($zoom, 4), -4);
  82. $zoom /= 10;
  83. $self->_zoom($self->_zoom / (1-$zoom));
  84. # In order to zoom around the mouse point we need to translate
  85. # the camera target
  86. my $size = Slic3r::Pointf->new($self->GetSizeWH);
  87. my $pos = Slic3r::Pointf->new($e->GetX, $size->y - $e->GetY); #-
  88. $self->_camera_target->translate(
  89. # ($pos - $size/2) represents the vector from the viewport center
  90. # to the mouse point. By multiplying it by $zoom we get the new,
  91. # transformed, length of such vector.
  92. # Since we want that point to stay fixed, we move our camera target
  93. # in the opposite direction by the delta of the length of such vector
  94. # ($zoom - 1). We then scale everything by 1/$self->_zoom since
  95. # $self->_camera_target is expressed in terms of model units.
  96. -($pos->x - $size->x/2) * ($zoom) / $self->_zoom,
  97. -($pos->y - $size->y/2) * ($zoom) / $self->_zoom,
  98. 0,
  99. ) if 0;
  100. $self->on_viewport_changed->() if $self->on_viewport_changed;
  101. $self->_dirty(1);
  102. $self->Refresh;
  103. });
  104. EVT_MOUSE_EVENTS($self, \&mouse_event);
  105. return $self;
  106. }
  107. sub mouse_event {
  108. my ($self, $e) = @_;
  109. my $pos = Slic3r::Pointf->new($e->GetPositionXY);
  110. if ($e->Entering && &Wx::wxMSW) {
  111. # wxMSW needs focus in order to catch mouse wheel events
  112. $self->SetFocus;
  113. } elsif ($e->LeftDClick) {
  114. $self->on_double_click->()
  115. if $self->on_double_click;
  116. } elsif ($e->LeftDown || $e->RightDown) {
  117. # If user pressed left or right button we first check whether this happened
  118. # on a volume or not.
  119. my $volume_idx = $self->_hover_volume_idx // -1;
  120. # select volume in this 3D canvas
  121. if ($self->enable_picking) {
  122. $self->deselect_volumes;
  123. $self->select_volume($volume_idx);
  124. if ($volume_idx != -1) {
  125. my $group_id = $self->volumes->[$volume_idx]->select_group_id;
  126. my @volumes;
  127. if ($group_id != -1) {
  128. $self->select_volume($_)
  129. for grep $self->volumes->[$_]->select_group_id == $group_id,
  130. 0..$#{$self->volumes};
  131. }
  132. }
  133. $self->Refresh;
  134. }
  135. # propagate event through callback
  136. $self->on_select->($volume_idx)
  137. if $self->on_select;
  138. if ($volume_idx != -1) {
  139. if ($e->LeftDown && $self->enable_moving) {
  140. $self->_drag_volume_idx($volume_idx);
  141. $self->_drag_start_pos($self->mouse_to_3d(@$pos));
  142. } elsif ($e->RightDown) {
  143. # if right clicking on volume, propagate event through callback
  144. $self->on_right_click->($e->GetPosition)
  145. if $self->on_right_click;
  146. }
  147. }
  148. } elsif ($e->Dragging && $e->LeftIsDown && defined($self->_drag_volume_idx)) {
  149. # get new position at the same Z of the initial click point
  150. my $mouse_ray = $self->mouse_ray($e->GetX, $e->GetY);
  151. my $cur_pos = $mouse_ray->intersect_plane($self->_drag_start_pos->z);
  152. # calculate the translation vector
  153. my $vector = $self->_drag_start_pos->vector_to($cur_pos);
  154. # get volume being dragged
  155. my $volume = $self->volumes->[$self->_drag_volume_idx];
  156. # get all volumes belonging to the same group, if any
  157. my @volumes;
  158. if ($volume->drag_group_id == -1) {
  159. @volumes = ($volume);
  160. } else {
  161. @volumes = grep $_->drag_group_id == $volume->drag_group_id, @{$self->volumes};
  162. }
  163. # apply new temporary volume origin and ignore Z
  164. $_->origin->translate($vector->x, $vector->y, 0) for @volumes; #,,
  165. $self->_drag_start_pos($cur_pos);
  166. $self->_dragged(1);
  167. $self->Refresh;
  168. } elsif ($e->Dragging) {
  169. if ($e->LeftIsDown) {
  170. # if dragging over blank area with left button, rotate
  171. if (defined $self->_drag_start_pos) {
  172. my $orig = $self->_drag_start_pos;
  173. if (TURNTABLE_MODE) {
  174. $self->_sphi($self->_sphi + ($pos->x - $orig->x) * TRACKBALLSIZE);
  175. $self->_stheta($self->_stheta - ($pos->y - $orig->y) * TRACKBALLSIZE); #-
  176. $self->_stheta(150) if $self->_stheta > 150;
  177. $self->_stheta(0) if $self->_stheta < 0;
  178. } else {
  179. my $size = $self->GetClientSize;
  180. my @quat = trackball(
  181. $orig->x / ($size->width / 2) - 1,
  182. 1 - $orig->y / ($size->height / 2), #/
  183. $pos->x / ($size->width / 2) - 1,
  184. 1 - $pos->y / ($size->height / 2), #/
  185. );
  186. $self->_quat(mulquats($self->_quat, \@quat));
  187. }
  188. $self->on_viewport_changed->() if $self->on_viewport_changed;
  189. $self->Refresh;
  190. }
  191. $self->_drag_start_pos($pos);
  192. } elsif ($e->MiddleIsDown || $e->RightIsDown) {
  193. # if dragging over blank area with right button, translate
  194. if (defined $self->_drag_start_xy) {
  195. # get point in model space at Z = 0
  196. my $cur_pos = $self->mouse_ray($e->GetX, $e->GetY)->intersect_plane(0);
  197. my $orig = $self->mouse_ray(@{$self->_drag_start_xy})->intersect_plane(0);
  198. $self->_camera_target->translate(
  199. @{$orig->vector_to($cur_pos)->negative},
  200. );
  201. $self->on_viewport_changed->() if $self->on_viewport_changed;
  202. $self->Refresh;
  203. }
  204. $self->_drag_start_xy($pos);
  205. }
  206. } elsif ($e->LeftUp || $e->MiddleUp || $e->RightUp) {
  207. if ($self->on_move && defined($self->_drag_volume_idx) && $self->_dragged) {
  208. # get all volumes belonging to the same group, if any
  209. my @volume_idxs;
  210. my $group_id = $self->volumes->[$self->_drag_volume_idx]->drag_group_id;
  211. if ($group_id == -1) {
  212. @volume_idxs = ($self->_drag_volume_idx);
  213. } else {
  214. @volume_idxs = grep $self->volumes->[$_]->drag_group_id == $group_id,
  215. 0..$#{$self->volumes};
  216. }
  217. $self->on_move->(@volume_idxs);
  218. }
  219. $self->_drag_volume_idx(undef);
  220. $self->_drag_start_pos(undef);
  221. $self->_drag_start_xy(undef);
  222. $self->_dragged(undef);
  223. } elsif ($e->Moving) {
  224. $self->_mouse_pos($pos);
  225. $self->Refresh;
  226. } else {
  227. $e->Skip();
  228. }
  229. }
  230. sub reset_objects {
  231. my ($self) = @_;
  232. $self->volumes([]);
  233. $self->_dirty(1);
  234. }
  235. sub set_viewport_from_scene {
  236. my ($self, $scene) = @_;
  237. $self->_sphi($scene->_sphi);
  238. $self->_stheta($scene->_stheta);
  239. $self->_camera_target($scene->_camera_target);
  240. $self->_zoom($scene->_zoom);
  241. $self->_quat($scene->_quat);
  242. $self->_dirty(1);
  243. }
  244. sub zoom_to_bounding_box {
  245. my ($self, $bb) = @_;
  246. # calculate the zoom factor needed to adjust viewport to
  247. # bounding box
  248. my $max_size = max(@{$bb->size}) * 2;
  249. my $min_viewport_size = min($self->GetSizeWH);
  250. $self->_zoom($min_viewport_size / $max_size);
  251. # center view around bounding box center
  252. $self->_camera_target($bb->center);
  253. $self->on_viewport_changed->() if $self->on_viewport_changed;
  254. }
  255. sub zoom_to_bed {
  256. my ($self) = @_;
  257. if ($self->bed_shape) {
  258. $self->zoom_to_bounding_box($self->bed_bounding_box);
  259. }
  260. }
  261. sub zoom_to_volume {
  262. my ($self, $volume_idx) = @_;
  263. my $volume = $self->volumes->[$volume_idx];
  264. my $bb = $volume->transformed_bounding_box;
  265. $self->zoom_to_bounding_box($bb);
  266. }
  267. sub zoom_to_volumes {
  268. my ($self) = @_;
  269. $self->zoom_to_bounding_box($self->volumes_bounding_box);
  270. }
  271. sub volumes_bounding_box {
  272. my ($self) = @_;
  273. my $bb = Slic3r::Geometry::BoundingBoxf3->new;
  274. $bb->merge($_->transformed_bounding_box) for @{$self->volumes};
  275. return $bb;
  276. }
  277. sub bed_bounding_box {
  278. my ($self) = @_;
  279. my $bb = Slic3r::Geometry::BoundingBoxf3->new;
  280. $bb->merge_point(Slic3r::Pointf3->new(@$_, 0)) for @{$self->bed_shape};
  281. return $bb;
  282. }
  283. sub max_bounding_box {
  284. my ($self) = @_;
  285. my $bb = $self->bed_bounding_box;
  286. $bb->merge($self->volumes_bounding_box);
  287. return $bb;
  288. }
  289. sub set_auto_bed_shape {
  290. my ($self, $bed_shape) = @_;
  291. # draw a default square bed around object center
  292. my $max_size = max(@{ $self->volumes_bounding_box->size });
  293. my $center = $self->volumes_bounding_box->center;
  294. $self->set_bed_shape([
  295. [ $center->x - $max_size, $center->y - $max_size ], #--
  296. [ $center->x + $max_size, $center->y - $max_size ], #--
  297. [ $center->x + $max_size, $center->y + $max_size ], #++
  298. [ $center->x - $max_size, $center->y + $max_size ], #++
  299. ]);
  300. $self->origin(Slic3r::Pointf->new(@$center[X,Y]));
  301. }
  302. sub set_bed_shape {
  303. my ($self, $bed_shape) = @_;
  304. $self->bed_shape($bed_shape);
  305. # triangulate bed
  306. my $expolygon = Slic3r::ExPolygon->new([ map [map scale($_), @$_], @$bed_shape ]);
  307. my $bed_bb = $expolygon->bounding_box;
  308. {
  309. my @points = ();
  310. foreach my $triangle (@{ $expolygon->triangulate }) {
  311. push @points, map {+ unscale($_->x), unscale($_->y), GROUND_Z } @$triangle; #))
  312. }
  313. $self->bed_triangles(OpenGL::Array->new_list(GL_FLOAT, @points));
  314. }
  315. {
  316. my @polylines = ();
  317. for (my $x = $bed_bb->x_min; $x <= $bed_bb->x_max; $x += scale 10) {
  318. push @polylines, Slic3r::Polyline->new([$x,$bed_bb->y_min], [$x,$bed_bb->y_max]);
  319. }
  320. for (my $y = $bed_bb->y_min; $y <= $bed_bb->y_max; $y += scale 10) {
  321. push @polylines, Slic3r::Polyline->new([$bed_bb->x_min,$y], [$bed_bb->x_max,$y]);
  322. }
  323. # clip with a slightly grown expolygon because our lines lay on the contours and
  324. # may get erroneously clipped
  325. my @lines = map Slic3r::Line->new(@$_[0,-1]),
  326. @{intersection_pl(\@polylines, [ @{$expolygon->offset(+scaled_epsilon)} ])};
  327. # append bed contours
  328. push @lines, map @{$_->lines}, @$expolygon;
  329. my @points = ();
  330. foreach my $line (@lines) {
  331. push @points, map {+ unscale($_->x), unscale($_->y), GROUND_Z } @$line; #))
  332. }
  333. $self->bed_grid_lines(OpenGL::Array->new_list(GL_FLOAT, @points));
  334. }
  335. $self->origin(Slic3r::Pointf->new(0,0));
  336. }
  337. sub deselect_volumes {
  338. my ($self) = @_;
  339. $_->selected(0) for @{$self->volumes};
  340. }
  341. sub select_volume {
  342. my ($self, $volume_idx) = @_;
  343. $self->volumes->[$volume_idx]->selected(1)
  344. if $volume_idx != -1;
  345. }
  346. sub SetCuttingPlane {
  347. my ($self, $z) = @_;
  348. $self->cutting_plane_z($z);
  349. # perform cut and cache section lines
  350. my @verts = ();
  351. foreach my $volume (@{$self->volumes}) {
  352. foreach my $volume (@{$self->volumes}) {
  353. next if !$volume->mesh;
  354. my $expolygons = $volume->mesh->slice([ $z - $volume->origin->z ])->[0];
  355. $expolygons = offset_ex([ map @$_, @$expolygons ], scale 0.1);
  356. foreach my $line (map @{$_->lines}, map @$_, @$expolygons) {
  357. push @verts, (
  358. unscale($line->a->x), unscale($line->a->y), $z, #))
  359. unscale($line->b->x), unscale($line->b->y), $z, #))
  360. );
  361. }
  362. }
  363. }
  364. $self->cut_lines_vertices(OpenGL::Array->new_list(GL_FLOAT, @verts));
  365. }
  366. # Given an axis and angle, compute quaternion.
  367. sub axis_to_quat {
  368. my ($ax, $phi) = @_;
  369. my $lena = sqrt(reduce { $a + $b } (map { $_ * $_ } @$ax));
  370. my @q = map { $_ * (1 / $lena) } @$ax;
  371. @q = map { $_ * sin($phi / 2.0) } @q;
  372. $q[$#q + 1] = cos($phi / 2.0);
  373. return @q;
  374. }
  375. # Project a point on the virtual trackball.
  376. # If it is inside the sphere, map it to the sphere, if it outside map it
  377. # to a hyperbola.
  378. sub project_to_sphere {
  379. my ($r, $x, $y) = @_;
  380. my $d = sqrt($x * $x + $y * $y);
  381. if ($d < $r * 0.70710678118654752440) { # Inside sphere
  382. return sqrt($r * $r - $d * $d);
  383. } else { # On hyperbola
  384. my $t = $r / 1.41421356237309504880;
  385. return $t * $t / $d;
  386. }
  387. }
  388. sub cross {
  389. my ($v1, $v2) = @_;
  390. return (@$v1[1] * @$v2[2] - @$v1[2] * @$v2[1],
  391. @$v1[2] * @$v2[0] - @$v1[0] * @$v2[2],
  392. @$v1[0] * @$v2[1] - @$v1[1] * @$v2[0]);
  393. }
  394. # Simulate a track-ball. Project the points onto the virtual trackball,
  395. # then figure out the axis of rotation, which is the cross product of
  396. # P1 P2 and O P1 (O is the center of the ball, 0,0,0) Note: This is a
  397. # deformed trackball-- is a trackball in the center, but is deformed
  398. # into a hyperbolic sheet of rotation away from the center.
  399. # It is assumed that the arguments to this routine are in the range
  400. # (-1.0 ... 1.0).
  401. sub trackball {
  402. my ($p1x, $p1y, $p2x, $p2y) = @_;
  403. if ($p1x == $p2x && $p1y == $p2y) {
  404. # zero rotation
  405. return (0.0, 0.0, 0.0, 1.0);
  406. }
  407. # First, figure out z-coordinates for projection of P1 and P2 to
  408. # deformed sphere
  409. my @p1 = ($p1x, $p1y, project_to_sphere(TRACKBALLSIZE, $p1x, $p1y));
  410. my @p2 = ($p2x, $p2y, project_to_sphere(TRACKBALLSIZE, $p2x, $p2y));
  411. # axis of rotation (cross product of P1 and P2)
  412. my @a = cross(\@p2, \@p1);
  413. # Figure out how much to rotate around that axis.
  414. my @d = map { $_ * $_ } (map { $p1[$_] - $p2[$_] } 0 .. $#p1);
  415. my $t = sqrt(reduce { $a + $b } @d) / (2.0 * TRACKBALLSIZE);
  416. # Avoid problems with out-of-control values...
  417. $t = 1.0 if ($t > 1.0);
  418. $t = -1.0 if ($t < -1.0);
  419. my $phi = 2.0 * asin($t);
  420. return axis_to_quat(\@a, $phi);
  421. }
  422. # Build a rotation matrix, given a quaternion rotation.
  423. sub quat_to_rotmatrix {
  424. my ($q) = @_;
  425. my @m = ();
  426. $m[0] = 1.0 - 2.0 * (@$q[1] * @$q[1] + @$q[2] * @$q[2]);
  427. $m[1] = 2.0 * (@$q[0] * @$q[1] - @$q[2] * @$q[3]);
  428. $m[2] = 2.0 * (@$q[2] * @$q[0] + @$q[1] * @$q[3]);
  429. $m[3] = 0.0;
  430. $m[4] = 2.0 * (@$q[0] * @$q[1] + @$q[2] * @$q[3]);
  431. $m[5] = 1.0 - 2.0 * (@$q[2] * @$q[2] + @$q[0] * @$q[0]);
  432. $m[6] = 2.0 * (@$q[1] * @$q[2] - @$q[0] * @$q[3]);
  433. $m[7] = 0.0;
  434. $m[8] = 2.0 * (@$q[2] * @$q[0] - @$q[1] * @$q[3]);
  435. $m[9] = 2.0 * (@$q[1] * @$q[2] + @$q[0] * @$q[3]);
  436. $m[10] = 1.0 - 2.0 * (@$q[1] * @$q[1] + @$q[0] * @$q[0]);
  437. $m[11] = 0.0;
  438. $m[12] = 0.0;
  439. $m[13] = 0.0;
  440. $m[14] = 0.0;
  441. $m[15] = 1.0;
  442. return @m;
  443. }
  444. sub mulquats {
  445. my ($q1, $rq) = @_;
  446. return (@$q1[3] * @$rq[0] + @$q1[0] * @$rq[3] + @$q1[1] * @$rq[2] - @$q1[2] * @$rq[1],
  447. @$q1[3] * @$rq[1] + @$q1[1] * @$rq[3] + @$q1[2] * @$rq[0] - @$q1[0] * @$rq[2],
  448. @$q1[3] * @$rq[2] + @$q1[2] * @$rq[3] + @$q1[0] * @$rq[1] - @$q1[1] * @$rq[0],
  449. @$q1[3] * @$rq[3] - @$q1[0] * @$rq[0] - @$q1[1] * @$rq[1] - @$q1[2] * @$rq[2])
  450. }
  451. sub mouse_to_3d {
  452. my ($self, $x, $y, $z) = @_;
  453. my @viewport = glGetIntegerv_p(GL_VIEWPORT); # 4 items
  454. my @mview = glGetDoublev_p(GL_MODELVIEW_MATRIX); # 16 items
  455. my @proj = glGetDoublev_p(GL_PROJECTION_MATRIX); # 16 items
  456. $y = $viewport[3] - $y;
  457. $z //= glReadPixels_p($x, $y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT);
  458. my @projected = gluUnProject_p($x, $y, $z, @mview, @proj, @viewport);
  459. return Slic3r::Pointf3->new(@projected);
  460. }
  461. sub mouse_ray {
  462. my ($self, $x, $y) = @_;
  463. return Slic3r::Linef3->new(
  464. $self->mouse_to_3d($x, $y, 0),
  465. $self->mouse_to_3d($x, $y, 1),
  466. );
  467. }
  468. sub GetContext {
  469. my ($self) = @_;
  470. if (Wx::wxVERSION >= 2.009) {
  471. return $self->{context} ||= Wx::GLContext->new($self);
  472. } else {
  473. return $self->SUPER::GetContext;
  474. }
  475. }
  476. sub SetCurrent {
  477. my ($self, $context) = @_;
  478. if (Wx::wxVERSION >= 2.009) {
  479. return $self->SUPER::SetCurrent($context);
  480. } else {
  481. return $self->SUPER::SetCurrent;
  482. }
  483. }
  484. sub Resize {
  485. my ($self, $x, $y) = @_;
  486. return unless $self->GetContext;
  487. $self->_dirty(0);
  488. $self->SetCurrent($self->GetContext);
  489. glViewport(0, 0, $x, $y);
  490. $x /= $self->_zoom;
  491. $y /= $self->_zoom;
  492. glMatrixMode(GL_PROJECTION);
  493. glLoadIdentity();
  494. my $depth = 10 * max(@{ $self->max_bounding_box->size });
  495. glOrtho(
  496. -$x/2, $x/2, -$y/2, $y/2,
  497. -$depth, 2*$depth,
  498. );
  499. glMatrixMode(GL_MODELVIEW);
  500. }
  501. sub InitGL {
  502. my $self = shift;
  503. return if $self->init;
  504. return unless $self->GetContext;
  505. $self->init(1);
  506. glClearColor(0, 0, 0, 1);
  507. glColor3f(1, 0, 0);
  508. glEnable(GL_DEPTH_TEST);
  509. glClearDepth(1.0);
  510. glDepthFunc(GL_LEQUAL);
  511. glEnable(GL_CULL_FACE);
  512. glEnable(GL_BLEND);
  513. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  514. # Set antialiasing/multisampling
  515. glDisable(GL_LINE_SMOOTH);
  516. glDisable(GL_POLYGON_SMOOTH);
  517. glEnable(GL_MULTISAMPLE);
  518. # ambient lighting
  519. glLightModelfv_p(GL_LIGHT_MODEL_AMBIENT, 0.3, 0.3, 0.3, 1);
  520. glEnable(GL_LIGHTING);
  521. glEnable(GL_LIGHT0);
  522. glEnable(GL_LIGHT1);
  523. # light from camera
  524. glLightfv_p(GL_LIGHT1, GL_POSITION, 1, 0, 1, 0);
  525. glLightfv_p(GL_LIGHT1, GL_SPECULAR, 0.3, 0.3, 0.3, 1);
  526. glLightfv_p(GL_LIGHT1, GL_DIFFUSE, 0.2, 0.2, 0.2, 1);
  527. # Enables Smooth Color Shading; try GL_FLAT for (lack of) fun.
  528. glShadeModel(GL_SMOOTH);
  529. glMaterialfv_p(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, 0.5, 0.3, 0.3, 1);
  530. glMaterialfv_p(GL_FRONT_AND_BACK, GL_SPECULAR, 1, 1, 1, 1);
  531. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50);
  532. glMaterialfv_p(GL_FRONT_AND_BACK, GL_EMISSION, 0.1, 0, 0, 0.9);
  533. # A handy trick -- have surface material mirror the color.
  534. glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  535. glEnable(GL_COLOR_MATERIAL);
  536. glEnable(GL_MULTISAMPLE);
  537. }
  538. sub Render {
  539. my ($self, $dc) = @_;
  540. # prevent calling SetCurrent() when window is not shown yet
  541. return unless $self->IsShownOnScreen;
  542. return unless my $context = $self->GetContext;
  543. $self->SetCurrent($context);
  544. $self->InitGL;
  545. glClearColor(1, 1, 1, 1);
  546. glClearDepth(1);
  547. glDepthFunc(GL_LESS);
  548. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  549. glMatrixMode(GL_MODELVIEW);
  550. glLoadIdentity();
  551. if (TURNTABLE_MODE) {
  552. glRotatef(-$self->_stheta, 1, 0, 0); # pitch
  553. glRotatef($self->_sphi, 0, 0, 1); # yaw
  554. } else {
  555. my @rotmat = quat_to_rotmatrix($self->quat);
  556. glMultMatrixd_p(@rotmat[0..15]);
  557. }
  558. glTranslatef(@{ $self->_camera_target->negative });
  559. # light from above
  560. glLightfv_p(GL_LIGHT0, GL_POSITION, -0.5, -0.5, 1, 0);
  561. glLightfv_p(GL_LIGHT0, GL_SPECULAR, 0.2, 0.2, 0.2, 1);
  562. glLightfv_p(GL_LIGHT0, GL_DIFFUSE, 0.5, 0.5, 0.5, 1);
  563. if ($self->enable_picking) {
  564. glDisable(GL_LIGHTING);
  565. $self->draw_volumes(1);
  566. glFlush();
  567. glFinish();
  568. if (my $pos = $self->_mouse_pos) {
  569. my $col = [ glReadPixels_p($pos->x, $self->GetSize->GetHeight - $pos->y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE) ];
  570. my $volume_idx = $col->[0] + $col->[1]*256 + $col->[2]*256*256;
  571. $self->_hover_volume_idx(undef);
  572. $_->hover(0) for @{$self->volumes};
  573. if ($volume_idx <= $#{$self->volumes}) {
  574. $self->_hover_volume_idx($volume_idx);
  575. $self->volumes->[$volume_idx]->hover(1);
  576. my $group_id = $self->volumes->[$volume_idx]->select_group_id;
  577. if ($group_id != -1) {
  578. $_->hover(1) for grep { $_->select_group_id == $group_id } @{$self->volumes};
  579. }
  580. $self->on_hover->($volume_idx) if $self->on_hover;
  581. }
  582. }
  583. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  584. glFlush();
  585. glFinish();
  586. glEnable(GL_LIGHTING);
  587. }
  588. # draw fixed background
  589. if ($self->background) {
  590. glDisable(GL_LIGHTING);
  591. glPushMatrix();
  592. glLoadIdentity();
  593. glMatrixMode(GL_PROJECTION);
  594. glPushMatrix();
  595. glLoadIdentity();
  596. glBegin(GL_QUADS);
  597. glColor3f(0.0,0.0,0.0);
  598. glVertex2f(-1.0,-1.0);
  599. glVertex2f(1,-1.0);
  600. glColor3f(10/255,98/255,144/255);
  601. glVertex2f(1, 1);
  602. glVertex2f(-1.0, 1);
  603. glEnd();
  604. glPopMatrix();
  605. glMatrixMode(GL_MODELVIEW);
  606. glPopMatrix();
  607. glEnable(GL_LIGHTING);
  608. }
  609. # draw ground and axes
  610. glDisable(GL_LIGHTING);
  611. # draw ground
  612. my $ground_z = GROUND_Z;
  613. if ($self->bed_triangles) {
  614. glDisable(GL_DEPTH_TEST);
  615. glEnable(GL_BLEND);
  616. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  617. glEnableClientState(GL_VERTEX_ARRAY);
  618. glColor4f(0.8, 0.6, 0.5, 0.4);
  619. glNormal3d(0,0,1);
  620. glVertexPointer_p(3, $self->bed_triangles);
  621. glDrawArrays(GL_TRIANGLES, 0, $self->bed_triangles->elements / 3);
  622. glDisableClientState(GL_VERTEX_ARRAY);
  623. # we need depth test for grid, otherwise it would disappear when looking
  624. # the object from below
  625. glEnable(GL_DEPTH_TEST);
  626. # draw grid
  627. glLineWidth(3);
  628. glColor4f(0.2, 0.2, 0.2, 0.4);
  629. glEnableClientState(GL_VERTEX_ARRAY);
  630. glVertexPointer_p(3, $self->bed_grid_lines);
  631. glDrawArrays(GL_LINES, 0, $self->bed_grid_lines->elements / 3);
  632. glDisableClientState(GL_VERTEX_ARRAY);
  633. glDisable(GL_BLEND);
  634. }
  635. my $volumes_bb = $self->volumes_bounding_box;
  636. {
  637. # draw axes
  638. # disable depth testing so that axes are not covered by ground
  639. glDisable(GL_DEPTH_TEST);
  640. my $origin = $self->origin;
  641. my $axis_len = max(
  642. 0.3 * max(@{ $self->bed_bounding_box->size }),
  643. 2 * max(@{ $volumes_bb->size }),
  644. );
  645. glLineWidth(2);
  646. glBegin(GL_LINES);
  647. # draw line for x axis
  648. glColor3f(1, 0, 0);
  649. glVertex3f(@$origin, $ground_z);
  650. glVertex3f($origin->x + $axis_len, $origin->y, $ground_z); #,,
  651. # draw line for y axis
  652. glColor3f(0, 1, 0);
  653. glVertex3f(@$origin, $ground_z);
  654. glVertex3f($origin->x, $origin->y + $axis_len, $ground_z); #++
  655. glEnd();
  656. # draw line for Z axis
  657. # (re-enable depth test so that axis is correctly shown when objects are behind it)
  658. glEnable(GL_DEPTH_TEST);
  659. glBegin(GL_LINES);
  660. glColor3f(0, 0, 1);
  661. glVertex3f(@$origin, $ground_z);
  662. glVertex3f(@$origin, $ground_z+$axis_len);
  663. glEnd();
  664. }
  665. glEnable(GL_LIGHTING);
  666. # draw objects
  667. $self->draw_volumes;
  668. # draw cutting plane
  669. if (defined $self->cutting_plane_z) {
  670. my $plane_z = $self->cutting_plane_z;
  671. my $bb = $volumes_bb;
  672. glDisable(GL_CULL_FACE);
  673. glDisable(GL_LIGHTING);
  674. glEnable(GL_BLEND);
  675. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  676. glBegin(GL_QUADS);
  677. glColor4f(0.8, 0.8, 0.8, 0.5);
  678. glVertex3f($bb->x_min-20, $bb->y_min-20, $plane_z);
  679. glVertex3f($bb->x_max+20, $bb->y_min-20, $plane_z);
  680. glVertex3f($bb->x_max+20, $bb->y_max+20, $plane_z);
  681. glVertex3f($bb->x_min-20, $bb->y_max+20, $plane_z);
  682. glEnd();
  683. glEnable(GL_CULL_FACE);
  684. glDisable(GL_BLEND);
  685. }
  686. glFlush();
  687. $self->SwapBuffers();
  688. }
  689. sub draw_volumes {
  690. my ($self, $fakecolor) = @_;
  691. glEnable(GL_BLEND);
  692. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  693. glEnableClientState(GL_VERTEX_ARRAY);
  694. glEnableClientState(GL_NORMAL_ARRAY);
  695. foreach my $volume_idx (0..$#{$self->volumes}) {
  696. my $volume = $self->volumes->[$volume_idx];
  697. glPushMatrix();
  698. glTranslatef(@{$volume->origin});
  699. if ($fakecolor) {
  700. my $r = ($volume_idx & 0x000000FF) >> 0;
  701. my $g = ($volume_idx & 0x0000FF00) >> 8;
  702. my $b = ($volume_idx & 0x00FF0000) >> 16;
  703. glColor4f($r/255.0, $g/255.0, $b/255.0, 1);
  704. } elsif ($volume->selected) {
  705. glColor4f(@{ &SELECTED_COLOR });
  706. } elsif ($volume->hover) {
  707. glColor4f(@{ &HOVER_COLOR });
  708. } else {
  709. glColor4f(@{ $volume->color });
  710. }
  711. my @sorted_z = ();
  712. my ($min_z, $max_z);
  713. if ($volume->range && $volume->offsets) {
  714. @sorted_z = sort { $a <=> $b } keys %{$volume->offsets};
  715. ($min_z, $max_z) = @{$volume->range};
  716. $min_z = first { $_ >= $min_z } @sorted_z;
  717. $max_z = first { $_ > $max_z } @sorted_z;
  718. }
  719. glCullFace(GL_BACK);
  720. if ($volume->qverts) {
  721. my ($min_offset, $max_offset);
  722. if (defined $min_z) {
  723. $min_offset = $volume->offsets->{$min_z}->[0];
  724. }
  725. if (defined $max_z) {
  726. $max_offset = $volume->offsets->{$max_z}->[0];
  727. }
  728. $min_offset //= 0;
  729. $max_offset //= $volume->qverts->size;
  730. glVertexPointer_c(3, GL_FLOAT, 0, $volume->qverts->verts_ptr);
  731. glNormalPointer_c(GL_FLOAT, 0, $volume->qverts->norms_ptr);
  732. glDrawArrays(GL_QUADS, $min_offset / 3, ($max_offset-$min_offset) / 3);
  733. }
  734. if ($volume->tverts) {
  735. my ($min_offset, $max_offset);
  736. if (defined $min_z) {
  737. $min_offset = $volume->offsets->{$min_z}->[1];
  738. }
  739. if (defined $max_z) {
  740. $max_offset = $volume->offsets->{$max_z}->[1];
  741. }
  742. $min_offset //= 0;
  743. $max_offset //= $volume->tverts->size;
  744. glVertexPointer_c(3, GL_FLOAT, 0, $volume->tverts->verts_ptr);
  745. glNormalPointer_c(GL_FLOAT, 0, $volume->tverts->norms_ptr);
  746. glDrawArrays(GL_TRIANGLES, $min_offset / 3, ($max_offset-$min_offset) / 3);
  747. }
  748. glPopMatrix();
  749. }
  750. glDisableClientState(GL_NORMAL_ARRAY);
  751. glDisable(GL_BLEND);
  752. if (defined $self->cutting_plane_z) {
  753. glLineWidth(2);
  754. glColor3f(0, 0, 0);
  755. glVertexPointer_p(3, $self->cut_lines_vertices);
  756. glDrawArrays(GL_LINES, 0, $self->cut_lines_vertices->elements / 3);
  757. }
  758. glDisableClientState(GL_VERTEX_ARRAY);
  759. }
  760. package Slic3r::GUI::3DScene::Volume;
  761. use Moo;
  762. has 'bounding_box' => (is => 'ro', required => 1);
  763. has 'origin' => (is => 'rw', default => sub { Slic3r::Pointf3->new(0,0,0) });
  764. has 'color' => (is => 'ro', required => 1);
  765. has 'select_group_id' => (is => 'rw', default => sub { -1 });
  766. has 'drag_group_id' => (is => 'rw', default => sub { -1 });
  767. has 'selected' => (is => 'rw', default => sub { 0 });
  768. has 'hover' => (is => 'rw', default => sub { 0 });
  769. has 'range' => (is => 'rw');
  770. # geometric data
  771. has 'qverts' => (is => 'rw'); # GLVertexArray object
  772. has 'tverts' => (is => 'rw'); # GLVertexArray object
  773. has 'mesh' => (is => 'rw'); # only required for cut contours
  774. has 'offsets' => (is => 'rw'); # [ z => [ qverts_idx, tverts_idx ] ]
  775. sub transformed_bounding_box {
  776. my ($self) = @_;
  777. my $bb = $self->bounding_box;
  778. $bb->translate(@{$self->origin});
  779. return $bb;
  780. }
  781. package Slic3r::GUI::3DScene;
  782. use base qw(Slic3r::GUI::3DScene::Base);
  783. use OpenGL qw(:glconstants :gluconstants :glufunctions);
  784. use List::Util qw(first min max);
  785. use Slic3r::Geometry qw(scale unscale epsilon);
  786. use Slic3r::Print::State ':steps';
  787. use constant COLORS => [ [1,1,0,1], [1,0.5,0.5,1], [0.5,1,0.5,1], [0.5,0.5,1,1] ];
  788. __PACKAGE__->mk_accessors(qw(
  789. color_by
  790. select_by
  791. drag_by
  792. volumes_by_object
  793. _objects_by_volumes
  794. ));
  795. sub new {
  796. my $class = shift;
  797. my $self = $class->SUPER::new(@_);
  798. $self->color_by('volume'); # object | volume
  799. $self->select_by('object'); # object | volume | instance
  800. $self->drag_by('instance'); # object | instance
  801. $self->volumes_by_object({}); # obj_idx => [ volume_idx, volume_idx ... ]
  802. $self->_objects_by_volumes({}); # volume_idx => [ obj_idx, instance_idx ]
  803. return $self;
  804. }
  805. sub load_object {
  806. my ($self, $model, $obj_idx, $instance_idxs) = @_;
  807. my $model_object;
  808. if ($model->isa('Slic3r::Model::Object')) {
  809. $model_object = $model;
  810. $model = $model_object->model;
  811. $obj_idx = 0;
  812. } else {
  813. $model_object = $model->get_object($obj_idx);
  814. }
  815. $instance_idxs ||= [0..$#{$model_object->instances}];
  816. my @volumes_idx = ();
  817. foreach my $volume_idx (0..$#{$model_object->volumes}) {
  818. my $volume = $model_object->volumes->[$volume_idx];
  819. foreach my $instance_idx (@$instance_idxs) {
  820. my $instance = $model_object->instances->[$instance_idx];
  821. my $mesh = $volume->mesh->clone;
  822. $instance->transform_mesh($mesh);
  823. my $color_idx;
  824. if ($self->color_by eq 'volume') {
  825. $color_idx = $volume_idx;
  826. } elsif ($self->color_by eq 'object') {
  827. $color_idx = $obj_idx;
  828. }
  829. my $color = [ @{COLORS->[ $color_idx % scalar(@{&COLORS}) ]} ];
  830. $color->[3] = $volume->modifier ? 0.5 : 1;
  831. push @{$self->volumes}, my $v = Slic3r::GUI::3DScene::Volume->new(
  832. bounding_box => $mesh->bounding_box,
  833. color => $color,
  834. );
  835. $v->mesh($mesh) if $self->enable_cutting;
  836. if ($self->select_by eq 'object') {
  837. $v->select_group_id($obj_idx*1000000);
  838. } elsif ($self->select_by eq 'volume') {
  839. $v->select_group_id($obj_idx*1000000 + $volume_idx*1000);
  840. } elsif ($self->select_by eq 'instance') {
  841. $v->select_group_id($obj_idx*1000000 + $volume_idx*1000 + $instance_idx);
  842. }
  843. if ($self->drag_by eq 'object') {
  844. $v->drag_group_id($obj_idx*1000);
  845. } elsif ($self->drag_by eq 'instance') {
  846. $v->drag_group_id($obj_idx*1000 + $instance_idx);
  847. }
  848. push @volumes_idx, my $scene_volume_idx = $#{$self->volumes};
  849. $self->_objects_by_volumes->{$scene_volume_idx} = [ $obj_idx, $volume_idx, $instance_idx ];
  850. my $verts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  851. $verts->load_mesh($mesh);
  852. $v->tverts($verts);
  853. }
  854. }
  855. $self->volumes_by_object->{$obj_idx} = [@volumes_idx];
  856. return @volumes_idx;
  857. }
  858. sub load_print_object_slices {
  859. my ($self, $object) = @_;
  860. my @verts = ();
  861. my @norms = ();
  862. my @quad_verts = ();
  863. my @quad_norms = ();
  864. foreach my $layer (@{$object->layers}) {
  865. my $gap = 0;
  866. my $top_z = $layer->print_z;
  867. my $bottom_z = $layer->print_z - $layer->height + $gap;
  868. foreach my $copy (@{ $object->_shifted_copies }) {
  869. {
  870. my @expolygons = map $_->clone, @{$layer->slices};
  871. $_->translate(@$copy) for @expolygons;
  872. $self->_expolygons_to_verts(\@expolygons, $layer->print_z, \@verts, \@norms);
  873. }
  874. foreach my $slice (@{$layer->slices}) {
  875. foreach my $polygon (@$slice) {
  876. foreach my $line (@{$polygon->lines}) {
  877. $line->translate(@$copy);
  878. push @quad_norms, (0,0,-1), (0,0,-1);
  879. push @quad_verts, (map unscale($_), @{$line->a}), $bottom_z;
  880. push @quad_verts, (map unscale($_), @{$line->b}), $bottom_z;
  881. push @quad_norms, (0,0,1), (0,0,1);
  882. push @quad_verts, (map unscale($_), @{$line->b}), $top_z;
  883. push @quad_verts, (map unscale($_), @{$line->a}), $top_z;
  884. # We'll use this for the middle normal when using 4 quads:
  885. #my $xy_normal = $line->normal;
  886. #$_xynormal->scale(1/$line->length);
  887. }
  888. }
  889. }
  890. }
  891. }
  892. my $obb = $object->bounding_box;
  893. my $bb = Slic3r::Geometry::BoundingBoxf3->new;
  894. $bb->merge_point(Slic3r::Pointf3->new_unscale(@{$obb->min_point}, 0));
  895. $bb->merge_point(Slic3r::Pointf3->new_unscale(@{$obb->max_point}, $object->size->z));
  896. push @{$self->volumes}, my $v = Slic3r::GUI::3DScene::Volume->new(
  897. bounding_box => $bb,
  898. color => COLORS->[0],
  899. verts => OpenGL::Array->new_list(GL_FLOAT, @verts),
  900. norms => OpenGL::Array->new_list(GL_FLOAT, @norms),
  901. quad_verts => OpenGL::Array->new_list(GL_FLOAT, @quad_verts),
  902. quad_norms => OpenGL::Array->new_list(GL_FLOAT, @quad_norms),
  903. );
  904. }
  905. sub load_print_toolpaths {
  906. my ($self, $print) = @_;
  907. return if !$print->step_done(STEP_SKIRT);
  908. return if !$print->step_done(STEP_BRIM);
  909. return if !$print->has_skirt && $print->config->brim_width == 0;
  910. my $qverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  911. my $tverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  912. my %offsets = (); # print_z => [ qverts, tverts ]
  913. my $skirt_height = 0; # number of layers
  914. if ($print->has_infinite_skirt) {
  915. $skirt_height = $print->total_layer_count;
  916. } else {
  917. $skirt_height = min($print->config->skirt_height, $print->total_layer_count);
  918. }
  919. $skirt_height ||= 1 if $print->config->brim_width > 0;
  920. # get first $skirt_height layers (maybe this should be moved to a PrintObject method?)
  921. my $object0 = $print->get_object(0);
  922. my @layers = ();
  923. push @layers, map $object0->get_layer($_-1), 1..min($skirt_height, $object0->layer_count);
  924. push @layers, map $object0->get_support_layer($_-1), 1..min($skirt_height, $object0->support_layer_count);
  925. @layers = sort { $a->print_z <=> $b->print_z } @layers;
  926. @layers = @layers[0..($skirt_height-1)];
  927. foreach my $i (0..($skirt_height-1)) {
  928. my $top_z = $layers[$i]->print_z;
  929. $offsets{$top_z} = [$qverts->size, $tverts->size];
  930. if ($i == 0) {
  931. $self->_extrusionentity_to_verts($print->brim, $top_z, Slic3r::Point->new(0,0), $qverts, $tverts);
  932. }
  933. $self->_extrusionentity_to_verts($print->skirt, $top_z, Slic3r::Point->new(0,0), $qverts, $tverts);
  934. }
  935. my $bb = Slic3r::Geometry::BoundingBoxf3->new;
  936. {
  937. my $pbb = $print->bounding_box;
  938. $bb->merge_point(Slic3r::Pointf3->new_unscale(@{$pbb->min_point}));
  939. $bb->merge_point(Slic3r::Pointf3->new_unscale(@{$pbb->max_point}));
  940. }
  941. push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new(
  942. bounding_box => $bb,
  943. color => COLORS->[2],
  944. qverts => $qverts,
  945. tverts => $tverts,
  946. offsets => { %offsets },
  947. );
  948. }
  949. sub load_print_object_toolpaths {
  950. my ($self, $object) = @_;
  951. my $perim_qverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  952. my $perim_tverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  953. my $infill_qverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  954. my $infill_tverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  955. my $support_qverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  956. my $support_tverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
  957. my %perim_offsets = (); # print_z => [ qverts, tverts ]
  958. my %infill_offsets = ();
  959. my %support_offsets = ();
  960. # order layers by print_z
  961. my @layers = sort { $a->print_z <=> $b->print_z }
  962. @{$object->layers}, @{$object->support_layers};
  963. foreach my $layer (@layers) {
  964. my $top_z = $layer->print_z;
  965. if (!exists $perim_offsets{$top_z}) {
  966. $perim_offsets{$top_z} = [
  967. $perim_qverts->size, $perim_tverts->size,
  968. ];
  969. $infill_offsets{$top_z} = [
  970. $infill_qverts->size, $infill_tverts->size,
  971. ];
  972. $support_offsets{$top_z} = [
  973. $support_qverts->size, $support_tverts->size,
  974. ];
  975. }
  976. foreach my $copy (@{ $object->_shifted_copies }) {
  977. foreach my $layerm (@{$layer->regions}) {
  978. if ($object->step_done(STEP_PERIMETERS)) {
  979. $self->_extrusionentity_to_verts($layerm->perimeters, $top_z, $copy,
  980. $perim_qverts, $perim_tverts);
  981. }
  982. if ($object->step_done(STEP_INFILL)) {
  983. $self->_extrusionentity_to_verts($layerm->fills, $top_z, $copy,
  984. $infill_qverts, $infill_tverts);
  985. }
  986. }
  987. if ($layer->isa('Slic3r::Layer::Support') && $object->step_done(STEP_SUPPORTMATERIAL)) {
  988. $self->_extrusionentity_to_verts($layer->support_fills, $top_z, $copy,
  989. $support_qverts, $support_tverts);
  990. $self->_extrusionentity_to_verts($layer->support_interface_fills, $top_z, $copy,
  991. $support_qverts, $support_tverts);
  992. }
  993. }
  994. }
  995. my $obb = $object->bounding_box;
  996. my $bb = Slic3r::Geometry::BoundingBoxf3->new;
  997. foreach my $copy (@{ $object->_shifted_copies }) {
  998. my $cbb = $obb->clone;
  999. $cbb->translate(@$copy);
  1000. $bb->merge_point(Slic3r::Pointf3->new_unscale(@{$cbb->min_point}, 0));
  1001. $bb->merge_point(Slic3r::Pointf3->new_unscale(@{$cbb->max_point}, $object->size->z));
  1002. }
  1003. push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new(
  1004. bounding_box => $bb,
  1005. color => COLORS->[0],
  1006. qverts => $perim_qverts,
  1007. tverts => $perim_tverts,
  1008. offsets => { %perim_offsets },
  1009. );
  1010. push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new(
  1011. bounding_box => $bb,
  1012. color => COLORS->[1],
  1013. qverts => $infill_qverts,
  1014. tverts => $infill_tverts,
  1015. offsets => { %infill_offsets },
  1016. );
  1017. push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new(
  1018. bounding_box => $bb,
  1019. color => COLORS->[2],
  1020. qverts => $support_qverts,
  1021. tverts => $support_tverts,
  1022. offsets => { %support_offsets },
  1023. );
  1024. }
  1025. sub set_toolpaths_range {
  1026. my ($self, $min_z, $max_z) = @_;
  1027. foreach my $volume (@{$self->volumes}) {
  1028. $volume->range([ $min_z, $max_z ]);
  1029. }
  1030. }
  1031. sub _expolygons_to_verts {
  1032. my ($self, $expolygons, $z, $verts, $norms) = @_;
  1033. my $tess = gluNewTess();
  1034. gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
  1035. gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
  1036. gluTessCallback($tess, GLU_TESS_VERTEX, sub {
  1037. my ($x, $y, $z) = @_;
  1038. push @$verts, $x, $y, $z;
  1039. push @$norms, (0,0,1), (0,0,1), (0,0,1);
  1040. });
  1041. gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
  1042. gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
  1043. gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT');
  1044. foreach my $expolygon (@$expolygons) {
  1045. gluTessBeginPolygon($tess);
  1046. foreach my $polygon (@$expolygon) {
  1047. gluTessBeginContour($tess);
  1048. gluTessVertex_p($tess, (map unscale($_), @$_), $z) for @$polygon;
  1049. gluTessEndContour($tess);
  1050. }
  1051. gluTessEndPolygon($tess);
  1052. }
  1053. gluDeleteTess($tess);
  1054. }
  1055. sub _extrusionentity_to_verts {
  1056. my ($self, $entity, $top_z, $copy, $qverts, $tverts) = @_;
  1057. my ($lines, $widths, $heights, $closed);
  1058. if ($entity->isa('Slic3r::ExtrusionPath::Collection')) {
  1059. $self->_extrusionentity_to_verts($_, $top_z, $copy, $qverts, $tverts)
  1060. for @$entity;
  1061. return;
  1062. } elsif ($entity->isa('Slic3r::ExtrusionPath')) {
  1063. my $polyline = $entity->polyline->clone;
  1064. $polyline->remove_duplicate_points;
  1065. $polyline->translate(@$copy);
  1066. $lines = $polyline->lines;
  1067. $widths = [ map $entity->width, 0..$#$lines ];
  1068. $heights = [ map $entity->height, 0..$#$lines ];
  1069. $closed = 0;
  1070. } else {
  1071. $lines = [];
  1072. $widths = [];
  1073. $heights = [];
  1074. $closed = 1;
  1075. foreach my $path (@$entity) {
  1076. my $polyline = $path->polyline->clone;
  1077. $polyline->remove_duplicate_points;
  1078. $polyline->translate(@$copy);
  1079. my $path_lines = $polyline->lines;
  1080. push @$lines, @$path_lines;
  1081. push @$widths, map $path->width, 0..$#$path_lines;
  1082. push @$heights, map $path->height, 0..$#$path_lines;
  1083. }
  1084. }
  1085. Slic3r::GUI::_3DScene::_extrusionentity_to_verts_do($lines, $widths, $heights,
  1086. $closed, $top_z, $copy, $qverts, $tverts);
  1087. }
  1088. sub object_idx {
  1089. my ($self, $volume_idx) = @_;
  1090. return $self->_objects_by_volumes->{$volume_idx}[0];
  1091. }
  1092. sub volume_idx {
  1093. my ($self, $volume_idx) = @_;
  1094. return $self->_objects_by_volumes->{$volume_idx}[1];
  1095. }
  1096. sub instance_idx {
  1097. my ($self, $volume_idx) = @_;
  1098. return $self->_objects_by_volumes->{$volume_idx}[2];
  1099. }
  1100. 1;