-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqtosgwidget.cpp
More file actions
289 lines (239 loc) · 9.4 KB
/
qtosgwidget.cpp
File metadata and controls
289 lines (239 loc) · 9.4 KB
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
#include "qtosgwidget.h"
#include "osghelper.h"
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>
QtOSGWidget::QtOSGWidget(qreal scaleX, qreal scaleY, QWidget* parent)
: QOpenGLWidget(parent)
, _mGraphicsWindow(new osgViewer::GraphicsWindowEmbedded( this->x(), this->y(),
this->width(), this->height() ) )
, _mViewer(new osgViewer::Viewer)
, m_scaleX(scaleX)
, m_scaleY(scaleY)
{
try{
// osg::setNotifyLevel(osg::WARN);
// osg::setNotifyHandler(new OsgLogger("mylog.txt"));
//initialize camera
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setViewport( this->x(), this->y(), this->width(), this->height() );
camera->setClearColor( osg::Vec4( 0.9f, 0.9f, 1.f, 1.f ) );
float aspectRatio = static_cast<float>( this->width()) / static_cast<float>( this->height() );
camera->setProjectionMatrixAsPerspective( 30.f, aspectRatio, 1.f, 1000.f );
camera->setGraphicsContext( _mGraphicsWindow );
_mViewer->setCamera(camera);
//intitalize hud
osg::ref_ptr<osg::Camera> hud_camera = new osg::Camera;
hud_camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));// set the view matrix
hud_camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
hud_camera->setViewMatrix(osg::Matrix::identity());// only clear the depth buffer
hud_camera->setClearMask(GL_DEPTH_BUFFER_BIT);// draw subgraph after main camera view.
hud_camera->setRenderOrder(osg::Camera::POST_RENDER);
hud_camera->setGraphicsContext(_mGraphicsWindow);
hud_camera->setViewport(this->x(), this->y(),this->width(), this->height());
_mViewer->addSlave(hud_camera, false);
//trackball navigation
osg::ref_ptr<osgGA::TrackballManipulator> manipulator = new osgGA::TrackballManipulator;
manipulator->setAllowThrow( false );
this->setMouseTracking(true);
_mViewer->setCameraManipulator(manipulator);
osg::Vec3d eye( 60.0, 40.0, 60.0 );
osg::Vec3d center( 0.0, 0.0, 0.0 );
osg::Vec3d up( 0.0, 1.0, 0.0 );
manipulator->setHomePosition(eye,center,up);
osg::ref_ptr<osg::Group> root = new osg::Group;
// root->addChild(hud_camera);
_mViewer->setSceneData(root);
_mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
_mViewer->realize();
}
catch(std::exception e)
{
std::cout<<e.what()<<std::endl;
}
}
void QtOSGWidget::setScene(osg::ref_ptr<osg::Group> root)
{
_mViewer->setSceneData(root);
}
osg::ref_ptr<osg::Group> QtOSGWidget::getScene()
{
osg::ref_ptr<osg::Group> root = dynamic_cast<osg::Group*>(_mViewer->getSceneData());
return root;
}
void QtOSGWidget::setScale(qreal X, qreal Y)
{
m_scaleX = X;
m_scaleY = Y;
this->resizeGL(this->width(), this->height());
}
void QtOSGWidget::paintGL() {
_mViewer->frame();
}
void QtOSGWidget::resizeGL( int width, int height )
{
this->getEventQueue()->windowResize(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY);
_mGraphicsWindow->resized(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY);
osg::ref_ptr<osg::Camera> camera = _mViewer->getCamera();
camera->setViewport(0, 0, this->width()*m_scaleX, this->height()* m_scaleY);
}
void QtOSGWidget::initializeGL(){
//basic shader setup
// disabled shader as of now, but in essence this is how it will work
// osg::ref_ptr<osg::StateSet> stateSet = this->getScene()->getOrCreateStateSet();
// osg::ref_ptr<osg::Program> program = new osg::Program;
// osg::ref_ptr<osg::Shader> vertShader = new osg::Shader(osg::Shader::VERTEX);
// if (!vertShader->loadShaderSourceFromFile("../shaders/myShader.vert"))
// std::cerr << "Could not read VERTEX shader from file" << std::endl;
// program->addShader(vertShader);
// osg::ref_ptr<osg::Shader> geomShader = new osg::Shader(osg::Shader::GEOMETRY);
// if (!geomShader->loadShaderSourceFromFile("../shaders/myShader.geom"))
// std::cerr << "Could not read GEOM shader from file" << std::endl;
// program->addShader(geomShader);
// osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT);
// if (!fragShader->loadShaderSourceFromFile("../shaders/myShader.frag"))
// std::cerr << "Could not read FRAGMENT shader from file" << std::endl;
// program->addShader(fragShader);
// stateSet->setAttributeAndModes(program.get(), osg::StateAttribute::ON);
// stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
// osg::Uniform* modelViewProjectionMatrix = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "osg_ModelViewProjectionMatrix");
// modelViewProjectionMatrix->setUpdateCallback(new ModelViewProjectionMatrixCallback(_mViewer->getCamera()));
// stateSet->addUniform(modelViewProjectionMatrix);
}
/*void QtOSGWidget::addHud(osg::Group hud)
{
hud_camera->addChild(hud);
hud_camera->setGraphicsContext(_mGraphicsWindow);
hud_camera->setViewport(this->x(), this->y(),this->width(), this->height());
}*/
void QtOSGWidget::mouseMoveEvent(QMouseEvent* event)
{
this->getEventQueue()->mouseMotion(event->x()*m_scaleX, event->y()*m_scaleY);
osg::Matrixd viewMatrix = _mViewer->getCamera()->getViewMatrix();
osg::Vec3d center;
osg::Vec3d eye;
osg::Vec3d up;
viewMatrix.getLookAt(eye,center,up);
std::vector<double> values;
values.push_back(eye.x());
values.push_back(eye.y());
values.push_back(eye.z());
values.push_back(center.x());
values.push_back(center.y());
values.push_back(center.z());
values.push_back(up.x());
values.push_back(up.y());
values.push_back(up.z());
emit orientationChanged(values);
}
void QtOSGWidget::mousePressEvent(QMouseEvent* event)
{
unsigned int button = 0;
switch (event->button()){
case Qt::LeftButton:
button = 1;
break;
case Qt::MiddleButton:
button = 2;
break;
case Qt::RightButton:
button = 3;
break;
default:
break;
}
this->getEventQueue()->mouseButtonPress(event->x()*m_scaleX, event->y()*m_scaleY, button);
}
void QtOSGWidget::mouseReleaseEvent(QMouseEvent* event)
{
unsigned int button = 0;
switch (event->button()){
case Qt::LeftButton:
button = 1;
break;
case Qt::MiddleButton:
button = 2;
break;
case Qt::RightButton:
button = 3;
break;
default:
break;
}
this->getEventQueue()->mouseButtonRelease(event->x()*m_scaleX, event->y()*m_scaleY, button);
}
void QtOSGWidget::wheelEvent(QWheelEvent* event)
{
int delta = event->delta();
osgGA::GUIEventAdapter::ScrollingMotion motion = delta > 0 ?
osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN;
this->getEventQueue()->mouseScroll(motion);
osg::Matrixd viewMatrix = _mViewer->getCamera()->getViewMatrix();
osg::Vec3d center;
osg::Vec3d eye;
osg::Vec3d up;
viewMatrix.getLookAt(eye,center,up);
std::vector<double> values;
values.push_back(eye.x());
values.push_back(eye.y());
values.push_back(eye.z());
values.push_back(center.x());
values.push_back(center.y());
values.push_back(center.z());
values.push_back(up.x());
values.push_back(up.y());
values.push_back(up.z());
emit orientationChanged(values);
}
bool QtOSGWidget::event(QEvent* event)
{
bool handled = QOpenGLWidget::event(event);
this->update();
return handled;
}
osgGA::EventQueue* QtOSGWidget::getEventQueue() const {
osgGA::EventQueue* eventQueue = _mGraphicsWindow->getEventQueue();
return eventQueue;
}
void QtOSGWidget::changedColor(QColor color)
{
int r = 0,b = 0,g = 0,a = 255;
osg::Vec4 vColor;
color.getRgb(&r,&b,&g,&a);
vColor[0] = ((float)r)/255.0f;
vColor[1] = ((float)g)/255.0f;
vColor[2] = ((float)b)/255.0f;
vColor[3] = ((float)a)/255.0f;
_mViewer->getCamera()->setClearColor(vColor);
}
void QtOSGWidget::openScene(std::string path)
{
if(boost::ends_with(path,".osg"))
{
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile(path);
if(loadedModel == NULL)
{
std::cout<<"failed to load model";
throw std::exception();
}
osg::ref_ptr<osg::Group> root = new osg::Group();
root->addChild(loadedModel);
_mViewer->setSceneData(root);
}else if(boost::ends_with(path,".step"))
{
osg::ref_ptr<osg::Geode> loadedModel = readStepFile(path);
if(loadedModel == NULL)
{
std::cout<<"failed to load model";
throw std::exception();
}
osg::ref_ptr<osg::Group> root = new osg::Group();
root->addChild(loadedModel);
_mViewer->setSceneData(root);
}
}
void QtOSGWidget::saveScene(std::string path)
{
osg::ref_ptr<osg::Node> root = _mViewer->getSceneData();
osgDB::writeNodeFile(*root,path);
}