GLCanvas3D.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. #include "GLCanvas3D.hpp"
  2. #include "../../slic3r/GUI/3DScene.hpp"
  3. #include "../../slic3r/GUI/GLShader.hpp"
  4. #include "../../libslic3r/ClipperUtils.hpp"
  5. #include "../../libslic3r/PrintConfig.hpp"
  6. #include "../../libslic3r/Print.hpp"
  7. #include <GL/glew.h>
  8. #include <wx/glcanvas.h>
  9. #include <wx/image.h>
  10. #include <iostream>
  11. #include <float.h>
  12. static const bool TURNTABLE_MODE = true;
  13. static const float GIMBALL_LOCK_THETA_MAX = 180.0f;
  14. static const float GROUND_Z = -0.02f;
  15. // phi / theta angles to orient the camera.
  16. static const float VIEW_DEFAULT[2] = { 45.0f, 45.0f };
  17. static const float VIEW_LEFT[2] = { 90.0f, 90.0f };
  18. static const float VIEW_RIGHT[2] = { -90.0f, 90.0f };
  19. static const float VIEW_TOP[2] = { 0.0f, 0.0f };
  20. static const float VIEW_BOTTOM[2] = { 0.0f, 180.0f };
  21. static const float VIEW_FRONT[2] = { 0.0f, 90.0f };
  22. static const float VIEW_REAR[2] = { 180.0f, 90.0f };
  23. static const float VARIABLE_LAYER_THICKNESS_BAR_WIDTH = 70.0f;
  24. static const float VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT = 22.0f;
  25. namespace Slic3r {
  26. namespace GUI {
  27. bool GeometryBuffer::set_from_triangles(const Polygons& triangles, float z)
  28. {
  29. m_data.clear();
  30. unsigned int size = 9 * (unsigned int)triangles.size();
  31. if (size == 0)
  32. return false;
  33. m_data = std::vector<float>(size, 0.0f);
  34. unsigned int coord = 0;
  35. for (const Polygon& t : triangles)
  36. {
  37. for (unsigned int v = 0; v < 3; ++v)
  38. {
  39. const Point& p = t.points[v];
  40. m_data[coord++] = (float)unscale(p.x);
  41. m_data[coord++] = (float)unscale(p.y);
  42. m_data[coord++] = z;
  43. }
  44. }
  45. return true;
  46. }
  47. bool GeometryBuffer::set_from_lines(const Lines& lines, float z)
  48. {
  49. m_data.clear();
  50. unsigned int size = 6 * (unsigned int)lines.size();
  51. if (size == 0)
  52. return false;
  53. m_data = std::vector<float>(size, 0.0f);
  54. unsigned int coord = 0;
  55. for (const Line& l : lines)
  56. {
  57. m_data[coord++] = (float)unscale(l.a.x);
  58. m_data[coord++] = (float)unscale(l.a.y);
  59. m_data[coord++] = z;
  60. m_data[coord++] = (float)unscale(l.b.x);
  61. m_data[coord++] = (float)unscale(l.b.y);
  62. m_data[coord++] = z;
  63. }
  64. return true;
  65. }
  66. const float* GeometryBuffer::get_data() const
  67. {
  68. return m_data.data();
  69. }
  70. unsigned int GeometryBuffer::get_data_size() const
  71. {
  72. return (unsigned int)m_data.size();
  73. }
  74. Size::Size()
  75. : m_width(0)
  76. , m_height(0)
  77. {
  78. }
  79. Size::Size(int width, int height)
  80. : m_width(width)
  81. , m_height(height)
  82. {
  83. }
  84. int Size::get_width() const
  85. {
  86. return m_width;
  87. }
  88. void Size::set_width(int width)
  89. {
  90. m_width = width;
  91. }
  92. int Size::get_height() const
  93. {
  94. return m_height;
  95. }
  96. void Size::set_height(int height)
  97. {
  98. m_height = height;
  99. }
  100. Rect::Rect()
  101. : m_left(0.0f)
  102. , m_top(0.0f)
  103. , m_right(0.0f)
  104. , m_bottom(0.0f)
  105. {
  106. }
  107. Rect::Rect(float left, float top, float right, float bottom)
  108. : m_left(left)
  109. , m_top(top)
  110. , m_right(right)
  111. , m_bottom(bottom)
  112. {
  113. }
  114. float Rect::get_left() const
  115. {
  116. return m_left;
  117. }
  118. void Rect::set_left(float left)
  119. {
  120. m_left = left;
  121. }
  122. float Rect::get_top() const
  123. {
  124. return m_top;
  125. }
  126. void Rect::set_top(float top)
  127. {
  128. m_top = top;
  129. }
  130. float Rect::get_right() const
  131. {
  132. return m_right;
  133. }
  134. void Rect::set_right(float right)
  135. {
  136. m_right = right;
  137. }
  138. float Rect::get_bottom() const
  139. {
  140. return m_bottom;
  141. }
  142. void Rect::set_bottom(float bottom)
  143. {
  144. m_bottom = bottom;
  145. }
  146. GLCanvas3D::Camera::Camera()
  147. : m_type(CT_Ortho)
  148. , m_zoom(1.0f)
  149. , m_phi(45.0f)
  150. , m_theta(45.0f)
  151. , m_distance(0.0f)
  152. , m_target(0.0, 0.0, 0.0)
  153. {
  154. }
  155. GLCanvas3D::Camera::EType GLCanvas3D::Camera::get_type() const
  156. {
  157. return m_type;
  158. }
  159. void GLCanvas3D::Camera::set_type(GLCanvas3D::Camera::EType type)
  160. {
  161. m_type = type;
  162. }
  163. std::string GLCanvas3D::Camera::get_type_as_string() const
  164. {
  165. switch (m_type)
  166. {
  167. default:
  168. case CT_Unknown:
  169. return "unknown";
  170. case CT_Perspective:
  171. return "perspective";
  172. case CT_Ortho:
  173. return "ortho";
  174. };
  175. }
  176. float GLCanvas3D::Camera::get_zoom() const
  177. {
  178. return m_zoom;
  179. }
  180. void GLCanvas3D::Camera::set_zoom(float zoom)
  181. {
  182. m_zoom = zoom;
  183. }
  184. float GLCanvas3D::Camera::get_phi() const
  185. {
  186. return m_phi;
  187. }
  188. void GLCanvas3D::Camera::set_phi(float phi)
  189. {
  190. m_phi = phi;
  191. }
  192. float GLCanvas3D::Camera::get_theta() const
  193. {
  194. return m_theta;
  195. }
  196. void GLCanvas3D::Camera::set_theta(float theta)
  197. {
  198. m_theta = theta;
  199. // clamp angle
  200. if (m_theta > GIMBALL_LOCK_THETA_MAX)
  201. m_theta = GIMBALL_LOCK_THETA_MAX;
  202. if (m_theta < 0.0f)
  203. m_theta = 0.0f;
  204. }
  205. float GLCanvas3D::Camera::get_distance() const
  206. {
  207. return m_distance;
  208. }
  209. void GLCanvas3D::Camera::set_distance(float distance)
  210. {
  211. m_distance = distance;
  212. }
  213. const Pointf3& GLCanvas3D::Camera::get_target() const
  214. {
  215. return m_target;
  216. }
  217. void GLCanvas3D::Camera::set_target(const Pointf3& target)
  218. {
  219. m_target = target;
  220. }
  221. const Pointfs& GLCanvas3D::Bed::get_shape() const
  222. {
  223. return m_shape;
  224. }
  225. void GLCanvas3D::Bed::set_shape(const Pointfs& shape)
  226. {
  227. m_shape = shape;
  228. _calc_bounding_box();
  229. ExPolygon poly;
  230. for (const Pointf& p : m_shape)
  231. {
  232. poly.contour.append(Point(scale_(p.x), scale_(p.y)));
  233. }
  234. _calc_triangles(poly);
  235. const BoundingBox& bed_bbox = poly.contour.bounding_box();
  236. _calc_gridlines(poly, bed_bbox);
  237. m_polygon = offset_ex(poly.contour, bed_bbox.radius() * 1.7, jtRound, scale_(0.5))[0].contour;
  238. }
  239. const BoundingBoxf3& GLCanvas3D::Bed::get_bounding_box() const
  240. {
  241. return m_bounding_box;
  242. }
  243. void GLCanvas3D::Bed::render() const
  244. {
  245. unsigned int triangles_vcount = m_triangles.get_data_size() / 3;
  246. if (triangles_vcount > 0)
  247. {
  248. ::glDisable(GL_LIGHTING);
  249. ::glDisable(GL_DEPTH_TEST);
  250. ::glEnable(GL_BLEND);
  251. ::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  252. ::glEnableClientState(GL_VERTEX_ARRAY);
  253. ::glColor4f(0.8f, 0.6f, 0.5f, 0.4f);
  254. ::glNormal3d(0.0f, 0.0f, 1.0f);
  255. ::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_triangles.get_data());
  256. ::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)triangles_vcount);
  257. // we need depth test for grid, otherwise it would disappear when looking
  258. // the object from below
  259. glEnable(GL_DEPTH_TEST);
  260. // draw grid
  261. unsigned int gridlines_vcount = m_gridlines.get_data_size() / 3;
  262. ::glLineWidth(3.0f);
  263. ::glColor4f(0.2f, 0.2f, 0.2f, 0.4f);
  264. ::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_gridlines.get_data());
  265. ::glDrawArrays(GL_LINES, 0, (GLsizei)gridlines_vcount);
  266. ::glDisableClientState(GL_VERTEX_ARRAY);
  267. ::glDisable(GL_BLEND);
  268. }
  269. }
  270. void GLCanvas3D::Bed::_calc_bounding_box()
  271. {
  272. m_bounding_box = BoundingBoxf3();
  273. for (const Pointf& p : m_shape)
  274. {
  275. m_bounding_box.merge(Pointf3(p.x, p.y, 0.0));
  276. }
  277. }
  278. void GLCanvas3D::Bed::_calc_triangles(const ExPolygon& poly)
  279. {
  280. Polygons triangles;
  281. poly.triangulate(&triangles);
  282. if (!m_triangles.set_from_triangles(triangles, GROUND_Z))
  283. printf("Unable to create bed triangles\n");
  284. }
  285. void GLCanvas3D::Bed::_calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox)
  286. {
  287. Polylines axes_lines;
  288. for (coord_t x = bed_bbox.min.x; x <= bed_bbox.max.x; x += scale_(10.0))
  289. {
  290. Polyline line;
  291. line.append(Point(x, bed_bbox.min.y));
  292. line.append(Point(x, bed_bbox.max.y));
  293. axes_lines.push_back(line);
  294. }
  295. for (coord_t y = bed_bbox.min.y; y <= bed_bbox.max.y; y += scale_(10.0))
  296. {
  297. Polyline line;
  298. line.append(Point(bed_bbox.min.x, y));
  299. line.append(Point(bed_bbox.max.x, y));
  300. axes_lines.push_back(line);
  301. }
  302. // clip with a slightly grown expolygon because our lines lay on the contours and may get erroneously clipped
  303. Lines gridlines = to_lines(intersection_pl(axes_lines, offset(poly, SCALED_EPSILON)));
  304. // append bed contours
  305. Lines contour_lines = to_lines(poly);
  306. std::copy(contour_lines.begin(), contour_lines.end(), std::back_inserter(gridlines));
  307. if (!m_gridlines.set_from_lines(gridlines, GROUND_Z))
  308. printf("Unable to create bed grid lines\n");
  309. }
  310. GLCanvas3D::Axes::Axes()
  311. : m_length(0.0f)
  312. {
  313. }
  314. const Pointf3& GLCanvas3D::Axes::get_origin() const
  315. {
  316. return m_origin;
  317. }
  318. void GLCanvas3D::Axes::set_origin(const Pointf3& origin)
  319. {
  320. m_origin = origin;
  321. }
  322. float GLCanvas3D::Axes::get_length() const
  323. {
  324. return m_length;
  325. }
  326. void GLCanvas3D::Axes::set_length(float length)
  327. {
  328. m_length = length;
  329. }
  330. void GLCanvas3D::Axes::render() const
  331. {
  332. ::glDisable(GL_LIGHTING);
  333. // disable depth testing so that axes are not covered by ground
  334. ::glDisable(GL_DEPTH_TEST);
  335. ::glLineWidth(2.0f);
  336. ::glBegin(GL_LINES);
  337. // draw line for x axis
  338. ::glColor3f(1.0f, 0.0f, 0.0f);
  339. ::glVertex3f((float)m_origin.x, (float)m_origin.y, (float)m_origin.z);
  340. ::glVertex3f((float)m_origin.x + m_length, (float)m_origin.y, (float)m_origin.z);
  341. // draw line for y axis
  342. ::glColor3f(0.0f, 1.0f, 0.0f);
  343. ::glVertex3f((float)m_origin.x, (float)m_origin.y, (float)m_origin.z);
  344. ::glVertex3f((float)m_origin.x, (float)m_origin.y + m_length, (float)m_origin.z);
  345. ::glEnd();
  346. // draw line for Z axis
  347. // (re-enable depth test so that axis is correctly shown when objects are behind it)
  348. ::glEnable(GL_DEPTH_TEST);
  349. ::glBegin(GL_LINES);
  350. ::glColor3f(0.0f, 0.0f, 1.0f);
  351. ::glVertex3f((float)m_origin.x, (float)m_origin.y, (float)m_origin.z);
  352. ::glVertex3f((float)m_origin.x, (float)m_origin.y, (float)m_origin.z + m_length);
  353. ::glEnd();
  354. }
  355. GLCanvas3D::CuttingPlane::CuttingPlane()
  356. : m_z(-1.0f)
  357. {
  358. }
  359. bool GLCanvas3D::CuttingPlane::set(float z, const ExPolygons& polygons)
  360. {
  361. m_z = z;
  362. // grow slices in order to display them better
  363. ExPolygons expolygons = offset_ex(polygons, scale_(0.1));
  364. Lines lines = to_lines(expolygons);
  365. return m_lines.set_from_lines(lines, m_z);
  366. }
  367. void GLCanvas3D::CuttingPlane::render(const BoundingBoxf3& bb) const
  368. {
  369. ::glDisable(GL_LIGHTING);
  370. _render_plane(bb);
  371. _render_contour();
  372. }
  373. void GLCanvas3D::CuttingPlane::_render_plane(const BoundingBoxf3& bb) const
  374. {
  375. if (m_z >= 0.0f)
  376. {
  377. ::glDisable(GL_CULL_FACE);
  378. ::glEnable(GL_BLEND);
  379. ::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  380. float margin = 20.0f;
  381. float min_x = bb.min.x - margin;
  382. float max_x = bb.max.x + margin;
  383. float min_y = bb.min.y - margin;
  384. float max_y = bb.max.y + margin;
  385. ::glBegin(GL_QUADS);
  386. ::glColor4f(0.8f, 0.8f, 0.8f, 0.5f);
  387. ::glVertex3f(min_x, min_y, m_z);
  388. ::glVertex3f(max_x, min_y, m_z);
  389. ::glVertex3f(max_x, max_y, m_z);
  390. ::glVertex3f(min_x, max_y, m_z);
  391. ::glEnd();
  392. ::glEnable(GL_CULL_FACE);
  393. ::glDisable(GL_BLEND);
  394. }
  395. }
  396. void GLCanvas3D::CuttingPlane::_render_contour() const
  397. {
  398. ::glEnableClientState(GL_VERTEX_ARRAY);
  399. if (m_z >= 0.0f)
  400. {
  401. unsigned int lines_vcount = m_lines.get_data_size() / 3;
  402. ::glLineWidth(2.0f);
  403. ::glColor3f(0.0f, 0.0f, 0.0f);
  404. ::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_lines.get_data());
  405. ::glDrawArrays(GL_LINES, 0, (GLsizei)lines_vcount);
  406. }
  407. ::glDisableClientState(GL_VERTEX_ARRAY);
  408. }
  409. GLCanvas3D::Shader::Shader()
  410. : m_shader(nullptr)
  411. {
  412. }
  413. GLCanvas3D::Shader::~Shader()
  414. {
  415. _reset();
  416. }
  417. bool GLCanvas3D::Shader::init(const std::string& vertex_shader_filename, const std::string& fragment_shader_filename)
  418. {
  419. if (is_initialized())
  420. return true;
  421. m_shader = new GLShader();
  422. if (m_shader != nullptr)
  423. {
  424. if (!m_shader->load_from_file(fragment_shader_filename.c_str(), vertex_shader_filename.c_str()))
  425. {
  426. std::cout << "Compilaton of shader failed:" << std::endl;
  427. std::cout << m_shader->last_error << std::endl;
  428. _reset();
  429. return false;
  430. }
  431. }
  432. return true;
  433. }
  434. bool GLCanvas3D::Shader::is_initialized() const
  435. {
  436. return (m_shader != nullptr);
  437. }
  438. bool GLCanvas3D::Shader::start_using() const
  439. {
  440. if (is_initialized())
  441. {
  442. m_shader->enable();
  443. return true;
  444. }
  445. else
  446. return false;
  447. }
  448. void GLCanvas3D::Shader::stop_using() const
  449. {
  450. if (m_shader != nullptr)
  451. m_shader->disable();
  452. }
  453. GLShader* GLCanvas3D::Shader::get_shader()
  454. {
  455. return m_shader;
  456. }
  457. void GLCanvas3D::Shader::_reset()
  458. {
  459. if (m_shader != nullptr)
  460. {
  461. m_shader->release();
  462. delete m_shader;
  463. m_shader = nullptr;
  464. }
  465. }
  466. GLCanvas3D::LayersEditing::GLTextureData::GLTextureData()
  467. : id(0)
  468. , width(0)
  469. , height(0)
  470. {
  471. }
  472. GLCanvas3D::LayersEditing::GLTextureData::GLTextureData(unsigned int id, int width, int height)
  473. : id(id)
  474. , width(width)
  475. , height(height)
  476. {
  477. }
  478. GLCanvas3D::LayersEditing::LayersEditing()
  479. : m_allowed(false)
  480. , m_enabled(false)
  481. , m_z_texture_id(0)
  482. {
  483. }
  484. GLCanvas3D::LayersEditing::~LayersEditing()
  485. {
  486. if (m_tooltip_texture.id != 0)
  487. {
  488. ::glDeleteTextures(1, &m_tooltip_texture.id);
  489. m_tooltip_texture = GLTextureData();
  490. }
  491. if (m_reset_texture.id != 0)
  492. {
  493. ::glDeleteTextures(1, &m_reset_texture.id);
  494. m_reset_texture = GLTextureData();
  495. }
  496. if (m_z_texture_id != 0)
  497. {
  498. ::glDeleteTextures(1, &m_z_texture_id);
  499. m_z_texture_id = 0;
  500. }
  501. }
  502. bool GLCanvas3D::LayersEditing::init(const std::string& vertex_shader_filename, const std::string& fragment_shader_filename)
  503. {
  504. if (!m_shader.init(vertex_shader_filename, fragment_shader_filename))
  505. return false;
  506. ::glGenTextures(1, (GLuint*)&m_z_texture_id);
  507. ::glBindTexture(GL_TEXTURE_2D, m_z_texture_id);
  508. ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  509. ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  510. ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  511. ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  512. ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  513. ::glBindTexture(GL_TEXTURE_2D, 0);
  514. return true;
  515. }
  516. bool GLCanvas3D::LayersEditing::is_allowed() const
  517. {
  518. return m_allowed;
  519. }
  520. void GLCanvas3D::LayersEditing::set_allowed(bool allowed)
  521. {
  522. m_allowed = allowed;
  523. }
  524. bool GLCanvas3D::LayersEditing::is_enabled() const
  525. {
  526. return m_enabled;
  527. }
  528. void GLCanvas3D::LayersEditing::set_enabled(bool enabled)
  529. {
  530. m_enabled = m_allowed && m_shader.is_initialized() && enabled;
  531. }
  532. unsigned int GLCanvas3D::LayersEditing::get_z_texture_id() const
  533. {
  534. return m_z_texture_id;
  535. }
  536. void GLCanvas3D::LayersEditing::render(const GLCanvas3D& canvas, const PrintObject& print_object) const
  537. {
  538. const Rect& bar_rect = _get_bar_rect_viewport(canvas);
  539. const Rect& reset_rect = _get_reset_rect_viewport(canvas);
  540. _render_tooltip_texture(canvas, bar_rect, reset_rect);
  541. _render_reset_texture(canvas, reset_rect);
  542. _render_profile(print_object, bar_rect);
  543. }
  544. GLShader* GLCanvas3D::LayersEditing::get_shader()
  545. {
  546. return m_shader.get_shader();
  547. }
  548. bool GLCanvas3D::LayersEditing::_is_initialized() const
  549. {
  550. return m_shader.is_initialized();
  551. }
  552. GLCanvas3D::LayersEditing::GLTextureData GLCanvas3D::LayersEditing::_load_texture_from_file(const std::string& filename) const
  553. {
  554. const std::string& path = resources_dir() + "/icons/";
  555. // Load a PNG with an alpha channel.
  556. wxImage image;
  557. if (!image.LoadFile(path + filename, wxBITMAP_TYPE_PNG))
  558. return GLTextureData();
  559. int width = image.GetWidth();
  560. int height = image.GetHeight();
  561. int n_pixels = width * height;
  562. if (n_pixels <= 0)
  563. return GLTextureData();
  564. // Get RGB & alpha raw data from wxImage, pack them into an array.
  565. unsigned char* img_rgb = image.GetData();
  566. if (img_rgb == nullptr)
  567. return GLTextureData();
  568. unsigned char* img_alpha = image.GetAlpha();
  569. std::vector<unsigned char> data(n_pixels * 4, 0);
  570. for (int i = 0; i < n_pixels; ++i)
  571. {
  572. int data_id = i * 4;
  573. int img_id = i * 3;
  574. data[data_id + 0] = img_rgb[img_id + 0];
  575. data[data_id + 1] = img_rgb[img_id + 1];
  576. data[data_id + 2] = img_rgb[img_id + 2];
  577. data[data_id + 3] = (img_alpha != nullptr) ? img_alpha[i] : 255;
  578. }
  579. // sends data to gpu
  580. GLuint tex_id;
  581. ::glGenTextures(1, &tex_id);
  582. ::glBindTexture(GL_TEXTURE_2D, tex_id);
  583. ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  584. ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  585. ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  586. ::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data());
  587. ::glBindTexture(GL_TEXTURE_2D, 0);
  588. return GLTextureData((unsigned int)tex_id, width, height);
  589. }
  590. void GLCanvas3D::LayersEditing::_render_tooltip_texture(const GLCanvas3D& canvas, const Rect& bar_rect, const Rect& reset_rect) const
  591. {
  592. if (m_tooltip_texture.id == 0)
  593. {
  594. m_tooltip_texture = _load_texture_from_file("variable_layer_height_tooltip.png");
  595. if (m_tooltip_texture.id == 0)
  596. return;
  597. }
  598. float zoom = canvas.get_camera_zoom();
  599. float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;
  600. float gap = 10.0f * inv_zoom;
  601. float bar_left = bar_rect.get_left();
  602. float reset_bottom = reset_rect.get_bottom();
  603. float l = bar_left - (float)m_tooltip_texture.width * inv_zoom - gap;
  604. float r = bar_left - gap;
  605. float t = reset_bottom + (float)m_tooltip_texture.height * inv_zoom + gap;
  606. float b = reset_bottom + gap;
  607. canvas.render_texture(m_tooltip_texture.id, l, r, b, t);
  608. }
  609. void GLCanvas3D::LayersEditing::_render_reset_texture(const GLCanvas3D& canvas, const Rect& reset_rect) const
  610. {
  611. if (m_reset_texture.id == 0)
  612. {
  613. m_reset_texture = _load_texture_from_file("variable_layer_height_reset.png");
  614. if (m_reset_texture.id == 0)
  615. return;
  616. }
  617. canvas.render_texture(m_reset_texture.id, reset_rect.get_left(), reset_rect.get_right(), reset_rect.get_bottom(), reset_rect.get_top());
  618. }
  619. void GLCanvas3D::LayersEditing::_render_profile(const PrintObject& print_object, const Rect& bar_rect) const
  620. {
  621. // FIXME show some kind of legend.
  622. // Get a maximum layer height value.
  623. // FIXME This is a duplicate code of Slicing.cpp.
  624. double layer_height_max = DBL_MAX;
  625. const PrintConfig& print_config = print_object.print()->config;
  626. const std::vector<double>& nozzle_diameters = dynamic_cast<const ConfigOptionFloats*>(print_config.option("nozzle_diameter"))->values;
  627. const std::vector<double>& layer_heights_min = dynamic_cast<const ConfigOptionFloats*>(print_config.option("min_layer_height"))->values;
  628. const std::vector<double>& layer_heights_max = dynamic_cast<const ConfigOptionFloats*>(print_config.option("max_layer_height"))->values;
  629. for (unsigned int i = 0; i < (unsigned int)nozzle_diameters.size(); ++i)
  630. {
  631. double lh_min = (layer_heights_min[i] == 0.0) ? 0.07 : std::max(0.01, layer_heights_min[i]);
  632. double lh_max = (layer_heights_max[i] == 0.0) ? (0.75 * nozzle_diameters[i]) : layer_heights_max[i];
  633. layer_height_max = std::min(layer_height_max, std::max(lh_min, lh_max));
  634. }
  635. // Make the vertical bar a bit wider so the layer height curve does not touch the edge of the bar region.
  636. layer_height_max *= 1.12;
  637. coordf_t max_z = unscale(print_object.size.z);
  638. double layer_height = dynamic_cast<const ConfigOptionFloat*>(print_object.config.option("layer_height"))->value;
  639. float l = bar_rect.get_left();
  640. float w = bar_rect.get_right() - l;
  641. float b = bar_rect.get_bottom();
  642. float t = bar_rect.get_top();
  643. float h = t - b;
  644. float scale_x = w / (float)layer_height_max;
  645. float scale_y = h / (float)max_z;
  646. float x = l + (float)layer_height * scale_x;
  647. // Baseline
  648. ::glColor3f(0.0f, 0.0f, 0.0f);
  649. ::glBegin(GL_LINE_STRIP);
  650. ::glVertex2f(x, b);
  651. ::glVertex2f(x, t);
  652. ::glEnd();
  653. // Curve
  654. const ModelObject* model_object = print_object.model_object();
  655. if (model_object->layer_height_profile_valid)
  656. {
  657. const std::vector<coordf_t>& profile = model_object->layer_height_profile;
  658. ::glColor3f(0.0f, 0.0f, 1.0f);
  659. ::glBegin(GL_LINE_STRIP);
  660. for (unsigned int i = 0; i < profile.size(); i += 2)
  661. {
  662. ::glVertex2f(l + (float)profile[i + 1] * scale_x, b + (float)profile[i] * scale_y);
  663. }
  664. ::glEnd();
  665. }
  666. }
  667. Rect GLCanvas3D::LayersEditing::_get_bar_rect_screen(const GLCanvas3D& canvas) const
  668. {
  669. const Size& cnv_size = canvas.get_canvas_size();
  670. float w = (float)cnv_size.get_width();
  671. float h = (float)cnv_size.get_height();
  672. return Rect(w - VARIABLE_LAYER_THICKNESS_BAR_WIDTH, 0.0f, w, h - VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT);
  673. }
  674. Rect GLCanvas3D::LayersEditing::_get_reset_rect_screen(const GLCanvas3D& canvas) const
  675. {
  676. const Size& cnv_size = canvas.get_canvas_size();
  677. float w = (float)cnv_size.get_width();
  678. float h = (float)cnv_size.get_height();
  679. return Rect(w - VARIABLE_LAYER_THICKNESS_BAR_WIDTH, h - VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT, w, h);
  680. }
  681. Rect GLCanvas3D::LayersEditing::_get_bar_rect_viewport(const GLCanvas3D& canvas) const
  682. {
  683. const Size& cnv_size = canvas.get_canvas_size();
  684. float half_w = 0.5f * (float)cnv_size.get_width();
  685. float half_h = 0.5f * (float)cnv_size.get_height();
  686. float zoom = canvas.get_camera_zoom();
  687. float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;
  688. return Rect((half_w - VARIABLE_LAYER_THICKNESS_BAR_WIDTH) * inv_zoom, half_h * inv_zoom, half_w * inv_zoom, (-half_h + VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT) * inv_zoom);
  689. }
  690. Rect GLCanvas3D::LayersEditing::_get_reset_rect_viewport(const GLCanvas3D& canvas) const
  691. {
  692. const Size& cnv_size = canvas.get_canvas_size();
  693. float half_w = 0.5f * (float)cnv_size.get_width();
  694. float half_h = 0.5f * (float)cnv_size.get_height();
  695. float zoom = canvas.get_camera_zoom();
  696. float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;
  697. return Rect((half_w - VARIABLE_LAYER_THICKNESS_BAR_WIDTH) * inv_zoom, (-half_h + VARIABLE_LAYER_THICKNESS_RESET_BUTTON_HEIGHT) * inv_zoom, half_w * inv_zoom, -half_h * inv_zoom);
  698. }
  699. GLCanvas3D::Mouse::Mouse()
  700. : m_dragging(false)
  701. {
  702. }
  703. bool GLCanvas3D::Mouse::is_dragging() const
  704. {
  705. return m_dragging;
  706. }
  707. void GLCanvas3D::Mouse::set_dragging(bool dragging)
  708. {
  709. m_dragging = dragging;
  710. }
  711. const Pointf& GLCanvas3D::Mouse::get_position() const
  712. {
  713. return m_position;
  714. }
  715. void GLCanvas3D::Mouse::set_position(const Pointf& position)
  716. {
  717. m_position = position;
  718. }
  719. GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, wxGLContext* context)
  720. : m_canvas(canvas)
  721. , m_context(context)
  722. , m_volumes(nullptr)
  723. , m_config(nullptr)
  724. , m_dirty(true)
  725. , m_apply_zoom_to_volumes_filter(false)
  726. , m_hover_volume_id(-1)
  727. , m_warning_texture_enabled(false)
  728. , m_legend_texture_enabled(false)
  729. , m_picking_enabled(false)
  730. , m_shader_enabled(false)
  731. , m_multisample_allowed(false)
  732. {
  733. }
  734. GLCanvas3D::~GLCanvas3D()
  735. {
  736. _deregister_callbacks();
  737. }
  738. bool GLCanvas3D::init(bool useVBOs, bool use_legacy_opengl)
  739. {
  740. ::glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  741. ::glEnable(GL_DEPTH_TEST);
  742. ::glClearDepth(1.0f);
  743. ::glDepthFunc(GL_LEQUAL);
  744. ::glEnable(GL_CULL_FACE);
  745. ::glEnable(GL_BLEND);
  746. ::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  747. // Set antialiasing / multisampling
  748. ::glDisable(GL_LINE_SMOOTH);
  749. ::glDisable(GL_POLYGON_SMOOTH);
  750. // ambient lighting
  751. GLfloat ambient[4] = { 0.3f, 0.3f, 0.3f, 1.0f };
  752. ::glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  753. ::glEnable(GL_LIGHT0);
  754. ::glEnable(GL_LIGHT1);
  755. // light from camera
  756. GLfloat position[4] = { 1.0f, 0.0f, 1.0f, 0.0f };
  757. ::glLightfv(GL_LIGHT1, GL_POSITION, position);
  758. GLfloat specular[4] = { 0.3f, 0.3f, 0.3f, 1.0f };
  759. ::glLightfv(GL_LIGHT1, GL_SPECULAR, specular);
  760. GLfloat diffuse[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
  761. ::glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
  762. // Enables Smooth Color Shading; try GL_FLAT for (lack of) fun.
  763. ::glShadeModel(GL_SMOOTH);
  764. // A handy trick -- have surface material mirror the color.
  765. ::glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  766. ::glEnable(GL_COLOR_MATERIAL);
  767. if (is_multisample_allowed())
  768. ::glEnable(GL_MULTISAMPLE);
  769. if (useVBOs && !m_shader.init("gouraud.vs", "gouraud.fs"))
  770. return false;
  771. if (useVBOs && !m_layers_editing.init("variable_layer_height.vs", "variable_layer_height.fs"))
  772. return false;
  773. m_layers_editing.set_allowed(!use_legacy_opengl);
  774. return true;
  775. }
  776. bool GLCanvas3D::set_current()
  777. {
  778. if ((m_canvas != nullptr) && (m_context != nullptr))
  779. {
  780. m_canvas->SetCurrent(*m_context);
  781. return true;
  782. }
  783. return false;
  784. }
  785. bool GLCanvas3D::is_dirty() const
  786. {
  787. return m_dirty;
  788. }
  789. void GLCanvas3D::set_dirty(bool dirty)
  790. {
  791. m_dirty = dirty;
  792. }
  793. bool GLCanvas3D::is_shown_on_screen() const
  794. {
  795. return (m_canvas != nullptr) ? m_canvas->IsShownOnScreen() : false;
  796. }
  797. void GLCanvas3D::resize(unsigned int w, unsigned int h)
  798. {
  799. if (m_context == nullptr)
  800. return;
  801. set_current();
  802. ::glViewport(0, 0, w, h);
  803. ::glMatrixMode(GL_PROJECTION);
  804. ::glLoadIdentity();
  805. BoundingBoxf3 bbox = max_bounding_box();
  806. switch (get_camera_type())
  807. {
  808. case Camera::CT_Ortho:
  809. {
  810. float w2 = w;
  811. float h2 = h;
  812. float two_zoom = 2.0f * get_camera_zoom();
  813. if (two_zoom != 0.0f)
  814. {
  815. float inv_two_zoom = 1.0f / two_zoom;
  816. w2 *= inv_two_zoom;
  817. h2 *= inv_two_zoom;
  818. }
  819. // FIXME: calculate a tighter value for depth will improve z-fighting
  820. float depth = 5.0f * (float)bbox.max_size();
  821. ::glOrtho(-w2, w2, -h2, h2, -depth, depth);
  822. break;
  823. }
  824. case Camera::CT_Perspective:
  825. {
  826. float bbox_r = (float)bbox.radius();
  827. float fov = PI * 45.0f / 180.0f;
  828. float fov_tan = tan(0.5f * fov);
  829. float cam_distance = 0.5f * bbox_r / fov_tan;
  830. set_camera_distance(cam_distance);
  831. float nr = cam_distance - bbox_r * 1.1f;
  832. float fr = cam_distance + bbox_r * 1.1f;
  833. if (nr < 1.0f)
  834. nr = 1.0f;
  835. if (fr < nr + 1.0f)
  836. fr = nr + 1.0f;
  837. float h2 = fov_tan * nr;
  838. float w2 = h2 * w / h;
  839. ::glFrustum(-w2, w2, -h2, h2, nr, fr);
  840. break;
  841. }
  842. default:
  843. {
  844. throw std::runtime_error("Invalid camera type.");
  845. break;
  846. }
  847. }
  848. ::glMatrixMode(GL_MODELVIEW);
  849. set_dirty(false);
  850. }
  851. GLVolumeCollection* GLCanvas3D::get_volumes()
  852. {
  853. return m_volumes;
  854. }
  855. void GLCanvas3D::set_volumes(GLVolumeCollection* volumes)
  856. {
  857. m_volumes = volumes;
  858. }
  859. void GLCanvas3D::reset_volumes()
  860. {
  861. if (set_current() && (m_volumes != nullptr))
  862. {
  863. m_volumes->release_geometry();
  864. m_volumes->clear();
  865. set_dirty(true);
  866. }
  867. }
  868. void GLCanvas3D::deselect_volumes()
  869. {
  870. if (m_volumes != nullptr)
  871. {
  872. for (GLVolume* vol : m_volumes->volumes)
  873. {
  874. if (vol != nullptr)
  875. vol->selected = false;
  876. }
  877. }
  878. }
  879. void GLCanvas3D::select_volume(unsigned int id)
  880. {
  881. if ((m_volumes != nullptr) && (id < (unsigned int)m_volumes->volumes.size()))
  882. {
  883. GLVolume* vol = m_volumes->volumes[id];
  884. if (vol != nullptr)
  885. vol->selected = true;
  886. }
  887. }
  888. DynamicPrintConfig* GLCanvas3D::get_config()
  889. {
  890. return m_config;
  891. }
  892. void GLCanvas3D::set_config(DynamicPrintConfig* config)
  893. {
  894. m_config = config;
  895. }
  896. void GLCanvas3D::set_bed_shape(const Pointfs& shape)
  897. {
  898. m_bed.set_shape(shape);
  899. // Set the origin and size for painting of the coordinate system axes.
  900. set_axes_origin(Pointf3(0.0, 0.0, (coordf_t)GROUND_Z));
  901. set_axes_length(0.3f * (float)bed_bounding_box().max_size());
  902. }
  903. void GLCanvas3D::set_auto_bed_shape()
  904. {
  905. // draw a default square bed around object center
  906. const BoundingBoxf3& bbox = volumes_bounding_box();
  907. coordf_t max_size = bbox.max_size();
  908. const Pointf3& center = bbox.center();
  909. Pointfs bed_shape;
  910. bed_shape.reserve(4);
  911. bed_shape.emplace_back(center.x - max_size, center.y - max_size);
  912. bed_shape.emplace_back(center.x + max_size, center.y - max_size);
  913. bed_shape.emplace_back(center.x + max_size, center.y + max_size);
  914. bed_shape.emplace_back(center.x - max_size, center.y + max_size);
  915. set_bed_shape(bed_shape);
  916. // Set the origin for painting of the coordinate system axes.
  917. set_axes_origin(Pointf3(center.x, center.y, (coordf_t)GROUND_Z));
  918. }
  919. const Pointf3& GLCanvas3D::get_axes_origin() const
  920. {
  921. return m_axes.get_origin();
  922. }
  923. void GLCanvas3D::set_axes_origin(const Pointf3& origin)
  924. {
  925. m_axes.set_origin(origin);
  926. }
  927. float GLCanvas3D::get_axes_length() const
  928. {
  929. return m_axes.get_length();
  930. }
  931. void GLCanvas3D::set_axes_length(float length)
  932. {
  933. return m_axes.set_length(length);
  934. }
  935. void GLCanvas3D::set_cutting_plane(float z, const ExPolygons& polygons)
  936. {
  937. m_cutting_plane.set(z, polygons);
  938. }
  939. GLCanvas3D::Camera::EType GLCanvas3D::get_camera_type() const
  940. {
  941. return m_camera.get_type();
  942. }
  943. void GLCanvas3D::set_camera_type(GLCanvas3D::Camera::EType type)
  944. {
  945. m_camera.set_type(type);
  946. }
  947. std::string GLCanvas3D::get_camera_type_as_string() const
  948. {
  949. return m_camera.get_type_as_string();
  950. }
  951. float GLCanvas3D::get_camera_zoom() const
  952. {
  953. return m_camera.get_zoom();
  954. }
  955. void GLCanvas3D::set_camera_zoom(float zoom)
  956. {
  957. m_camera.set_zoom(zoom);
  958. }
  959. float GLCanvas3D::get_camera_phi() const
  960. {
  961. return m_camera.get_phi();
  962. }
  963. void GLCanvas3D::set_camera_phi(float phi)
  964. {
  965. m_camera.set_phi(phi);
  966. }
  967. float GLCanvas3D::get_camera_theta() const
  968. {
  969. return m_camera.get_theta();
  970. }
  971. void GLCanvas3D::set_camera_theta(float theta)
  972. {
  973. m_camera.set_theta(theta);
  974. }
  975. float GLCanvas3D::get_camera_distance() const
  976. {
  977. return m_camera.get_distance();
  978. }
  979. void GLCanvas3D::set_camera_distance(float distance)
  980. {
  981. m_camera.set_distance(distance);
  982. }
  983. const Pointf3& GLCanvas3D::get_camera_target() const
  984. {
  985. return m_camera.get_target();
  986. }
  987. void GLCanvas3D::set_camera_target(const Pointf3& target)
  988. {
  989. m_camera.set_target(target);
  990. }
  991. BoundingBoxf3 GLCanvas3D::bed_bounding_box() const
  992. {
  993. return m_bed.get_bounding_box();
  994. }
  995. BoundingBoxf3 GLCanvas3D::volumes_bounding_box() const
  996. {
  997. BoundingBoxf3 bb;
  998. if (m_volumes != nullptr)
  999. {
  1000. for (const GLVolume* volume : m_volumes->volumes)
  1001. {
  1002. if (!m_apply_zoom_to_volumes_filter || ((volume != nullptr) && volume->zoom_to_volumes))
  1003. bb.merge(volume->transformed_bounding_box());
  1004. }
  1005. }
  1006. return bb;
  1007. }
  1008. BoundingBoxf3 GLCanvas3D::max_bounding_box() const
  1009. {
  1010. BoundingBoxf3 bb = bed_bounding_box();
  1011. bb.merge(volumes_bounding_box());
  1012. return bb;
  1013. }
  1014. bool GLCanvas3D::is_layers_editing_enabled() const
  1015. {
  1016. return m_layers_editing.is_enabled();
  1017. }
  1018. bool GLCanvas3D::is_picking_enabled() const
  1019. {
  1020. return m_picking_enabled;
  1021. }
  1022. bool GLCanvas3D::is_multisample_allowed() const
  1023. {
  1024. return m_multisample_allowed;
  1025. }
  1026. void GLCanvas3D::enable_layers_editing(bool enable)
  1027. {
  1028. m_layers_editing.set_enabled(enable);
  1029. }
  1030. void GLCanvas3D::enable_warning_texture(bool enable)
  1031. {
  1032. m_warning_texture_enabled = enable;
  1033. }
  1034. void GLCanvas3D::enable_legend_texture(bool enable)
  1035. {
  1036. m_legend_texture_enabled = enable;
  1037. }
  1038. void GLCanvas3D::enable_picking(bool enable)
  1039. {
  1040. m_picking_enabled = enable;
  1041. }
  1042. void GLCanvas3D::enable_shader(bool enable)
  1043. {
  1044. m_shader_enabled = enable;
  1045. }
  1046. void GLCanvas3D::allow_multisample(bool allow)
  1047. {
  1048. m_multisample_allowed = allow;
  1049. }
  1050. bool GLCanvas3D::is_mouse_dragging() const
  1051. {
  1052. return m_mouse.is_dragging();
  1053. }
  1054. void GLCanvas3D::set_mouse_dragging(bool dragging)
  1055. {
  1056. m_mouse.set_dragging(dragging);
  1057. }
  1058. const Pointf& GLCanvas3D::get_mouse_position() const
  1059. {
  1060. return m_mouse.get_position();
  1061. }
  1062. void GLCanvas3D::set_mouse_position(const Pointf& position)
  1063. {
  1064. m_mouse.set_position(position);
  1065. }
  1066. int GLCanvas3D::get_hover_volume_id() const
  1067. {
  1068. return m_hover_volume_id;
  1069. }
  1070. void GLCanvas3D::set_hover_volume_id(int id)
  1071. {
  1072. m_hover_volume_id = id;
  1073. }
  1074. void GLCanvas3D::zoom_to_bed()
  1075. {
  1076. _zoom_to_bounding_box(bed_bounding_box());
  1077. }
  1078. void GLCanvas3D::zoom_to_volumes()
  1079. {
  1080. m_apply_zoom_to_volumes_filter = true;
  1081. _zoom_to_bounding_box(volumes_bounding_box());
  1082. m_apply_zoom_to_volumes_filter = false;
  1083. }
  1084. void GLCanvas3D::select_view(const std::string& direction)
  1085. {
  1086. const float* dir_vec = nullptr;
  1087. if (direction == "iso")
  1088. dir_vec = VIEW_DEFAULT;
  1089. else if (direction == "left")
  1090. dir_vec = VIEW_LEFT;
  1091. else if (direction == "right")
  1092. dir_vec = VIEW_RIGHT;
  1093. else if (direction == "top")
  1094. dir_vec = VIEW_TOP;
  1095. else if (direction == "bottom")
  1096. dir_vec = VIEW_BOTTOM;
  1097. else if (direction == "front")
  1098. dir_vec = VIEW_FRONT;
  1099. else if (direction == "rear")
  1100. dir_vec = VIEW_REAR;
  1101. if ((dir_vec != nullptr) && !empty(volumes_bounding_box()))
  1102. {
  1103. m_camera.set_phi(dir_vec[0]);
  1104. m_camera.set_theta(dir_vec[1]);
  1105. m_on_viewport_changed_callback.call();
  1106. if (m_canvas != nullptr)
  1107. m_canvas->Refresh();
  1108. }
  1109. }
  1110. bool GLCanvas3D::start_using_shader() const
  1111. {
  1112. return m_shader.start_using();
  1113. }
  1114. void GLCanvas3D::stop_using_shader() const
  1115. {
  1116. m_shader.stop_using();
  1117. }
  1118. void GLCanvas3D::picking_pass()
  1119. {
  1120. if (is_picking_enabled() && !is_mouse_dragging() && (m_volumes != nullptr))
  1121. {
  1122. // Render the object for picking.
  1123. // FIXME This cannot possibly work in a multi - sampled context as the color gets mangled by the anti - aliasing.
  1124. // Better to use software ray - casting on a bounding - box hierarchy.
  1125. if (is_multisample_allowed())
  1126. ::glDisable(GL_MULTISAMPLE);
  1127. ::glDisable(GL_LIGHTING);
  1128. ::glDisable(GL_BLEND);
  1129. ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1130. ::glPushAttrib(GL_ENABLE_BIT);
  1131. render_volumes(true);
  1132. ::glPopAttrib();
  1133. if (is_multisample_allowed())
  1134. ::glEnable(GL_MULTISAMPLE);
  1135. const Size& cnv_size = get_canvas_size();
  1136. const Pointf& pos = get_mouse_position();
  1137. GLubyte color[4];
  1138. ::glReadPixels(pos.x, cnv_size.get_height() - pos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void*)color);
  1139. int volume_id = color[0] + color[1] * 256 + color[2] * 256 * 256;
  1140. set_hover_volume_id(-1);
  1141. for (GLVolume* vol : m_volumes->volumes)
  1142. {
  1143. vol->hover = false;
  1144. }
  1145. if (volume_id < m_volumes->volumes.size())
  1146. {
  1147. set_hover_volume_id(volume_id);
  1148. m_volumes->volumes[volume_id]->hover = true;
  1149. int group_id = m_volumes->volumes[volume_id]->select_group_id;
  1150. if (group_id != -1)
  1151. {
  1152. for (GLVolume* vol : m_volumes->volumes)
  1153. {
  1154. if (vol->select_group_id == group_id)
  1155. vol->hover = true;
  1156. }
  1157. }
  1158. }
  1159. }
  1160. }
  1161. void GLCanvas3D::render_background() const
  1162. {
  1163. static const float COLOR[3] = { 10.0f / 255.0f, 98.0f / 255.0f, 144.0f / 255.0f };
  1164. ::glDisable(GL_LIGHTING);
  1165. ::glPushMatrix();
  1166. ::glLoadIdentity();
  1167. ::glMatrixMode(GL_PROJECTION);
  1168. ::glPushMatrix();
  1169. ::glLoadIdentity();
  1170. // Draws a bluish bottom to top gradient over the complete screen.
  1171. ::glDisable(GL_DEPTH_TEST);
  1172. ::glBegin(GL_QUADS);
  1173. ::glColor3f(0.0f, 0.0f, 0.0f);
  1174. ::glVertex3f(-1.0f, -1.0f, 1.0f);
  1175. ::glVertex3f(1.0f, -1.0f, 1.0f);
  1176. ::glColor3f(COLOR[0], COLOR[1], COLOR[2]);
  1177. ::glVertex3f(1.0f, 1.0f, 1.0f);
  1178. ::glVertex3f(-1.0f, 1.0f, 1.0f);
  1179. ::glEnd();
  1180. ::glEnable(GL_DEPTH_TEST);
  1181. ::glPopMatrix();
  1182. ::glMatrixMode(GL_MODELVIEW);
  1183. ::glPopMatrix();
  1184. }
  1185. unsigned int GLCanvas3D::get_layers_editing_z_texture_id() const
  1186. {
  1187. return m_layers_editing.get_z_texture_id();
  1188. }
  1189. GLShader* GLCanvas3D::get_layers_editing_shader()
  1190. {
  1191. return m_layers_editing.get_shader();
  1192. }
  1193. void GLCanvas3D::render_bed() const
  1194. {
  1195. m_bed.render();
  1196. }
  1197. void GLCanvas3D::render_axes() const
  1198. {
  1199. m_axes.render();
  1200. }
  1201. void GLCanvas3D::render_volumes(bool fake_colors) const
  1202. {
  1203. static const float INV_255 = 1.0f / 255.0f;
  1204. if (m_volumes == nullptr)
  1205. return;
  1206. if (fake_colors)
  1207. ::glDisable(GL_LIGHTING);
  1208. else
  1209. ::glEnable(GL_LIGHTING);
  1210. // do not cull backfaces to show broken geometry, if any
  1211. ::glDisable(GL_CULL_FACE);
  1212. ::glEnable(GL_BLEND);
  1213. ::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1214. ::glEnableClientState(GL_VERTEX_ARRAY);
  1215. ::glEnableClientState(GL_NORMAL_ARRAY);
  1216. unsigned int volume_id = 0;
  1217. for (GLVolume* vol : m_volumes->volumes)
  1218. {
  1219. if (fake_colors)
  1220. {
  1221. // Object picking mode. Render the object with a color encoding the object index.
  1222. unsigned int r = (volume_id & 0x000000FF) >> 0;
  1223. unsigned int g = (volume_id & 0x0000FF00) >> 8;
  1224. unsigned int b = (volume_id & 0x00FF0000) >> 16;
  1225. ::glColor4f((float)r * INV_255, (float)g * INV_255, (float)b * INV_255, 1.0f);
  1226. }
  1227. else
  1228. {
  1229. vol->set_render_color();
  1230. ::glColor4f(vol->render_color[0], vol->render_color[1], vol->render_color[2], vol->render_color[3]);
  1231. }
  1232. vol->render();
  1233. ++volume_id;
  1234. }
  1235. ::glDisableClientState(GL_NORMAL_ARRAY);
  1236. ::glDisableClientState(GL_VERTEX_ARRAY);
  1237. ::glDisable(GL_BLEND);
  1238. ::glEnable(GL_CULL_FACE);
  1239. }
  1240. void GLCanvas3D::render_objects(bool useVBOs)
  1241. {
  1242. if ((m_volumes == nullptr) || m_volumes->empty())
  1243. return;
  1244. ::glEnable(GL_LIGHTING);
  1245. if (!m_shader_enabled)
  1246. render_volumes(false);
  1247. else if (useVBOs)
  1248. {
  1249. if (is_picking_enabled())
  1250. {
  1251. m_on_mark_volumes_for_layer_height_callback.call();
  1252. if (m_config != nullptr)
  1253. {
  1254. const BoundingBoxf3& bed_bb = bed_bounding_box();
  1255. m_volumes->set_print_box((float)bed_bb.min.x, (float)bed_bb.min.y, 0.0f, (float)bed_bb.max.x, (float)bed_bb.max.y, (float)m_config->opt_float("max_print_height"));
  1256. m_volumes->check_outside_state(m_config);
  1257. }
  1258. // do not cull backfaces to show broken geometry, if any
  1259. ::glDisable(GL_CULL_FACE);
  1260. }
  1261. start_using_shader();
  1262. m_volumes->render_VBOs();
  1263. stop_using_shader();
  1264. if (is_picking_enabled())
  1265. ::glEnable(GL_CULL_FACE);
  1266. }
  1267. else
  1268. {
  1269. // do not cull backfaces to show broken geometry, if any
  1270. if (is_picking_enabled())
  1271. ::glDisable(GL_CULL_FACE);
  1272. m_volumes->render_legacy();
  1273. if (is_picking_enabled())
  1274. ::glEnable(GL_CULL_FACE);
  1275. }
  1276. }
  1277. void GLCanvas3D::render_cutting_plane() const
  1278. {
  1279. m_cutting_plane.render(volumes_bounding_box());
  1280. }
  1281. void GLCanvas3D::render_warning_texture() const
  1282. {
  1283. if (!m_warning_texture_enabled)
  1284. return;
  1285. // If the warning texture has not been loaded into the GPU, do it now.
  1286. unsigned int tex_id = _3DScene::finalize_warning_texture();
  1287. if (tex_id > 0)
  1288. {
  1289. unsigned int w = _3DScene::get_warning_texture_width();
  1290. unsigned int h = _3DScene::get_warning_texture_height();
  1291. if ((w > 0) && (h > 0))
  1292. {
  1293. ::glDisable(GL_DEPTH_TEST);
  1294. ::glPushMatrix();
  1295. ::glLoadIdentity();
  1296. const Size& cnv_size = get_canvas_size();
  1297. float zoom = get_camera_zoom();
  1298. float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;
  1299. float l = (-0.5f * (float)w) * inv_zoom;
  1300. float t = (-0.5f * (float)cnv_size.get_height() + (float)h) * inv_zoom;
  1301. float r = l + (float)w * inv_zoom;
  1302. float b = t - (float)h * inv_zoom;
  1303. render_texture(tex_id, l, r, b, t);
  1304. ::glPopMatrix();
  1305. ::glEnable(GL_DEPTH_TEST);
  1306. }
  1307. }
  1308. }
  1309. void GLCanvas3D::render_legend_texture() const
  1310. {
  1311. if (!m_legend_texture_enabled)
  1312. return;
  1313. // If the legend texture has not been loaded into the GPU, do it now.
  1314. unsigned int tex_id = _3DScene::finalize_legend_texture();
  1315. if (tex_id > 0)
  1316. {
  1317. unsigned int w = _3DScene::get_legend_texture_width();
  1318. unsigned int h = _3DScene::get_legend_texture_height();
  1319. if ((w > 0) && (h > 0))
  1320. {
  1321. ::glDisable(GL_DEPTH_TEST);
  1322. ::glPushMatrix();
  1323. ::glLoadIdentity();
  1324. const Size& cnv_size = get_canvas_size();
  1325. float zoom = get_camera_zoom();
  1326. float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;
  1327. float l = (-0.5f * (float)cnv_size.get_width()) * inv_zoom;
  1328. float t = (0.5f * (float)cnv_size.get_height()) * inv_zoom;
  1329. float r = l + (float)w * inv_zoom;
  1330. float b = t - (float)h * inv_zoom;
  1331. render_texture(tex_id, l, r, b, t);
  1332. ::glPopMatrix();
  1333. ::glEnable(GL_DEPTH_TEST);
  1334. }
  1335. }
  1336. }
  1337. void GLCanvas3D::render_layer_editing_textures(const PrintObject& print_object) const
  1338. {
  1339. m_layers_editing.render(*this, print_object);
  1340. }
  1341. void GLCanvas3D::render_texture(unsigned int tex_id, float left, float right, float bottom, float top) const
  1342. {
  1343. ::glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  1344. ::glDisable(GL_LIGHTING);
  1345. ::glEnable(GL_BLEND);
  1346. ::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1347. ::glEnable(GL_TEXTURE_2D);
  1348. ::glBindTexture(GL_TEXTURE_2D, (GLuint)tex_id);
  1349. ::glBegin(GL_QUADS);
  1350. ::glTexCoord2d(0.0f, 1.0f); glVertex3f(left, bottom, 0.0f);
  1351. ::glTexCoord2d(1.0f, 1.0f); glVertex3f(right, bottom, 0.0f);
  1352. ::glTexCoord2d(1.0f, 0.0f); glVertex3f(right, top, 0.0f);
  1353. ::glTexCoord2d(0.0f, 0.0f); glVertex3f(left, top, 0.0f);
  1354. ::glEnd();
  1355. ::glBindTexture(GL_TEXTURE_2D, 0);
  1356. ::glDisable(GL_TEXTURE_2D);
  1357. ::glDisable(GL_BLEND);
  1358. ::glEnable(GL_LIGHTING);
  1359. }
  1360. void GLCanvas3D::register_on_viewport_changed_callback(void* callback)
  1361. {
  1362. if (callback != nullptr)
  1363. m_on_viewport_changed_callback.register_callback(callback);
  1364. }
  1365. void GLCanvas3D::register_on_mark_volumes_for_layer_height_callback(void* callback)
  1366. {
  1367. if (callback != nullptr)
  1368. m_on_mark_volumes_for_layer_height_callback.register_callback(callback);
  1369. }
  1370. void GLCanvas3D::on_size(wxSizeEvent& evt)
  1371. {
  1372. set_dirty(true);
  1373. }
  1374. void GLCanvas3D::on_idle(wxIdleEvent& evt)
  1375. {
  1376. if (!is_dirty() || !is_shown_on_screen())
  1377. return;
  1378. if (m_canvas != nullptr)
  1379. {
  1380. const Size& cnv_size = get_canvas_size();
  1381. resize((unsigned int)cnv_size.get_width(), (unsigned int)cnv_size.get_height());
  1382. m_canvas->Refresh();
  1383. }
  1384. }
  1385. void GLCanvas3D::on_char(wxKeyEvent& evt)
  1386. {
  1387. if (evt.HasModifiers())
  1388. evt.Skip();
  1389. else
  1390. {
  1391. int keyCode = evt.GetKeyCode();
  1392. switch (keyCode - 48)
  1393. {
  1394. // numerical input
  1395. case 0: { select_view("iso"); break; }
  1396. case 1: { select_view("top"); break; }
  1397. case 2: { select_view("bottom"); break; }
  1398. case 3: { select_view("front"); break; }
  1399. case 4: { select_view("rear"); break; }
  1400. case 5: { select_view("left"); break; }
  1401. case 6: { select_view("right"); break; }
  1402. default:
  1403. {
  1404. // text input
  1405. switch (keyCode)
  1406. {
  1407. // key B/b
  1408. case 66:
  1409. case 98: { zoom_to_bed(); break; }
  1410. // key Z/z
  1411. case 90:
  1412. case 122: { zoom_to_volumes(); break; }
  1413. default: { evt.Skip(); break; }
  1414. }
  1415. }
  1416. }
  1417. }
  1418. }
  1419. Size GLCanvas3D::get_canvas_size() const
  1420. {
  1421. int w = 0;
  1422. int h = 0;
  1423. if (m_canvas != nullptr)
  1424. m_canvas->GetSize(&w, &h);
  1425. return Size(w, h);
  1426. }
  1427. void GLCanvas3D::_zoom_to_bounding_box(const BoundingBoxf3& bbox)
  1428. {
  1429. // Calculate the zoom factor needed to adjust viewport to bounding box.
  1430. float zoom = _get_zoom_to_bounding_box_factor(bbox);
  1431. if (zoom > 0.0f)
  1432. {
  1433. set_camera_zoom(zoom);
  1434. // center view around bounding box center
  1435. set_camera_target(bbox.center());
  1436. m_on_viewport_changed_callback.call();
  1437. if (is_shown_on_screen())
  1438. {
  1439. const Size& cnv_size = get_canvas_size();
  1440. resize((unsigned int)cnv_size.get_width(), (unsigned int)cnv_size.get_height());
  1441. if (m_canvas != nullptr)
  1442. m_canvas->Refresh();
  1443. }
  1444. }
  1445. }
  1446. float GLCanvas3D::_get_zoom_to_bounding_box_factor(const BoundingBoxf3& bbox) const
  1447. {
  1448. float max_bb_size = bbox.max_size();
  1449. if (max_bb_size == 0.0f)
  1450. return -1.0f;
  1451. // project the bbox vertices on a plane perpendicular to the camera forward axis
  1452. // then calculates the vertices coordinate on this plane along the camera xy axes
  1453. // we need the view matrix, we let opengl calculate it(same as done in render sub)
  1454. ::glMatrixMode(GL_MODELVIEW);
  1455. ::glLoadIdentity();
  1456. if (TURNTABLE_MODE)
  1457. {
  1458. // Turntable mode is enabled by default.
  1459. ::glRotatef(-get_camera_theta(), 1.0f, 0.0f, 0.0f); // pitch
  1460. ::glRotatef(get_camera_phi(), 0.0f, 0.0f, 1.0f); // yaw
  1461. }
  1462. else
  1463. {
  1464. // Shift the perspective camera.
  1465. Pointf3 camera_pos(0.0, 0.0, -(coordf_t)get_camera_distance());
  1466. ::glTranslatef((float)camera_pos.x, (float)camera_pos.y, (float)camera_pos.z);
  1467. // my @rotmat = quat_to_rotmatrix($self->quat); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< TEMPORARY COMMENTED OUT
  1468. // glMultMatrixd_p(@rotmat[0..15]); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< TEMPORARY COMMENTED OUT
  1469. }
  1470. const Pointf3& target = get_camera_target();
  1471. ::glTranslatef(-(float)target.x, -(float)target.y, -(float)target.z);
  1472. // get the view matrix back from opengl
  1473. GLfloat matrix[16];
  1474. ::glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  1475. // camera axes
  1476. Pointf3 right((coordf_t)matrix[0], (coordf_t)matrix[4], (coordf_t)matrix[8]);
  1477. Pointf3 up((coordf_t)matrix[1], (coordf_t)matrix[5], (coordf_t)matrix[9]);
  1478. Pointf3 forward((coordf_t)matrix[2], (coordf_t)matrix[6], (coordf_t)matrix[10]);
  1479. Pointf3 bb_min = bbox.min;
  1480. Pointf3 bb_max = bbox.max;
  1481. Pointf3 bb_center = bbox.center();
  1482. // bbox vertices in world space
  1483. std::vector<Pointf3> vertices;
  1484. vertices.reserve(8);
  1485. vertices.push_back(bb_min);
  1486. vertices.emplace_back(bb_max.x, bb_min.y, bb_min.z);
  1487. vertices.emplace_back(bb_max.x, bb_max.y, bb_min.z);
  1488. vertices.emplace_back(bb_min.x, bb_max.y, bb_min.z);
  1489. vertices.emplace_back(bb_min.x, bb_min.y, bb_max.z);
  1490. vertices.emplace_back(bb_max.x, bb_min.y, bb_max.z);
  1491. vertices.push_back(bb_max);
  1492. vertices.emplace_back(bb_min.x, bb_max.y, bb_max.z);
  1493. coordf_t max_x = 0.0;
  1494. coordf_t max_y = 0.0;
  1495. // margin factor to give some empty space around the bbox
  1496. coordf_t margin_factor = 1.25;
  1497. for (const Pointf3 v : vertices)
  1498. {
  1499. // project vertex on the plane perpendicular to camera forward axis
  1500. Pointf3 pos(v.x - bb_center.x, v.y - bb_center.y, v.z - bb_center.z);
  1501. Pointf3 proj_on_plane = pos - dot(pos, forward) * forward;
  1502. // calculates vertex coordinate along camera xy axes
  1503. coordf_t x_on_plane = dot(proj_on_plane, right);
  1504. coordf_t y_on_plane = dot(proj_on_plane, up);
  1505. max_x = std::max(max_x, margin_factor * std::abs(x_on_plane));
  1506. max_y = std::max(max_y, margin_factor * std::abs(y_on_plane));
  1507. }
  1508. if ((max_x == 0.0) || (max_y == 0.0))
  1509. return -1.0f;
  1510. max_x *= 2.0;
  1511. max_y *= 2.0;
  1512. const Size& cnv_size = get_canvas_size();
  1513. return (float)std::min((coordf_t)cnv_size.get_width() / max_x, (coordf_t)cnv_size.get_height() / max_y);
  1514. }
  1515. void GLCanvas3D::_deregister_callbacks()
  1516. {
  1517. m_on_viewport_changed_callback.deregister_callback();
  1518. m_on_mark_volumes_for_layer_height_callback.deregister_callback();
  1519. }
  1520. } // namespace GUI
  1521. } // namespace Slic3r