-
Notifications
You must be signed in to change notification settings - Fork 10
/
Osg3dViewWithCamera.cpp
299 lines (238 loc) · 8.8 KB
/
Osg3dViewWithCamera.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "Osg3dViewWithCamera.h"
#include <math.h>
#include <QApplication>
#include <QAction>
#include <QtGui/QKeyEvent>
#include <osg/LightModel>
#include <osgViewer/Renderer>
#include <osgGA/TrackballManipulator>
#include <osgUtil/LineSegmentIntersector>
#include <QTextStream>
#include "NodeMask.h"
Osg3dViewWithCamera::Osg3dViewWithCamera(QWidget *parent)
: QOpenGLWidget(parent)
, m_scene(new osg::Group)
, m_root(new osg::Switch)
, m_currentLineWidth(1.0)
, m_cameraModel(new CameraModel)
{
setFocusPolicy(Qt::StrongFocus);
// Construct the embedded graphics window
m_osgGraphicsWindow = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
getCamera()->setGraphicsContext(m_osgGraphicsWindow);
// Set up the camera
getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
// By default draw everthing that has even 1 bit set in the nodemask
getCamera()->setCullMask( ~(unsigned)0 );
getCamera()->setDataVariance(osg::Object::DYNAMIC);
// As of July 2010 there wasn't really a good way to multi-thread OSG
// under Qt so just set the threading model to be SingleThreaded
setThreadingModel(osgViewer::Viewer::SingleThreaded);
// draw both sides of polygons
setLightingTwoSided();
// Set the minimum size for this viewer window
setMinimumSize(64, 64);
osg::ref_ptr<osg::StateSet> ss = m_scene->getOrCreateStateSet();
m_point = new osg::Point(m_currentLineWidth);
ss->setAttribute(m_point);
m_lineWidth = new osg::LineWidth(m_currentLineWidth);
ss->setAttribute(m_lineWidth);
m_root->setNewChildDefaultValue(true);
m_root->addChild(m_scene);
this->setSceneData(m_root);
m_cameraModel->setBoundingNode(m_scene);
m_cameraModel->computeInitialView();
connect(m_cameraModel, SIGNAL(changed()), this, SLOT(update()));
}
Osg3dViewWithCamera::~Osg3dViewWithCamera()
{
}
void Osg3dViewWithCamera::paintGL()
{
// Update the camera
osg::Camera *cam = this->getCamera();
//const osg::Viewport* vp = cam->getViewport();
if (cam->getCullMask() != m_cameraModel->cullMask())
cam->setCullMask( m_cameraModel->cullMask() );
m_cameraModel->setAspect((double)width() / (double)height());
cam->setViewMatrix(m_cameraModel->getModelViewMatrix() );
cam->setProjectionMatrix(m_cameraModel->computeProjection());
// Invoke the OSG traversal pipeline
frame();
emit updated();
}
void Osg3dViewWithCamera::setCameraModel(osg::ref_ptr<CameraModel> cameraControl)
{
disconnect(m_cameraModel.get(), SIGNAL(changed()), this, SLOT(update()));
m_cameraModel = cameraControl;
m_cameraModel->setBoundingNode(m_scene);
connect(m_cameraModel.get(), SIGNAL(changed()), this, SLOT(update()));
update();
}
void Osg3dViewWithCamera::resizeGL(int width, int height)
{
m_osgGraphicsWindow->resized(0,0,width,height);
}
// Camera callback that force binds an FBO
// Used to make sure the main camera writes to the default Qt FBO
struct CameraPreDrawCallback : public osg::Camera::DrawCallback
{
CameraPreDrawCallback(int fboInt) :
_fboInt(fboInt)
{
}
virtual void operator () (const osg::Camera& /*camera*/) const
{
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glBindFramebuffer(GL_FRAMEBUFFER, _fboInt);
}
int _fboInt;
};
void Osg3dViewWithCamera::initializeGL()
{
initializeOpenGLFunctions();
QString vendor = (const char *)glGetString(GL_VENDOR);
QString renderer = (const char *)glGetString(GL_RENDERER);
QString version = (const char *)glGetString(GL_VERSION);
QString glsl_version = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
QString htmlString;
QTextStream htmlStream(&htmlString);
htmlStream << "<table>";
htmlStream << "<tr><td>GL_VENDOR</td><td>";
htmlStream << vendor << "</td></tr>" ;
htmlStream << "<tr><td>GL_RENDERER</td><td>";
htmlStream << renderer << "</td></tr>" ;
htmlStream << "<tr><td>GL_VERSION </td><td>";
htmlStream << version << "</td></tr>" ;
htmlStream << "<tr><td>GLSL_VERSION </td><td>";
htmlStream << glsl_version << "</td></tr>" ;
htmlStream << "</table>";
htmlStream.flush();
qApp->setProperty("OpenGLinfoHTML", htmlString );
QString plainString;
QTextStream stream(&plainString);
stream << "GL_VENDOR : ";
stream << vendor << endl;
stream << "GL_RENDERER : ";
stream << renderer << endl;
stream << "GL_VERSION : ";
stream << version << endl;
stream << "GLSL_VERSION : ";
stream << glsl_version << endl;
stream.flush();
qApp->setProperty("OpenGLinfo", plainString);
// Add a callback to the main camera to make it use the default Qt framebuffer
int fboInt = this->defaultFramebufferObject();
this->getCamera()->setPreDrawCallback(new CameraPreDrawCallback(fboInt));
}
osg::Vec2d Osg3dViewWithCamera::getNormalizedDeviceCoords(const int ix, const int iy)
{
osg::Vec2d ndc;
int center = width()/2;
ndc[0] = ((double)ix - (double)center) / (double)center;
if (ndc[0] > 1.0) ndc[0] = 1.0;
center = height()/2;
int invertedY = height() - iy;
ndc[1] = ((double)invertedY - (double)center) / (double)center;
if (ndc[1] > 1.0) ndc[1] = 1.0;
return ndc;
}
void Osg3dViewWithCamera::addNode(osg::Node *root)
{
m_scene->addChild(root);
}
void Osg3dViewWithCamera::removeNode(osg::Node *root)
{
m_scene->removeChild(root);
}
void Osg3dViewWithCamera::clearNodes()
{
m_scene->removeChildren(0, m_scene->getNumChildren());
}
void Osg3dViewWithCamera::setLightingTwoSided(bool tf)
{
osg::ref_ptr<osg::LightModel> lm = new osg::LightModel;
lm->setTwoSided(tf);
lm->setAmbientIntensity(osg::Vec4(0.1f,0.1f,0.1f,1.0f));
osg::StateSet *ss;
for (int i=0 ; i < 2 ; i++ ) {
ss = ((osgViewer::Renderer *)getCamera()->getRenderer())->
getSceneView(i)->getGlobalStateSet();
ss->setAttributeAndModes(lm, osg::StateAttribute::ON);
}
}
void Osg3dViewWithCamera::setDrawMode(osg::PolygonMode::Mode drawMode)
{
osg::ref_ptr<osg::StateSet> ss =
m_scene->getOrCreateStateSet();
osg::ref_ptr<osg::PolygonMode> pm =
dynamic_cast<osg::PolygonMode *>
(ss->getAttribute(osg::StateAttribute::POLYGONMODE));
if(!pm) {
pm = new osg::PolygonMode;
ss->setAttribute(pm.get());
}
pm->setMode(osg::PolygonMode::FRONT_AND_BACK, drawMode);
switch (drawMode) {
case osg::PolygonMode::LINE:
case osg::PolygonMode::POINT:
ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
break;
default:
ss->setMode(GL_LIGHTING, osg::StateAttribute::ON);
break;
}
emit drawModeChanged(drawMode);
update();
}
void Osg3dViewWithCamera::setDrawModeFromQAction()
{
QAction *a = dynamic_cast<QAction *>(sender());
if (!a)
return;
bool ok = false;
osg::PolygonMode::Mode drawMode =
static_cast<osg::PolygonMode::Mode>(a->data().toUInt(&ok));
if (ok) setDrawMode(drawMode);
}
void Osg3dViewWithCamera::setLineWidth(int size)
{
m_point->setSize((float)size);
m_lineWidth->setWidth((float)size);
emit lineWidthChanged(size);
update();
}
void printIntersectorDebugging(const int x, const int y, unsigned mask,
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector
)
{
qDebug("intersectUnderCursor(%d %d %s):", x, y, qPrintable(NodeMask::maskToString(mask)));
auto intersections = intersector->getIntersections();
for (auto isect=intersections.begin() ; isect != intersections.end() ; isect++) {
osg::Vec3d pt = (*isect).getWorldIntersectPoint();
QString path;
foreach (osg::Node *n, (*isect).nodePath) {
if (n->getName().size() > 0)
path.append(QString("/%1(%2)")
.arg(n->getName().c_str())
.arg( NodeMask::maskToString(n->getNodeMask()) ));
else
path.append(QString("/%1(%2)")
.arg(n->className())
.arg( NodeMask::maskToString(n->getNodeMask()) ));
}
qDebug(" ipt %10g %10g %10g %s", pt.x(), pt.y(), pt.z(), qPrintable(path));
}
qDebug("endintersections:");
}
osg::ref_ptr<osgUtil::LineSegmentIntersector>
Osg3dViewWithCamera::intersectUnderCursor(const int x, const int y, unsigned mask)
{
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector =
new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW,
x, height()- y);
osgUtil::IntersectionVisitor intersectVisitor( intersector.get() );
intersectVisitor.setTraversalMask(mask);
getCamera()->accept(intersectVisitor);
return intersector;
}