OSGWidget.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Include own headers
  2. #include "OSGWidget.hpp"
  3. #include "PickHandler.hpp"
  4. // Include dependencies
  5. #include <osgViewer/Viewer>
  6. #include <osg/ShapeDrawable>
  7. #include <osg/Geode>
  8. #include <osgDB/ReadFile>
  9. #include <osg/Group>
  10. #include <osg/Switch>
  11. #include <osg/MatrixTransform>
  12. #include <osg/Matrix>
  13. #include <osgGA/TrackballManipulator>
  14. #include <osgUtil/LineSegmentIntersector>
  15. #include <osgUtil/IntersectionVisitor>
  16. #include <osgUtil/SmoothingVisitor>
  17. #include <osg/PolygonMode>
  18. #include <osgDB/WriteFile>
  19. #include <osg/Material>
  20. #include <osg/StateSet>
  21. #include <cassert>
  22. #include <stdexcept>
  23. #include <vector>
  24. #include <QDebug>
  25. #include <QKeyEvent>
  26. #include <QPainter>
  27. #include <QWheelEvent>
  28. namespace osgWidget {
  29. void Viewer::setupThreading() {
  30. if (_threadingModel == SingleThreaded) {
  31. if (_threadsRunning) {
  32. stopThreading();
  33. }
  34. } else {
  35. if (!_threadsRunning) {
  36. startThreading();
  37. }
  38. }
  39. }
  40. }
  41. OSGWidget::OSGWidget(QWidget* parent): QOpenGLWidget(parent),
  42. graphicsWindow_(new osgViewer::GraphicsWindowEmbedded(this->x(), this->y(), this->width(), this->height())),
  43. _viewer(new osgWidget::Viewer),
  44. selectionActive_(false),
  45. selectionFinished_(true)
  46. {
  47. this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  48. // Root node of the scene
  49. _root = new osg::Group;
  50. // Create the camera
  51. float aspectRatio = static_cast<float>(this->width()) / static_cast<float>(this->height());
  52. auto pixelRatio = this->devicePixelRatio();
  53. osg::Camera* camera = new osg::Camera;
  54. camera->setViewport(0, 0, this->width() * pixelRatio, this->height() * pixelRatio);
  55. camera->setClearColor(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
  56. camera->setProjectionMatrixAsPerspective(30.0f, aspectRatio, 1.0f, 1000.0f);
  57. camera->setGraphicsContext(graphicsWindow_);
  58. // Create the viewer
  59. _view = new osgViewer::View;
  60. _view->setCamera(camera);
  61. _view->setSceneData(_root);
  62. // Create coordinate axes
  63. _coordinateAxes = new osg::Geode;
  64. osg::ref_ptr<osg::ShapeDrawable> zAxis = new osg::ShapeDrawable();
  65. zAxis->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 0.1f, 100.0f));
  66. zAxis->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
  67. _coordinateAxes->addDrawable(zAxis.get());
  68. {
  69. osg::StateSet* stateSet = _coordinateAxes->getOrCreateStateSet();
  70. osg::Material* material = new osg::Material;
  71. material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
  72. stateSet->setAttributeAndModes(material, osg::StateAttribute::ON);
  73. stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
  74. }
  75. _root->addChild(_coordinateAxes);
  76. _axesNode = new osg::Geode;
  77. // Attach a manipulator (it's usually done for us when we use viewer.run())
  78. osg::ref_ptr<osgGA::TrackballManipulator> tm = new osgGA::TrackballManipulator;
  79. tm->setAllowThrow(false);
  80. _view->setCameraManipulator(tm);
  81. _viewer->addView(_view);
  82. _viewer->setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
  83. _viewer->realize();
  84. _storeHandler = new StoreHandler(_root);
  85. _openScadRenderer = new OpenScadRenderer();
  86. _threeMFWriter = new ThreeMFWriter();
  87. osg::ref_ptr<PickHandler> picker = new PickHandler(_storeHandler, _openScadRenderer, _threeMFWriter, _axesNode);
  88. _root->addChild(picker->getOrCreateSelectionCylinder());
  89. _view->addEventHandler(picker.get());
  90. // This ensures that the widget will receive keyboard events. This focus
  91. // policy is not set by default. The default, Qt::NoFocus, will result in
  92. // keyboard events that are ignored.
  93. this->setFocusPolicy(Qt::StrongFocus);
  94. this->setMinimumSize(100, 100);
  95. // Ensures that the widget receives mouse move events even though no
  96. // mouse button has been pressed. We require this in order to let the
  97. // graphics window switch viewports properly.
  98. this->setMouseTracking(true);
  99. }
  100. OSGWidget::~OSGWidget() {
  101. }
  102. void OSGWidget::renderBaseMesh(const std::vector<Lib3MF::sPosition> &verticesBuffer, const std::vector<Lib3MF::sTriangle> &triangleBuffer) {
  103. _root->removeChild(_axesNode);
  104. osg::ref_ptr<osg::Geometry> meshGeometry = new osg::Geometry;
  105. osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
  106. for (const Lib3MF::sPosition vertex: verticesBuffer) {
  107. vertices->push_back(osg::Vec3(vertex.m_Coordinates[0], vertex.m_Coordinates[1], vertex.m_Coordinates[2]));
  108. }
  109. meshGeometry->setVertexArray(vertices.get());
  110. osg::ref_ptr<osg::DrawElementsUInt> primitiveSet = new osg::DrawElementsUInt(GL_TRIANGLES);
  111. for (const Lib3MF::sTriangle triangle: triangleBuffer) {
  112. for (unsigned char i = 0; i < 3; i++) {
  113. primitiveSet->push_back(triangle.m_Indices[i]);
  114. }
  115. }
  116. meshGeometry->addPrimitiveSet(primitiveSet);
  117. osgUtil::SmoothingVisitor::smooth(*meshGeometry);
  118. _axesNode->addDrawable(meshGeometry.get());
  119. // Set material for basic lighting and enable depth tests. Else, the sphere
  120. // will suffer from rendering errors.
  121. {
  122. osg::StateSet* stateSet = _axesNode->getOrCreateStateSet();
  123. osg::Material* material = new osg::Material;
  124. material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
  125. stateSet->setAttributeAndModes(material, osg::StateAttribute::ON);
  126. stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
  127. }
  128. _root->addChild(_axesNode);
  129. }
  130. void OSGWidget::paintEvent(QPaintEvent*) {
  131. this->makeCurrent();
  132. QPainter painter(this);
  133. painter.setRenderHint(QPainter::Antialiasing);
  134. this->paintGL();
  135. painter.end();
  136. this->doneCurrent();
  137. }
  138. void OSGWidget::paintGL() {
  139. _viewer->frame();
  140. }
  141. void OSGWidget::resizeGL(int width, int height) {
  142. this->getEventQueue()->windowResize(this->x(), this->y(), width, height);
  143. graphicsWindow_->resized(this->x(), this->y(), width, height);
  144. this->onResize(width, height);
  145. }
  146. void OSGWidget::keyPressEvent(QKeyEvent* event) {
  147. QString keyString = event->text();
  148. const char* keyData = keyString.toLocal8Bit().data();
  149. if (event->key() == Qt::Key_H) {
  150. this->onHome();
  151. return;
  152. }
  153. this->getEventQueue()->keyPress(osgGA::GUIEventAdapter::KeySymbol(*keyData));
  154. }
  155. void OSGWidget::keyReleaseEvent(QKeyEvent* event) {
  156. QString keyString = event->text();
  157. const char* keyData = keyString.toLocal8Bit().data();
  158. this->getEventQueue()->keyRelease(osgGA::GUIEventAdapter::KeySymbol(*keyData));
  159. }
  160. void OSGWidget::mouseMoveEvent(QMouseEvent* event) {
  161. auto pixelRatio = this->devicePixelRatio();
  162. this->getEventQueue()->mouseMotion(static_cast<float>(event->position().x() * pixelRatio), static_cast<float>(event->position().y() * pixelRatio));
  163. }
  164. void OSGWidget::mousePressEvent(QMouseEvent* event) {
  165. unsigned int button = 0;
  166. switch(event->button()) {
  167. case Qt::LeftButton:
  168. button = 1;
  169. break;
  170. case Qt::MiddleButton:
  171. button = 2;
  172. break;
  173. case Qt::RightButton:
  174. button = 3;
  175. break;
  176. default:
  177. break;
  178. }
  179. auto pixelRatio = this->devicePixelRatio();
  180. this->getEventQueue()->mouseButtonPress(static_cast<float>(event->position().x() * pixelRatio), static_cast<float>(event->position().y() * pixelRatio), button);
  181. }
  182. void OSGWidget::mouseReleaseEvent(QMouseEvent* event) {
  183. unsigned int button = 0;
  184. switch(event->button()) {
  185. case Qt::LeftButton:
  186. button = 1;
  187. break;
  188. case Qt::MiddleButton:
  189. button = 2;
  190. break;
  191. case Qt::RightButton:
  192. button = 3;
  193. break;
  194. default:
  195. break;
  196. }
  197. auto pixelRatio = this->devicePixelRatio();
  198. this->getEventQueue()->mouseButtonRelease(static_cast<float>(pixelRatio * event->position().x()), static_cast<float>(pixelRatio * event->position().y()), button);
  199. }
  200. void OSGWidget::wheelEvent(QWheelEvent* event) {
  201. event->accept();
  202. int delta = event->angleDelta().y();
  203. osgGA::GUIEventAdapter::ScrollingMotion motion = delta > 0 ? osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN;
  204. this->getEventQueue()->mouseScroll(motion);
  205. }
  206. bool OSGWidget::event(QEvent* event) {
  207. bool handled = QOpenGLWidget::event(event);
  208. // This ensures that the OSG widget is always going to be repainted after the
  209. // user performed some interaction. Doing this in the event handler ensures
  210. // that we don't forget about some event and prevents duplicate code.
  211. switch(event->type()) {
  212. case QEvent::KeyPress:
  213. case QEvent::KeyRelease:
  214. case QEvent::MouseButtonDblClick:
  215. case QEvent::MouseButtonPress:
  216. case QEvent::MouseButtonRelease:
  217. case QEvent::MouseMove:
  218. case QEvent::Wheel:
  219. this->update();
  220. break;
  221. case QEvent::Resize:
  222. this->onResize(this->width(), this->height());
  223. break;
  224. default:
  225. break;
  226. }
  227. return handled;
  228. }
  229. void OSGWidget::onHome() {
  230. osgViewer::ViewerBase::Views views;
  231. _viewer->getViews( views );
  232. for(std::size_t i = 0; i < views.size(); i++) {
  233. osgViewer::View* view = views.at(i);
  234. view->home();
  235. }
  236. }
  237. void OSGWidget::onResize(int width, int height) {
  238. std::vector<osg::Camera*> cameras;
  239. _viewer->getCameras(cameras);
  240. assert(cameras.size() == 1);
  241. auto pixelRatio = this->devicePixelRatio();
  242. cameras[0]->setViewport(0, 0, width * pixelRatio, height * pixelRatio);
  243. }
  244. osgGA::EventQueue* OSGWidget::getEventQueue() const {
  245. osgGA::EventQueue* eventQueue = graphicsWindow_->getEventQueue();
  246. if (eventQueue) {
  247. return eventQueue;
  248. } else {
  249. throw std::runtime_error("Unable to obtain valid event queue");
  250. }
  251. }