Fixed opengl drawing in qtquick window

This commit is contained in:
Michaël Lemaire 2014-08-27 18:19:48 +02:00
parent 3814f63ac0
commit d909ff380f
6 changed files with 94 additions and 29 deletions

View file

@ -45,6 +45,9 @@ endif
run_cli:build run_cli:build
LD_LIBRARY_PATH=$(LIBRARY_PATH) ${RUNNER} ${BUILDPATH}/interface/commandline/paysages-cli $(ARGS) 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 run:build
LD_LIBRARY_PATH=$(LIBRARY_PATH) ${RUNNER} ${BUILDPATH}/interface/desktop/paysages-gui $(ARGS) LD_LIBRARY_PATH=$(LIBRARY_PATH) ${RUNNER} ${BUILDPATH}/interface/desktop/paysages-gui $(ARGS)

View file

@ -1,10 +1,12 @@
#include "OpenGLView.h" #include "OpenGLView.h"
#include <QQuickWindow> #include <QQuickWindow>
#include <QHoverEvent>
// TEMP // TEMP
#include "OpenGLRenderer.h" #include "OpenGLRenderer.h"
#include "Scenery.h" #include "Scenery.h"
#include "CameraDefinition.h"
static OpenGLRenderer renderer; static OpenGLRenderer renderer;
static Scenery scenery; static Scenery scenery;
@ -12,19 +14,26 @@ OpenGLView::OpenGLView(QQuickItem *parent) :
QQuickItem(parent) QQuickItem(parent)
{ {
initialized = false; initialized = false;
window = NULL;
connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*))); setAcceptedMouseButtons(Qt::AllButtons);
setAcceptHoverEvents(true);
setKeepMouseGrab(true);
scenery.autoPreset(); scenery.autoPreset();
renderer.setScenery(&scenery); renderer.setScenery(&scenery);
connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
startTimer(250);
} }
void OpenGLView::handleWindowChanged(QQuickWindow *win) void OpenGLView::handleWindowChanged(QQuickWindow *win)
{ {
if (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); win->setClearBeforeRendering(false);
@ -41,7 +50,39 @@ void OpenGLView::paint()
} }
renderer.resize(width(), height()); renderer.resize(width(), height());
renderer.prepareOpenGLState();
renderer.paint(); 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();
}
} }

View file

@ -18,8 +18,13 @@ public slots:
void handleWindowChanged(QQuickWindow *win); void handleWindowChanged(QQuickWindow *win);
void paint(); void paint();
protected:
virtual void hoverMoveEvent(QHoverEvent *event) override;
virtual void timerEvent(QTimerEvent *event) override;
private: private:
bool initialized; bool initialized;
QQuickWindow *window;
}; };
} }

View file

@ -10,6 +10,8 @@ SOURCES += main.cpp \
RESOURCES += \ RESOURCES += \
qml/app.qrc qml/app.qrc
TARGET = paysages-modeler
# Additional import path used to resolve QML modules in Qt Creator's code model # Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH = ../extension QML_IMPORT_PATH = ../extension

View file

@ -16,6 +16,8 @@ OpenGLRenderer::OpenGLRenderer(Scenery* scenery):
SoftwareRenderer(scenery) SoftwareRenderer(scenery)
{ {
ready = false; ready = false;
vp_width = 1;
vp_height = 1;
render_quality = 3; render_quality = 3;
@ -50,25 +52,7 @@ void OpenGLRenderer::initialize()
if (ready) if (ready)
{ {
functions->glClearColor(0.0, 0.0, 0.0, 0.0); prepareOpenGLState();
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);
prepare(); prepare();
@ -92,28 +76,55 @@ void OpenGLRenderer::initialize()
} }
} }
void OpenGLRenderer::resize(int width, int height) void OpenGLRenderer::prepareOpenGLState()
{ {
if (ready) 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); getScenery()->getCamera()->setRenderSize(width, height);
render_camera->setRenderSize(width, height); render_camera->setRenderSize(width, height);
cameraChangeEvent(getScenery()->getCamera()); cameraChangeEvent(getScenery()->getCamera());
prepareOpenGLState();
} }
void OpenGLRenderer::paint() void OpenGLRenderer::paint()
{ {
if (ready) if (ready)
{ {
functions->glClearColor(0.0, 0.0, 0.0, 0.0);
functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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(); skybox->render();
terrain->render(); terrain->render();
water->render(); water->render();

View file

@ -18,6 +18,7 @@ public:
virtual ~OpenGLRenderer(); virtual ~OpenGLRenderer();
void initialize(); void initialize();
void prepareOpenGLState();
void resize(int width, int height); void resize(int width, int height);
void paint(); void paint();
@ -31,6 +32,8 @@ public:
private: private:
bool ready; bool ready;
int vp_width;
int vp_height;
OpenGLFunctions* functions; OpenGLFunctions* functions;
OpenGLSharedState* shared_state; OpenGLSharedState* shared_state;