diff --git a/glwidget.h b/glwidget.h index 6cc739f..1f1c03c 100644 --- a/glwidget.h +++ b/glwidget.h @@ -72,7 +72,7 @@ public: public slots: void stop(); - void start(float freq = 60.0); + void start(float freq = 0.0); void setBackColor(const QColor & c); void setLineWidth(const qreal & arg); void setFOV(const qreal & arg); diff --git a/openglwindow.cpp b/openglwindow.cpp index 6228c0a..02dab2b 100644 --- a/openglwindow.cpp +++ b/openglwindow.cpp @@ -1,72 +1,26 @@ -/* - QGL OpenGLWindow - Ivan Pelipenko peri4ko@yandex.ru - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser 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 Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . -*/ - #include "openglwindow.h" #include #include #include #include +#include -OpenGLWindow::OpenGLWindow(QWindow *parent) - : QWindow(parent) - , m_context(nullptr) - , m_device(nullptr) -{ +OpenGLWindow::OpenGLWindow(QWindow *parent) : QWindow(parent) { setFlags(flags() | Qt::FramelessWindowHint); setSurfaceType(QWindow::OpenGLSurface); QSurfaceFormat format = QSurfaceFormat::defaultFormat(); -// qDebug() << format; #ifdef QT_OPENGL_ES_2 format.setRenderableType(QSurfaceFormat::OpenGLES); #else if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { - format.setVersion(4, 0); - format.setProfile(QSurfaceFormat::CoreProfile); + format.setVersion(2, 0); + format.setProfile(QSurfaceFormat::NoProfile); } #endif format.setDepthBufferSize(24); format.setSamples(8); setFormat(format); - QSurfaceFormat::setDefaultFormat(format); -} - - -OpenGLWindow::~OpenGLWindow() { - delete m_device; -} - - -void OpenGLWindow::render(QPainter *painter) { -} - - -void OpenGLWindow::initialize() { -} - - -void OpenGLWindow::render() { -// if (!m_device) m_device = new QOpenGLPaintDevice; -// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); -// m_device->setSize(size() * devicePixelRatio()); -// m_device->setDevicePixelRatio(devicePixelRatio()); -// QPainter painter(m_device); -// render(&painter); } @@ -77,10 +31,10 @@ void OpenGLWindow::renderLater() { bool OpenGLWindow::event(QEvent *event) { switch (event->type()) { - case QEvent::UpdateRequest: - renderNow(); + case QEvent::UpdateRequest: + renderNow(); return true; - default: + default: return QWindow::event(event); } } @@ -91,15 +45,54 @@ void OpenGLWindow::exposeEvent(QExposeEvent *event) { } +void OpenGLWindow::setVSync(bool on) { + QSurfaceFormat f = requestedFormat(); + if (on) { + if (f.swapInterval() != 1) { + f.setSwapInterval(1); + setFormat(f); + format_change = true; + } + } else { + if (f.swapInterval() != 0) { + f.setSwapInterval(0); + setFormat(f); + format_change = true; + } + } +} + + +bool OpenGLWindow::getVSync() const { + return (requestedFormat().swapInterval() == 1); +} + + +void OpenGLWindow::setSamples(int samples) { + QSurfaceFormat f = requestedFormat(); + if (f.samples() != samples) { + f.setSamples(samples); + setFormat(f); + format_change = true; + } +} + + +int OpenGLWindow::getSamples() const { + return requestedFormat().samples(); +} + + void OpenGLWindow::renderNow() { - if (!isExposed()) - return; + if (!isExposed()) return; bool needsInitialize = false; - if (!m_context) { + if (!m_context || format_change) { + if (m_context) delete m_context; m_context = new QOpenGLContext(this); m_context->setFormat(requestedFormat()); m_context->create(); needsInitialize = true; + format_change = false; } m_context->makeCurrent(this); if (needsInitialize) { @@ -108,5 +101,6 @@ void OpenGLWindow::renderNow() { } render(); m_context->swapBuffers(this); + frame_cnt++; } diff --git a/openglwindow.h b/openglwindow.h index 8ecfe8e..64bee41 100644 --- a/openglwindow.h +++ b/openglwindow.h @@ -1,21 +1,3 @@ -/* - QGL OpenGLWindow - Ivan Pelipenko peri4ko@yandex.ru - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser 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 Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . -*/ - #include #include @@ -24,17 +6,21 @@ class QOpenGLContext; class QOpenGLPaintDevice; -class OpenGLWindow: public QWindow, public QOpenGLExtraFunctions -{ +class OpenGLWindow : public QWindow, public QOpenGLExtraFunctions { Q_OBJECT public: explicit OpenGLWindow(QWindow *parent = nullptr); - ~OpenGLWindow(); + virtual ~OpenGLWindow() {} + + virtual void render() {} + virtual void initialize() {} - virtual void render(QPainter *painter); - virtual void render(); - virtual void initialize(); QOpenGLContext * context() {return m_context;} + void setVSync(bool on); + bool getVSync() const; + void setSamples(int samples); + int getSamples() const; + int getFrameCounter() const {return frame_cnt;} public slots: void renderLater(); @@ -45,7 +31,8 @@ protected: void exposeEvent(QExposeEvent *event) override; private: - QOpenGLContext *m_context; - QOpenGLPaintDevice *m_device; + QOpenGLContext * m_context = nullptr; + bool format_change = false; + int frame_cnt = 0; }; diff --git a/qglview.h b/qglview.h index 2ad9d31..4ffc97e 100644 --- a/qglview.h +++ b/qglview.h @@ -100,7 +100,7 @@ public: Q_ENUM(CameraLightMode) void stop(); - void start(float freq = 60.); + void start(float freq = 0.); QColor backColor() const {return backColor_;} float lineWidth() const {return lineWidth_;} diff --git a/widgets/view_editor.cpp b/widgets/view_editor.cpp index ab48597..b0bdeff 100644 --- a/widgets/view_editor.cpp +++ b/widgets/view_editor.cpp @@ -53,6 +53,8 @@ void ViewEditor::assignQGLView(QGLView * v) { ui->checkService->setChecked(view->isServiceMode()); ui->lineHDR->setProperty("GLpath", view->environmentMapFile()); ui->lineHDR->setText(QFileInfo(view->environmentMapFile()).fileName()); + ui->checkVSync->setChecked(view->getVSync()); + ui->spinSamples->setValue(view->getSamples()); active = true; } @@ -177,3 +179,14 @@ void ViewEditor::on_buttonHDRSelect_clicked() { ui->lineHDR->setProperty("GLpath", str); view->setEnvironmentMapFile(str); } + + +void ViewEditor::on_checkVSync_clicked(bool val) { + view->setVSync(val); +} + + +void ViewEditor::on_spinSamples_valueChanged(int arg1) { + view->setSamples(arg1); +} + diff --git a/widgets/view_editor.h b/widgets/view_editor.h index 405fa4b..0e91c2d 100644 --- a/widgets/view_editor.h +++ b/widgets/view_editor.h @@ -57,11 +57,10 @@ private slots: void on_checkCameraOrbit_clicked(bool val); void on_checkService_clicked(bool val); void on_checkCameraLight_stateChanged(int s); + void on_checkVSync_clicked(bool val); + void on_spinSamples_valueChanged(int arg1); void on_buttonHDRClear_clicked(); void on_buttonHDRSelect_clicked(); - -signals: - }; #endif // VIEW_EDITOR_H diff --git a/widgets/view_editor.ui b/widgets/view_editor.ui index 744b0a5..3097383 100644 --- a/widgets/view_editor.ui +++ b/widgets/view_editor.ui @@ -117,7 +117,7 @@ - + :/icons/edit-delete.png:/icons/edit-delete.png @@ -125,7 +125,7 @@ - + :/icons/document-open.png:/icons/document-open.png @@ -137,6 +137,26 @@ + + + + Camera light + + + true + + + + + + + Service mode + + + true + + + @@ -151,23 +171,29 @@ - - + + - Service mode + VSync true - - - - Camera light + + + + Samples: - - true + + -1 + + + 16 + + + -1 @@ -504,8 +530,8 @@ - - + +