-
Notifications
You must be signed in to change notification settings - Fork 3
/
GlobeOsg.cpp
245 lines (189 loc) · 6.26 KB
/
GlobeOsg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// qmlosg
#include <GlobeOsg.h>
#include <osgQuickNode.h>
// qt quick stuff
#include<QQuickWindow>
#include<QOpenGLFunctions>
#include <QSGSimpleTextureNode>
// osg
#include <osg/ShapeDrawable>
#include <osg/PolygonMode>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Shape>
// stl
#include <math.h>
#include <iostream>
class RotateCallback : public osg::NodeCallback
{
public:
RotateCallback(osg::PositionAttitudeTransform* pat) :
_pat(pat),
_angle(0.f)
{}
/** Callback method called by the NodeVisitor when visiting a node.*/
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::Quat rotationX;
float delta = osg::PI / 6.0 / 60.f; // 30 degrees in a second
_angle += delta;
rotationX.makeRotate(_angle, osg::Z_AXIS);
_pat->setAttitude(rotationX);
//std::cout << "current rotation is: " << osg::RadiansToDegrees(_angle) << std::endl;
// note, callback is responsible for scenegraph traversal so
// they must call traverse(node,nv) to ensure that the
// scene graph subtree (and associated callbacks) are traversed.
traverse(node,nv);
}
protected:
osg::ref_ptr<osg::PositionAttitudeTransform> _pat;
float _angle;
};
class PostDrawDebug : public osg::Camera::DrawCallback
{
public:
/** Functor method called by rendering thread. Users will typically override this method to carry tasks such as screen capture.*/
virtual void operator () (osg::RenderInfo& renderInfo) const
{
std::cout << "YES, I've rendered also frame: " << renderInfo.getState()->getFrameStamp()->getFrameNumber() << std::endl;
}
};
GlobeOsg::GlobeOsg() :
_osgQuickNode(NULL),
_zoomProp(1.0)
{
setFlag(ItemHasContents);
// start timer to force a render loop
startTimer(16);
}
GlobeOsg::~GlobeOsg()
{
}
void GlobeOsg::setZoom(qreal zoom)
{
_orbit->setDistance(zoom * _defaultDistance);
_zoomProp = zoom;
}
qreal GlobeOsg::getZoom()
{
return _zoomProp;
}
void GlobeOsg::setAnimate(bool animate)
{
if(animate)
{
if(_pat->getUpdateCallback() == NULL)
_pat->setUpdateCallback(new RotateCallback(_pat) );
}
else
_pat->setUpdateCallback(NULL);
}
bool GlobeOsg::getAnimate()
{
return (_pat->getUpdateCallback() != NULL);
}
QSGNode* GlobeOsg::updatePaintNode(QSGNode* qNode, UpdatePaintNodeData* qNodeData)
{
//std::cout << "updatePaintNode() " << std::endl;
if(!_osgQuickNode)
{
_osgQuickNode = new OsgQuickNode;
_osgQuickNode->setQuickWindow(window());
setupScene();
}
// pass events here if any occured
if(!_mousePressVec.empty())
{
int x = _mousePressVec[0].x();
int y = _mousePressVec[0].y();
_osgQuickNode->getViewer()->getEventQueue()->mouseButtonPress(x, y, osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON);
_mousePressVec.clear();
}
// pass events here if any occured
if(!_mouseDragVec.empty())
{
int x = _mouseDragVec[0].x();
int y = _mouseDragVec[0].y();
_osgQuickNode->getViewer()->getEventQueue()->mouseMotion(x, y);
std::cout << "Mouse Motion at " << x << ", " << y << std::endl;
_mouseDragVec.clear();
}
return _osgQuickNode;
}
void GlobeOsg::home()
{
// reset to home
_osgQuickNode->getViewer()->home();
}
void GlobeOsg::setupScene()
{
// setup a sphere to render
osg::ShapeDrawable* pSD = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, 0, 0), 10));
pSD->setColor(osg::Vec4(1, 0, 1, 0.5));
osg::Geode* geode = new osg::Geode;
geode->addDrawable(pSD);
osg::StateSet* ss = geode->getOrCreateStateSet();
ss->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
//geode->getOrCreateStateSet()->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE));
//geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
osg::Shader* pvert = new osg::Shader(osg::Shader::VERTEX);
// read vertex shader from resources
{
QFile file(":/shaders/globe.vert");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
QString line = in.readAll();
pvert->setShaderSource(line.toStdString());
}
osg::Shader* pFrag = new osg::Shader(osg::Shader::FRAGMENT);
// read frag shader from resources
{
QFile file(":/shaders/globe.frag");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
QString line = in.readAll();
pFrag->setShaderSource(line.toStdString());
}
osg::Program* pProg = new osg::Program;
pProg->addShader(pvert);
pProg->addShader(pFrag);
geode->getOrCreateStateSet()->setAttributeAndModes(pProg);
_pat = new osg::PositionAttitudeTransform;
_pat->addChild(geode);
osg::Group* sceneRoot = dynamic_cast<osg::Group*>(_osgQuickNode->getViewer()->getSceneData());
if(sceneRoot == NULL)
{
std::cout << "Creating NEW scene home" << std::endl;
sceneRoot = new osg::Group;
_osgQuickNode->getViewer()->setSceneData(sceneRoot);
}
sceneRoot->addChild(_pat.get());
// setup a nice default FOV
osg::Camera* pCamera = _osgQuickNode->getViewer()->getCamera();
double fov, ar, zMin, zMax;
pCamera->getProjectionMatrixAsPerspective(fov, ar, zMin, zMax);
fov = 65.0f / ar;
pCamera->setProjectionMatrixAsPerspective(fov, ar, zMin, zMax);
//pCamera->setPostDrawCallback(new PostDrawDebug() );
// also add an orbit manipulator
_orbit = new osgGA::OrbitManipulator;
_osgQuickNode->getViewer()->setCameraManipulator(_orbit.get());
_osgQuickNode->getViewer()->home();
_defaultDistance = _orbit->getDistance();
}
void GlobeOsg::timerEvent(QTimerEvent *)
{
update();
}
void GlobeOsg::_mousePressEvent(int x, int y)
{
// fwd mouse event to osg
_mousePressVec.push_back(osg::Vec2(x, y));
//_osgViewer->getEventQueue()->mouseButtonPress(x, y, osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON);
}
void GlobeOsg::_mouseDragEvent(int x, int y)
{
// fwd mouse event to osg
//std::cout << "MouseDragged at " << x << ", " << y << std::endl;
_mouseDragVec.push_back(osg::Vec2(x, y));
//_osgViewer->getEventQueue()->mouseMotion(x, y);
}