Browse Source

Add axes preview (Closes #5)

Johannes Kreutz 2 years ago
parent
commit
96ff70f95f

+ 1 - 0
trackpoint-app/CMakeLists.txt

@@ -71,6 +71,7 @@ QT_ADD_EXECUTABLE(TrackpointApp
   src/StringBasics.cpp
   src/TrackPointRenderer.cpp
   src/PointShape.cpp
+  src/HudCallback.cpp
 )
 
 if(NOT BUILD_STATIC_RELEASE)

+ 15 - 0
trackpoint-app/include/HudCallback.hpp

@@ -0,0 +1,15 @@
+#pragma once
+
+// Include dependencies
+#include <osg/Node>
+#include <osg/NodeVisitor>
+#include <osg/Camera>
+
+class HudCallback: public osg::NodeCallback {
+public:
+  HudCallback(osg::Camera* camera);//: m_viewer(viewer);
+  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
+
+private:
+  osg::Camera* _camera;
+};

+ 2 - 0
trackpoint-app/include/OSGWidget.hpp

@@ -56,6 +56,8 @@ private:
   virtual void onHome();
   virtual void onResize(int width, int height);
 
+  osg::ref_ptr<osg::Group> createAxesPreview();
+
   osgGA::EventQueue* getEventQueue() const;
   PickHandler* _picker;
   TrackPointRenderer* _pointRenderer;

+ 29 - 0
trackpoint-app/src/HudCallback.cpp

@@ -0,0 +1,29 @@
+// Include own headers
+#include "HudCallback.hpp"
+
+// Include dependencies
+#include <osg/MatrixTransform>
+
+HudCallback::HudCallback(osg::Camera* camera) {
+  _camera = camera;
+}
+
+void HudCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) {
+  osg::MatrixTransform* pTM = dynamic_cast<osg::MatrixTransform*>(node);
+	if (pTM) {
+    osg::Camera* camera = _camera;
+    osg::Vec3 translate = pTM->getMatrix().getTrans();
+    osg::Vec3 scale = pTM->getMatrix().getScale();
+    osg::Matrix mv = camera->getViewMatrix();
+
+    osg::Matrix inv_mv = camera->getInverseViewMatrix();  // Convert the view matrix to a normal transformation matrix
+    osg::Quat inv_q = inv_mv.getRotate();
+    osg::Quat q = mv.getRotate();
+
+    // The current rotating coordinate system of the model, and the coordinate system in which the camera is located, rotates clockwise 90 degrees around the x-axis.
+    osg::Quat dq(osg::DegreesToRadians(90.0f), osg::Vec3(1.0f, 0.0f, 0.0f));
+    static osg::Matrix mm = osg::Matrix::rotate(dq);
+    mv.setTrans(translate);
+    pTM->setMatrix(osg::Matrix::scale(scale) *  mv /** mm*/);
+  }
+}

+ 72 - 0
trackpoint-app/src/OSGWidget.cpp

@@ -4,6 +4,7 @@
 // Include modules
 #include "PickHandler.hpp"
 #include "TrackPointRenderer.hpp"
+#include "HudCallback.hpp"
 
 // Include dependencies
 #include <osgViewer/Viewer>
@@ -133,6 +134,25 @@ OSGWidget::OSGWidget(QWidget* parent): QOpenGLWidget(parent),
 
   _mesh = new osg::Geode;
 
+  // Add axes preview
+  osg::ref_ptr<osg::Camera> hudCamera = new osg::Camera;
+  int width = 1024;
+  int height = 1024;
+	hudCamera->setProjectionMatrixAsOrtho(0, width, 0, height, 1, 100);
+	hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
+	hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
+	hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
+
+  osg::ref_ptr<osg::Group> axesPreview = createAxesPreview();
+	osg::MatrixTransform* pTM = new osg::MatrixTransform;
+	pTM->addChild(axesPreview.get());
+	axesPreview->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
+	pTM->setMatrix(osg::Matrix::scale(osg::Vec3(width/12, width/12, width/12)) * osg::Matrix::translate(osg::Vec3(width/20, width/20, 1)));
+	pTM->setUpdateCallback(new HudCallback(camera));
+	hudCamera->addChild(pTM);
+	hudCamera->setViewMatrixAsLookAt(osg::Vec3(0, 0, 1), osg::Vec3(0, 0, 0), osg::Vec3(0, 1, 0)); // opengl default camera position
+  _root->addChild(hudCamera.release());
+
   // Attach a manipulator (it's usually done for us when we use viewer.run())
   osg::ref_ptr<osgGA::TrackballManipulator> tm = new osgGA::TrackballManipulator;
   tm->setAllowThrow(false);
