From d909ff380f571c6ca26b0cc7d96932567859ad38 Mon Sep 17 00:00:00 2001 From: Michael Lemaire Date: Wed, 27 Aug 2014 18:19:48 +0200 Subject: [PATCH] Fixed opengl drawing in qtquick window --- Makefile | 3 + src/interface/modeler/quickapp/OpenGLView.cpp | 49 +++++++++++++-- src/interface/modeler/quickapp/OpenGLView.h | 5 ++ src/interface/modeler/quickapp/quickapp.pro | 2 + src/render/opengl/OpenGLRenderer.cpp | 61 +++++++++++-------- src/render/opengl/OpenGLRenderer.h | 3 + 6 files changed, 94 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index f3cb8b7..d3b49be 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,9 @@ endif run_cli:build LD_LIBRARY_PATH=$(LIBRARY_PATH) ${RUNNER} ${BUILDPATH}/interface/commandline/paysages-cli $(ARGS) +run_modeler:build + LD_LIBRARY_PATH=$(LIBRARY_PATH) ${RUNNER} ${BUILDPATH}/interface/modeler/quickapp/paysages-modeler $(ARGS) + run:build LD_LIBRARY_PATH=$(LIBRARY_PATH) ${RUNNER} ${BUILDPATH}/interface/desktop/paysages-gui $(ARGS) diff --git a/src/interface/modeler/quickapp/OpenGLView.cpp b/src/interface/modeler/quickapp/OpenGLView.cpp index 24dbac4..a9953df 100644 --- a/src/interface/modeler/quickapp/OpenGLView.cpp +++ b/src/interface/modeler/quickapp/OpenGLView.cpp @@ -1,10 +1,12 @@ #include "OpenGLView.h" #include +#include // TEMP #include "OpenGLRenderer.h" #include "Scenery.h" +#include "CameraDefinition.h" static OpenGLRenderer renderer; static Scenery scenery; @@ -12,19 +14,26 @@ OpenGLView::OpenGLView(QQuickItem *parent) : QQuickItem(parent) { initialized = false; + window = NULL; - connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*))); + setAcceptedMouseButtons(Qt::AllButtons); + setAcceptHoverEvents(true); + setKeepMouseGrab(true); scenery.autoPreset(); - renderer.setScenery(&scenery); + + connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*))); + startTimer(250); } void OpenGLView::handleWindowChanged(QQuickWindow *win) { if (win) { - connect(window(), SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection); + window = win; + + connect(window, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection); win->setClearBeforeRendering(false); @@ -41,7 +50,39 @@ void OpenGLView::paint() } renderer.resize(width(), height()); + renderer.prepareOpenGLState(); renderer.paint(); - window()->resetOpenGLState(); + if (window) + { + window->resetOpenGLState(); + } +} + +void OpenGLView::hoverMoveEvent(QHoverEvent *event) +{ + CameraDefinition camera; + renderer.getScenery()->getCamera(&camera); + + QPointF diff = event->posF() - event->oldPosF(); + camera.strafeRight(diff.x() * 0.1); + camera.strafeUp(diff.y() * 0.1); + camera.validate(); + + camera.copy(renderer.render_camera); + renderer.getScenery()->setCamera(&camera); + renderer.getScenery()->getCamera(&camera); + renderer.cameraChangeEvent(&camera); + if (window) + { + window->update(); + } +} + +void OpenGLView::timerEvent(QTimerEvent *) +{ + if (window) + { + window->update(); + } } diff --git a/src/interface/modeler/quickapp/OpenGLView.h b/src/interface/modeler/quickapp/OpenGLView.h index cdd41ae..5675818 100644 --- a/src/interface/modeler/quickapp/OpenGLView.h +++ b/src/interface/modeler/quickapp/OpenGLView.h @@ -18,8 +18,13 @@ public slots: void handleWindowChanged(QQuickWindow *win); void paint(); +protected: + virtual void hoverMoveEvent(QHoverEvent *event) override; + virtual void timerEvent(QTimerEvent *event) override; + private: bool initialized; + QQuickWindow *window; }; } diff --git a/src/interface/modeler/quickapp/quickapp.pro b/src/interface/modeler/quickapp/quickapp.pro index 2b227cf..0416b57 100644 --- a/src/interface/modeler/quickapp/quickapp.pro +++ b/src/interface/modeler/quickapp/quickapp.pro @@ -10,6 +10,8 @@ SOURCES += main.cpp \ RESOURCES += \ qml/app.qrc +TARGET = paysages-modeler + # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = ../extension diff --git a/src/render/opengl/OpenGLRenderer.cpp b/src/render/opengl/OpenGLRenderer.cpp index 8eb2424..1361795 100644 --- a/src/render/opengl/OpenGLRenderer.cpp +++ b/src/render/opengl/OpenGLRenderer.cpp @@ -16,6 +16,8 @@ OpenGLRenderer::OpenGLRenderer(Scenery* scenery): SoftwareRenderer(scenery) { ready = false; + vp_width = 1; + vp_height = 1; render_quality = 3; @@ -50,25 +52,7 @@ void OpenGLRenderer::initialize() if (ready) { - functions->glClearColor(0.0, 0.0, 0.0, 0.0); - - functions->glDisable(GL_LIGHTING); - - functions->glFrontFace(GL_CCW); - functions->glCullFace(GL_BACK); - functions->glEnable(GL_CULL_FACE); - - functions->glDepthFunc(GL_LESS); - functions->glDepthMask(1); - functions->glEnable(GL_DEPTH_TEST); - - functions->glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - functions->glEnable(GL_LINE_SMOOTH); - functions->glLineWidth(1.0); - - functions->glDisable(GL_FOG); - - functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + prepareOpenGLState(); prepare(); @@ -92,28 +76,55 @@ void OpenGLRenderer::initialize() } } -void OpenGLRenderer::resize(int width, int height) +void OpenGLRenderer::prepareOpenGLState() { if (ready) { - functions->glViewport(0, 0, width, height); + functions->glDisable(GL_LIGHTING); + + functions->glFrontFace(GL_CCW); + functions->glCullFace(GL_BACK); + functions->glEnable(GL_CULL_FACE); + + functions->glDepthFunc(GL_LESS); + functions->glDepthMask(1); + functions->glEnable(GL_DEPTH_TEST); + + functions->glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + functions->glEnable(GL_LINE_SMOOTH); + functions->glLineWidth(1.0); + + functions->glDisable(GL_FOG); + + functions->glEnable(GL_BLEND); + functions->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + functions->glClearColor(0.0, 0.0, 0.0, 0.0); + functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + functions->glViewport(0, 0, vp_width, vp_height); } +} + +void OpenGLRenderer::resize(int width, int height) +{ + vp_width = width; + vp_height = height; + getScenery()->getCamera()->setRenderSize(width, height); render_camera->setRenderSize(width, height); cameraChangeEvent(getScenery()->getCamera()); + + prepareOpenGLState(); } void OpenGLRenderer::paint() { if (ready) { - functions->glClearColor(0.0, 0.0, 0.0, 0.0); functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - functions->glEnable(GL_BLEND); - functions->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - skybox->render(); terrain->render(); water->render(); diff --git a/src/render/opengl/OpenGLRenderer.h b/src/render/opengl/OpenGLRenderer.h index d4daeab..5a3e90c 100644 --- a/src/render/opengl/OpenGLRenderer.h +++ b/src/render/opengl/OpenGLRenderer.h @@ -18,6 +18,7 @@ public: virtual ~OpenGLRenderer(); void initialize(); + void prepareOpenGLState(); void resize(int width, int height); void paint(); @@ -31,6 +32,8 @@ public: private: bool ready; + int vp_width; + int vp_height; OpenGLFunctions* functions; OpenGLSharedState* shared_state;