OSGWidget.cpp 11 KB

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