@@ -360,3 +380,55 @@ osgGA::EventQueue* OSGWidget::getEventQueue() const {
     throw std::runtime_error("Unable to obtain valid event queue");
   }
 }
+
+osg::ref_ptr<osg::Group> OSGWidget::createAxesPreview() {
+  osg::ref_ptr<osg::Group> axesPreview = new osg::Group;
+
+  osg::Vec4 red = osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f);
+  osg::Vec4 green = osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f);
+  osg::Vec4 blue = osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f);
+
+  osg::ref_ptr<osg::MatrixTransform> xRotation = new osg::MatrixTransform;
+  xRotation->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), osg::Vec3f(1.0f, 0.0f, 0.0f)));
+  osg::ref_ptr<osg::Geode> xAxis = new osg::Geode;
+  osg::ref_ptr<osg::ShapeDrawable> xAxisShape = new osg::ShapeDrawable();
+  xAxisShape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.5f), 0.01f, 1.0f));
+  xAxisShape->setColor(red);
+  xAxis->addDrawable(xAxisShape.get());
+  osg::ref_ptr<osg::ShapeDrawable> xConeShape = new osg::ShapeDrawable();
+  xConeShape->setShape(new osg::Cone(osg::Vec3(0.0f, 0.0f, 1.0f), 0.05f, 0.2f));
+  xConeShape->setColor(red);
+  xAxis->addDrawable(xConeShape.get());
+  xRotation->addChild(xAxis.get());
+
+  osg::ref_ptr<osg::MatrixTransform> yRotation = new osg::MatrixTransform;
+  yRotation->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), osg::Vec3f(0.0f, 1.0f, 0.0f)));
+  osg::ref_ptr<osg::Geode> yAxis = new osg::Geode;
+  osg::ref_ptr<osg::ShapeDrawable> yAxisShape = new osg::ShapeDrawable();
+  yAxisShape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.5f), 0.01f, 1.0f));
+  yAxisShape->setColor(green);
+  yAxis->addDrawable(yAxisShape.get());
+  osg::ref_ptr<osg::ShapeDrawable> yConeShape = new osg::ShapeDrawable();
+  yConeShape->setShape(new osg::Cone(osg::Vec3(0.0f, 0.0f, 1.0f), 0.05f, 0.2f));
+  yConeShape->setColor(green);
+  yAxis->addDrawable(yConeShape.get());
+  yRotation->addChild(yAxis.get());
+
+  osg::ref_ptr<osg::Geode> zAxis = new osg::Geode;
+  osg::ref_ptr<osg::ShapeDrawable> zAxisShape = new osg::ShapeDrawable();
+  zAxisShape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.5f), 0.01f, 1.0f));
+  zAxisShape->setColor(blue);
+  osg::ref_ptr<osg::ShapeDrawable> zConeShape = new osg::ShapeDrawable();
+  zConeShape->setShape(new osg::Cone(osg::Vec3(0.0f, 0.0f, 1.0f), 0.05f, 0.2f));
+  zConeShape->setColor(blue);
+  zAxis->addDrawable(zConeShape.get());
+  zAxis->addDrawable(zAxisShape.get());
+
+  axesPreview->addChild(xRotation.get());
+  axesPreview->addChild(yRotation.get());
+  axesPreview->addChild(zAxis.get());
+
+  OSGWidget::fixMaterialState(axesPreview);
+
+  return axesPreview;
+}