Merged in f5soh/librepilot/LP-575_fedora_package (pull request #491)
[librepilot.git] / ground / gcs / src / libs / osgearth / osgQtQuick / OSGTransformNode.cpp
blob0b28bb3d2f15dae784ed9ac49d0190d38e682aca
1 /**
2 ******************************************************************************
4 * @file OSGTransformNode.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
6 * @addtogroup
7 * @{
8 * @addtogroup
9 * @{
10 * @brief
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "OSGTransformNode.hpp"
30 #include <osg/Quat>
31 #include <osg/PositionAttitudeTransform>
32 #include <osg/Math>
34 #include <QDebug>
36 namespace osgQtQuick {
37 // NOTE : these flags should not overlap with OSGGroup flags!!!
38 // TODO : find a better way...
39 enum DirtyFlag { Scale = 1 << 10, Position = 1 << 11, Attitude = 1 << 12 };
41 struct OSGTransformNode::Hidden : public QObject {
42 Q_OBJECT
44 private:
45 OSGTransformNode * const self;
47 osg::ref_ptr<osg::PositionAttitudeTransform> transform;
49 public:
50 QVector3D scale;
51 QVector3D attitude;
52 QVector3D position;
54 Hidden(OSGTransformNode *self) : QObject(self), self(self)
57 osg::Node *createNode()
59 transform = new osg::PositionAttitudeTransform();
60 return transform;
63 void updateScale()
65 // qDebug() << "OSGTransformNode::updateScale" << scale;
66 if ((scale.x() != 0.0) || (scale.y() != 0.0) || (scale.z() != 0.0)) {
67 transform->setScale(osg::Vec3d(scale.x(), scale.y(), scale.z()));
68 // transform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
69 transform->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);
73 void updateAttitude()
75 double roll = osg::DegreesToRadians(attitude.x());
76 double pitch = osg::DegreesToRadians(attitude.y());
77 double yaw = osg::DegreesToRadians(attitude.z());
78 osg::Quat q = osg::Quat(
79 roll, osg::Vec3d(0, 1, 0),
80 pitch, osg::Vec3d(1, 0, 0),
81 yaw, osg::Vec3d(0, 0, -1));
83 transform->setAttitude(q);
86 void updatePosition()
88 transform->setPosition(osg::Vec3d(position.x(), position.y(), position.z()));
92 /* class OSGTransformNode */
94 OSGTransformNode::OSGTransformNode(QObject *parent) : Inherited(parent), h(new Hidden(this))
97 OSGTransformNode::~OSGTransformNode()
99 delete h;
102 QVector3D OSGTransformNode::scale() const
104 return h->scale;
107 void OSGTransformNode::setScale(QVector3D arg)
109 if (h->scale != arg) {
110 h->scale = arg;
111 setDirty(Scale);
112 emit scaleChanged(scale());
116 QVector3D OSGTransformNode::attitude() const
118 return h->attitude;
121 void OSGTransformNode::setAttitude(QVector3D arg)
123 if (h->attitude != arg) {
124 h->attitude = arg;
125 setDirty(Attitude);
126 emit attitudeChanged(attitude());
130 QVector3D OSGTransformNode::position() const
132 return h->position;
135 void OSGTransformNode::setPosition(QVector3D arg)
137 if (h->position != arg) {
138 h->position = arg;
139 setDirty(Position);
140 emit positionChanged(position());
144 osg::Node *OSGTransformNode::createNode()
146 return h->createNode();
149 void OSGTransformNode::updateNode()
151 Inherited::updateNode();
153 if (isDirty(Scale)) {
154 h->updateScale();
156 if (isDirty(Attitude)) {
157 h->updateAttitude();
159 if (isDirty(Position)) {
160 h->updatePosition();
163 } // namespace osgQtQuick
165 #include "OSGTransformNode.moc"