3DScene.pm 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  1. # Implements pure perl packages
  2. #
  3. # Slic3r::GUI::3DScene::Base;
  4. # Slic3r::GUI::3DScene;
  5. #
  6. # Slic3r::GUI::Plater::3D derives from Slic3r::GUI::3DScene,
  7. # Slic3r::GUI::Plater::3DPreview,
  8. # Slic3r::GUI::Plater::ObjectCutDialog and Slic3r::GUI::Plater::ObjectPartsPanel
  9. # own $self->{canvas} of the Slic3r::GUI::3DScene type.
  10. #
  11. # Therefore the 3DScene supports renderng of STLs, extrusions and cutting planes,
  12. # and camera manipulation.
  13. package Slic3r::GUI::3DScene::Base;
  14. use strict;
  15. use warnings;
  16. use Wx qw(wxTheApp :timer :bitmap :icon :dialog);
  17. #==============================================================================================================================
  18. use Wx::Event qw(EVT_PAINT EVT_IDLE EVT_MOUSEWHEEL EVT_MOUSE_EVENTS EVT_CHAR EVT_TIMER);
  19. #use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_ERASE_BACKGROUND EVT_IDLE EVT_MOUSEWHEEL EVT_MOUSE_EVENTS EVT_CHAR EVT_TIMER);
  20. #==============================================================================================================================
  21. # must load OpenGL *before* Wx::GLCanvas
  22. use OpenGL qw(:glconstants :glfunctions :glufunctions :gluconstants);
  23. use base qw(Wx::GLCanvas Class::Accessor);
  24. use Math::Trig qw(asin tan);
  25. use List::Util qw(reduce min max first);
  26. use Slic3r::Geometry qw(X Y normalize scale unscale scaled_epsilon);
  27. use Slic3r::Geometry::Clipper qw(offset_ex intersection_pl JT_ROUND);
  28. use Wx::GLCanvas qw(:all);
  29. use Slic3r::Geometry qw(PI);
  30. # _dirty: boolean flag indicating, that the screen has to be redrawn on EVT_IDLE.
  31. # volumes: reference to vector of Slic3r::GUI::3DScene::Volume.
  32. # _camera_type: 'perspective' or 'ortho'
  33. #==============================================================================================================================
  34. __PACKAGE__->mk_accessors( qw(_quat init
  35. enable_moving
  36. on_viewport_changed
  37. on_select
  38. on_double_click
  39. on_right_click
  40. on_move
  41. on_model_update
  42. volumes
  43. _drag_volume_idx
  44. _drag_start_pos
  45. _drag_volume_center_offset
  46. _drag_start_xy
  47. _dragged
  48. _layer_height_edited
  49. ) );
  50. #__PACKAGE__->mk_accessors( qw(_quat _dirty init
  51. # enable_picking
  52. # enable_moving
  53. # use_plain_shader
  54. # on_viewport_changed
  55. # on_hover
  56. # on_select
  57. # on_double_click
  58. # on_right_click
  59. # on_move
  60. # on_model_update
  61. # volumes
  62. # _sphi _stheta
  63. # cutting_plane_z
  64. # cut_lines_vertices
  65. # bed_shape
  66. # bed_triangles
  67. # bed_grid_lines
  68. # bed_polygon
  69. # background
  70. # origin
  71. # _mouse_pos
  72. # _hover_volume_idx
  73. #
  74. # _drag_volume_idx
  75. # _drag_start_pos
  76. # _drag_volume_center_offset
  77. # _drag_start_xy
  78. # _dragged
  79. #
  80. # _layer_height_edited
  81. #
  82. # _camera_type
  83. # _camera_target
  84. # _camera_distance
  85. # _zoom
  86. #
  87. # _legend_enabled
  88. # _warning_enabled
  89. # _apply_zoom_to_volumes_filter
  90. # _mouse_dragging
  91. #
  92. # ) );
  93. #==============================================================================================================================
  94. use constant TRACKBALLSIZE => 0.8;
  95. use constant TURNTABLE_MODE => 1;
  96. #==============================================================================================================================
  97. #use constant GROUND_Z => -0.02;
  98. ## For mesh selection: Not selected - bright yellow.
  99. #use constant DEFAULT_COLOR => [1,1,0];
  100. ## For mesh selection: Selected - bright green.
  101. #use constant SELECTED_COLOR => [0,1,0,1];
  102. ## For mesh selection: Mouse hovers over the object, but object not selected yet - dark green.
  103. #use constant HOVER_COLOR => [0.4,0.9,0,1];
  104. #
  105. ## phi / theta angles to orient the camera.
  106. #use constant VIEW_DEFAULT => [45.0,45.0];
  107. #use constant VIEW_LEFT => [90.0,90.0];
  108. #use constant VIEW_RIGHT => [-90.0,90.0];
  109. #use constant VIEW_TOP => [0.0,0.0];
  110. #use constant VIEW_BOTTOM => [0.0,180.0];
  111. #use constant VIEW_FRONT => [0.0,90.0];
  112. #use constant VIEW_REAR => [180.0,90.0];
  113. #
  114. #use constant MANIPULATION_IDLE => 0;
  115. #use constant MANIPULATION_DRAGGING => 1;
  116. #use constant MANIPULATION_LAYER_HEIGHT => 2;
  117. #
  118. #use constant GIMBALL_LOCK_THETA_MAX => 180;
  119. #==============================================================================================================================
  120. use constant VARIABLE_LAYER_THICKNESS_BAR_WIDTH => 70;
  121. use constant VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT => 22;
  122. # make OpenGL::Array thread-safe
  123. {
  124. no warnings 'redefine';
  125. *OpenGL::Array::CLONE_SKIP = sub { 1 };
  126. }
  127. sub new {
  128. my ($class, $parent) = @_;
  129. # We can only enable multi sample anti aliasing wih wxWidgets 3.0.3 and with a hacked Wx::GLCanvas,
  130. # which exports some new WX_GL_XXX constants, namely WX_GL_SAMPLE_BUFFERS and WX_GL_SAMPLES.
  131. my $can_multisample =
  132. ! wxTheApp->{app_config}->get('use_legacy_opengl') &&
  133. Wx::wxVERSION >= 3.000003 &&
  134. defined Wx::GLCanvas->can('WX_GL_SAMPLE_BUFFERS') &&
  135. defined Wx::GLCanvas->can('WX_GL_SAMPLES');
  136. my $attrib = [WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 24];
  137. if ($can_multisample) {
  138. # Request a window with multi sampled anti aliasing. This is a new feature in Wx 3.0.3 (backported from 3.1.0).
  139. # Use eval to avoid compilation, if the subs WX_GL_SAMPLE_BUFFERS and WX_GL_SAMPLES are missing.
  140. eval 'push(@$attrib, (WX_GL_SAMPLE_BUFFERS, 1, WX_GL_SAMPLES, 4));';
  141. }
  142. # wxWidgets expect the attrib list to be ended by zero.
  143. push(@$attrib, 0);
  144. # we request a depth buffer explicitely because it looks like it's not created by
  145. # default on Linux, causing transparency issues
  146. my $self = $class->SUPER::new($parent, -1, Wx::wxDefaultPosition, Wx::wxDefaultSize, 0, "", $attrib);
  147. #==============================================================================================================================
  148. # if (Wx::wxVERSION >= 3.000003) {
  149. # # Wx 3.0.3 contains an ugly hack to support some advanced OpenGL attributes through the attribute list.
  150. # # The attribute list is transferred between the wxGLCanvas and wxGLContext constructors using a single static array s_wglContextAttribs.
  151. # # Immediatelly force creation of the OpenGL context to consume the static variable s_wglContextAttribs.
  152. # $self->GetContext();
  153. # }
  154. #==============================================================================================================================
  155. #==============================================================================================================================
  156. Slic3r::GUI::_3DScene::add_canvas($self, $self->GetContext);
  157. Slic3r::GUI::_3DScene::allow_multisample($self, $can_multisample);
  158. # my $context = $self->GetContext;
  159. # $self->SetCurrent($context);
  160. # Slic3r::GUI::_3DScene::add_canvas($self, $context);
  161. #
  162. # $self->{can_multisample} = $can_multisample;
  163. # $self->background(1);
  164. #==============================================================================================================================
  165. $self->_quat((0, 0, 0, 1));
  166. #==============================================================================================================================
  167. # $self->_stheta(45);
  168. # $self->_sphi(45);
  169. # $self->_zoom(1);
  170. # $self->_legend_enabled(0);
  171. # $self->_warning_enabled(0);
  172. # $self->use_plain_shader(0);
  173. # $self->_apply_zoom_to_volumes_filter(0);
  174. # $self->_mouse_dragging(0);
  175. #==============================================================================================================================
  176. # Collection of GLVolume objects
  177. $self->volumes(Slic3r::GUI::_3DScene::GLVolume::Collection->new);
  178. #==============================================================================================================================
  179. Slic3r::GUI::_3DScene::set_volumes($self, $self->volumes);
  180. #==============================================================================================================================
  181. # 3D point in model space
  182. #==============================================================================================================================
  183. # $self->_camera_type('ortho');
  184. ## $self->_camera_type('perspective');
  185. # $self->_camera_target(Slic3r::Pointf3->new(0,0,0));
  186. # $self->_camera_distance(0.);
  187. # $self->layer_editing_enabled(0);
  188. #==============================================================================================================================
  189. $self->{layer_height_edit_band_width} = 2.;
  190. $self->{layer_height_edit_strength} = 0.005;
  191. $self->{layer_height_edit_last_object_id} = -1;
  192. $self->{layer_height_edit_last_z} = 0.;
  193. $self->{layer_height_edit_last_action} = 0;
  194. #==============================================================================================================================
  195. Slic3r::GUI::_3DScene::reset_volumes($self);
  196. # $self->reset_objects;
  197. #==============================================================================================================================
  198. EVT_PAINT($self, sub {
  199. my $dc = Wx::PaintDC->new($self);
  200. $self->Render($dc);
  201. });
  202. #=======================================================================================================================
  203. # EVT_SIZE($self, sub { $self->_dirty(1) });
  204. #=======================================================================================================================
  205. #==============================================================================================================================
  206. # EVT_IDLE($self, sub {
  207. # return unless $self->_dirty;
  208. # return if !$self->IsShownOnScreen;
  209. # $self->Resize( $self->GetSizeWH );
  210. # $self->Refresh;
  211. # });
  212. #==============================================================================================================================
  213. EVT_MOUSEWHEEL($self, \&mouse_wheel_event);
  214. EVT_MOUSE_EVENTS($self, \&mouse_event);
  215. #==============================================================================================================================
  216. ## EVT_KEY_DOWN($self, sub {
  217. # EVT_CHAR($self, sub {
  218. # my ($s, $event) = @_;
  219. # if ($event->HasModifiers) {
  220. # $event->Skip;
  221. # } else {
  222. # my $key = $event->GetKeyCode;
  223. # if ($key == ord('0')) {
  224. # $self->select_view('iso');
  225. # } elsif ($key == ord('1')) {
  226. # $self->select_view('top');
  227. # } elsif ($key == ord('2')) {
  228. # $self->select_view('bottom');
  229. # } elsif ($key == ord('3')) {
  230. # $self->select_view('front');
  231. # } elsif ($key == ord('4')) {
  232. # $self->select_view('rear');
  233. # } elsif ($key == ord('5')) {
  234. # $self->select_view('left');
  235. # } elsif ($key == ord('6')) {
  236. # $self->select_view('right');
  237. # } elsif ($key == ord('z')) {
  238. # $self->zoom_to_volumes;
  239. # } elsif ($key == ord('b')) {
  240. # $self->zoom_to_bed;
  241. # } else {
  242. # $event->Skip;
  243. # }
  244. # }
  245. # });
  246. #==============================================================================================================================
  247. $self->{layer_height_edit_timer_id} = &Wx::NewId();
  248. $self->{layer_height_edit_timer} = Wx::Timer->new($self, $self->{layer_height_edit_timer_id});
  249. EVT_TIMER($self, $self->{layer_height_edit_timer_id}, sub {
  250. my ($self, $event) = @_;
  251. return if $self->_layer_height_edited != 1;
  252. return if $self->{layer_height_edit_last_object_id} == -1;
  253. $self->_variable_layer_thickness_action(undef);
  254. });
  255. #==============================================================================================================================
  256. my $on_mark_volumes_for_layer_height = sub {
  257. $self->mark_volumes_for_layer_height;
  258. };
  259. Slic3r::GUI::_3DScene::register_on_mark_volumes_for_layer_height_callback($self, $on_mark_volumes_for_layer_height);
  260. #==============================================================================================================================
  261. return $self;
  262. }
  263. #==============================================================================================================================
  264. #sub set_legend_enabled {
  265. # my ($self, $value) = @_;
  266. # $self->_legend_enabled($value);
  267. #}
  268. #
  269. #sub set_warning_enabled {
  270. # my ($self, $value) = @_;
  271. # $self->_warning_enabled($value);
  272. #}
  273. #==============================================================================================================================
  274. sub Destroy {
  275. my ($self) = @_;
  276. $self->{layer_height_edit_timer}->Stop;
  277. $self->DestroyGL;
  278. #==============================================================================================================================
  279. Slic3r::GUI::_3DScene::remove_canvas($self);
  280. #==============================================================================================================================
  281. return $self->SUPER::Destroy;
  282. }
  283. #==============================================================================================================================
  284. #sub layer_editing_enabled {
  285. # my ($self, $value) = @_;
  286. # if (@_ == 2) {
  287. # $self->{layer_editing_enabled} = $value;
  288. # if ($value) {
  289. # if (! $self->{layer_editing_initialized}) {
  290. # # Enabling the layer editing for the first time. This triggers compilation of the necessary OpenGL shaders.
  291. # # If compilation fails, a message box is shown with the error codes.
  292. # $self->SetCurrent($self->GetContext);
  293. # my $shader = new Slic3r::GUI::_3DScene::GLShader;
  294. # my $error_message;
  295. # if (! $shader->load_from_text($self->_fragment_shader_variable_layer_height, $self->_vertex_shader_variable_layer_height)) {
  296. # # Compilation or linking of the shaders failed.
  297. # $error_message = "Cannot compile an OpenGL Shader, therefore the Variable Layer Editing will be disabled.\n\n"
  298. # . $shader->last_error;
  299. # $shader = undef;
  300. # } else {
  301. # $self->{layer_height_edit_shader} = $shader;
  302. # ($self->{layer_preview_z_texture_id}) = glGenTextures_p(1);
  303. # glBindTexture(GL_TEXTURE_2D, $self->{layer_preview_z_texture_id});
  304. # glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  305. # glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  306. # glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  307. # glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  308. # glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  309. # glBindTexture(GL_TEXTURE_2D, 0);
  310. # }
  311. # if (defined($error_message)) {
  312. # # Don't enable the layer editing tool.
  313. # $self->{layer_editing_enabled} = 0;
  314. # # 2 means failed
  315. # $self->{layer_editing_initialized} = 2;
  316. # # Show the error message.
  317. # Wx::MessageBox($error_message, "Slic3r Error", wxOK | wxICON_EXCLAMATION, $self);
  318. # } else {
  319. # $self->{layer_editing_initialized} = 1;
  320. # }
  321. # } elsif ($self->{layer_editing_initialized} == 2) {
  322. # # Initilization failed before. Don't try to initialize and disable layer editing.
  323. # $self->{layer_editing_enabled} = 0;
  324. # }
  325. # }
  326. # }
  327. # return $self->{layer_editing_enabled};
  328. #}
  329. #==============================================================================================================================
  330. sub layer_editing_allowed {
  331. my ($self) = @_;
  332. # Allow layer editing if either the shaders were not initialized yet and we don't know
  333. # whether it will be possible to initialize them,
  334. # or if the initialization was done already and it failed.
  335. return ! (defined($self->{layer_editing_initialized}) && $self->{layer_editing_initialized} == 2);
  336. }
  337. sub _first_selected_object_id_for_variable_layer_height_editing {
  338. my ($self) = @_;
  339. for my $i (0..$#{$self->volumes}) {
  340. if ($self->volumes->[$i]->selected) {
  341. my $object_id = int($self->volumes->[$i]->select_group_id / 1000000);
  342. # Objects with object_id >= 1000 have a specific meaning, for example the wipe tower proxy.
  343. return ($object_id >= $self->{print}->object_count) ? -1 : $object_id
  344. if $object_id < 10000;
  345. }
  346. }
  347. return -1;
  348. }
  349. # Returns an array with (left, top, right, bottom) of the variable layer thickness bar on the screen.
  350. sub _variable_layer_thickness_bar_rect_screen {
  351. my ($self) = @_;
  352. my ($cw, $ch) = $self->GetSizeWH;
  353. return ($cw - VARIABLE_LAYER_THICKNESS_BAR_WIDTH, 0, $cw, $ch - VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT);
  354. }
  355. sub _variable_layer_thickness_bar_rect_viewport {
  356. my ($self) = @_;
  357. my ($cw, $ch) = $self->GetSizeWH;
  358. #==============================================================================================================================
  359. my $zoom = Slic3r::GUI::_3DScene::get_camera_zoom($self);
  360. return ((0.5*$cw-VARIABLE_LAYER_THICKNESS_BAR_WIDTH)/$zoom, (-0.5*$ch+VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT)/$zoom, $cw/(2*$zoom), $ch/(2*$zoom));
  361. # return ((0.5*$cw-VARIABLE_LAYER_THICKNESS_BAR_WIDTH)/$self->_zoom, (-0.5*$ch+VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT)/$self->_zoom, $cw/(2*$self->_zoom), $ch/(2*$self->_zoom));
  362. #==============================================================================================================================
  363. }
  364. # Returns an array with (left, top, right, bottom) of the variable layer thickness bar on the screen.
  365. sub _variable_layer_thickness_reset_rect_screen {
  366. my ($self) = @_;
  367. my ($cw, $ch) = $self->GetSizeWH;
  368. return ($cw - VARIABLE_LAYER_THICKNESS_BAR_WIDTH, $ch - VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT, $cw, $ch);
  369. }
  370. sub _variable_layer_thickness_reset_rect_viewport {
  371. my ($self) = @_;
  372. my ($cw, $ch) = $self->GetSizeWH;
  373. #==============================================================================================================================
  374. my $zoom = Slic3r::GUI::_3DScene::get_camera_zoom($self);
  375. return ((0.5*$cw-VARIABLE_LAYER_THICKNESS_BAR_WIDTH)/$zoom, -$ch/(2*$zoom), $cw/(2*$zoom), (-0.5*$ch+VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT)/$zoom);
  376. # return ((0.5*$cw-VARIABLE_LAYER_THICKNESS_BAR_WIDTH)/$self->_zoom, -$ch/(2*$self->_zoom), $cw/(2*$self->_zoom), (-0.5*$ch+VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT)/$self->_zoom);
  377. #==============================================================================================================================
  378. }
  379. sub _variable_layer_thickness_bar_rect_mouse_inside {
  380. my ($self, $mouse_evt) = @_;
  381. my ($bar_left, $bar_top, $bar_right, $bar_bottom) = $self->_variable_layer_thickness_bar_rect_screen;
  382. return $mouse_evt->GetX >= $bar_left && $mouse_evt->GetX <= $bar_right && $mouse_evt->GetY >= $bar_top && $mouse_evt->GetY <= $bar_bottom;
  383. }
  384. sub _variable_layer_thickness_reset_rect_mouse_inside {
  385. my ($self, $mouse_evt) = @_;
  386. my ($bar_left, $bar_top, $bar_right, $bar_bottom) = $self->_variable_layer_thickness_reset_rect_screen;
  387. return $mouse_evt->GetX >= $bar_left && $mouse_evt->GetX <= $bar_right && $mouse_evt->GetY >= $bar_top && $mouse_evt->GetY <= $bar_bottom;
  388. }
  389. sub _variable_layer_thickness_bar_mouse_cursor_z_relative {
  390. my ($self) = @_;
  391. my $mouse_pos = $self->ScreenToClientPoint(Wx::GetMousePosition());
  392. my ($bar_left, $bar_top, $bar_right, $bar_bottom) = $self->_variable_layer_thickness_bar_rect_screen;
  393. return ($mouse_pos->x >= $bar_left && $mouse_pos->x <= $bar_right && $mouse_pos->y >= $bar_top && $mouse_pos->y <= $bar_bottom) ?
  394. # Inside the bar.
  395. ($bar_bottom - $mouse_pos->y - 1.) / ($bar_bottom - $bar_top - 1) :
  396. # Outside the bar.
  397. -1000.;
  398. }
  399. sub _variable_layer_thickness_action {
  400. my ($self, $mouse_event, $do_modification) = @_;
  401. # A volume is selected. Test, whether hovering over a layer thickness bar.
  402. return if $self->{layer_height_edit_last_object_id} == -1;
  403. if (defined($mouse_event)) {
  404. my ($bar_left, $bar_top, $bar_right, $bar_bottom) = $self->_variable_layer_thickness_bar_rect_screen;
  405. $self->{layer_height_edit_last_z} = unscale($self->{print}->get_object($self->{layer_height_edit_last_object_id})->size->z)
  406. * ($bar_bottom - $mouse_event->GetY - 1.) / ($bar_bottom - $bar_top);
  407. $self->{layer_height_edit_last_action} = $mouse_event->ShiftDown ? ($mouse_event->RightIsDown ? 3 : 2) : ($mouse_event->RightIsDown ? 0 : 1);
  408. }
  409. # Mark the volume as modified, so Print will pick its layer height profile? Where to mark it?
  410. # Start a timer to refresh the print? schedule_background_process() ?
  411. # The PrintObject::adjust_layer_height_profile() call adjusts the profile of its associated ModelObject, it does not modify the profile of the PrintObject itself.
  412. $self->{print}->get_object($self->{layer_height_edit_last_object_id})->adjust_layer_height_profile(
  413. $self->{layer_height_edit_last_z},
  414. $self->{layer_height_edit_strength},
  415. $self->{layer_height_edit_band_width},
  416. $self->{layer_height_edit_last_action});
  417. $self->volumes->[$self->{layer_height_edit_last_object_id}]->generate_layer_height_texture(
  418. $self->{print}->get_object($self->{layer_height_edit_last_object_id}), 1);
  419. $self->Refresh;
  420. # Automatic action on mouse down with the same coordinate.
  421. $self->{layer_height_edit_timer}->Start(100, wxTIMER_CONTINUOUS);
  422. }
  423. sub mouse_event {
  424. my ($self, $e) = @_;
  425. my $pos = Slic3r::Pointf->new($e->GetPositionXY);
  426. #==============================================================================================================================
  427. my $object_idx_selected = $self->{layer_height_edit_last_object_id} = (Slic3r::GUI::_3DScene::is_layers_editing_enabled($self) && $self->{print}) ? $self->_first_selected_object_id_for_variable_layer_height_editing : -1;
  428. # my $object_idx_selected = $self->{layer_height_edit_last_object_id} = ($self->layer_editing_enabled && $self->{print}) ? $self->_first_selected_object_id_for_variable_layer_height_editing : -1;
  429. #==============================================================================================================================
  430. #==============================================================================================================================
  431. Slic3r::GUI::_3DScene::set_mouse_dragging($self, $e->Dragging);
  432. # $self->_mouse_dragging($e->Dragging);
  433. #==============================================================================================================================
  434. if ($e->Entering && (&Wx::wxMSW || $^O eq 'linux')) {
  435. # wxMSW needs focus in order to catch mouse wheel events
  436. $self->SetFocus;
  437. $self->_drag_start_xy(undef);
  438. } elsif ($e->LeftDClick) {
  439. if ($object_idx_selected != -1 && $self->_variable_layer_thickness_bar_rect_mouse_inside($e)) {
  440. } elsif ($self->on_double_click) {
  441. $self->on_double_click->();
  442. }
  443. } elsif ($e->LeftDown || $e->RightDown) {
  444. # If user pressed left or right button we first check whether this happened
  445. # on a volume or not.
  446. #==============================================================================================================================
  447. my $volume_idx = Slic3r::GUI::_3DScene::get_hover_volume_id($self);
  448. # my $volume_idx = $self->_hover_volume_idx // -1;
  449. #==============================================================================================================================
  450. $self->_layer_height_edited(0);
  451. if ($object_idx_selected != -1 && $self->_variable_layer_thickness_bar_rect_mouse_inside($e)) {
  452. # A volume is selected and the mouse is hovering over a layer thickness bar.
  453. # Start editing the layer height.
  454. $self->_layer_height_edited(1);
  455. $self->_variable_layer_thickness_action($e);
  456. } elsif ($object_idx_selected != -1 && $self->_variable_layer_thickness_reset_rect_mouse_inside($e)) {
  457. $self->{print}->get_object($object_idx_selected)->reset_layer_height_profile;
  458. # Index 2 means no editing, just wait for mouse up event.
  459. $self->_layer_height_edited(2);
  460. $self->Refresh;
  461. $self->Update;
  462. } else {
  463. # The mouse_to_3d gets the Z coordinate from the Z buffer at the screen coordinate $pos->x,y,
  464. # an converts the screen space coordinate to unscaled object space.
  465. my $pos3d = ($volume_idx == -1) ? undef : $self->mouse_to_3d(@$pos);
  466. # Select volume in this 3D canvas.
  467. # Don't deselect a volume if layer editing is enabled. We want the object to stay selected
  468. # during the scene manipulation.
  469. #==============================================================================================================================
  470. #==============================================================================================================================
  471. if (Slic3r::GUI::_3DScene::is_picking_enabled($self) && ($volume_idx != -1 || ! Slic3r::GUI::_3DScene::is_layers_editing_enabled($self))) {
  472. Slic3r::GUI::_3DScene::deselect_volumes($self);
  473. Slic3r::GUI::_3DScene::select_volume($self, $volume_idx);
  474. # if ($self->enable_picking && ($volume_idx != -1 || ! $self->layer_editing_enabled)) {
  475. # $self->deselect_volumes;
  476. # $self->select_volume($volume_idx);
  477. #==============================================================================================================================
  478. if ($volume_idx != -1) {
  479. my $group_id = $self->volumes->[$volume_idx]->select_group_id;
  480. my @volumes;
  481. if ($group_id != -1) {
  482. #==============================================================================================================================
  483. Slic3r::GUI::_3DScene::select_volume($self, $_)
  484. # $self->select_volume($_)
  485. #==============================================================================================================================
  486. for grep $self->volumes->[$_]->select_group_id == $group_id,
  487. 0..$#{$self->volumes};
  488. }
  489. }
  490. $self->Refresh;
  491. $self->Update;
  492. }
  493. # propagate event through callback
  494. $self->on_select->($volume_idx)
  495. if $self->on_select;
  496. if ($volume_idx != -1) {
  497. if ($e->LeftDown && $self->enable_moving) {
  498. # Only accept the initial position, if it is inside the volume bounding box.
  499. my $volume_bbox = $self->volumes->[$volume_idx]->transformed_bounding_box;
  500. $volume_bbox->offset(1.);
  501. if ($volume_bbox->contains_point($pos3d)) {
  502. # The dragging operation is initiated.
  503. $self->_drag_volume_idx($volume_idx);
  504. $self->_drag_start_pos($pos3d);
  505. # Remember the shift to to the object center. The object center will later be used
  506. # to limit the object placement close to the bed.
  507. $self->_drag_volume_center_offset($pos3d->vector_to($volume_bbox->center));
  508. }
  509. } elsif ($e->RightDown) {
  510. # if right clicking on volume, propagate event through callback
  511. $self->on_right_click->($e->GetPosition)
  512. if $self->on_right_click;
  513. }
  514. }
  515. }
  516. } elsif ($e->Dragging && $e->LeftIsDown && ! $self->_layer_height_edited && defined($self->_drag_volume_idx)) {
  517. # Get new position at the same Z of the initial click point.
  518. my $cur_pos = Slic3r::Linef3->new(
  519. $self->mouse_to_3d($e->GetX, $e->GetY, 0),
  520. $self->mouse_to_3d($e->GetX, $e->GetY, 1))
  521. ->intersect_plane($self->_drag_start_pos->z);
  522. #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  523. # >>>>>>>>>>>>>>>>>>>>>>>>>> TEMPORARY DISABLED DUE TO bed_polygon REMOVAL <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  524. #
  525. # # Clip the new position, so the object center remains close to the bed.
  526. # {
  527. # $cur_pos->translate(@{$self->_drag_volume_center_offset});
  528. # my $cur_pos2 = Slic3r::Point->new(scale($cur_pos->x), scale($cur_pos->y));
  529. # if (! $self->bed_polygon->contains_point($cur_pos2)) {
  530. # my $ip = $self->bed_polygon->point_projection($cur_pos2);
  531. # $cur_pos->set_x(unscale($ip->x));
  532. # $cur_pos->set_y(unscale($ip->y));
  533. # }
  534. # $cur_pos->translate(@{$self->_drag_volume_center_offset->negative});
  535. # }
  536. #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  537. # Calculate the translation vector.
  538. my $vector = $self->_drag_start_pos->vector_to($cur_pos);
  539. # Get the volume being dragged.
  540. my $volume = $self->volumes->[$self->_drag_volume_idx];
  541. # Get all volumes belonging to the same group, if any.
  542. my @volumes = ($volume->drag_group_id == -1) ?
  543. ($volume) :
  544. grep $_->drag_group_id == $volume->drag_group_id, @{$self->volumes};
  545. # Apply new temporary volume origin and ignore Z.
  546. $_->translate($vector->x, $vector->y, 0) for @volumes;
  547. $self->_drag_start_pos($cur_pos);
  548. $self->_dragged(1);
  549. $self->Refresh;
  550. $self->Update;
  551. } elsif ($e->Dragging) {
  552. if ($self->_layer_height_edited && $object_idx_selected != -1) {
  553. $self->_variable_layer_thickness_action($e) if ($self->_layer_height_edited == 1);
  554. } elsif ($e->LeftIsDown) {
  555. # if dragging over blank area with left button, rotate
  556. if (defined $self->_drag_start_pos) {
  557. my $orig = $self->_drag_start_pos;
  558. if (TURNTABLE_MODE) {
  559. # Turntable mode is enabled by default.
  560. #==============================================================================================================================
  561. Slic3r::GUI::_3DScene::set_camera_phi($self, Slic3r::GUI::_3DScene::get_camera_phi($self) + ($pos->x - $orig->x) * TRACKBALLSIZE);
  562. Slic3r::GUI::_3DScene::set_camera_theta($self, Slic3r::GUI::_3DScene::get_camera_theta($self) - ($pos->y - $orig->y) * TRACKBALLSIZE);
  563. # $self->_sphi($self->_sphi + ($pos->x - $orig->x) * TRACKBALLSIZE);
  564. # $self->_stheta($self->_stheta - ($pos->y - $orig->y) * TRACKBALLSIZE); #-
  565. # $self->_stheta(GIMBALL_LOCK_THETA_MAX) if $self->_stheta > GIMBALL_LOCK_THETA_MAX;
  566. # $self->_stheta(0) if $self->_stheta < 0;
  567. #==============================================================================================================================
  568. } else {
  569. my $size = $self->GetClientSize;
  570. my @quat = trackball(
  571. $orig->x / ($size->width / 2) - 1,
  572. 1 - $orig->y / ($size->height / 2), #/
  573. $pos->x / ($size->width / 2) - 1,
  574. 1 - $pos->y / ($size->height / 2), #/
  575. );
  576. $self->_quat(mulquats($self->_quat, \@quat));
  577. }
  578. $self->on_viewport_changed->() if $self->on_viewport_changed;
  579. $self->Refresh;
  580. $self->Update;
  581. }
  582. $self->_drag_start_pos($pos);
  583. } elsif ($e->MiddleIsDown || $e->RightIsDown) {
  584. # If dragging over blank area with right button, pan.
  585. if (defined $self->_drag_start_xy) {
  586. # get point in model space at Z = 0
  587. my $cur_pos = $self->mouse_to_3d($e->GetX, $e->GetY, 0);
  588. my $orig = $self->mouse_to_3d($self->_drag_start_xy->x, $self->_drag_start_xy->y, 0);
  589. #==============================================================================================================================
  590. my $camera_target = Slic3r::GUI::_3DScene::get_camera_target($self);
  591. $camera_target->translate(@{$orig->vector_to($cur_pos)->negative});
  592. Slic3r::GUI::_3DScene::set_camera_target($self, $camera_target);
  593. # $self->_camera_target->translate(@{$orig->vector_to($cur_pos)->negative});
  594. #==============================================================================================================================
  595. $self->on_viewport_changed->() if $self->on_viewport_changed;
  596. $self->Refresh;
  597. $self->Update;
  598. }
  599. $self->_drag_start_xy($pos);
  600. }
  601. } elsif ($e->LeftUp || $e->MiddleUp || $e->RightUp) {
  602. if ($self->_layer_height_edited) {
  603. $self->_layer_height_edited(undef);
  604. $self->{layer_height_edit_timer}->Stop;
  605. $self->on_model_update->()
  606. if ($object_idx_selected != -1 && $self->on_model_update);
  607. } elsif ($self->on_move && defined($self->_drag_volume_idx) && $self->_dragged) {
  608. # get all volumes belonging to the same group, if any
  609. my @volume_idxs;
  610. my $group_id = $self->volumes->[$self->_drag_volume_idx]->drag_group_id;
  611. if ($group_id == -1) {
  612. @volume_idxs = ($self->_drag_volume_idx);
  613. } else {
  614. @volume_idxs = grep $self->volumes->[$_]->drag_group_id == $group_id,
  615. 0..$#{$self->volumes};
  616. }
  617. $self->on_move->(@volume_idxs);
  618. }
  619. $self->_drag_volume_idx(undef);
  620. $self->_drag_start_pos(undef);
  621. $self->_drag_start_xy(undef);
  622. $self->_dragged(undef);
  623. } elsif ($e->Moving) {
  624. #==============================================================================================================================
  625. Slic3r::GUI::_3DScene::set_mouse_position($self, $pos);
  626. # $self->_mouse_pos($pos);
  627. #==============================================================================================================================
  628. # Only refresh if picking is enabled, in that case the objects may get highlighted if the mouse cursor
  629. # hovers over.
  630. #==============================================================================================================================
  631. if (Slic3r::GUI::_3DScene::is_picking_enabled($self)) {
  632. # if ($self->enable_picking) {
  633. #==============================================================================================================================
  634. $self->Update;
  635. $self->Refresh;
  636. }
  637. } else {
  638. $e->Skip();
  639. }
  640. }
  641. sub mouse_wheel_event {
  642. my ($self, $e) = @_;
  643. if ($e->MiddleIsDown) {
  644. # Ignore the wheel events if the middle button is pressed.
  645. return;
  646. }
  647. #==============================================================================================================================
  648. if (Slic3r::GUI::_3DScene::is_layers_editing_enabled($self) && $self->{print}) {
  649. # if ($self->layer_editing_enabled && $self->{print}) {
  650. #==============================================================================================================================
  651. my $object_idx_selected = $self->_first_selected_object_id_for_variable_layer_height_editing;
  652. if ($object_idx_selected != -1) {
  653. # A volume is selected. Test, whether hovering over a layer thickness bar.
  654. if ($self->_variable_layer_thickness_bar_rect_mouse_inside($e)) {
  655. # Adjust the width of the selection.
  656. $self->{layer_height_edit_band_width} = max(min($self->{layer_height_edit_band_width} * (1 + 0.1 * $e->GetWheelRotation() / $e->GetWheelDelta()), 10.), 1.5);
  657. $self->Refresh;
  658. return;
  659. }
  660. }
  661. }
  662. # Calculate the zoom delta and apply it to the current zoom factor
  663. my $zoom = $e->GetWheelRotation() / $e->GetWheelDelta();
  664. $zoom = max(min($zoom, 4), -4);
  665. $zoom /= 10;
  666. #==============================================================================================================================
  667. $zoom = Slic3r::GUI::_3DScene::get_camera_zoom($self) / (1-$zoom);
  668. # $zoom = $self->_zoom / (1-$zoom);
  669. #==============================================================================================================================
  670. # Don't allow to zoom too far outside the scene.
  671. #==============================================================================================================================
  672. my $zoom_min = $self->get_zoom_to_bounding_box_factor(Slic3r::GUI::_3DScene::get_max_bounding_box($self));
  673. # my $zoom_min = $self->get_zoom_to_bounding_box_factor($self->max_bounding_box);
  674. #==============================================================================================================================
  675. $zoom_min *= 0.4 if defined $zoom_min;
  676. $zoom = $zoom_min if defined $zoom_min && $zoom < $zoom_min;
  677. #==============================================================================================================================
  678. $zoom = Slic3r::GUI::_3DScene::set_camera_zoom($self, $zoom);
  679. # $self->_zoom($zoom);
  680. #==============================================================================================================================
  681. # # In order to zoom around the mouse point we need to translate
  682. # # the camera target
  683. # my $size = Slic3r::Pointf->new($self->GetSizeWH);
  684. # my $pos = Slic3r::Pointf->new($e->GetX, $size->y - $e->GetY); #-
  685. # $self->_camera_target->translate(
  686. # # ($pos - $size/2) represents the vector from the viewport center
  687. # # to the mouse point. By multiplying it by $zoom we get the new,
  688. # # transformed, length of such vector.
  689. # # Since we want that point to stay fixed, we move our camera target
  690. # # in the opposite direction by the delta of the length of such vector
  691. # # ($zoom - 1). We then scale everything by 1/$self->_zoom since
  692. # # $self->_camera_target is expressed in terms of model units.
  693. # -($pos->x - $size->x/2) * ($zoom) / $self->_zoom,
  694. # -($pos->y - $size->y/2) * ($zoom) / $self->_zoom,
  695. # 0,
  696. # ) if 0;
  697. $self->on_viewport_changed->() if $self->on_viewport_changed;
  698. #==============================================================================================================================
  699. #==============================================================================================================================
  700. Slic3r::GUI::_3DScene::resize($self, $self->GetSizeWH) if Slic3r::GUI::_3DScene::is_shown_on_screen($self);
  701. # $self->Resize($self->GetSizeWH) if $self->IsShownOnScreen;
  702. #==============================================================================================================================
  703. $self->Refresh;
  704. }
  705. #==============================================================================================================================
  706. ## Reset selection.
  707. #sub reset_objects {
  708. # my ($self) = @_;
  709. # if ($self->GetContext) {
  710. # $self->SetCurrent($self->GetContext);
  711. # $self->volumes->release_geometry;
  712. # }
  713. # $self->volumes->erase;
  714. # $self->_dirty(1);
  715. #}
  716. #==============================================================================================================================
  717. # Setup camera to view all objects.
  718. sub set_viewport_from_scene {
  719. my ($self, $scene) = @_;
  720. #==============================================================================================================================
  721. Slic3r::GUI::_3DScene::set_camera_phi($self, Slic3r::GUI::_3DScene::get_camera_phi($scene));
  722. Slic3r::GUI::_3DScene::set_camera_theta($self, Slic3r::GUI::_3DScene::get_camera_theta($scene));
  723. Slic3r::GUI::_3DScene::set_camera_target($self, Slic3r::GUI::_3DScene::get_camera_target($scene));
  724. Slic3r::GUI::_3DScene::set_camera_zoom($self, Slic3r::GUI::_3DScene::get_camera_zoom($scene));
  725. # $self->_sphi($scene->_sphi);
  726. # $self->_stheta($scene->_stheta);
  727. # $self->_camera_target($scene->_camera_target);
  728. # $self->_zoom($scene->_zoom);
  729. #==============================================================================================================================
  730. $self->_quat($scene->_quat);
  731. #==============================================================================================================================
  732. Slic3r::GUI::_3DScene::set_dirty($self, 1);
  733. # $self->_dirty(1);
  734. #==============================================================================================================================
  735. }
  736. #==============================================================================================================================
  737. ## Set the camera to a default orientation,
  738. ## zoom to volumes.
  739. #sub select_view {
  740. # my ($self, $direction) = @_;
  741. #
  742. # my $dirvec;
  743. # if (ref($direction)) {
  744. # $dirvec = $direction;
  745. # } else {
  746. # if ($direction eq 'iso') {
  747. # $dirvec = VIEW_DEFAULT;
  748. # } elsif ($direction eq 'left') {
  749. # $dirvec = VIEW_LEFT;
  750. # } elsif ($direction eq 'right') {
  751. # $dirvec = VIEW_RIGHT;
  752. # } elsif ($direction eq 'top') {
  753. # $dirvec = VIEW_TOP;
  754. # } elsif ($direction eq 'bottom') {
  755. # $dirvec = VIEW_BOTTOM;
  756. # } elsif ($direction eq 'front') {
  757. # $dirvec = VIEW_FRONT;
  758. # } elsif ($direction eq 'rear') {
  759. # $dirvec = VIEW_REAR;
  760. # }
  761. # }
  762. # my $bb = $self->volumes_bounding_box;
  763. # if (! $bb->empty) {
  764. # $self->_sphi($dirvec->[0]);
  765. # $self->_stheta($dirvec->[1]);
  766. # # Avoid gimball lock.
  767. # $self->_stheta(GIMBALL_LOCK_THETA_MAX) if $self->_stheta > GIMBALL_LOCK_THETA_MAX;
  768. # $self->_stheta(0) if $self->_stheta < 0;
  769. # $self->on_viewport_changed->() if $self->on_viewport_changed;
  770. # $self->Refresh;
  771. # }
  772. #}
  773. #==============================================================================================================================
  774. sub get_zoom_to_bounding_box_factor {
  775. my ($self, $bb) = @_;
  776. my $max_bb_size = max(@{ $bb->size });
  777. return undef if ($max_bb_size == 0);
  778. # project the bbox vertices on a plane perpendicular to the camera forward axis
  779. # then calculates the vertices coordinate on this plane along the camera xy axes
  780. # we need the view matrix, we let opengl calculate it (same as done in render sub)
  781. glMatrixMode(GL_MODELVIEW);
  782. glLoadIdentity();
  783. if (!TURNTABLE_MODE) {
  784. # Shift the perspective camera.
  785. #==============================================================================================================================
  786. my $camera_pos = Slic3r::Pointf3->new(0,0,-Slic3r::GUI::_3DScene::get_camera_distance($self));
  787. # my $camera_pos = Slic3r::Pointf3->new(0,0,-$self->_camera_distance);
  788. #==============================================================================================================================
  789. glTranslatef(@$camera_pos);
  790. }
  791. if (TURNTABLE_MODE) {
  792. # Turntable mode is enabled by default.
  793. #==============================================================================================================================
  794. glRotatef(-Slic3r::GUI::_3DScene::get_camera_theta($self), 1, 0, 0); # pitch
  795. glRotatef(Slic3r::GUI::_3DScene::get_camera_phi($self), 0, 0, 1); # yaw
  796. # glRotatef(-$self->_stheta, 1, 0, 0); # pitch
  797. # glRotatef($self->_sphi, 0, 0, 1); # yaw
  798. #==============================================================================================================================
  799. } else {
  800. # Shift the perspective camera.
  801. #==============================================================================================================================
  802. my $camera_pos = Slic3r::Pointf3->new(0,0,-Slic3r::GUI::_3DScene::get_camera_distance($self));
  803. # my $camera_pos = Slic3r::Pointf3->new(0,0,-$self->_camera_distance);
  804. #==============================================================================================================================
  805. glTranslatef(@$camera_pos);
  806. my @rotmat = quat_to_rotmatrix($self->quat);
  807. glMultMatrixd_p(@rotmat[0..15]);
  808. }
  809. #==============================================================================================================================
  810. glTranslatef(@{ Slic3r::GUI::_3DScene::get_camera_target($self)->negative });
  811. # glTranslatef(@{ $self->_camera_target->negative });
  812. #==============================================================================================================================
  813. # get the view matrix back from opengl
  814. my @matrix = glGetFloatv_p(GL_MODELVIEW_MATRIX);
  815. # camera axes
  816. my $right = Slic3r::Pointf3->new($matrix[0], $matrix[4], $matrix[8]);
  817. my $up = Slic3r::Pointf3->new($matrix[1], $matrix[5], $matrix[9]);
  818. my $forward = Slic3r::Pointf3->new($matrix[2], $matrix[6], $matrix[10]);
  819. my $bb_min = $bb->min_point();
  820. my $bb_max = $bb->max_point();
  821. my $bb_center = $bb->center();
  822. # bbox vertices in world space
  823. my @vertices = ();
  824. push(@vertices, $bb_min);
  825. push(@vertices, Slic3r::Pointf3->new($bb_max->x(), $bb_min->y(), $bb_min->z()));
  826. push(@vertices, Slic3r::Pointf3->new($bb_max->x(), $bb_max->y(), $bb_min->z()));
  827. push(@vertices, Slic3r::Pointf3->new($bb_min->x(), $bb_max->y(), $bb_min->z()));
  828. push(@vertices, Slic3r::Pointf3->new($bb_min->x(), $bb_min->y(), $bb_max->z()));
  829. push(@vertices, Slic3r::Pointf3->new($bb_max->x(), $bb_min->y(), $bb_max->z()));
  830. push(@vertices, $bb_max);
  831. push(@vertices, Slic3r::Pointf3->new($bb_min->x(), $bb_max->y(), $bb_max->z()));
  832. my $max_x = 0.0;
  833. my $max_y = 0.0;
  834. # margin factor to give some empty space around the bbox
  835. my $margin_factor = 1.25;
  836. foreach my $v (@vertices) {
  837. # project vertex on the plane perpendicular to camera forward axis
  838. my $pos = Slic3r::Pointf3->new($v->x() - $bb_center->x(), $v->y() - $bb_center->y(), $v->z() - $bb_center->z());
  839. my $proj_on_normal = $pos->x() * $forward->x() + $pos->y() * $forward->y() + $pos->z() * $forward->z();
  840. my $proj_on_plane = Slic3r::Pointf3->new($pos->x() - $proj_on_normal * $forward->x(), $pos->y() - $proj_on_normal * $forward->y(), $pos->z() - $proj_on_normal * $forward->z());
  841. # calculates vertex coordinate along camera xy axes
  842. my $x_on_plane = $proj_on_plane->x() * $right->x() + $proj_on_plane->y() * $right->y() + $proj_on_plane->z() * $right->z();
  843. my $y_on_plane = $proj_on_plane->x() * $up->x() + $proj_on_plane->y() * $up->y() + $proj_on_plane->z() * $up->z();
  844. $max_x = max($max_x, $margin_factor * 2 * abs($x_on_plane));
  845. $max_y = max($max_y, $margin_factor * 2 * abs($y_on_plane));
  846. }
  847. return undef if (($max_x == 0) || ($max_y == 0));
  848. my ($cw, $ch) = $self->GetSizeWH;
  849. my $min_ratio = min($cw / $max_x, $ch / $max_y);
  850. return $min_ratio;
  851. }
  852. #==============================================================================================================================
  853. #sub zoom_to_bounding_box {
  854. # my ($self, $bb) = @_;
  855. # # Calculate the zoom factor needed to adjust viewport to bounding box.
  856. # my $zoom = $self->get_zoom_to_bounding_box_factor($bb);
  857. # if (defined $zoom) {
  858. # $self->_zoom($zoom);
  859. # # center view around bounding box center
  860. # $self->_camera_target($bb->center);
  861. # $self->on_viewport_changed->() if $self->on_viewport_changed;
  862. # $self->Resize($self->GetSizeWH) if $self->IsShownOnScreen;
  863. # $self->Refresh;
  864. # }
  865. #}
  866. #
  867. #sub zoom_to_bed {
  868. # my ($self) = @_;
  869. #
  870. # if ($self->bed_shape) {
  871. # $self->zoom_to_bounding_box($self->bed_bounding_box);
  872. # }
  873. #}
  874. #
  875. #sub zoom_to_volume {
  876. # my ($self, $volume_idx) = @_;
  877. #
  878. # my $volume = $self->volumes->[$volume_idx];
  879. # my $bb = $volume->transformed_bounding_box;
  880. # $self->zoom_to_bounding_box($bb);
  881. #}
  882. #
  883. #sub zoom_to_volumes {
  884. # my ($self) = @_;
  885. #
  886. # $self->_apply_zoom_to_volumes_filter(1);
  887. # $self->zoom_to_bounding_box($self->volumes_bounding_box);
  888. # $self->_apply_zoom_to_volumes_filter(0);
  889. #}
  890. #
  891. #sub volumes_bounding_box {
  892. # my ($self) = @_;
  893. #
  894. # my $bb = Slic3r::Geometry::BoundingBoxf3->new;
  895. # foreach my $v (@{$self->volumes}) {
  896. # $bb->merge($v->transformed_bounding_box) if (! $self->_apply_zoom_to_volumes_filter || $v->zoom_to_volumes);
  897. # }
  898. # return $bb;
  899. #}
  900. #
  901. #sub bed_bounding_box {
  902. # my ($self) = @_;
  903. #
  904. # my $bb = Slic3r::Geometry::BoundingBoxf3->new;
  905. # if ($self->bed_shape) {
  906. # $bb->merge_point(Slic3r::Pointf3->new(@$_, 0)) for @{$self->bed_shape};
  907. # }
  908. # return $bb;
  909. #}
  910. #
  911. #sub max_bounding_box {
  912. # my ($self) = @_;
  913. #
  914. # my $bb = $self->bed_bounding_box;
  915. # $bb->merge($self->volumes_bounding_box);
  916. # return $bb;
  917. #}
  918. #
  919. ## Used by ObjectCutDialog and ObjectPartsPanel to generate a rectangular ground plane
  920. ## to support the scene objects.
  921. #sub set_auto_bed_shape {
  922. # my ($self, $bed_shape) = @_;
  923. #
  924. # # draw a default square bed around object center
  925. # my $max_size = max(@{ $self->volumes_bounding_box->size });
  926. # my $center = $self->volumes_bounding_box->center;
  927. # $self->set_bed_shape([
  928. # [ $center->x - $max_size, $center->y - $max_size ], #--
  929. # [ $center->x + $max_size, $center->y - $max_size ], #--
  930. # [ $center->x + $max_size, $center->y + $max_size ], #++
  931. # [ $center->x - $max_size, $center->y + $max_size ], #++
  932. # ]);
  933. # # Set the origin for painting of the coordinate system axes.
  934. # $self->origin(Slic3r::Pointf->new(@$center[X,Y]));
  935. #}
  936. #
  937. ## Set the bed shape to a single closed 2D polygon (array of two element arrays),
  938. ## triangulate the bed and store the triangles into $self->bed_triangles,
  939. ## fills the $self->bed_grid_lines and sets $self->origin.
  940. ## Sets $self->bed_polygon to limit the object placement.
  941. #sub set_bed_shape {
  942. # my ($self, $bed_shape) = @_;
  943. #
  944. # $self->bed_shape($bed_shape);
  945. #
  946. # # triangulate bed
  947. # my $expolygon = Slic3r::ExPolygon->new([ map [map scale($_), @$_], @$bed_shape ]);
  948. # my $bed_bb = $expolygon->bounding_box;
  949. #
  950. # {
  951. # my @points = ();
  952. # foreach my $triangle (@{ $expolygon->triangulate }) {
  953. # push @points, map {+ unscale($_->x), unscale($_->y), GROUND_Z } @$triangle;
  954. # }
  955. # $self->bed_triangles(OpenGL::Array->new_list(GL_FLOAT, @points));
  956. # }
  957. #
  958. # {
  959. # my @polylines = ();
  960. # for (my $x = $bed_bb->x_min; $x <= $bed_bb->x_max; $x += scale 10) {
  961. # push @polylines, Slic3r::Polyline->new([$x,$bed_bb->y_min], [$x,$bed_bb->y_max]);
  962. # }
  963. # for (my $y = $bed_bb->y_min; $y <= $bed_bb->y_max; $y += scale 10) {
  964. # push @polylines, Slic3r::Polyline->new([$bed_bb->x_min,$y], [$bed_bb->x_max,$y]);
  965. # }
  966. # # clip with a slightly grown expolygon because our lines lay on the contours and
  967. # # may get erroneously clipped
  968. # my @lines = map Slic3r::Line->new(@$_[0,-1]),
  969. # @{intersection_pl(\@polylines, [ @{$expolygon->offset(+scaled_epsilon)} ])};
  970. #
  971. # # append bed contours
  972. # push @lines, map @{$_->lines}, @$expolygon;
  973. #
  974. # my @points = ();
  975. # foreach my $line (@lines) {
  976. # push @points, map {+ unscale($_->x), unscale($_->y), GROUND_Z } @$line; #))
  977. # }
  978. # $self->bed_grid_lines(OpenGL::Array->new_list(GL_FLOAT, @points));
  979. # }
  980. #
  981. # # Set the origin for painting of the coordinate system axes.
  982. # $self->origin(Slic3r::Pointf->new(0,0));
  983. #
  984. # $self->bed_polygon(offset_ex([$expolygon->contour], $bed_bb->radius * 1.7, JT_ROUND, scale(0.5))->[0]->contour->clone);
  985. #}
  986. #
  987. #sub deselect_volumes {
  988. # my ($self) = @_;
  989. # $_->set_selected(0) for @{$self->volumes};
  990. #}
  991. #
  992. #sub select_volume {
  993. # my ($self, $volume_idx) = @_;
  994. #
  995. # return if ($volume_idx >= scalar(@{$self->volumes}));
  996. #
  997. # $self->volumes->[$volume_idx]->set_selected(1)
  998. # if $volume_idx != -1;
  999. #}
  1000. #
  1001. #sub SetCuttingPlane {
  1002. # my ($self, $z, $expolygons) = @_;
  1003. #
  1004. # $self->cutting_plane_z($z);
  1005. #
  1006. # # grow slices in order to display them better
  1007. # $expolygons = offset_ex([ map @$_, @$expolygons ], scale 0.1);
  1008. #
  1009. # my @verts = ();
  1010. # foreach my $line (map @{$_->lines}, map @$_, @$expolygons) {
  1011. # push @verts, (
  1012. # unscale($line->a->x), unscale($line->a->y), $z, #))
  1013. # unscale($line->b->x), unscale($line->b->y), $z, #))
  1014. # );
  1015. # }
  1016. # $self->cut_lines_vertices(OpenGL::Array->new_list(GL_FLOAT, @verts));
  1017. #}
  1018. #==============================================================================================================================
  1019. # Given an axis and angle, compute quaternion.
  1020. sub axis_to_quat {
  1021. my ($ax, $phi) = @_;
  1022. my $lena = sqrt(reduce { $a + $b } (map { $_ * $_ } @$ax));
  1023. my @q = map { $_ * (1 / $lena) } @$ax;
  1024. @q = map { $_ * sin($phi / 2.0) } @q;
  1025. $q[$#q + 1] = cos($phi / 2.0);
  1026. return @q;
  1027. }
  1028. # Project a point on the virtual trackball.
  1029. # If it is inside the sphere, map it to the sphere, if it outside map it
  1030. # to a hyperbola.
  1031. sub project_to_sphere {
  1032. my ($r, $x, $y) = @_;
  1033. my $d = sqrt($x * $x + $y * $y);
  1034. if ($d < $r * 0.70710678118654752440) { # Inside sphere
  1035. return sqrt($r * $r - $d * $d);
  1036. } else { # On hyperbola
  1037. my $t = $r / 1.41421356237309504880;
  1038. return $t * $t / $d;
  1039. }
  1040. }
  1041. sub cross {
  1042. my ($v1, $v2) = @_;
  1043. return (@$v1[1] * @$v2[2] - @$v1[2] * @$v2[1],
  1044. @$v1[2] * @$v2[0] - @$v1[0] * @$v2[2],
  1045. @$v1[0] * @$v2[1] - @$v1[1] * @$v2[0]);
  1046. }
  1047. # Simulate a track-ball. Project the points onto the virtual trackball,
  1048. # then figure out the axis of rotation, which is the cross product of
  1049. # P1 P2 and O P1 (O is the center of the ball, 0,0,0) Note: This is a
  1050. # deformed trackball-- is a trackball in the center, but is deformed
  1051. # into a hyperbolic sheet of rotation away from the center.
  1052. # It is assumed that the arguments to this routine are in the range
  1053. # (-1.0 ... 1.0).
  1054. sub trackball {
  1055. my ($p1x, $p1y, $p2x, $p2y) = @_;
  1056. if ($p1x == $p2x && $p1y == $p2y) {
  1057. # zero rotation
  1058. return (0.0, 0.0, 0.0, 1.0);
  1059. }
  1060. # First, figure out z-coordinates for projection of P1 and P2 to
  1061. # deformed sphere
  1062. my @p1 = ($p1x, $p1y, project_to_sphere(TRACKBALLSIZE, $p1x, $p1y));
  1063. my @p2 = ($p2x, $p2y, project_to_sphere(TRACKBALLSIZE, $p2x, $p2y));
  1064. # axis of rotation (cross product of P1 and P2)
  1065. my @a = cross(\@p2, \@p1);
  1066. # Figure out how much to rotate around that axis.
  1067. my @d = map { $_ * $_ } (map { $p1[$_] - $p2[$_] } 0 .. $#p1);
  1068. my $t = sqrt(reduce { $a + $b } @d) / (2.0 * TRACKBALLSIZE);
  1069. # Avoid problems with out-of-control values...
  1070. $t = 1.0 if ($t > 1.0);
  1071. $t = -1.0 if ($t < -1.0);
  1072. my $phi = 2.0 * asin($t);
  1073. return axis_to_quat(\@a, $phi);
  1074. }
  1075. # Build a rotation matrix, given a quaternion rotation.
  1076. sub quat_to_rotmatrix {
  1077. my ($q) = @_;
  1078. my @m = ();
  1079. $m[0] = 1.0 - 2.0 * (@$q[1] * @$q[1] + @$q[2] * @$q[2]);
  1080. $m[1] = 2.0 * (@$q[0] * @$q[1] - @$q[2] * @$q[3]);
  1081. $m[2] = 2.0 * (@$q[2] * @$q[0] + @$q[1] * @$q[3]);
  1082. $m[3] = 0.0;
  1083. $m[4] = 2.0 * (@$q[0] * @$q[1] + @$q[2] * @$q[3]);
  1084. $m[5] = 1.0 - 2.0 * (@$q[2] * @$q[2] + @$q[0] * @$q[0]);
  1085. $m[6] = 2.0 * (@$q[1] * @$q[2] - @$q[0] * @$q[3]);
  1086. $m[7] = 0.0;
  1087. $m[8] = 2.0 * (@$q[2] * @$q[0] - @$q[1] * @$q[3]);
  1088. $m[9] = 2.0 * (@$q[1] * @$q[2] + @$q[0] * @$q[3]);
  1089. $m[10] = 1.0 - 2.0 * (@$q[1] * @$q[1] + @$q[0] * @$q[0]);
  1090. $m[11] = 0.0;
  1091. $m[12] = 0.0;
  1092. $m[13] = 0.0;
  1093. $m[14] = 0.0;
  1094. $m[15] = 1.0;
  1095. return @m;
  1096. }
  1097. sub mulquats {
  1098. my ($q1, $rq) = @_;
  1099. return (@$q1[3] * @$rq[0] + @$q1[0] * @$rq[3] + @$q1[1] * @$rq[2] - @$q1[2] * @$rq[1],
  1100. @$q1[3] * @$rq[1] + @$q1[1] * @$rq[3] + @$q1[2] * @$rq[0] - @$q1[0] * @$rq[2],
  1101. @$q1[3] * @$rq[2] + @$q1[2] * @$rq[3] + @$q1[0] * @$rq[1] - @$q1[1] * @$rq[0],
  1102. @$q1[3] * @$rq[3] - @$q1[0] * @$rq[0] - @$q1[1] * @$rq[1] - @$q1[2] * @$rq[2])
  1103. }
  1104. # Convert the screen space coordinate to an object space coordinate.
  1105. # If the Z screen space coordinate is not provided, a depth buffer value is substituted.
  1106. sub mouse_to_3d {
  1107. my ($self, $x, $y, $z) = @_;
  1108. return unless $self->GetContext;
  1109. $self->SetCurrent($self->GetContext);
  1110. my @viewport = glGetIntegerv_p(GL_VIEWPORT); # 4 items
  1111. my @mview = glGetDoublev_p(GL_MODELVIEW_MATRIX); # 16 items
  1112. my @proj = glGetDoublev_p(GL_PROJECTION_MATRIX); # 16 items
  1113. $y = $viewport[3] - $y;
  1114. $z //= glReadPixels_p($x, $y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT);
  1115. my @projected = gluUnProject_p($x, $y, $z, @mview, @proj, @viewport);
  1116. return Slic3r::Pointf3->new(@projected);
  1117. }
  1118. sub GetContext {
  1119. my ($self) = @_;
  1120. return $self->{context} ||= Wx::GLContext->new($self);
  1121. }
  1122. sub SetCurrent {
  1123. my ($self, $context) = @_;
  1124. return $self->SUPER::SetCurrent($context);
  1125. }
  1126. sub UseVBOs {
  1127. my ($self) = @_;
  1128. #==============================================================================================================================
  1129. return 0 if (! $self->init && $^O eq 'linux');
  1130. return Slic3r::GUI::_3DScene::use_VBOs();
  1131. # if (! defined ($self->{use_VBOs})) {
  1132. # my $use_legacy = wxTheApp->{app_config}->get('use_legacy_opengl');
  1133. # if ($use_legacy eq '1') {
  1134. # # Disable OpenGL 2.0 rendering.
  1135. # $self->{use_VBOs} = 0;
  1136. # # Don't enable the layer editing tool.
  1137. # $self->{layer_editing_enabled} = 0;
  1138. # # 2 means failed
  1139. # $self->{layer_editing_initialized} = 2;
  1140. # return 0;
  1141. # }
  1142. # # This is a special path for wxWidgets on GTK, where an OpenGL context is initialized
  1143. # # first when an OpenGL widget is shown for the first time. How ugly.
  1144. # return 0 if (! $self->init && $^O eq 'linux');
  1145. # # Don't use VBOs if anything fails.
  1146. # $self->{use_VBOs} = 0;
  1147. # if ($self->GetContext) {
  1148. # $self->SetCurrent($self->GetContext);
  1149. # Slic3r::GUI::_3DScene::_glew_init;
  1150. # my @gl_version = split(/\./, glGetString(GL_VERSION));
  1151. # $self->{use_VBOs} = int($gl_version[0]) >= 2;
  1152. # # print "UseVBOs $self OpenGL major: $gl_version[0], minor: $gl_version[1]. Use VBOs: ", $self->{use_VBOs}, "\n";
  1153. # }
  1154. # }
  1155. # return $self->{use_VBOs};
  1156. #==============================================================================================================================
  1157. }
  1158. #==============================================================================================================================
  1159. #sub Resize {
  1160. # my ($self, $x, $y) = @_;
  1161. #
  1162. # return unless $self->GetContext;
  1163. # $self->_dirty(0);
  1164. #
  1165. # $self->SetCurrent($self->GetContext);
  1166. # glViewport(0, 0, $x, $y);
  1167. #
  1168. # $x /= $self->_zoom;
  1169. # $y /= $self->_zoom;
  1170. #
  1171. # glMatrixMode(GL_PROJECTION);
  1172. # glLoadIdentity();
  1173. # if ($self->_camera_type eq 'ortho') {
  1174. # #FIXME setting the size of the box 10x larger than necessary
  1175. # # is only a workaround for an incorrectly set camera.
  1176. # # This workaround harms Z-buffer accuracy!
  1177. ## my $depth = 1.05 * $self->max_bounding_box->radius();
  1178. # my $depth = 5.0 * max(@{ $self->max_bounding_box->size });
  1179. # glOrtho(
  1180. # -$x/2, $x/2, -$y/2, $y/2,
  1181. # -$depth, $depth,
  1182. # );
  1183. # } else {
  1184. # die "Invalid camera type: ", $self->_camera_type, "\n" if ($self->_camera_type ne 'perspective');
  1185. # my $bbox_r = $self->max_bounding_box->radius();
  1186. # my $fov = PI * 45. / 180.;
  1187. # my $fov_tan = tan(0.5 * $fov);
  1188. # my $cam_distance = 0.5 * $bbox_r / $fov_tan;
  1189. # $self->_camera_distance($cam_distance);
  1190. # my $nr = $cam_distance - $bbox_r * 1.1;
  1191. # my $fr = $cam_distance + $bbox_r * 1.1;
  1192. # $nr = 1 if ($nr < 1);
  1193. # $fr = $nr + 1 if ($fr < $nr + 1);
  1194. # my $h2 = $fov_tan * $nr;
  1195. # my $w2 = $h2 * $x / $y;
  1196. # glFrustum(-$w2, $w2, -$h2, $h2, $nr, $fr);
  1197. # }
  1198. # glMatrixMode(GL_MODELVIEW);
  1199. #}
  1200. #==============================================================================================================================
  1201. sub InitGL {
  1202. my $self = shift;
  1203. return if $self->init;
  1204. return unless $self->GetContext;
  1205. $self->init(1);
  1206. #==============================================================================================================================
  1207. Slic3r::GUI::_3DScene::init_gl;
  1208. #==============================================================================================================================
  1209. # This is a special path for wxWidgets on GTK, where an OpenGL context is initialized
  1210. # first when an OpenGL widget is shown for the first time. How ugly.
  1211. # In that case the volumes are wainting to be moved to Vertex Buffer Objects
  1212. # after the OpenGL context is being initialized.
  1213. $self->volumes->finalize_geometry(1)
  1214. if ($^O eq 'linux' && $self->UseVBOs);
  1215. #==============================================================================================================================
  1216. Slic3r::GUI::_3DScene::zoom_to_bed($self);
  1217. Slic3r::GUI::_3DScene::init($self, $self->UseVBOs);
  1218. # $self->zoom_to_bed;
  1219. #
  1220. # glClearColor(0, 0, 0, 1);
  1221. # glColor3f(1, 0, 0);
  1222. # glEnable(GL_DEPTH_TEST);
  1223. # glClearDepth(1.0);
  1224. # glDepthFunc(GL_LEQUAL);
  1225. # glEnable(GL_CULL_FACE);
  1226. # glEnable(GL_BLEND);
  1227. # glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1228. #
  1229. # # Set antialiasing/multisampling
  1230. # glDisable(GL_LINE_SMOOTH);
  1231. # glDisable(GL_POLYGON_SMOOTH);
  1232. #
  1233. # # See "GL_MULTISAMPLE and GL_ARRAY_BUFFER_ARB messages on failed launch"
  1234. # # https://github.com/alexrj/Slic3r/issues/4085
  1235. # eval {
  1236. # # Disable the multi sampling by default, so the picking by color will work correctly.
  1237. # glDisable(GL_MULTISAMPLE);
  1238. # };
  1239. # # Disable multi sampling if the eval failed.
  1240. # $self->{can_multisample} = 0 if $@;
  1241. #
  1242. # # ambient lighting
  1243. # glLightModelfv_p(GL_LIGHT_MODEL_AMBIENT, 0.3, 0.3, 0.3, 1);
  1244. #
  1245. # glEnable(GL_LIGHTING);
  1246. # glEnable(GL_LIGHT0);
  1247. # glEnable(GL_LIGHT1);
  1248. #
  1249. # # light from camera
  1250. # glLightfv_p(GL_LIGHT1, GL_POSITION, 1, 0, 1, 0);
  1251. # glLightfv_p(GL_LIGHT1, GL_SPECULAR, 0.3, 0.3, 0.3, 1);
  1252. # glLightfv_p(GL_LIGHT1, GL_DIFFUSE, 0.2, 0.2, 0.2, 1);
  1253. #
  1254. # # Enables Smooth Color Shading; try GL_FLAT for (lack of) fun.
  1255. # glShadeModel(GL_SMOOTH);
  1256. #
  1257. ## glMaterialfv_p(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, 0.5, 0.3, 0.3, 1);
  1258. ## glMaterialfv_p(GL_FRONT_AND_BACK, GL_SPECULAR, 1, 1, 1, 1);
  1259. ## glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50);
  1260. ## glMaterialfv_p(GL_FRONT_AND_BACK, GL_EMISSION, 0.1, 0, 0, 0.9);
  1261. #
  1262. # # A handy trick -- have surface material mirror the color.
  1263. # glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  1264. # glEnable(GL_COLOR_MATERIAL);
  1265. # glEnable(GL_MULTISAMPLE) if ($self->{can_multisample});
  1266. #
  1267. # if ($self->UseVBOs) {
  1268. # my $shader = new Slic3r::GUI::_3DScene::GLShader;
  1269. ## if (! $shader->load($self->_fragment_shader_Phong, $self->_vertex_shader_Phong)) {
  1270. # print "Compilaton of path shader failed: \n" . $shader->last_error . "\n";
  1271. # $shader = undef;
  1272. # } else {
  1273. # $self->{plain_shader} = $shader;
  1274. # }
  1275. # }
  1276. #===================================================================================================================================
  1277. }
  1278. sub DestroyGL {
  1279. my $self = shift;
  1280. if ($self->GetContext) {
  1281. $self->SetCurrent($self->GetContext);
  1282. #===================================================================================================================================
  1283. # if ($self->{plain_shader}) {
  1284. # $self->{plain_shader}->release;
  1285. # delete $self->{plain_shader};
  1286. # }
  1287. # if ($self->{layer_height_edit_shader}) {
  1288. # $self->{layer_height_edit_shader}->release;
  1289. # delete $self->{layer_height_edit_shader};
  1290. # }
  1291. #===================================================================================================================================
  1292. $self->volumes->release_geometry;
  1293. }
  1294. }
  1295. sub Render {
  1296. my ($self, $dc) = @_;
  1297. # prevent calling SetCurrent() when window is not shown yet
  1298. #==============================================================================================================================
  1299. return unless Slic3r::GUI::_3DScene::is_shown_on_screen($self);
  1300. # return unless $self->IsShownOnScreen;
  1301. #==============================================================================================================================
  1302. return unless my $context = $self->GetContext;
  1303. $self->SetCurrent($context);
  1304. $self->InitGL;
  1305. glClearColor(1, 1, 1, 1);
  1306. glClearDepth(1);
  1307. glDepthFunc(GL_LESS);
  1308. #==============================================================================================================================
  1309. # glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1310. #==============================================================================================================================
  1311. glMatrixMode(GL_MODELVIEW);
  1312. glLoadIdentity();
  1313. if (!TURNTABLE_MODE) {
  1314. # Shift the perspective camera.
  1315. #==============================================================================================================================
  1316. my $camera_pos = Slic3r::Pointf3->new(0,0,-Slic3r::GUI::_3DScene::get_camera_distance($self));
  1317. # my $camera_pos = Slic3r::Pointf3->new(0,0,-$self->_camera_distance);
  1318. #==============================================================================================================================
  1319. glTranslatef(@$camera_pos);
  1320. }
  1321. if (TURNTABLE_MODE) {
  1322. # Turntable mode is enabled by default.
  1323. #==============================================================================================================================
  1324. glRotatef(-Slic3r::GUI::_3DScene::get_camera_theta($self), 1, 0, 0); # pitch
  1325. glRotatef(Slic3r::GUI::_3DScene::get_camera_phi($self), 0, 0, 1); # yaw
  1326. # glRotatef(-$self->_stheta, 1, 0, 0); # pitch
  1327. # glRotatef($self->_sphi, 0, 0, 1); # yaw
  1328. #==============================================================================================================================
  1329. } else {
  1330. my @rotmat = quat_to_rotmatrix($self->quat);
  1331. glMultMatrixd_p(@rotmat[0..15]);
  1332. }
  1333. #==============================================================================================================================
  1334. glTranslatef(@{ Slic3r::GUI::_3DScene::get_camera_target($self)->negative });
  1335. # glTranslatef(@{ $self->_camera_target->negative });
  1336. #==============================================================================================================================
  1337. # light from above
  1338. glLightfv_p(GL_LIGHT0, GL_POSITION, -0.5, -0.5, 1, 0);
  1339. glLightfv_p(GL_LIGHT0, GL_SPECULAR, 0.2, 0.2, 0.2, 1);
  1340. glLightfv_p(GL_LIGHT0, GL_DIFFUSE, 0.5, 0.5, 0.5, 1);
  1341. # Head light
  1342. glLightfv_p(GL_LIGHT1, GL_POSITION, 1, 0, 1, 0);
  1343. #==============================================================================================================================
  1344. Slic3r::GUI::_3DScene::picking_pass($self);
  1345. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1346. Slic3r::GUI::_3DScene::render_background($self);
  1347. Slic3r::GUI::_3DScene::render_bed($self);
  1348. Slic3r::GUI::_3DScene::render_axes($self);
  1349. Slic3r::GUI::_3DScene::render_objects($self, $self->UseVBOs);
  1350. Slic3r::GUI::_3DScene::render_cutting_plane($self);
  1351. Slic3r::GUI::_3DScene::render_warning_texture($self);
  1352. Slic3r::GUI::_3DScene::render_legend_texture($self);
  1353. # if ($self->enable_picking && !$self->_mouse_dragging) {
  1354. # if (my $pos = $self->_mouse_pos) {
  1355. # # Render the object for picking.
  1356. # # FIXME This cannot possibly work in a multi-sampled context as the color gets mangled by the anti-aliasing.
  1357. # # Better to use software ray-casting on a bounding-box hierarchy.
  1358. # glPushAttrib(GL_ENABLE_BIT);
  1359. # glDisable(GL_MULTISAMPLE) if ($self->{can_multisample});
  1360. # glDisable(GL_LIGHTING);
  1361. # glDisable(GL_BLEND);
  1362. # $self->draw_volumes(1);
  1363. # glPopAttrib();
  1364. # glFlush();
  1365. # my $col = [ glReadPixels_p($pos->x, $self->GetSize->GetHeight - $pos->y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE) ];
  1366. # my $volume_idx = $col->[0] + $col->[1]*256 + $col->[2]*256*256;
  1367. # $self->_hover_volume_idx(undef);
  1368. # $_->set_hover(0) for @{$self->volumes};
  1369. # if ($volume_idx <= $#{$self->volumes}) {
  1370. # $self->_hover_volume_idx($volume_idx);
  1371. #
  1372. # $self->volumes->[$volume_idx]->set_hover(1);
  1373. # my $group_id = $self->volumes->[$volume_idx]->select_group_id;
  1374. # if ($group_id != -1) {
  1375. # $_->set_hover(1) for grep { $_->select_group_id == $group_id } @{$self->volumes};
  1376. # }
  1377. #
  1378. # $self->on_hover->($volume_idx) if $self->on_hover;
  1379. # }
  1380. # glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1381. # }
  1382. # }
  1383. #
  1384. # # draw fixed background
  1385. # if ($self->background) {
  1386. # glDisable(GL_LIGHTING);
  1387. # glPushMatrix();
  1388. # glLoadIdentity();
  1389. #
  1390. # glMatrixMode(GL_PROJECTION);
  1391. # glPushMatrix();
  1392. # glLoadIdentity();
  1393. #
  1394. # # Draws a bluish bottom to top gradient over the complete screen.
  1395. # glDisable(GL_DEPTH_TEST);
  1396. # glBegin(GL_QUADS);
  1397. # glColor3f(0.0,0.0,0.0);
  1398. # glVertex3f(-1.0,-1.0, 1.0);
  1399. # glVertex3f( 1.0,-1.0, 1.0);
  1400. # glColor3f(10/255,98/255,144/255);
  1401. # glVertex3f( 1.0, 1.0, 1.0);
  1402. # glVertex3f(-1.0, 1.0, 1.0);
  1403. # glEnd();
  1404. # glPopMatrix();
  1405. # glEnable(GL_DEPTH_TEST);
  1406. #
  1407. # glMatrixMode(GL_MODELVIEW);
  1408. # glPopMatrix();
  1409. # glEnable(GL_LIGHTING);
  1410. # }
  1411. #
  1412. # # draw ground and axes
  1413. # glDisable(GL_LIGHTING);
  1414. #
  1415. # # draw ground
  1416. # my $ground_z = GROUND_Z;
  1417. #
  1418. # if ($self->bed_triangles) {
  1419. # glDisable(GL_DEPTH_TEST);
  1420. #
  1421. # glEnable(GL_BLEND);
  1422. # glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1423. #
  1424. # glEnableClientState(GL_VERTEX_ARRAY);
  1425. # glColor4f(0.8, 0.6, 0.5, 0.4);
  1426. # glNormal3d(0,0,1);
  1427. # glVertexPointer_c(3, GL_FLOAT, 0, $self->bed_triangles->ptr());
  1428. # glDrawArrays(GL_TRIANGLES, 0, $self->bed_triangles->elements / 3);
  1429. # glDisableClientState(GL_VERTEX_ARRAY);
  1430. #
  1431. # # we need depth test for grid, otherwise it would disappear when looking
  1432. # # the object from below
  1433. # glEnable(GL_DEPTH_TEST);
  1434. #
  1435. # # draw grid
  1436. # glLineWidth(3);
  1437. # glColor4f(0.2, 0.2, 0.2, 0.4);
  1438. # glEnableClientState(GL_VERTEX_ARRAY);
  1439. # glVertexPointer_c(3, GL_FLOAT, 0, $self->bed_grid_lines->ptr());
  1440. # glDrawArrays(GL_LINES, 0, $self->bed_grid_lines->elements / 3);
  1441. # glDisableClientState(GL_VERTEX_ARRAY);
  1442. #
  1443. # glDisable(GL_BLEND);
  1444. # }
  1445. #
  1446. # my $volumes_bb = $self->volumes_bounding_box;
  1447. #
  1448. # {
  1449. # # draw axes
  1450. # # disable depth testing so that axes are not covered by ground
  1451. # glDisable(GL_DEPTH_TEST);
  1452. # my $origin = $self->origin;
  1453. # my $axis_len = $self->use_plain_shader ? 0.3 * max(@{ $self->bed_bounding_box->size }) : 2 * max(@{ $volumes_bb->size });
  1454. # glLineWidth(2);
  1455. # glBegin(GL_LINES);
  1456. # # draw line for x axis
  1457. # glColor3f(1, 0, 0);
  1458. # glVertex3f(@$origin, $ground_z);
  1459. # glVertex3f($origin->x + $axis_len, $origin->y, $ground_z); #,,
  1460. # # draw line for y axis
  1461. # glColor3f(0, 1, 0);
  1462. # glVertex3f(@$origin, $ground_z);
  1463. # glVertex3f($origin->x, $origin->y + $axis_len, $ground_z); #++
  1464. # glEnd();
  1465. # # draw line for Z axis
  1466. # # (re-enable depth test so that axis is correctly shown when objects are behind it)
  1467. # glEnable(GL_DEPTH_TEST);
  1468. # glBegin(GL_LINES);
  1469. # glColor3f(0, 0, 1);
  1470. # glVertex3f(@$origin, $ground_z);
  1471. # glVertex3f(@$origin, $ground_z+$axis_len);
  1472. # glEnd();
  1473. # }
  1474. #
  1475. # glEnable(GL_LIGHTING);
  1476. #
  1477. # # draw objects
  1478. # if (! $self->use_plain_shader) {
  1479. # $self->draw_volumes;
  1480. # } elsif ($self->UseVBOs) {
  1481. # if ($self->enable_picking) {
  1482. # $self->mark_volumes_for_layer_height;
  1483. # $self->volumes->set_print_box($self->bed_bounding_box->x_min, $self->bed_bounding_box->y_min, 0.0, $self->bed_bounding_box->x_max, $self->bed_bounding_box->y_max, $self->{config}->get('max_print_height'));
  1484. # $self->volumes->check_outside_state($self->{config});
  1485. # # do not cull backfaces to show broken geometry, if any
  1486. # glDisable(GL_CULL_FACE);
  1487. # }
  1488. # $self->{plain_shader}->enable if $self->{plain_shader};
  1489. # $self->volumes->render_VBOs;
  1490. # $self->{plain_shader}->disable;
  1491. # glEnable(GL_CULL_FACE) if ($self->enable_picking);
  1492. # } else {
  1493. # # do not cull backfaces to show broken geometry, if any
  1494. # glDisable(GL_CULL_FACE) if ($self->enable_picking);
  1495. # $self->volumes->render_legacy;
  1496. # glEnable(GL_CULL_FACE) if ($self->enable_picking);
  1497. # }
  1498. #
  1499. # if (defined $self->cutting_plane_z) {
  1500. # # draw cutting plane
  1501. # my $plane_z = $self->cutting_plane_z;
  1502. # my $bb = $volumes_bb;
  1503. # glDisable(GL_CULL_FACE);
  1504. # glDisable(GL_LIGHTING);
  1505. # glEnable(GL_BLEND);
  1506. # glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1507. # glBegin(GL_QUADS);
  1508. # glColor4f(0.8, 0.8, 0.8, 0.5);
  1509. # glVertex3f($bb->x_min-20, $bb->y_min-20, $plane_z);
  1510. # glVertex3f($bb->x_max+20, $bb->y_min-20, $plane_z);
  1511. # glVertex3f($bb->x_max+20, $bb->y_max+20, $plane_z);
  1512. # glVertex3f($bb->x_min-20, $bb->y_max+20, $plane_z);
  1513. # glEnd();
  1514. # glEnable(GL_CULL_FACE);
  1515. # glDisable(GL_BLEND);
  1516. #
  1517. # # draw cutting contours
  1518. # glEnableClientState(GL_VERTEX_ARRAY);
  1519. # glLineWidth(2);
  1520. # glColor3f(0, 0, 0);
  1521. # glVertexPointer_c(3, GL_FLOAT, 0, $self->cut_lines_vertices->ptr());
  1522. # glDrawArrays(GL_LINES, 0, $self->cut_lines_vertices->elements / 3);
  1523. # glVertexPointer_c(3, GL_FLOAT, 0, 0);
  1524. # glDisableClientState(GL_VERTEX_ARRAY);
  1525. # }
  1526. #
  1527. # # draw warning message
  1528. # $self->draw_warning;
  1529. #
  1530. # # draw gcode preview legend
  1531. # $self->draw_legend;
  1532. #==============================================================================================================================
  1533. $self->draw_active_object_annotations;
  1534. $self->SwapBuffers();
  1535. }
  1536. #==============================================================================================================================
  1537. #sub draw_volumes {
  1538. # # $fakecolor is a boolean indicating, that the objects shall be rendered in a color coding the object index for picking.
  1539. # my ($self, $fakecolor) = @_;
  1540. #
  1541. # # do not cull backfaces to show broken geometry, if any
  1542. # glDisable(GL_CULL_FACE);
  1543. #
  1544. # glEnable(GL_BLEND);
  1545. # glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1546. #
  1547. # glEnableClientState(GL_VERTEX_ARRAY);
  1548. # glEnableClientState(GL_NORMAL_ARRAY);
  1549. #
  1550. # foreach my $volume_idx (0..$#{$self->volumes}) {
  1551. # my $volume = $self->volumes->[$volume_idx];
  1552. #
  1553. # if ($fakecolor) {
  1554. # # Object picking mode. Render the object with a color encoding the object index.
  1555. # my $r = ($volume_idx & 0x000000FF) >> 0;
  1556. # my $g = ($volume_idx & 0x0000FF00) >> 8;
  1557. # my $b = ($volume_idx & 0x00FF0000) >> 16;
  1558. # glColor4f($r/255.0, $g/255.0, $b/255.0, 1);
  1559. # } elsif ($volume->selected) {
  1560. # glColor4f(@{ &SELECTED_COLOR });
  1561. # } elsif ($volume->hover) {
  1562. # glColor4f(@{ &HOVER_COLOR });
  1563. # } else {
  1564. # glColor4f(@{ $volume->color });
  1565. # }
  1566. #
  1567. # $volume->render;
  1568. # }
  1569. # glDisableClientState(GL_NORMAL_ARRAY);
  1570. # glDisableClientState(GL_VERTEX_ARRAY);
  1571. #
  1572. # glDisable(GL_BLEND);
  1573. # glEnable(GL_CULL_FACE);
  1574. #}
  1575. #==============================================================================================================================
  1576. sub mark_volumes_for_layer_height {
  1577. my ($self) = @_;
  1578. foreach my $volume_idx (0..$#{$self->volumes}) {
  1579. my $volume = $self->volumes->[$volume_idx];
  1580. my $object_id = int($volume->select_group_id / 1000000);
  1581. #==============================================================================================================================
  1582. my $shader = Slic3r::GUI::_3DScene::get_layers_editing_shader($self);
  1583. if (Slic3r::GUI::_3DScene::is_layers_editing_enabled($self) && $shader && $volume->selected &&
  1584. $volume->has_layer_height_texture && $object_id < $self->{print}->object_count) {
  1585. $volume->set_layer_height_texture_data(Slic3r::GUI::_3DScene::get_layers_editing_z_texture_id($self), $shader->shader_program_id,
  1586. $self->{print}->get_object($object_id), $self->_variable_layer_thickness_bar_mouse_cursor_z_relative, $self->{layer_height_edit_band_width});
  1587. # if ($self->layer_editing_enabled && $volume->selected && $self->{layer_height_edit_shader} &&
  1588. # $volume->has_layer_height_texture && $object_id < $self->{print}->object_count) {
  1589. # $volume->set_layer_height_texture_data($self->{layer_preview_z_texture_id}, $self->{layer_height_edit_shader}->shader_program_id,
  1590. # $self->{print}->get_object($object_id), $self->_variable_layer_thickness_bar_mouse_cursor_z_relative, $self->{layer_height_edit_band_width});
  1591. #==============================================================================================================================
  1592. } else {
  1593. $volume->reset_layer_height_texture_data();
  1594. }
  1595. }
  1596. }
  1597. sub _load_image_set_texture {
  1598. my ($self, $file_name) = @_;
  1599. # Load a PNG with an alpha channel.
  1600. my $img = Wx::Image->new;
  1601. $img->LoadFile(Slic3r::var($file_name), wxBITMAP_TYPE_PNG);
  1602. # Get RGB & alpha raw data from wxImage, interleave them into a Perl array.
  1603. my @rgb = unpack 'C*', $img->GetData();
  1604. my @alpha = $img->HasAlpha ? unpack 'C*', $img->GetAlpha() : (255) x (int(@rgb) / 3);
  1605. my $n_pixels = int(@alpha);
  1606. my @data = (0)x($n_pixels * 4);
  1607. for (my $i = 0; $i < $n_pixels; $i += 1) {
  1608. $data[$i*4 ] = $rgb[$i*3];
  1609. $data[$i*4+1] = $rgb[$i*3+1];
  1610. $data[$i*4+2] = $rgb[$i*3+2];
  1611. $data[$i*4+3] = $alpha[$i];
  1612. }
  1613. # Initialize a raw bitmap data.
  1614. my $params = {
  1615. loaded => 1,
  1616. valid => $n_pixels > 0,
  1617. width => $img->GetWidth,
  1618. height => $img->GetHeight,
  1619. data => OpenGL::Array->new_list(GL_UNSIGNED_BYTE, @data),
  1620. texture_id => glGenTextures_p(1)
  1621. };
  1622. # Create and initialize a texture with the raw data.
  1623. glBindTexture(GL_TEXTURE_2D, $params->{texture_id});
  1624. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1625. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1626. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1627. glTexImage2D_c(GL_TEXTURE_2D, 0, GL_RGBA8, $params->{width}, $params->{height}, 0, GL_RGBA, GL_UNSIGNED_BYTE, $params->{data}->ptr);
  1628. glBindTexture(GL_TEXTURE_2D, 0);
  1629. return $params;
  1630. }
  1631. #==============================================================================================================================
  1632. #sub _variable_layer_thickness_load_overlay_image {
  1633. # my ($self) = @_;
  1634. # $self->{layer_preview_annotation} = $self->_load_image_set_texture('variable_layer_height_tooltip.png')
  1635. # if (! $self->{layer_preview_annotation}->{loaded});
  1636. # return $self->{layer_preview_annotation}->{valid};
  1637. #}
  1638. #
  1639. #sub _variable_layer_thickness_load_reset_image {
  1640. # my ($self) = @_;
  1641. # $self->{layer_preview_reset_image} = $self->_load_image_set_texture('variable_layer_height_reset.png')
  1642. # if (! $self->{layer_preview_reset_image}->{loaded});
  1643. # return $self->{layer_preview_reset_image}->{valid};
  1644. #}
  1645. #
  1646. ## Paint the tooltip.
  1647. #sub _render_image {
  1648. # my ($self, $image, $l, $r, $b, $t) = @_;
  1649. # $self->_render_texture($image->{texture_id}, $l, $r, $b, $t);
  1650. #}
  1651. #
  1652. #sub _render_texture {
  1653. # my ($self, $tex_id, $l, $r, $b, $t) = @_;
  1654. #
  1655. # glColor4f(1.,1.,1.,1.);
  1656. # glDisable(GL_LIGHTING);
  1657. # glEnable(GL_BLEND);
  1658. # glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1659. # glEnable(GL_TEXTURE_2D);
  1660. # glBindTexture(GL_TEXTURE_2D, $tex_id);
  1661. # glBegin(GL_QUADS);
  1662. # glTexCoord2d(0.,1.); glVertex3f($l, $b, 0);
  1663. # glTexCoord2d(1.,1.); glVertex3f($r, $b, 0);
  1664. # glTexCoord2d(1.,0.); glVertex3f($r, $t, 0);
  1665. # glTexCoord2d(0.,0.); glVertex3f($l, $t, 0);
  1666. # glEnd();
  1667. # glBindTexture(GL_TEXTURE_2D, 0);
  1668. # glDisable(GL_TEXTURE_2D);
  1669. # glDisable(GL_BLEND);
  1670. # glEnable(GL_LIGHTING);
  1671. #}
  1672. #==============================================================================================================================
  1673. sub draw_active_object_annotations {
  1674. # $fakecolor is a boolean indicating, that the objects shall be rendered in a color coding the object index for picking.
  1675. my ($self) = @_;
  1676. #==============================================================================================================================
  1677. return if (!Slic3r::GUI::_3DScene::is_layers_editing_enabled($self));
  1678. # return if (! $self->{layer_height_edit_shader} || ! $self->layer_editing_enabled);
  1679. #==============================================================================================================================
  1680. # Find the selected volume, over which the layer editing is active.
  1681. my $volume;
  1682. foreach my $volume_idx (0..$#{$self->volumes}) {
  1683. my $v = $self->volumes->[$volume_idx];
  1684. if ($v->selected && $v->has_layer_height_texture) {
  1685. $volume = $v;
  1686. last;
  1687. }
  1688. }
  1689. return if (! $volume);
  1690. # If the active object was not allocated at the Print, go away. This should only be a momentary case between an object addition / deletion
  1691. # and an update by Platter::async_apply_config.
  1692. my $object_idx = int($volume->select_group_id / 1000000);
  1693. return if $object_idx >= $self->{print}->object_count;
  1694. # The viewport and camera are set to complete view and glOrtho(-$x/2, $x/2, -$y/2, $y/2, -$depth, $depth),
  1695. # where x, y is the window size divided by $self->_zoom.
  1696. my ($bar_left, $bar_bottom, $bar_right, $bar_top) = $self->_variable_layer_thickness_bar_rect_viewport;
  1697. my ($reset_left, $reset_bottom, $reset_right, $reset_top) = $self->_variable_layer_thickness_reset_rect_viewport;
  1698. my $z_cursor_relative = $self->_variable_layer_thickness_bar_mouse_cursor_z_relative;
  1699. my $print_object = $self->{print}->get_object($object_idx);
  1700. my $z_max = $print_object->model_object->bounding_box->z_max;
  1701. #==============================================================================================================================
  1702. my $shader = Slic3r::GUI::_3DScene::get_layers_editing_shader($self);
  1703. $shader->enable;
  1704. $shader->set_uniform('z_to_texture_row', $volume->layer_height_texture_z_to_row_id);
  1705. $shader->set_uniform('z_texture_row_to_normalized', 1. / $volume->layer_height_texture_height);
  1706. $shader->set_uniform('z_cursor', $z_max * $z_cursor_relative);
  1707. $shader->set_uniform('z_cursor_band_width', $self->{layer_height_edit_band_width});
  1708. # $self->{layer_height_edit_shader}->enable;
  1709. # $self->{layer_height_edit_shader}->set_uniform('z_to_texture_row', $volume->layer_height_texture_z_to_row_id);
  1710. # $self->{layer_height_edit_shader}->set_uniform('z_texture_row_to_normalized', 1. / $volume->layer_height_texture_height);
  1711. # $self->{layer_height_edit_shader}->set_uniform('z_cursor', $z_max * $z_cursor_relative);
  1712. # $self->{layer_height_edit_shader}->set_uniform('z_cursor_band_width', $self->{layer_height_edit_band_width});
  1713. #==============================================================================================================================
  1714. #==============================================================================================================================
  1715. glBindTexture(GL_TEXTURE_2D, Slic3r::GUI::_3DScene::get_layers_editing_z_texture_id($self));
  1716. # glBindTexture(GL_TEXTURE_2D, $self->{layer_preview_z_texture_id});
  1717. #==============================================================================================================================
  1718. glTexImage2D_c(GL_TEXTURE_2D, 0, GL_RGBA8, $volume->layer_height_texture_width, $volume->layer_height_texture_height,
  1719. 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  1720. glTexImage2D_c(GL_TEXTURE_2D, 1, GL_RGBA8, $volume->layer_height_texture_width / 2, $volume->layer_height_texture_height / 2,
  1721. 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  1722. glTexSubImage2D_c(GL_TEXTURE_2D, 0, 0, 0, $volume->layer_height_texture_width, $volume->layer_height_texture_height,
  1723. GL_RGBA, GL_UNSIGNED_BYTE, $volume->layer_height_texture_data_ptr_level0);
  1724. glTexSubImage2D_c(GL_TEXTURE_2D, 1, 0, 0, $volume->layer_height_texture_width / 2, $volume->layer_height_texture_height / 2,
  1725. GL_RGBA, GL_UNSIGNED_BYTE, $volume->layer_height_texture_data_ptr_level1);
  1726. # Render the color bar.
  1727. glDisable(GL_DEPTH_TEST);
  1728. # The viewport and camera are set to complete view and glOrtho(-$x/2, $x/2, -$y/2, $y/2, -$depth, $depth),
  1729. # where x, y is the window size divided by $self->_zoom.
  1730. glPushMatrix();
  1731. glLoadIdentity();
  1732. # Paint the overlay.
  1733. glBegin(GL_QUADS);
  1734. glVertex3f($bar_left, $bar_bottom, 0);
  1735. glVertex3f($bar_right, $bar_bottom, 0);
  1736. glVertex3f($bar_right, $bar_top, $z_max);
  1737. glVertex3f($bar_left, $bar_top, $z_max);
  1738. glEnd();
  1739. glBindTexture(GL_TEXTURE_2D, 0);
  1740. #==============================================================================================================================
  1741. $shader->disable;
  1742. # $self->{layer_height_edit_shader}->disable;
  1743. #==============================================================================================================================
  1744. #==============================================================================================================================
  1745. Slic3r::GUI::_3DScene::render_layer_editing_textures($self, $print_object);
  1746. # # Paint the tooltip.
  1747. # if ($self->_variable_layer_thickness_load_overlay_image)
  1748. # my $gap = 10/$self->_zoom;
  1749. # my ($l, $r, $b, $t) = ($bar_left - $self->{layer_preview_annotation}->{width}/$self->_zoom - $gap, $bar_left - $gap, $reset_bottom + $self->{layer_preview_annotation}->{height}/$self->_zoom + $gap, $reset_bottom + $gap);
  1750. # $self->_render_image($self->{layer_preview_annotation}, $l, $r, $t, $b);
  1751. # }
  1752. #
  1753. # # Paint the reset button.
  1754. # if ($self->_variable_layer_thickness_load_reset_image) {
  1755. # $self->_render_image($self->{layer_preview_reset_image}, $reset_left, $reset_right, $reset_bottom, $reset_top);
  1756. # }
  1757. #
  1758. # # Paint the graph.
  1759. # #FIXME show some kind of legend.
  1760. # my $max_z = unscale($print_object->size->z);
  1761. # my $profile = $print_object->model_object->layer_height_profile;
  1762. # my $layer_height = $print_object->config->get('layer_height');
  1763. # my $layer_height_max = 10000000000.;
  1764. # {
  1765. # # Get a maximum layer height value.
  1766. # #FIXME This is a duplicate code of Slicing.cpp.
  1767. # my $nozzle_diameters = $print_object->print->config->get('nozzle_diameter');
  1768. # my $layer_heights_min = $print_object->print->config->get('min_layer_height');
  1769. # my $layer_heights_max = $print_object->print->config->get('max_layer_height');
  1770. # for (my $i = 0; $i < scalar(@{$nozzle_diameters}); $i += 1) {
  1771. # my $lh_min = ($layer_heights_min->[$i] == 0.) ? 0.07 : max(0.01, $layer_heights_min->[$i]);
  1772. # my $lh_max = ($layer_heights_max->[$i] == 0.) ? (0.75 * $nozzle_diameters->[$i]) : $layer_heights_max->[$i];
  1773. # $layer_height_max = min($layer_height_max, max($lh_min, $lh_max));
  1774. # }
  1775. # }
  1776. # # Make the vertical bar a bit wider so the layer height curve does not touch the edge of the bar region.
  1777. # $layer_height_max *= 1.12;
  1778. # # Baseline
  1779. # glColor3f(0., 0., 0.);
  1780. # glBegin(GL_LINE_STRIP);
  1781. # glVertex2f($bar_left + $layer_height * ($bar_right - $bar_left) / $layer_height_max, $bar_bottom);
  1782. # glVertex2f($bar_left + $layer_height * ($bar_right - $bar_left) / $layer_height_max, $bar_top);
  1783. # glEnd();
  1784. # # Curve
  1785. # glColor3f(0., 0., 1.);
  1786. # glBegin(GL_LINE_STRIP);
  1787. # for (my $i = 0; $i < int(@{$profile}); $i += 2) {
  1788. # my $z = $profile->[$i];
  1789. # my $h = $profile->[$i+1];
  1790. # glVertex3f($bar_left + $h * ($bar_right - $bar_left) / $layer_height_max, $bar_bottom + $z * ($bar_top - $bar_bottom) / $max_z, $z);
  1791. # }
  1792. # glEnd();
  1793. #==============================================================================================================================
  1794. # Revert the matrices.
  1795. glPopMatrix();
  1796. glEnable(GL_DEPTH_TEST);
  1797. }
  1798. #==============================================================================================================================
  1799. #sub draw_legend {
  1800. # my ($self) = @_;
  1801. #
  1802. # if (!$self->_legend_enabled) {
  1803. # return;
  1804. # }
  1805. #
  1806. # # If the legend texture has not been loaded into the GPU, do it now.
  1807. # my $tex_id = Slic3r::GUI::_3DScene::finalize_legend_texture;
  1808. # if ($tex_id > 0)
  1809. # {
  1810. # my $tex_w = Slic3r::GUI::_3DScene::get_legend_texture_width;
  1811. # my $tex_h = Slic3r::GUI::_3DScene::get_legend_texture_height;
  1812. # if (($tex_w > 0) && ($tex_h > 0))
  1813. # {
  1814. # glDisable(GL_DEPTH_TEST);
  1815. # glPushMatrix();
  1816. # glLoadIdentity();
  1817. #
  1818. # my ($cw, $ch) = $self->GetSizeWH;
  1819. #
  1820. # my $l = (-0.5 * $cw) / $self->_zoom;
  1821. # my $t = (0.5 * $ch) / $self->_zoom;
  1822. # my $r = $l + $tex_w / $self->_zoom;
  1823. # my $b = $t - $tex_h / $self->_zoom;
  1824. # $self->_render_texture($tex_id, $l, $r, $b, $t);
  1825. #
  1826. # glPopMatrix();
  1827. # glEnable(GL_DEPTH_TEST);
  1828. # }
  1829. # }
  1830. #}
  1831. #
  1832. #sub draw_warning {
  1833. # my ($self) = @_;
  1834. #
  1835. # if (!$self->_warning_enabled) {
  1836. # return;
  1837. # }
  1838. #
  1839. # # If the warning texture has not been loaded into the GPU, do it now.
  1840. # my $tex_id = Slic3r::GUI::_3DScene::finalize_warning_texture;
  1841. # if ($tex_id > 0)
  1842. # {
  1843. # my $tex_w = Slic3r::GUI::_3DScene::get_warning_texture_width;
  1844. # my $tex_h = Slic3r::GUI::_3DScene::get_warning_texture_height;
  1845. # if (($tex_w > 0) && ($tex_h > 0))
  1846. # {
  1847. # glDisable(GL_DEPTH_TEST);
  1848. # glPushMatrix();
  1849. # glLoadIdentity();
  1850. #
  1851. # my ($cw, $ch) = $self->GetSizeWH;
  1852. #
  1853. # my $l = (-0.5 * $tex_w) / $self->_zoom;
  1854. # my $t = (-0.5 * $ch + $tex_h) / $self->_zoom;
  1855. # my $r = $l + $tex_w / $self->_zoom;
  1856. # my $b = $t - $tex_h / $self->_zoom;
  1857. # $self->_render_texture($tex_id, $l, $r, $b, $t);
  1858. #
  1859. # glPopMatrix();
  1860. # glEnable(GL_DEPTH_TEST);
  1861. # }
  1862. # }
  1863. #}
  1864. #==============================================================================================================================
  1865. sub update_volumes_colors_by_extruder {
  1866. my ($self, $config) = @_;
  1867. $self->volumes->update_colors_by_extruder($config);
  1868. }
  1869. sub opengl_info
  1870. {
  1871. my ($self, %params) = @_;
  1872. my %tag = Slic3r::tags($params{format});
  1873. my $gl_version = glGetString(GL_VERSION);
  1874. my $gl_vendor = glGetString(GL_VENDOR);
  1875. my $gl_renderer = glGetString(GL_RENDERER);
  1876. my $glsl_version = glGetString(GL_SHADING_LANGUAGE_VERSION);
  1877. my $out = '';
  1878. $out .= "$tag{h2start}OpenGL installation$tag{h2end}$tag{eol}";
  1879. $out .= " $tag{bstart}Using POGL$tag{bend} v$OpenGL::BUILD_VERSION$tag{eol}";
  1880. $out .= " $tag{bstart}GL version: $tag{bend}${gl_version}$tag{eol}";
  1881. $out .= " $tag{bstart}vendor: $tag{bend}${gl_vendor}$tag{eol}";
  1882. $out .= " $tag{bstart}renderer: $tag{bend}${gl_renderer}$tag{eol}";
  1883. $out .= " $tag{bstart}GLSL version: $tag{bend}${glsl_version}$tag{eol}";
  1884. # Check for other OpenGL extensions
  1885. $out .= "$tag{h2start}Installed extensions (* implemented in the module):$tag{h2end}$tag{eol}";
  1886. my $extensions = glGetString(GL_EXTENSIONS);
  1887. my @extensions = split(' ',$extensions);
  1888. foreach my $ext (sort @extensions) {
  1889. my $stat = glpCheckExtension($ext);
  1890. $out .= sprintf("%s ${ext}$tag{eol}", $stat?' ':'*');
  1891. $out .= sprintf(" ${stat}$tag{eol}") if ($stat && $stat !~ m|^$ext |);
  1892. }
  1893. return $out;
  1894. }
  1895. sub _report_opengl_state
  1896. {
  1897. my ($self, $comment) = @_;
  1898. my $err = glGetError();
  1899. return 0 if ($err == 0);
  1900. # gluErrorString() hangs. Don't use it.
  1901. # my $errorstr = gluErrorString();
  1902. my $errorstr = '';
  1903. if ($err == 0x0500) {
  1904. $errorstr = 'GL_INVALID_ENUM';
  1905. } elsif ($err == GL_INVALID_VALUE) {
  1906. $errorstr = 'GL_INVALID_VALUE';
  1907. } elsif ($err == GL_INVALID_OPERATION) {
  1908. $errorstr = 'GL_INVALID_OPERATION';
  1909. } elsif ($err == GL_STACK_OVERFLOW) {
  1910. $errorstr = 'GL_STACK_OVERFLOW';
  1911. } elsif ($err == GL_OUT_OF_MEMORY) {
  1912. $errorstr = 'GL_OUT_OF_MEMORY';
  1913. } else {
  1914. $errorstr = 'unknown';
  1915. }
  1916. if (defined($comment)) {
  1917. printf("OpenGL error at %s, nr %d (0x%x): %s\n", $comment, $err, $err, $errorstr);
  1918. } else {
  1919. printf("OpenGL error nr %d (0x%x): %s\n", $err, $err, $errorstr);
  1920. }
  1921. }
  1922. #===================================================================================================================================
  1923. #sub _vertex_shader_Gouraud {
  1924. # return <<'VERTEX';
  1925. ##version 110
  1926. #
  1927. ##define INTENSITY_CORRECTION 0.6
  1928. #
  1929. #// normalized values for (-0.6/1.31, 0.6/1.31, 1./1.31)
  1930. #const vec3 LIGHT_TOP_DIR = vec3(-0.4574957, 0.4574957, 0.7624929);
  1931. ##define LIGHT_TOP_DIFFUSE (0.8 * INTENSITY_CORRECTION)
  1932. ##define LIGHT_TOP_SPECULAR (0.125 * INTENSITY_CORRECTION)
  1933. ##define LIGHT_TOP_SHININESS 20.0
  1934. #
  1935. #// normalized values for (1./1.43, 0.2/1.43, 1./1.43)
  1936. #const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);
  1937. ##define LIGHT_FRONT_DIFFUSE (0.3 * INTENSITY_CORRECTION)
  1938. #//#define LIGHT_FRONT_SPECULAR (0.0 * INTENSITY_CORRECTION)
  1939. #//#define LIGHT_FRONT_SHININESS 5.0
  1940. #
  1941. ##define INTENSITY_AMBIENT 0.3
  1942. #
  1943. #const vec3 ZERO = vec3(0.0, 0.0, 0.0);
  1944. #
  1945. #struct PrintBoxDetection
  1946. #{
  1947. # vec3 min;
  1948. # vec3 max;
  1949. # // xyz contains the offset, if w == 1.0 detection needs to be performed
  1950. # vec4 volume_origin;
  1951. #};
  1952. #
  1953. #uniform PrintBoxDetection print_box;
  1954. #
  1955. #// x = tainted, y = specular;
  1956. #varying vec2 intensity;
  1957. #
  1958. #varying vec3 delta_box_min;
  1959. #varying vec3 delta_box_max;
  1960. #
  1961. #void main()
  1962. #{
  1963. # // First transform the normal into camera space and normalize the result.
  1964. # vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
  1965. #
  1966. # // Compute the cos of the angle between the normal and lights direction. The light is directional so the direction is constant for every vertex.
  1967. # // Since these two are normalized the cosine is the dot product. We also need to clamp the result to the [0,1] range.
  1968. # float NdotL = max(dot(normal, LIGHT_TOP_DIR), 0.0);
  1969. #
  1970. # intensity.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE;
  1971. # intensity.y = 0.0;
  1972. #
  1973. # if (NdotL > 0.0)
  1974. # intensity.y += LIGHT_TOP_SPECULAR * pow(max(dot(normal, reflect(-LIGHT_TOP_DIR, normal)), 0.0), LIGHT_TOP_SHININESS);
  1975. #
  1976. # // Perform the same lighting calculation for the 2nd light source (no specular applied).
  1977. # NdotL = max(dot(normal, LIGHT_FRONT_DIR), 0.0);
  1978. # intensity.x += NdotL * LIGHT_FRONT_DIFFUSE;
  1979. #
  1980. # // compute deltas for out of print volume detection (world coordinates)
  1981. # if (print_box.volume_origin.w == 1.0)
  1982. # {
  1983. # vec3 v = gl_Vertex.xyz + print_box.volume_origin.xyz;
  1984. # delta_box_min = v - print_box.min;
  1985. # delta_box_max = v - print_box.max;
  1986. # }
  1987. # else
  1988. # {
  1989. # delta_box_min = ZERO;
  1990. # delta_box_max = ZERO;
  1991. # }
  1992. #
  1993. # gl_Position = ftransform();
  1994. #}
  1995. #
  1996. #VERTEX
  1997. #}
  1998. #
  1999. #sub _fragment_shader_Gouraud {
  2000. # return <<'FRAGMENT';
  2001. ##version 110
  2002. #
  2003. #const vec3 ZERO = vec3(0.0, 0.0, 0.0);
  2004. #
  2005. #// x = tainted, y = specular;
  2006. #varying vec2 intensity;
  2007. #
  2008. #varying vec3 delta_box_min;
  2009. #varying vec3 delta_box_max;
  2010. #
  2011. #uniform vec4 uniform_color;
  2012. #
  2013. #void main()
  2014. #{
  2015. # // if the fragment is outside the print volume -> use darker color
  2016. # vec3 color = (any(lessThan(delta_box_min, ZERO)) || any(greaterThan(delta_box_max, ZERO))) ? mix(uniform_color.rgb, ZERO, 0.3333) : uniform_color.rgb;
  2017. # gl_FragColor = vec4(vec3(intensity.y, intensity.y, intensity.y) + color * intensity.x, uniform_color.a);
  2018. #}
  2019. #
  2020. #FRAGMENT
  2021. #}
  2022. #
  2023. #sub _vertex_shader_Phong {
  2024. # return <<'VERTEX';
  2025. ##version 110
  2026. #
  2027. #varying vec3 normal;
  2028. #varying vec3 eye;
  2029. #void main(void)
  2030. #{
  2031. # eye = normalize(vec3(gl_ModelViewMatrix * gl_Vertex));
  2032. # normal = normalize(gl_NormalMatrix * gl_Normal);
  2033. # gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  2034. #}
  2035. #VERTEX
  2036. #}
  2037. #
  2038. #sub _fragment_shader_Phong {
  2039. # return <<'FRAGMENT';
  2040. ##version 110
  2041. #
  2042. ##define INTENSITY_CORRECTION 0.7
  2043. #
  2044. ##define LIGHT_TOP_DIR -0.6/1.31, 0.6/1.31, 1./1.31
  2045. ##define LIGHT_TOP_DIFFUSE (0.8 * INTENSITY_CORRECTION)
  2046. ##define LIGHT_TOP_SPECULAR (0.5 * INTENSITY_CORRECTION)
  2047. #//#define LIGHT_TOP_SHININESS 50.
  2048. ##define LIGHT_TOP_SHININESS 10.
  2049. #
  2050. ##define LIGHT_FRONT_DIR 1./1.43, 0.2/1.43, 1./1.43
  2051. ##define LIGHT_FRONT_DIFFUSE (0.3 * INTENSITY_CORRECTION)
  2052. ##define LIGHT_FRONT_SPECULAR (0.0 * INTENSITY_CORRECTION)
  2053. ##define LIGHT_FRONT_SHININESS 50.
  2054. #
  2055. ##define INTENSITY_AMBIENT 0.0
  2056. #
  2057. #varying vec3 normal;
  2058. #varying vec3 eye;
  2059. #uniform vec4 uniform_color;
  2060. #void main() {
  2061. #
  2062. # float intensity_specular = 0.;
  2063. # float intensity_tainted = 0.;
  2064. # float intensity = max(dot(normal,vec3(LIGHT_TOP_DIR)), 0.0);
  2065. # // if the vertex is lit compute the specular color
  2066. # if (intensity > 0.0) {
  2067. # intensity_tainted = LIGHT_TOP_DIFFUSE * intensity;
  2068. # // compute the half vector
  2069. # vec3 h = normalize(vec3(LIGHT_TOP_DIR) + eye);
  2070. # // compute the specular term into spec
  2071. # intensity_specular = LIGHT_TOP_SPECULAR * pow(max(dot(h, normal), 0.0), LIGHT_TOP_SHININESS);
  2072. # }
  2073. # intensity = max(dot(normal,vec3(LIGHT_FRONT_DIR)), 0.0);
  2074. # // if the vertex is lit compute the specular color
  2075. # if (intensity > 0.0) {
  2076. # intensity_tainted += LIGHT_FRONT_DIFFUSE * intensity;
  2077. # // compute the half vector
  2078. #// vec3 h = normalize(vec3(LIGHT_FRONT_DIR) + eye);
  2079. # // compute the specular term into spec
  2080. #// intensity_specular += LIGHT_FRONT_SPECULAR * pow(max(dot(h,normal), 0.0), LIGHT_FRONT_SHININESS);
  2081. # }
  2082. #
  2083. # gl_FragColor = max(
  2084. # vec4(intensity_specular, intensity_specular, intensity_specular, 0.) + uniform_color * intensity_tainted,
  2085. # INTENSITY_AMBIENT * uniform_color);
  2086. # gl_FragColor.a = uniform_color.a;
  2087. #}
  2088. #FRAGMENT
  2089. #}
  2090. #
  2091. #sub _vertex_shader_variable_layer_height {
  2092. # return <<'VERTEX';
  2093. ##version 110
  2094. #
  2095. ##define INTENSITY_CORRECTION 0.6
  2096. #
  2097. #const vec3 LIGHT_TOP_DIR = vec3(-0.4574957, 0.4574957, 0.7624929);
  2098. ##define LIGHT_TOP_DIFFUSE (0.8 * INTENSITY_CORRECTION)
  2099. ##define LIGHT_TOP_SPECULAR (0.125 * INTENSITY_CORRECTION)
  2100. ##define LIGHT_TOP_SHININESS 20.0
  2101. #
  2102. #const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);
  2103. ##define LIGHT_FRONT_DIFFUSE (0.3 * INTENSITY_CORRECTION)
  2104. #//#define LIGHT_FRONT_SPECULAR (0.0 * INTENSITY_CORRECTION)
  2105. #//#define LIGHT_FRONT_SHININESS 5.0
  2106. #
  2107. ##define INTENSITY_AMBIENT 0.3
  2108. #
  2109. #// x = tainted, y = specular;
  2110. #varying vec2 intensity;
  2111. #
  2112. #varying float object_z;
  2113. #
  2114. #void main()
  2115. #{
  2116. # // First transform the normal into camera space and normalize the result.
  2117. # vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
  2118. #
  2119. # // Compute the cos of the angle between the normal and lights direction. The light is directional so the direction is constant for every vertex.
  2120. # // Since these two are normalized the cosine is the dot product. We also need to clamp the result to the [0,1] range.
  2121. # float NdotL = max(dot(normal, LIGHT_TOP_DIR), 0.0);
  2122. #
  2123. # intensity.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE;
  2124. # intensity.y = 0.0;
  2125. #
  2126. # if (NdotL > 0.0)
  2127. # intensity.y += LIGHT_TOP_SPECULAR * pow(max(dot(normal, reflect(-LIGHT_TOP_DIR, normal)), 0.0), LIGHT_TOP_SHININESS);
  2128. #
  2129. # // Perform the same lighting calculation for the 2nd light source (no specular)
  2130. # NdotL = max(dot(normal, LIGHT_FRONT_DIR), 0.0);
  2131. #
  2132. # intensity.x += NdotL * LIGHT_FRONT_DIFFUSE;
  2133. #
  2134. # // Scaled to widths of the Z texture.
  2135. # object_z = gl_Vertex.z;
  2136. #
  2137. # gl_Position = ftransform();
  2138. #}
  2139. #
  2140. #VERTEX
  2141. #}
  2142. #
  2143. #sub _fragment_shader_variable_layer_height {
  2144. # return <<'FRAGMENT';
  2145. ##version 110
  2146. #
  2147. ##define M_PI 3.1415926535897932384626433832795
  2148. #
  2149. #// 2D texture (1D texture split by the rows) of color along the object Z axis.
  2150. #uniform sampler2D z_texture;
  2151. #// Scaling from the Z texture rows coordinate to the normalized texture row coordinate.
  2152. #uniform float z_to_texture_row;
  2153. #uniform float z_texture_row_to_normalized;
  2154. #uniform float z_cursor;
  2155. #uniform float z_cursor_band_width;
  2156. #
  2157. #// x = tainted, y = specular;
  2158. #varying vec2 intensity;
  2159. #
  2160. #varying float object_z;
  2161. #
  2162. #void main()
  2163. #{
  2164. # float object_z_row = z_to_texture_row * object_z;
  2165. # // Index of the row in the texture.
  2166. # float z_texture_row = floor(object_z_row);
  2167. # // Normalized coordinate from 0. to 1.
  2168. # float z_texture_col = object_z_row - z_texture_row;
  2169. # float z_blend = 0.25 * cos(min(M_PI, abs(M_PI * (object_z - z_cursor) * 1.8 / z_cursor_band_width))) + 0.25;
  2170. # // Calculate level of detail from the object Z coordinate.
  2171. # // This makes the slowly sloping surfaces to be show with high detail (with stripes),
  2172. # // and the vertical surfaces to be shown with low detail (no stripes)
  2173. # float z_in_cells = object_z_row * 190.;
  2174. # // Gradient of Z projected on the screen.
  2175. # float dx_vtc = dFdx(z_in_cells);
  2176. # float dy_vtc = dFdy(z_in_cells);
  2177. # float lod = clamp(0.5 * log2(max(dx_vtc*dx_vtc, dy_vtc*dy_vtc)), 0., 1.);
  2178. # // Sample the Z texture. Texture coordinates are normalized to <0, 1>.
  2179. # vec4 color =
  2180. # mix(texture2D(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row + 0.5 )), -10000.),
  2181. # texture2D(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row * 2. + 1.)), 10000.), lod);
  2182. #
  2183. # // Mix the final color.
  2184. # gl_FragColor =
  2185. # vec4(intensity.y, intensity.y, intensity.y, 1.0) + intensity.x * mix(color, vec4(1.0, 1.0, 0.0, 1.0), z_blend);
  2186. #}
  2187. #
  2188. #FRAGMENT
  2189. #}
  2190. #===================================================================================================================================
  2191. # The 3D canvas to display objects and tool paths.
  2192. package Slic3r::GUI::3DScene;
  2193. use base qw(Slic3r::GUI::3DScene::Base);
  2194. use OpenGL qw(:glconstants :gluconstants :glufunctions);
  2195. use List::Util qw(first min max);
  2196. use Slic3r::Geometry qw(scale unscale epsilon);
  2197. use Slic3r::Print::State ':steps';
  2198. __PACKAGE__->mk_accessors(qw(
  2199. color_by
  2200. select_by
  2201. drag_by
  2202. ));
  2203. sub new {
  2204. my $class = shift;
  2205. my $self = $class->SUPER::new(@_);
  2206. $self->color_by('volume'); # object | volume
  2207. $self->select_by('object'); # object | volume | instance
  2208. $self->drag_by('instance'); # object | instance
  2209. return $self;
  2210. }
  2211. sub load_object {
  2212. my ($self, $model, $print, $obj_idx, $instance_idxs) = @_;
  2213. $self->SetCurrent($self->GetContext) if $self->UseVBOs;
  2214. my $model_object;
  2215. if ($model->isa('Slic3r::Model::Object')) {
  2216. $model_object = $model;
  2217. $model = $model_object->model;
  2218. $obj_idx = 0;
  2219. } else {
  2220. $model_object = $model->get_object($obj_idx);
  2221. }
  2222. $instance_idxs ||= [0..$#{$model_object->instances}];
  2223. my $volume_indices = $self->volumes->load_object(
  2224. $model_object, $obj_idx, $instance_idxs, $self->color_by, $self->select_by, $self->drag_by,
  2225. $self->UseVBOs);
  2226. return @{$volume_indices};
  2227. }
  2228. # Create 3D thick extrusion lines for a skirt and brim.
  2229. # Adds a new Slic3r::GUI::3DScene::Volume to $self->volumes.
  2230. sub load_print_toolpaths {
  2231. my ($self, $print, $colors) = @_;
  2232. $self->SetCurrent($self->GetContext) if $self->UseVBOs;
  2233. Slic3r::GUI::_3DScene::_load_print_toolpaths($print, $self->volumes, $colors, $self->UseVBOs)
  2234. if ($print->step_done(STEP_SKIRT) && $print->step_done(STEP_BRIM));
  2235. }
  2236. # Create 3D thick extrusion lines for object forming extrusions.
  2237. # Adds a new Slic3r::GUI::3DScene::Volume to $self->volumes,
  2238. # one for perimeters, one for infill and one for supports.
  2239. sub load_print_object_toolpaths {
  2240. my ($self, $object, $colors) = @_;
  2241. $self->SetCurrent($self->GetContext) if $self->UseVBOs;
  2242. Slic3r::GUI::_3DScene::_load_print_object_toolpaths($object, $self->volumes, $colors, $self->UseVBOs);
  2243. }
  2244. # Create 3D thick extrusion lines for wipe tower extrusions.
  2245. sub load_wipe_tower_toolpaths {
  2246. my ($self, $print, $colors) = @_;
  2247. $self->SetCurrent($self->GetContext) if $self->UseVBOs;
  2248. Slic3r::GUI::_3DScene::_load_wipe_tower_toolpaths($print, $self->volumes, $colors, $self->UseVBOs)
  2249. if ($print->step_done(STEP_WIPE_TOWER));
  2250. }
  2251. sub load_gcode_preview {
  2252. my ($self, $print, $gcode_preview_data, $colors) = @_;
  2253. $self->SetCurrent($self->GetContext) if $self->UseVBOs;
  2254. Slic3r::GUI::_3DScene::load_gcode_preview($print, $gcode_preview_data, $self->volumes, $colors, $self->UseVBOs);
  2255. }
  2256. sub set_toolpaths_range {
  2257. my ($self, $min_z, $max_z) = @_;
  2258. $self->volumes->set_range($min_z, $max_z);
  2259. }
  2260. sub reset_legend_texture {
  2261. Slic3r::GUI::_3DScene::reset_legend_texture();
  2262. }
  2263. sub get_current_print_zs {
  2264. my ($self, $active_only) = @_;
  2265. return $self->volumes->get_current_print_zs($active_only);
  2266. }
  2267. 1;