/* Stanley Designer Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "qglview_window.h" #include "renderer.h" #include "glwidget.h" #include "gltexture_manager.h" #include #include #include #include "qad_types.h" QGLViewWindow::QGLViewWindow(QWidget * parent): QMainWindow(parent), Ui::QGLViewWindow() { setupUi(this); session.setFile("session_qglview_test.conf"); session.addEntry(this); //view->view()->camera()->setPos(QVector3D(2, 2, 2)); //view->view()->camera()->setAim(QVector3D()); //view->view()->camera()->flyToDistance(2.); // view->setFrameShape(QFrame::NoFrame); view->view()->setMouseRotateEnabled(true); view->view()->setMouseSelectionEnabled(true); view->view()->setSelectionHaloEnabled(true); view->view()->setHoverHaloEnabled(true); view->view()->setBackColor(Qt::lightGray); view->view()->setDepthStart(0.1); view->view()->setSelectionMode(Scene::smMultiSelection); spinFOV->setValue(view->view()->FOV()); spinDepthStart->setValue(view->view()->depthStart()); groupHoverHalo->setChecked(view->view()->isHoverHaloEnabled()); groupSelectionHalo->setChecked(view->view()->isSelectionHaloEnabled()); spinHoverHaloFill->setValue(view->view()->hoverHaloFillAlpha()); spinSelectionHaloFill->setValue(view->view()->selectionHaloFillAlpha()); colorHoverHalo->setColor(view->view()->hoverHaloColor()); colorSelectionHalo->setColor(view->view()->selectionHaloColor()); checkFXAA->setChecked(view->view()->isFeatureEnabled(QGLView::qglFXAA)); colorBack->setColor(view->view()->backColor()); colorAmbient->setColor(view->view()->ambientColor()); checkCameraOrbit->setChecked(view->view()->isCameraOrbit()); groupShadows->setChecked(view->view()->isFeatureEnabled(QGLView::qglShadowsEnabled)); groupEyeAccomodation->setChecked(view->view()->isFeatureEnabled(QGLView::qglEyeAccomodationEnabled)); groupBloom->setChecked(view->view()->isFeatureEnabled(QGLView::qglBloomEnabled)); groupMotionBlur->setChecked(view->view()->isFeatureEnabled(QGLView::qglMotionBlurEnabled)); groupReflections->setChecked(view->view()->isFeatureEnabled(QGLView::qglReflectionsEnabled)); checkSoftShadows->setChecked(view->view()->isFeatureEnabled(QGLView::qglShadowsSoftEnabled)); groupSSAO->setChecked(view->view()->isFeatureEnabled(QGLView::qglSSAOEnabled)); spinAccom->setValue(view->view()->feature(QGLView::qglEyeAccomodationTime).toDouble()); spinAccomMS->setValue(view->view()->feature(QGLView::qglEyeAccomodationMaxSpeed).toDouble()); checkReflectionsBlur->setChecked(view->view()->isFeatureEnabled(QGLView::qglReflectionsBlur)); spinShadowmapSize->setValue(view->view()->feature(QGLView::qglShadowsMapSize).toInt()); spinMotionBlurFactor->setValue(view->view()->feature(QGLView::qglMotionBlurFactor).toDouble()); spinMotionBlurSteps->setValue(view->view()->feature(QGLView::qglMotionBlurSteps).toInt()); spinBloomFactor->setValue(view->view()->feature(QGLView::qglBloomFactor).toDouble()); spinBloomRadius->setValue(view->view()->feature(QGLView::qglBloomRadius).toInt()); spinBloomThreshold->setValue(view->view()->feature(QGLView::qglBloomThreshold).toDouble()); spinSSAORadius->setValue(view->view()->feature(QGLView::qglSSAORadius).toInt()); groupDOF->setChecked(view->view()->isFeatureEnabled(QGLView::qglDepthOfFieldEnabled)); checkDOFAutoFocus->setChecked(view->view()->isFeatureEnabled(QGLView::qglDepthOfFieldAutoFocusEnabled)); spinDOFFocus->setValue(view->view()->feature(QGLView::qglDepthOfFieldFocus).toDouble()); spinDOFDiaphragm->setValue(view->view()->feature(QGLView::qglDepthOfFieldDiaphragm).toDouble()); spinDOFSpeed->setValue(view->view()->feature(QGLView::qglDepthOfFieldAutoFocusSpeed).toDouble()); view->view()->start(-1); startTimer(1000/60); connect(view->view(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged())); connect(view->view(), SIGNAL(keyEvent(Qt::Key, Qt::KeyboardModifiers)), this, SLOT(view_keyEvent(Qt::Key, Qt::KeyboardModifiers))); //connect(matEditor, SIGNAL(changed()), this, SLOT(materialChanged())); sceneTree->assignQGLView(view->view()); matEditor->assignQGLView(view->view()); objectEditor->assignQGLView(view->view()); primitiveEditor->assignQGLView(view->view()); session.load(); //loadFile("axis.DAE.qgl"); //matEditor->setMaterial(const_cast(view->view()->scene()->rootObject()->child(0))->material()); /*Scene * sc = loadScene("truck.obj"); //view->view()->scene()->addScene(sc); sc->rootObject()->moveY(-8); for (int i = 0; i < 7; ++i) { sc->rootObject()->moveY(2); view->view()->scene()->addScene(sc); } //view->view()->scene()->dump(); delete sc;*/ } QGLViewWindow::~QGLViewWindow() { session.save(); //delete ps; } void QGLViewWindow::changeEvent(QEvent * e) { QMainWindow::changeEvent(e); if (e->type() == QEvent::LanguageChange) { retranslateUi(this); return; } } void QGLViewWindow::timerEvent(QTimerEvent * ) { //static double t = 0.; //((RendererSimple*)(view->view()->renderer()))->mpos = view->view()->mapFromGlobal(QCursor::pos()); statusBar()->showMessage(QString("FPS: %1").arg(QString::number(view->view()->currentFPS(), 'f', 2))); } void QGLViewWindow::loadFile(const QString & path, bool import) { prev_path = path; QApplication::setOverrideCursor(Qt::WaitCursor); QFileInfo fi(path); Scene * s = nullptr; if (fi.suffix().toLower() == "qgl") s = loadFromQGLFile(path); else s = loadScene(path); QApplication::restoreOverrideCursor(); if (!s) { QMessageBox::critical(this, "Import", "Can`t load " + path + "!"); return; } s->setName(fi.baseName()); if (import) view->scene()->addScene(s); else { TextureManager::clearSearchPathes(); view->scene()->assignFrom(s); view->view()->focusOn(view->scene()->boundingBox()); } TextureManager::addSearchPath(fi.absoluteDir().path()); delete s; } void QGLViewWindow::selectionChanged() { ObjectBase * sel_obj = view->view()->selectedObject(); labelName->setText(sel_obj ? sel_obj->name() : ""); /**if (cur == 0) box->hide(); else { box->setScale(cur->boundingBox().size()); box->setPos(cur->boundingBox().pos()); Box3D b = cur->boundingBox().movedTo(-cur->boundingBox().center()); b.z = -b.z - b.height; ps->setEmitterRect(b); cur->addChild(box); box->show(); }*/ //qDebug() << sel_obj->boundingBox(); } void QGLViewWindow::on_actionReset_triggered() { ///view->view()->removeObject(axis, false); view->view()->scene()->destroy(); ///view->view()->addObject(axis); } void QGLViewWindow::on_actionImport_triggered() { QStringList fl = QFileDialog::getOpenFileNames(this, "Select files", prev_path, "Supported types(*.qgl *.ase *.3ds *.obj *.dae);;" "QGLView(*.qgl);;" "Ascii Scene Export(*.ase);;" "3D Studio(*.3ds);;" "Wavefront OBJ(*.obj);;" "Collada(*.dae)"); if (fl.isEmpty()) return; prev_path = fl.back(); foreach (QString f, fl) loadFile(f, true); } void QGLViewWindow::on_actionSave_triggered() { QString f = QFileDialog::getSaveFileName(this, "Select file", prev_path, "QGLView(*.qgl)"); if (f.isEmpty()) return; if (f.right(4).toLower() != ".qgl") f += ".qgl"; prev_path = f; QApplication::setOverrideCursor(Qt::WaitCursor); saveToQGLFile(f, view->scene()); QApplication::restoreOverrideCursor(); } void QGLViewWindow::on_actionSaveSelected_triggered() { ObjectBase * sel_obj = view->view()->selectedObject(); if (!sel_obj) return; QString f = QFileDialog::getSaveFileName(this, "Select file", prev_path, "QGLView(*.qgl)"); if (f.isEmpty()) return; if (f.right(4).toLower() != ".qgl") f += ".qgl"; prev_path = f; QApplication::setOverrideCursor(Qt::WaitCursor); ///saveToQGLFile(f, sel_obj); QApplication::restoreOverrideCursor(); } void QGLViewWindow::on_actionOpen_triggered() { QString f = QFileDialog::getOpenFileName(this, "Select file", prev_path, "Supported types(*.qgl *.ase *.3ds *.obj *.dae);;" "QGLView(*.qgl);;" "Ascii Scene Export(*.ase);;" "3D Studio(*.3ds);;" "Wavefront OBJ(*.obj);;" "Collada(*.dae)"); if (f.isEmpty()) return; prev_path = f; loadFile(f); } void QGLViewWindow::view_keyEvent(Qt::Key k, Qt::KeyboardModifiers m) { //qDebug() << k; double spd = 0.2; if (m.testFlag(Qt::ShiftModifier)) spd = 0.5; switch (k) { case Qt::Key_W: view->view()->camera()->moveForward(spd); break; case Qt::Key_S: view->view()->camera()->moveBackward(spd); break; case Qt::Key_A: view->view()->camera()->moveLeft(spd); break; case Qt::Key_D: view->view()->camera()->moveRight(spd); break; default: break; } } \ void QGLViewWindow::on_pushButton_clicked() { //view->view()->removeLight(view->view()->lightsCount() - 1); //setWindowTitle(QString::number(view->view()->lightsCount())); //QVector3D wp = view->view()->light(0)->worldPos(); //view->view()->camera()->setPos(wp); //view->view()->camera()->setAim(wp + (view->view()->light(0)->worldTransform() * QVector4D(view->view()->light(0)->direction)).toVector3D()*100); } void QGLViewWindow::on_pushButton_3_clicked() { /*ObjectBaseList ol = view->view()->objects(true); qDebug() << ol.size(); foreach (ObjectBase * i, ol) { //i->VBO().rebuffer(); }*/ } void QGLViewWindow::on_actionAdd_plane_triggered() { ObjectBase * o = new ObjectBase(Primitive::plane()); o->setName("plane"); view->addObject(o->clone()); delete o; } void QGLViewWindow::on_actionAdd_cube_triggered() { ObjectBase * o = new ObjectBase(Primitive::cube()); o->setName("cube"); view->addObject(o->clone()); delete o; } void QGLViewWindow::on_actionAdd_sphere_triggered() { int seg_xy = 16, seg_z = 16; seg_xy = QInputDialog::getInt(this, "New sphere", "XY segments:", seg_xy, 1, 200); seg_z = QInputDialog::getInt(this, "New sphere", "Z segments:", seg_z , 1, 200); ObjectBase * o = new ObjectBase(Primitive::ellipsoid(seg_xy, seg_z)); o->setName("sphere"); view->addObject(o->clone()); delete o; } void QGLViewWindow::on_actionArrow_triggered(bool on) { actionArrow ->setChecked(true); actionMove ->setChecked(false); actionRotate->setChecked(false); actionScale ->setChecked(false); view->view()->setCurrentAction(RendererService::haNoAction); } void QGLViewWindow::on_actionMove_triggered(bool on) { actionArrow ->setChecked(false); actionMove ->setChecked(true); actionRotate->setChecked(false); actionScale ->setChecked(false); view->view()->setCurrentAction(RendererService::haMove); } void QGLViewWindow::on_actionRotate_triggered(bool on) { actionArrow ->setChecked(false); actionMove ->setChecked(false); actionRotate->setChecked(true); actionScale ->setChecked(false); view->view()->setCurrentAction(RendererService::haRotate); } void QGLViewWindow::on_actionScale_triggered(bool on) { actionArrow ->setChecked(false); actionMove ->setChecked(false); actionRotate->setChecked(false); actionScale ->setChecked(true); view->view()->setCurrentAction(RendererService::haScale); }