Rev 17781 | Rev 22521 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
13839 | fschmid | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | /************************************************************************** |
||
8 | * Copyright (C) 2009 by Franz Schmid * |
||
9 | * franz.schmid@altmuehlnet.de * |
||
10 | * * |
||
11 | * This program is free software; you can redistribute it and/or modify * |
||
12 | * it under the terms of the GNU General Public License as published by * |
||
13 | * the Free Software Foundation; either version 2 of the License, or * |
||
14 | * (at your option) any later version. * |
||
15 | * * |
||
16 | * This program is distributed in the hope that it will be useful, * |
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
19 | * GNU General Public License for more details. * |
||
20 | * * |
||
21 | * You should have received a copy of the GNU General Public License * |
||
22 | * along with this program; if not, write to the * |
||
23 | * Free Software Foundation, Inc., * |
||
18122 | mrdocs | 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
13839 | fschmid | 25 | ***************************************************************************/ |
26 | |||
27 | #include <osgDB/ReadFile> |
||
17133 | fschmid | 28 | #include <osgDB/ReaderWriter> |
29 | #include <osgDB/PluginQuery> |
||
13839 | fschmid | 30 | #include <osgViewer/Viewer> |
31 | #include <osgGA/TrackballManipulator> |
||
32 | #include <osgUtil/Optimizer> |
||
13874 | fschmid | 33 | #include <osg/PolygonMode> |
34 | #include <osg/PolygonOffset> |
||
35 | #include <osg/Point> |
||
36 | #include <osg/Material> |
||
37 | #include <osg/BlendFunc> |
||
13839 | fschmid | 38 | #include "osgeditor.h" |
39 | #include "commonstrings.h" |
||
40 | #include "sccolorengine.h" |
||
41 | #include "ui/customfdialog.h" |
||
42 | #include "prefsfile.h" |
||
43 | #include "prefsmanager.h" |
||
44 | #include "units.h" |
||
45 | #include "util.h" |
||
46 | #include <QFileDialog> |
||
47 | #include <QInputDialog> |
||
13874 | fschmid | 48 | #include <QColorDialog> |
13839 | fschmid | 49 | |
17781 | fschmid | 50 | OSGEditorDialog::OSGEditorDialog(QWidget* parent, PageItem_OSGFrame *frame, QString osgFilterString) : QDialog(parent) |
13839 | fschmid | 51 | { |
52 | setupUi(this); |
||
53 | setModal(true); |
||
54 | currItem = frame; |
||
17781 | fschmid | 55 | filterString = osgFilterString; |
13839 | fschmid | 56 | int wd = 300; |
57 | int hd = 300; |
||
58 | double asp = currItem->width() / currItem->height(); |
||
59 | if (asp > 1.0) |
||
60 | hd = qRound(300 / asp); |
||
61 | else |
||
62 | wd = qRound(300 * asp); |
||
63 | drawingarea->setFixedSize(wd, hd); |
||
64 | drawingarea->resize(wd, hd); |
||
65 | updateGeometry(); |
||
66 | viewMap = currItem->viewMap; |
||
67 | currentViewName = currItem->currentView; |
||
68 | currentView = viewMap[currentViewName]; |
||
69 | viewCombo->addItems(viewMap.keys()); |
||
70 | setCurrentComboItem(viewCombo, currentViewName); |
||
71 | fovAngle->setValue(30.0); |
||
72 | fovAngle->setNewUnit(SC_DEGREES); |
||
13874 | fschmid | 73 | QPixmap pm(54, 14); |
74 | pm.fill(currentView.colorAC); |
||
75 | buttonACcolor->setIcon(pm); |
||
76 | buttonACcolor->setText( QString::null ); |
||
77 | pm.fill(currentView.colorFC); |
||
78 | buttonFCcolor->setIcon(pm); |
||
79 | buttonFCcolor->setText( QString::null ); |
||
80 | transpSpin->setValue(qRound(currentView.addedTransparency * 100)); |
||
17420 | fschmid | 81 | transpSpin->setDecimals(0); |
13874 | fschmid | 82 | rootnode = new osg::Group; |
83 | decorator = new osg::Group; |
||
13839 | fschmid | 84 | if (currItem->fillColor() != CommonStrings::None) |
85 | { |
||
86 | const ScColor& col = currItem->doc()->PageColors[currItem->fillColor()]; |
||
87 | QColor fillColor = ScColorEngine::getShadeColorProof(col, currItem->doc(), currItem->fillShade()); |
||
88 | drawingarea->getCamera()->setClearColor(osg::Vec4(fillColor.redF(), fillColor.greenF(), fillColor.blueF(), 0.0)); |
||
89 | } |
||
90 | if (currItem->loadedModel) |
||
91 | { |
||
92 | loadedModel = currItem->loadedModel; |
||
13874 | fschmid | 93 | default_stateset = loadedModel->getOrCreateStateSet(); |
94 | usedModel = dynamic_cast<osg::Node*>(loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL)); |
||
95 | usedModel2 = dynamic_cast<osg::Node*>(loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL)); |
||
13839 | fschmid | 96 | modelFile = currItem->modelFile; |
97 | renderStyleCombo->setCurrentIndex(currentView.rendermode); |
||
98 | lightStyleCombo->setCurrentIndex(currentView.illumination); |
||
99 | fovAngle->setValue(currentView.angleFOV); |
||
13874 | fschmid | 100 | transpSpin->setValue(qRound(currentView.addedTransparency * 100)); |
13839 | fschmid | 101 | drawingarea->setCameraManipulator ( new osgGA::TrackballManipulator ); |
13874 | fschmid | 102 | rootnode->addChild(usedModel); |
103 | drawingarea->setSceneData(rootnode); |
||
13839 | fschmid | 104 | osgGA::TrackballManipulator *trb = dynamic_cast<osgGA::TrackballManipulator*>(drawingarea->getCameraManipulator()); |
105 | trb->setByMatrix(currentView.trackerMatrix); |
||
106 | trb->setCenter(currentView.trackerCenter); |
||
107 | trb->setDistance(currentView.trackerDist); |
||
108 | trb->setTrackballSize(currentView.trackerSize); |
||
109 | drawingarea->updateTraversal(); |
||
110 | double fovy, aspectRatio, zNear, zFar; |
||
111 | drawingarea->getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar); |
||
112 | drawingarea->getCamera()->setProjectionMatrixAsPerspective(currentView.angleFOV, aspectRatio, zNear, zFar); |
||
13874 | fschmid | 113 | changeRenderMode(static_cast<int>(currentView.rendermode)); |
13839 | fschmid | 114 | drawingarea->updateGL(); |
115 | connect(drawingarea, SIGNAL(mouseMoved()), this, SLOT(reportCamera())); |
||
116 | connect(viewCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeView(QString))); |
||
117 | connect(buttonAddView, SIGNAL(clicked()), this, SLOT(addView())); |
||
118 | connect(buttonRemoveView, SIGNAL(clicked()), this, SLOT(removeView())); |
||
13874 | fschmid | 119 | connect(buttonACcolor, SIGNAL(clicked()), this, SLOT(changeACcolor())); |
120 | connect(buttonFCcolor, SIGNAL(clicked()), this, SLOT(changeFCcolor())); |
||
17420 | fschmid | 121 | connect(transpSpin, SIGNAL(valueChanged(double)), this, SLOT(changeTransparency(double))); |
13839 | fschmid | 122 | groupBox_5->setEnabled(true); |
123 | buttonRemoveView->setEnabled(true); |
||
124 | buttonAddView->setEnabled(true); |
||
125 | clearButton->setEnabled(true); |
||
126 | } |
||
127 | else |
||
128 | { |
||
129 | groupBox_5->setEnabled(false); |
||
130 | buttonRemoveView->setEnabled(false); |
||
131 | buttonAddView->setEnabled(false); |
||
132 | clearButton->setEnabled(false); |
||
133 | } |
||
134 | connect(fovAngle, SIGNAL(valueChanged(double)), this, SLOT(setCameraValues())); |
||
135 | connect(loadButton, SIGNAL(clicked()), this, SLOT(openFile())); |
||
136 | connect(clearButton, SIGNAL(clicked()), this, SLOT(clearDisplay())); |
||
137 | connect(renderStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeRenderMode(int))); |
||
138 | connect(lightStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLightMode(int))); |
||
139 | } |
||
140 | |||
141 | void OSGEditorDialog::clearDisplay() |
||
142 | { |
||
143 | disconnect(fovAngle, SIGNAL(valueChanged(double)), this, SLOT(setCameraValues())); |
||
144 | disconnect(renderStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeRenderMode(int))); |
||
145 | disconnect(lightStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLightMode(int))); |
||
146 | disconnect(drawingarea, SIGNAL(mouseMoved()), this, SLOT(reportCamera())); |
||
147 | disconnect(viewCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeView(QString))); |
||
148 | disconnect(buttonAddView, SIGNAL(clicked()), this, SLOT(addView())); |
||
149 | disconnect(buttonRemoveView, SIGNAL(clicked()), this, SLOT(removeView())); |
||
150 | currentView.trackerCenter = osg::Vec3d(); |
||
151 | currentView.cameraPosition = osg::Vec3d(); |
||
152 | currentView.trackerMatrix = osg::Matrixd(); |
||
13874 | fschmid | 153 | rootnode->removeChild(usedModel); |
154 | if (rootnode->getNumChildren() > 1) |
||
155 | { |
||
156 | rootnode->removeChild(decorator); |
||
157 | decorator->removeChild(usedModel2); |
||
158 | } |
||
13839 | fschmid | 159 | drawingarea->setSceneData(0); |
160 | loadedModel = 0; |
||
13874 | fschmid | 161 | usedModel = 0; |
13839 | fschmid | 162 | currentView.angleFOV = 30.0; |
163 | fovAngle->setValue(currentView.angleFOV); |
||
164 | currentView.illumination = PageItem_OSGFrame::Headlamp; |
||
165 | currentView.rendermode = PageItem_OSGFrame::Solid; |
||
166 | renderStyleCombo->setCurrentIndex(currentView.rendermode); |
||
167 | lightStyleCombo->setCurrentIndex(currentView.illumination); |
||
168 | modelFile = ""; |
||
169 | viewMap.clear(); |
||
170 | currentView.trackerDist = 0.0; |
||
171 | currentView.trackerSize = 0.0; |
||
172 | viewMap.insert( tr("Default"), currentView); |
||
173 | currentViewName = tr("Default"); |
||
174 | viewCombo->addItems(viewMap.keys()); |
||
175 | setCurrentComboItem(viewCombo, currentViewName); |
||
176 | groupBox_5->setEnabled(false); |
||
177 | buttonRemoveView->setEnabled(false); |
||
178 | buttonAddView->setEnabled(false); |
||
179 | clearButton->setEnabled(false); |
||
180 | connect(fovAngle, SIGNAL(valueChanged(double)), this, SLOT(setCameraValues())); |
||
181 | connect(renderStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeRenderMode(int))); |
||
182 | connect(lightStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLightMode(int))); |
||
183 | } |
||
184 | |||
185 | void OSGEditorDialog::setCameraValues() |
||
186 | { |
||
187 | drawingarea->getCamera()->setProjectionMatrixAsPerspective(fovAngle->value(), static_cast<double>(drawingarea->width()) / static_cast<double>(drawingarea->height()), 1.0f, 10000.0f ); |
||
188 | drawingarea->updateGL(); |
||
189 | currentView.angleFOV = fovAngle->value(); |
||
190 | } |
||
191 | |||
192 | void OSGEditorDialog::changeView(QString viewName) |
||
193 | { |
||
194 | disconnect(fovAngle, SIGNAL(valueChanged(double)), this, SLOT(setCameraValues())); |
||
195 | disconnect(renderStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeRenderMode(int))); |
||
196 | disconnect(lightStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLightMode(int))); |
||
17420 | fschmid | 197 | disconnect(transpSpin, SIGNAL(valueChanged(double)), this, SLOT(changeTransparency(double))); |
13839 | fschmid | 198 | viewMap[currentViewName] = currentView; |
199 | currentView = viewMap[viewName]; |
||
200 | currentViewName = viewName; |
||
201 | renderStyleCombo->setCurrentIndex(currentView.rendermode); |
||
202 | lightStyleCombo->setCurrentIndex(currentView.illumination); |
||
203 | fovAngle->setValue(currentView.angleFOV); |
||
13874 | fschmid | 204 | transpSpin->setValue(qRound(currentView.addedTransparency * 100)); |
205 | QPixmap pm(54, 14); |
||
206 | pm.fill(currentView.colorAC); |
||
207 | buttonACcolor->setIcon(pm); |
||
208 | buttonACcolor->setText( QString::null ); |
||
209 | pm.fill(currentView.colorFC); |
||
210 | buttonFCcolor->setIcon(pm); |
||
211 | buttonFCcolor->setText( QString::null ); |
||
13839 | fschmid | 212 | osgGA::TrackballManipulator *trb = dynamic_cast<osgGA::TrackballManipulator*>(drawingarea->getCameraManipulator()); |
213 | trb->setByMatrix(currentView.trackerMatrix); |
||
214 | trb->setCenter(currentView.trackerCenter); |
||
215 | trb->setDistance(currentView.trackerDist); |
||
216 | trb->setTrackballSize(currentView.trackerSize); |
||
217 | drawingarea->updateTraversal(); |
||
218 | double fovy, aspectRatio, zNear, zFar; |
||
219 | drawingarea->getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar); |
||
220 | drawingarea->getCamera()->setProjectionMatrixAsPerspective(currentView.angleFOV, aspectRatio, zNear, zFar); |
||
13874 | fschmid | 221 | changeRenderMode(static_cast<int>(currentView.rendermode)); |
13839 | fschmid | 222 | drawingarea->updateGL(); |
223 | connect(fovAngle, SIGNAL(valueChanged(double)), this, SLOT(setCameraValues())); |
||
224 | connect(renderStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeRenderMode(int))); |
||
225 | connect(lightStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLightMode(int))); |
||
17420 | fschmid | 226 | connect(transpSpin, SIGNAL(valueChanged(double)), this, SLOT(changeTransparency(double))); |
13839 | fschmid | 227 | } |
228 | |||
229 | void OSGEditorDialog::addView() |
||
230 | { |
||
231 | disconnect(viewCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeView(QString))); |
||
232 | viewMap[currentViewName] = currentView; |
||
233 | bool ok; |
||
234 | QString newName = currentViewName; |
||
235 | while (viewMap.contains(newName)) |
||
236 | { |
||
237 | newName = QInputDialog::getText( this, tr("Add a new View"), tr("Name:"), QLineEdit::Normal, tr("New View"), &ok); |
||
238 | } |
||
239 | if (ok && !newName.isEmpty()) |
||
240 | { |
||
241 | currentViewName = newName; |
||
242 | viewMap.insert(currentViewName, currentView); |
||
243 | viewCombo->addItem(currentViewName); |
||
244 | setCurrentComboItem(viewCombo, currentViewName); |
||
245 | } |
||
246 | connect(viewCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeView(QString))); |
||
247 | } |
||
248 | |||
249 | void OSGEditorDialog::removeView() |
||
250 | { |
||
251 | disconnect(viewCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeView(QString))); |
||
252 | if (viewMap.size() > 1) |
||
253 | { |
||
254 | viewMap.remove(currentViewName); |
||
255 | viewCombo->clear(); |
||
256 | QStringList vKey = viewMap.keys(); |
||
257 | viewCombo->addItems(vKey); |
||
258 | currentViewName = vKey[0]; |
||
259 | setCurrentComboItem(viewCombo, currentViewName); |
||
260 | currentView = viewMap[currentViewName]; |
||
261 | changeView(currentViewName); |
||
262 | } |
||
263 | connect(viewCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeView(QString))); |
||
264 | } |
||
265 | |||
266 | void OSGEditorDialog::renameView(QString newName) |
||
267 | { |
||
268 | viewMap.remove(currentViewName); |
||
269 | currentViewName = newName; |
||
270 | viewMap.insert(currentViewName, currentView); |
||
271 | } |
||
272 | |||
13874 | fschmid | 273 | void OSGEditorDialog::analyse(osg::Node *nd, double transparency) |
274 | { |
||
275 | osg::Geode *geode = dynamic_cast<osg::Geode *> ( nd ); |
||
276 | if ( geode ) |
||
277 | { |
||
278 | analyseGeode ( geode, transparency ); |
||
279 | } |
||
280 | else |
||
281 | { |
||
282 | osg::Group *gp = dynamic_cast<osg::Group *> ( nd ); |
||
283 | if ( gp ) |
||
284 | { |
||
285 | for ( unsigned int ic=0; ic<gp->getNumChildren(); ic++ ) |
||
286 | { |
||
287 | analyse ( gp->getChild ( ic ), transparency ); |
||
288 | } |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | |||
293 | void OSGEditorDialog::analyseGeode(osg::Geode *geode, double transparency) |
||
294 | { |
||
295 | osg::StateAttribute* pRAP; |
||
296 | osg::StateSet* theState = geode->getStateSet(); |
||
297 | if (theState) |
||
298 | { |
||
299 | pRAP = theState->getAttribute(osg::StateAttribute::MATERIAL); |
||
300 | if (pRAP != NULL) |
||
301 | { |
||
302 | osg::Material *material = dynamic_cast<osg::Material*>(pRAP); |
||
303 | if (material != NULL) |
||
304 | { |
||
305 | material->setColorMode(osg::Material::OFF); |
||
306 | material->setAlpha(osg::Material::FRONT_AND_BACK, transparency); |
||
307 | osg::ref_ptr<osg::BlendFunc> bf = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA ); |
||
308 | theState->setAttributeAndModes(bf); |
||
309 | theState->setMode(GL_BLEND,osg::StateAttribute::ON); |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | for ( unsigned int i=0; i<geode->getNumDrawables(); i++ ) |
||
314 | { |
||
315 | osg::Drawable *drawable = geode->getDrawable ( i ); |
||
316 | if (drawable) |
||
317 | { |
||
318 | osg::StateSet* theState = drawable->getStateSet(); |
||
319 | if (theState) |
||
320 | { |
||
321 | pRAP = theState->getAttribute(osg::StateAttribute::MATERIAL); |
||
322 | if (pRAP != NULL) |
||
323 | { |
||
324 | osg::Material *material = dynamic_cast<osg::Material*>(pRAP); |
||
325 | if (material != NULL) |
||
326 | { |
||
327 | material->setColorMode(osg::Material::OFF); |
||
328 | material->setAlpha(osg::Material::FRONT_AND_BACK, transparency); |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | } |
||
335 | |||
13839 | fschmid | 336 | void OSGEditorDialog::changeRenderMode(int mode) |
337 | { |
||
13874 | fschmid | 338 | osg::ref_ptr<osg::StateSet> stateset; |
339 | osg::ref_ptr<osg::StateSet> stateset2; |
||
340 | osg::ref_ptr<osg::Material> material; |
||
341 | osg::ref_ptr<osg::Material> material2; |
||
342 | osg::ref_ptr<osg::PolygonMode> polymode; |
||
343 | osg::ref_ptr<osg::PolygonOffset> polyoffset; |
||
344 | osg::ref_ptr<osg::Point> point; |
||
345 | osg::ref_ptr<osg::BlendFunc> bf; |
||
346 | osg::Vec4 colAC = osg::Vec4(currentView.colorAC.redF(), currentView.colorAC.greenF(), currentView.colorAC.blueF(), 1.0f); |
||
347 | osg::Vec4 colFC = osg::Vec4(currentView.colorFC.redF(), currentView.colorFC.greenF(), currentView.colorFC.blueF(), 1.0f); |
||
13839 | fschmid | 348 | currentView.rendermode = static_cast<PageItem_OSGFrame::RenderType>(mode); |
13874 | fschmid | 349 | stateset = new osg::StateSet; |
350 | rootnode->removeChild(usedModel); |
||
351 | usedModel->releaseGLObjects(); |
||
352 | usedModel = dynamic_cast<osg::Node*>(loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL)); |
||
353 | if (rootnode->getNumChildren() > 0) |
||
354 | { |
||
355 | rootnode->removeChild(decorator); |
||
356 | decorator->removeChild(usedModel2); |
||
357 | decorator->setStateSet(default_stateset); |
||
358 | usedModel2->releaseGLObjects(); |
||
359 | usedModel2 = dynamic_cast<osg::Node*>(loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL)); |
||
360 | } |
||
361 | rootnode->addChild(usedModel); |
||
362 | label->setEnabled(false); |
||
363 | buttonACcolor->setEnabled(false); |
||
364 | label_2->setEnabled(false); |
||
365 | buttonFCcolor->setEnabled(false); |
||
366 | label_3->setEnabled(false); |
||
367 | transpSpin->setEnabled(false); |
||
368 | switch (currentView.rendermode) |
||
369 | { |
||
370 | case PageItem_OSGFrame::Transparent: |
||
371 | analyse(usedModel.get(), currentView.addedTransparency); |
||
372 | stateset->setMode(GL_BLEND,osg::StateAttribute::ON); |
||
373 | bf = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA ); |
||
374 | stateset->setAttributeAndModes(bf); |
||
375 | usedModel.get()->setStateSet(stateset); |
||
376 | label_3->setEnabled(true); |
||
377 | transpSpin->setEnabled(true); |
||
378 | break; |
||
379 | case PageItem_OSGFrame::Wireframe: |
||
380 | label->setEnabled(true); |
||
381 | buttonACcolor->setEnabled(true); |
||
382 | polymode = new osg::PolygonMode; |
||
383 | material = new osg::Material; |
||
384 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
||
385 | stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
386 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
||
387 | material->setColorMode(osg::Material::OFF); |
||
388 | material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
389 | material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
390 | material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
391 | material->setEmission(osg::Material::FRONT_AND_BACK, colAC); |
||
392 | stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
393 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
394 | usedModel.get()->setStateSet(stateset); |
||
395 | break; |
||
396 | case PageItem_OSGFrame::ShadedWireframe: |
||
397 | polymode = new osg::PolygonMode; |
||
398 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
||
399 | stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
400 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
||
401 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
||
402 | usedModel.get()->setStateSet(stateset); |
||
403 | break; |
||
404 | case PageItem_OSGFrame::Vertices: |
||
405 | label->setEnabled(true); |
||
406 | buttonACcolor->setEnabled(true); |
||
407 | polymode = new osg::PolygonMode; |
||
408 | material = new osg::Material; |
||
409 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::POINT); |
||
410 | stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
411 | point = new osg::Point(); |
||
412 | point->setSize(3); |
||
413 | stateset->setAttribute(point); |
||
414 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
||
415 | material->setColorMode(osg::Material::OFF); |
||
416 | material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
417 | material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
418 | material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
419 | material->setEmission(osg::Material::FRONT_AND_BACK, colAC); |
||
420 | stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
421 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
422 | usedModel.get()->setStateSet(stateset); |
||
423 | break; |
||
424 | case PageItem_OSGFrame::ShadedVertices: |
||
425 | polymode = new osg::PolygonMode; |
||
426 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::POINT); |
||
427 | stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
428 | point = new osg::Point(); |
||
429 | point->setSize(3); |
||
430 | stateset->setAttribute(point); |
||
431 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
||
432 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
||
433 | usedModel.get()->setStateSet(stateset); |
||
434 | break; |
||
435 | case PageItem_OSGFrame::Illustration: |
||
436 | label->setEnabled(true); |
||
437 | buttonACcolor->setEnabled(true); |
||
438 | label_2->setEnabled(true); |
||
439 | buttonFCcolor->setEnabled(true); |
||
440 | polymode = new osg::PolygonMode; |
||
441 | polyoffset = new osg::PolygonOffset; |
||
442 | material = new osg::Material; |
||
443 | polyoffset->setFactor(-1.0f); |
||
444 | polyoffset->setUnits(-1.0f); |
||
445 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
||
446 | stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
447 | stateset->setAttributeAndModes(polyoffset,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
448 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
||
449 | material->setColorMode(osg::Material::OFF); |
||
450 | material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
451 | material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
452 | material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
453 | material->setEmission(osg::Material::FRONT_AND_BACK, colAC); |
||
454 | stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
455 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
456 | rootnode->insertChild(0, decorator); |
||
457 | stateset2 = new osg::StateSet; |
||
458 | material2 = new osg::Material; |
||
459 | material2->setColorMode(osg::Material::OFF); |
||
460 | material2->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
461 | material2->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
462 | material2->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
463 | material2->setEmission(osg::Material::FRONT_AND_BACK, colFC); |
||
464 | stateset2->setAttributeAndModes(material2,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
465 | stateset2->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
466 | decorator->addChild(usedModel2); |
||
467 | decorator->setStateSet(stateset2); |
||
468 | usedModel.get()->setStateSet(stateset); |
||
469 | break; |
||
470 | case PageItem_OSGFrame::SolidWireframe: |
||
471 | case PageItem_OSGFrame::SolidOutline: |
||
472 | case PageItem_OSGFrame::TransparentWireframe: |
||
473 | label->setEnabled(true); |
||
474 | buttonACcolor->setEnabled(true); |
||
475 | if (currentView.rendermode == PageItem_OSGFrame::TransparentWireframe) |
||
476 | { |
||
477 | label_3->setEnabled(true); |
||
478 | transpSpin->setEnabled(true); |
||
479 | analyse(usedModel2.get(), currentView.addedTransparency); |
||
480 | stateset2 = new osg::StateSet; |
||
481 | stateset2->setMode(GL_BLEND,osg::StateAttribute::ON); |
||
482 | bf = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA ); |
||
483 | stateset2->setAttributeAndModes(bf); |
||
484 | usedModel2.get()->setStateSet(stateset2); |
||
485 | } |
||
486 | polymode = new osg::PolygonMode; |
||
487 | polyoffset = new osg::PolygonOffset; |
||
488 | material = new osg::Material; |
||
489 | polyoffset->setFactor(-1.0f); |
||
490 | polyoffset->setUnits(-1.0f); |
||
491 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
||
492 | stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
493 | stateset->setAttributeAndModes(polyoffset,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
494 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
||
495 | material->setColorMode(osg::Material::OFF); |
||
496 | material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
497 | material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
498 | material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f)); |
||
499 | material->setEmission(osg::Material::FRONT_AND_BACK, colAC); |
||
500 | stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
501 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
||
502 | rootnode->insertChild(0, decorator); |
||
503 | decorator->addChild(usedModel2); |
||
504 | usedModel.get()->setStateSet(stateset); |
||
505 | break; |
||
506 | default: |
||
507 | stateset = default_stateset; |
||
508 | usedModel.get()->setStateSet(stateset); |
||
509 | break; |
||
510 | } |
||
13839 | fschmid | 511 | } |
512 | |||
13874 | fschmid | 513 | void OSGEditorDialog::changeACcolor() |
514 | { |
||
515 | QColor neu = QColor(); |
||
516 | neu = QColorDialog::getColor(currentView.colorAC, this); |
||
517 | if (neu.isValid()) |
||
518 | { |
||
519 | QPixmap pm(54, 14); |
||
520 | pm.fill(neu); |
||
521 | currentView.colorAC = neu; |
||
522 | buttonACcolor->setIcon(pm); |
||
523 | changeRenderMode(static_cast<int>(currentView.rendermode)); |
||
524 | } |
||
525 | } |
||
526 | |||
527 | void OSGEditorDialog::changeFCcolor() |
||
528 | { |
||
529 | QColor neu = QColor(); |
||
530 | neu = QColorDialog::getColor(currentView.colorAC, this); |
||
531 | if (neu.isValid()) |
||
532 | { |
||
533 | QPixmap pm(54, 14); |
||
534 | pm.fill(neu); |
||
535 | currentView.colorFC = neu; |
||
536 | buttonFCcolor->setIcon(pm); |
||
537 | changeRenderMode(static_cast<int>(currentView.rendermode)); |
||
538 | } |
||
539 | } |
||
540 | |||
17420 | fschmid | 541 | void OSGEditorDialog::changeTransparency(double value) |
13874 | fschmid | 542 | { |
17420 | fschmid | 543 | currentView.addedTransparency = value / 100.0; |
13874 | fschmid | 544 | changeRenderMode(static_cast<int>(currentView.rendermode)); |
545 | } |
||
546 | |||
13839 | fschmid | 547 | void OSGEditorDialog::changeLightMode(int mode) |
548 | { |
||
549 | currentView.illumination = static_cast<PageItem_OSGFrame::LightType>(mode); |
||
550 | } |
||
551 | |||
552 | void OSGEditorDialog::reportCamera() |
||
553 | { |
||
554 | disconnect(fovAngle, SIGNAL(valueChanged(double)), this, SLOT(setCameraValues())); |
||
555 | osg::Vec3d vecEye, vecCenter, vecUp; |
||
556 | drawingarea->getCamera()->getViewMatrixAsLookAt(vecEye, vecCenter, vecUp); |
||
557 | currentView.cameraPosition = vecEye; |
||
558 | currentView.cameraUp = vecUp; |
||
559 | osgGA::TrackballManipulator *trb = dynamic_cast<osgGA::TrackballManipulator*>(drawingarea->getCameraManipulator()); |
||
560 | currentView.trackerCenter = trb->getCenter(); |
||
561 | currentView.trackerMatrix = trb->getMatrix(); |
||
562 | currentView.trackerDist = trb->getDistance(); |
||
563 | currentView.trackerSize = trb->getTrackballSize(); |
||
564 | double fovy, aspectRatio, zNear, zFar; |
||
565 | drawingarea->getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar); |
||
566 | currentView.angleFOV = fovy; |
||
567 | fovAngle->setValue(currentView.angleFOV); |
||
568 | connect(fovAngle, SIGNAL(valueChanged(double)), this, SLOT(setCameraValues())); |
||
569 | } |
||
570 | |||
571 | void OSGEditorDialog::openFile() |
||
572 | { |
||
573 | QString fileName; |
||
574 | PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs"); |
||
575 | QString wdir = dirs->get("models", "."); |
||
17781 | fschmid | 576 | CustomFDialog dia(this, wdir, tr("Import 3-D Model"), filterString, fdHidePreviewCheckBox); |
13839 | fschmid | 577 | if (dia.exec() == QDialog::Accepted) |
578 | fileName = dia.selectedFile(); |
||
579 | else |
||
580 | return; |
||
13874 | fschmid | 581 | if (loadedModel) |
582 | { |
||
583 | if (rootnode->getNumChildren() > 1) |
||
584 | { |
||
585 | rootnode->removeChild(decorator); |
||
586 | decorator->removeChild(usedModel2); |
||
587 | } |
||
588 | rootnode->removeChild(usedModel); |
||
589 | drawingarea->setSceneData(0); |
||
590 | loadedModel = 0; |
||
591 | usedModel = 0; |
||
592 | disconnect(drawingarea, SIGNAL(mouseMoved()), this, SLOT(reportCamera())); |
||
593 | disconnect(viewCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeView(QString))); |
||
594 | disconnect(buttonAddView, SIGNAL(clicked()), this, SLOT(addView())); |
||
595 | disconnect(buttonRemoveView, SIGNAL(clicked()), this, SLOT(removeView())); |
||
596 | disconnect(buttonACcolor, SIGNAL(clicked()), this, SLOT(changeACcolor())); |
||
597 | disconnect(buttonFCcolor, SIGNAL(clicked()), this, SLOT(changeFCcolor())); |
||
17420 | fschmid | 598 | disconnect(transpSpin, SIGNAL(valueChanged(double)), this, SLOT(changeTransparency(double))); |
13874 | fschmid | 599 | } |
13839 | fschmid | 600 | if (!fileName.isEmpty()) |
601 | { |
||
602 | loadedModel = osgDB::readNodeFile ( fileName.toStdString() ); |
||
603 | if (loadedModel) |
||
604 | { |
||
605 | osgUtil::Optimizer optimzer; |
||
606 | optimzer.optimize(loadedModel); |
||
607 | modelFile = fileName; |
||
13874 | fschmid | 608 | default_stateset = loadedModel->getOrCreateStateSet(); |
609 | usedModel = dynamic_cast<osg::Node*>(loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL)); |
||
610 | usedModel2 = dynamic_cast<osg::Node*>(loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL)); |
||
13839 | fschmid | 611 | drawingarea->setCameraManipulator ( new osgGA::TrackballManipulator ); |
13874 | fschmid | 612 | rootnode->setStateSet(default_stateset); |
613 | rootnode->addChild(usedModel); |
||
614 | drawingarea->setSceneData(rootnode); |
||
13839 | fschmid | 615 | reportCamera(); |
616 | groupBox_5->setEnabled(true); |
||
617 | buttonRemoveView->setEnabled(true); |
||
618 | buttonAddView->setEnabled(true); |
||
619 | clearButton->setEnabled(true); |
||
13874 | fschmid | 620 | changeRenderMode(static_cast<int>(currentView.rendermode)); |
13839 | fschmid | 621 | connect(drawingarea, SIGNAL(mouseMoved()), this, SLOT(reportCamera())); |
622 | connect(viewCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeView(QString))); |
||
623 | connect(buttonAddView, SIGNAL(clicked()), this, SLOT(addView())); |
||
624 | connect(buttonRemoveView, SIGNAL(clicked()), this, SLOT(removeView())); |
||
13874 | fschmid | 625 | connect(buttonACcolor, SIGNAL(clicked()), this, SLOT(changeACcolor())); |
626 | connect(buttonFCcolor, SIGNAL(clicked()), this, SLOT(changeFCcolor())); |
||
17420 | fschmid | 627 | connect(transpSpin, SIGNAL(valueChanged(double)), this, SLOT(changeTransparency(double))); |
13839 | fschmid | 628 | } |
629 | dirs->set("models", fileName.left(fileName.lastIndexOf("/"))); |
||
630 | } |
||
631 | } |
||
632 | |||
633 | void OSGEditorDialog::accept() |
||
634 | { |
||
635 | if (loadedModel) |
||
636 | { |
||
637 | currItem->loadedModel = loadedModel; |
||
638 | osg::Vec3d vecEye, vecCenter, vecUp; |
||
639 | drawingarea->getCamera()->getViewMatrixAsLookAt(vecEye, vecCenter, vecUp); |
||
640 | currentView.cameraPosition = vecEye; |
||
641 | currentView.cameraUp = vecUp; |
||
642 | osgGA::TrackballManipulator *trb = dynamic_cast<osgGA::TrackballManipulator*>(drawingarea->getCameraManipulator()); |
||
643 | currentView.trackerCenter = trb->getCenter(); |
||
644 | currentView.trackerMatrix = trb->getMatrix(); |
||
645 | currentView.trackerDist = trb->getDistance(); |
||
646 | currentView.trackerSize = trb->getTrackballSize(); |
||
647 | double fovy, aspectRatio, zNear, zFar; |
||
648 | drawingarea->getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar); |
||
649 | currentView.angleFOV = fovy; |
||
650 | currentView.illumination = static_cast<PageItem_OSGFrame::LightType>(lightStyleCombo->currentIndex()); |
||
651 | currentView.rendermode = static_cast<PageItem_OSGFrame::RenderType>(renderStyleCombo->currentIndex()); |
||
652 | currItem->modelFile = modelFile; |
||
653 | QImage image = drawingarea->grabFrameBuffer(); |
||
654 | currItem->setImage(image); |
||
655 | viewMap[currentViewName] = currentView; |
||
656 | currItem->viewMap = viewMap; |
||
657 | currItem->currentView = currentViewName; |
||
658 | } |
||
659 | else |
||
660 | { |
||
661 | currItem->clearContents(); |
||
662 | } |
||
663 | QDialog::accept(); |
||
664 | } |