| 1 | /* |
| 2 | Copyright 2014 Jan Adamec, Michalis Kamburelis. |
| 3 | |
| 4 | This file is part of "Castle Game Engine". |
| 5 | |
| 6 | "Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 | included in this distribution, for details about the copyright. |
| 8 | |
| 9 | "Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | |
| 13 | ---------------------------------------------------------------------------- |
| 14 | */ |
| 15 | |
| 16 | #include "mainwindow.h" |
| 17 | #include "ui_mainwindow.h" |
| 18 | #include "glwidget.h" |
| 19 | |
| 20 | #include <QDialog> |
| 21 | #include <QFileDialog> |
| 22 | #include <QPlainTextEdit> |
| 23 | #include <QVBoxLayout> |
| 24 | |
| 25 | #include "../../../src/deprecated_library/castleengine.h" |
| 26 | |
| 27 | MainWindow::MainWindow(QWidget *parent) : |
| 28 | QMainWindow(parent), |
| 29 | ui(new Ui::MainWindow) |
| 30 | { |
| 31 | ui->setupUi(this); |
| 32 | |
| 33 | m_nViewpointCount = m_iCurrentViewpoint = 0; |
| 34 | m_pConsoleWnd = NULL; |
| 35 | |
| 36 | CGE_LoadLibrary(); |
| 37 | |
| 38 | QSurfaceFormat aFormat; |
| 39 | aFormat.setSamples(4); |
| 40 | |
| 41 | m_pGlWidget = new GLWidget(aFormat, this); // init with multisampling |
| 42 | setCentralWidget(m_pGlWidget); |
| 43 | |
| 44 | connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(OnFileOpenClick())); |
| 45 | connect(ui->actionWalk, SIGNAL(triggered()), this, SLOT(OnWalkClick())); |
| 46 | connect(ui->actionFly, SIGNAL(triggered()), this, SLOT(OnFlyClick())); |
| 47 | connect(ui->actionExamine, SIGNAL(triggered()), this, SLOT(OnExamineClick())); |
| 48 | connect(ui->actionTurntable, SIGNAL(triggered()), this, SLOT(OnTurntableClick())); |
| 49 | connect(ui->actionNextView, SIGNAL(triggered()), this, SLOT(OnNextViewClick())); |
| 50 | connect(ui->actionPrevView, SIGNAL(triggered()), this, SLOT(OnPrevViewClick())); |
| 51 | |
| 52 | connect(ui->actionSSAO, SIGNAL(triggered()), this, SLOT(MenuSoftShadowsClick())); |
| 53 | connect(ui->actionHead_Bobbing, SIGNAL(triggered()), this, SLOT(MenuWalkingEffectClick())); |
| 54 | connect(ui->actionMouse_Look, SIGNAL(triggered()), this, SLOT(MenuMouseLookClick())); |
| 55 | connect(ui->actionMultiSampling, SIGNAL(triggered()), this, SLOT(MenuAntiAliasingClick())); |
| 56 | connect(ui->actionOpenGL_Information, SIGNAL(triggered()), this, SLOT(MenuOpenGLInfoClick())); |
| 57 | connect(ui->actionShow_Warnings, SIGNAL(triggered()), this, SLOT(MenuShowWarningClick())); |
| 58 | |
| 59 | ui->actionMultiSampling->setChecked(m_pGlWidget->format().samples()>1); |
| 60 | |
| 61 | // Use this to load some default scene |
| 62 | //m_pGlWidget->OpenScene("../../../../demo-models/navigation/type_walk.wrl"); |
| 63 | } |
| 64 | |
| 65 | MainWindow::~MainWindow() |
| 66 | { |
| 67 | CGE_Finalize(); |
| 68 | delete ui; |
| 69 | } |
| 70 | |
| 71 | void MainWindow::OnFileOpenClick() |
| 72 | { |
| 73 | QString sFile = QFileDialog::getOpenFileName(this, tr("Open Scene"), ".", tr("3D scenes") + |
| 74 | " (*.wrl *.wrl.gz *.wrz *.x3d *.x3dz *.x3d.gz *.x3dv *.x3dvz *.x3dv.gz *.kanim *.castle-anim-frames *.dae *.iv *.3ds *.md3 *.obj *.geo *.json *.stl *.gltf *.glb)"); |
| 75 | if (!sFile.isEmpty()) |
| 76 | m_pGlWidget->OpenScene(sFile); |
| 77 | } |
| 78 | |
| 79 | void MainWindow::UpdateNavigationButtons() |
| 80 | { |
| 81 | ECgeNavigationType eNav = (ECgeNavigationType)CGE_GetNavigationType(); |
| 82 | ui->actionWalk->setChecked(eNav == ecgenavWalk); |
| 83 | ui->actionFly->setChecked(eNav == ecgenavFly); |
| 84 | ui->actionExamine->setChecked(eNav == ecgenavExamine); |
| 85 | ui->actionTurntable->setChecked(eNav == ecgenavTurntable); |
| 86 | } |
| 87 | |
| 88 | void MainWindow::OnWalkClick() |
| 89 | { |
| 90 | CGE_SetNavigationType(ecgenavWalk); |
| 91 | } |
| 92 | |
| 93 | void MainWindow::OnFlyClick() |
| 94 | { |
| 95 | CGE_SetNavigationType(ecgenavFly); |
| 96 | } |
| 97 | |
| 98 | void MainWindow::OnExamineClick() |
| 99 | { |
| 100 | CGE_SetNavigationType(ecgenavExamine); |
| 101 | } |
| 102 | |
| 103 | void MainWindow::OnTurntableClick() |
| 104 | { |
| 105 | CGE_SetNavigationType(ecgenavTurntable); |
| 106 | } |
| 107 | |
| 108 | void MainWindow::UpdateAfterSceneLoaded() |
| 109 | { |
| 110 | ui->menuViewpoints->clear(); |
| 111 | // show viewpoints available |
| 112 | int nCount = CGE_GetViewpointsCount(); |
| 113 | for (int i = 0; i < nCount; i++) |
| 114 | { |
| 115 | char sName[512]; |
| 116 | CGE_GetViewpointName(i, sName, 512); |
| 117 | ActionWithTag *pAct = new ActionWithTag(QString::fromUtf8(sName), i, ui->menuViewpoints); |
| 118 | connect(pAct, SIGNAL(triggered()), this, SLOT(OnMoveToViewpointClick())); |
| 119 | ui->menuViewpoints->addAction(pAct); |
| 120 | } |
| 121 | m_iCurrentViewpoint = 0; |
| 122 | m_nViewpointCount = nCount; |
| 123 | |
| 124 | ui->actionHead_Bobbing->setChecked(CGE_GetVariableInt(ecgevarWalkHeadBobbing)>0); |
| 125 | ui->actionHeadlight->setChecked(CGE_GetVariableInt(ecgevarHeadlight)>0); |
| 126 | ui->actionSSAO->setChecked(CGE_GetVariableInt(ecgevarEffectSSAO)>0); |
| 127 | |
| 128 | if (m_aNavKeeper.ApplyState()) // when scene loading was caused by reloading (changing multisampling, etc) |
| 129 | UpdateNavigationButtons(); |
| 130 | } |
| 131 | |
| 132 | void MainWindow::OnMoveToViewpointClick() |
| 133 | { |
| 134 | ActionWithTag *pAct = qobject_cast<ActionWithTag*>(sender()); |
| 135 | if (pAct == NULL) return; |
| 136 | MoveToViewpoint(pAct->m_nTag); |
| 137 | } |
| 138 | |
| 139 | void MainWindow::OnNextViewClick() |
| 140 | { |
| 141 | MoveToViewpoint(m_iCurrentViewpoint+1); |
| 142 | } |
| 143 | |
| 144 | void MainWindow::OnPrevViewClick() |
| 145 | { |
| 146 | MoveToViewpoint(m_iCurrentViewpoint-1); |
| 147 | } |
| 148 | |
| 149 | void MainWindow::MoveToViewpoint(int nView) |
| 150 | { |
| 151 | if (nView < 0) nView = m_nViewpointCount-1; // for cycling |
| 152 | if (nView > m_nViewpointCount) nView = 0; |
| 153 | m_iCurrentViewpoint = nView; |
| 154 | CGE_MoveToViewpoint(m_iCurrentViewpoint, true); |
| 155 | } |
| 156 | |
| 157 | ActionWithTag::ActionWithTag(QString const& sCaption, int nTag, QObject * parent) |
| 158 | : QAction(parent), m_nTag(nTag) |
| 159 | { |
| 160 | setText(sCaption); |
| 161 | } |
| 162 | |
| 163 | void MainWindow::MenuSoftShadowsClick() |
| 164 | { |
| 165 | bool bSwitchOn = ui->actionSSAO->isChecked(); |
| 166 | CGE_SetVariableInt(ecgevarEffectSSAO, bSwitchOn ? 1 : 0); |
| 167 | } |
| 168 | |
| 169 | void MainWindow::MenuAntiAliasingClick() |
| 170 | { |
| 171 | // keep camera position |
| 172 | m_aNavKeeper.SaveState(); |
| 173 | |
| 174 | QString sScene = m_pGlWidget->m_sSceneToOpen; |
| 175 | takeCentralWidget(); |
| 176 | delete m_pGlWidget; |
| 177 | m_pGlWidget = NULL; |
| 178 | |
| 179 | QSurfaceFormat aFormat; |
| 180 | aFormat.setSamples(ui->actionMultiSampling->isChecked() ? 4 : 0); |
| 181 | |
| 182 | m_pGlWidget = new GLWidget(aFormat, this); // init with multisampling |
| 183 | setCentralWidget(m_pGlWidget); |
| 184 | m_pGlWidget->OpenScene(sScene); |
| 185 | } |
| 186 | |
| 187 | void MainWindow::MenuWalkingEffectClick() |
| 188 | { |
| 189 | bool bSwitchOn = ui->actionHead_Bobbing->isChecked(); |
| 190 | CGE_SetVariableInt(ecgevarWalkHeadBobbing, bSwitchOn ? 1 : 0); |
| 191 | } |
| 192 | |
| 193 | void MainWindow::MenuMouseLookClick() |
| 194 | { |
| 195 | CGE_SetVariableInt(ecgevarMouseLook, 1); |
| 196 | CGE_SetVariableInt(ecgevarCrossHair, 1); |
| 197 | } |
| 198 | |
| 199 | void MainWindow::on_actionHeadlight_triggered() |
| 200 | { |
| 201 | bool bSwitchOn = ui->actionHeadlight->isChecked(); |
| 202 | CGE_SetVariableInt(ecgevarHeadlight, bSwitchOn ? 1 : 0); |
| 203 | } |
| 204 | |
| 205 | void MainWindow::AddNewWarning(QString const& sWarning) |
| 206 | { |
| 207 | if (m_pConsoleWnd==NULL) |
| 208 | MenuShowWarningClick(); |
| 209 | QPlainTextEdit *pEdit = qobject_cast<QPlainTextEdit*>(m_pConsoleWnd->layout()->itemAt(0)->widget()); |
| 210 | if (pEdit!=NULL) |
| 211 | pEdit->appendPlainText(sWarning); |
| 212 | } |
| 213 | |
| 214 | void MainWindow::MenuShowWarningClick() |
| 215 | { |
| 216 | if (m_pConsoleWnd==NULL) |
| 217 | { |
| 218 | m_pConsoleWnd = new QDialog(this); |
| 219 | m_pConsoleWnd->setWindowTitle(tr("Log - Warnings")); |
| 220 | m_pConsoleWnd->setWindowFlags(m_pConsoleWnd->windowFlags() & ~Qt::WindowContextHelpButtonHint); |
| 221 | |
| 222 | QPlainTextEdit *pEdit = new QPlainTextEdit(m_pConsoleWnd); |
| 223 | pEdit->setMinimumSize(600, 500); |
| 224 | pEdit->setReadOnly(true); |
| 225 | |
| 226 | QVBoxLayout *pLayout = new QVBoxLayout(m_pConsoleWnd); |
| 227 | pLayout->setContentsMargins(0, 0, 0, 0); |
| 228 | m_pConsoleWnd->setLayout(pLayout); |
| 229 | pLayout->insertWidget(0, pEdit); |
| 230 | m_pConsoleWnd->resize(m_pConsoleWnd->minimumSize()); |
| 231 | } |
| 232 | m_pConsoleWnd->show(); |
| 233 | } |
| 234 | |
| 235 | void MainWindow::MenuOpenGLInfoClick() |
| 236 | { |
| 237 | char szBuf[8192]; |
| 238 | CGE_GetOpenGLInformation(szBuf, 8192); |
| 239 | |
| 240 | QDialog aDlg(this); |
| 241 | aDlg.setWindowTitle(tr("OpenGL Information")); |
| 242 | aDlg.setWindowFlags(aDlg.windowFlags() & ~Qt::WindowContextHelpButtonHint); |
| 243 | |
| 244 | QPlainTextEdit *pEdit = new QPlainTextEdit(&aDlg); |
| 245 | pEdit->setPlainText(QString::fromUtf8(szBuf)); |
| 246 | pEdit->setMinimumSize(500, 500); |
| 247 | pEdit->setReadOnly(true); |
| 248 | |
| 249 | QVBoxLayout *pLayout = new QVBoxLayout(&aDlg); |
| 250 | pLayout->setContentsMargins(0, 0, 0, 0); |
| 251 | aDlg.setLayout(pLayout); |
| 252 | pLayout->insertWidget(0, pEdit); |
| 253 | aDlg.resize(aDlg.minimumSize()); |
| 254 | aDlg.exec(); |
| 255 | } |
| 256 | |
| 257 | void MainWindow::on_actionSave_Screenshot_triggered() |
| 258 | { |
| 259 | QString sFile = QFileDialog::getSaveFileName(this, "Save as image", "CGE-Screenshot.jpg", "JPEG (*.jpg)"); |
| 260 | if (sFile.isEmpty()) return; |
| 261 | |
| 262 | CGE_SaveScreenshotToFile(sFile.toUtf8()); // TODO: this filename string conversion is not perfect: should be in filesystem representation, not utf8 |
| 263 | } |
| 264 | |
| 265 | NavKeeper::NavKeeper() |
| 266 | { |
| 267 | bToBeApplied = false; |
| 268 | } |
| 269 | |
| 270 | void NavKeeper::SaveState() |
| 271 | { |
| 272 | CGE_GetViewCoords(&fPosX, &fPosY, &fPosZ, &fDirX, &fDirY, &fDirZ, &fUpX, &fUpY, &fUpZ, &fGravX, &fGravY, &fGravZ); |
| 273 | eNavType = CGE_GetNavigationType(); |
| 274 | bToBeApplied = true; |
| 275 | } |
| 276 | |
| 277 | bool NavKeeper::ApplyState() |
| 278 | { |
| 279 | if (!bToBeApplied) return false; |
| 280 | |
| 281 | CGE_MoveViewToCoords(fPosX, fPosY, fPosZ, fDirX, fDirY, fDirZ, fUpX, fUpY, fUpZ, fGravX, fGravY, fGravZ, false); |
| 282 | CGE_SetNavigationType(eNavType); |
| 283 | |
| 284 | bToBeApplied = false; |
| 285 | return true; |
| 286 | } |
| 287 |