paysages3d/src/interface/modeler/quickapp/OpenGLView.cpp

93 lines
1.9 KiB
C++
Raw Normal View History

#include "OpenGLView.h"
#include <QQuickWindow>
2014-08-27 16:19:48 +00:00
#include <QHoverEvent>
2014-08-28 08:29:12 +00:00
#include "MainModelerWindow.h"
#include "CameraDefinition.h"
#include "OpenGLRenderer.h"
#include "Scenery.h"
OpenGLView::OpenGLView(QQuickItem *parent) :
QQuickItem(parent)
{
initialized = false;
2014-08-27 16:19:48 +00:00
window = NULL;
2014-08-28 08:29:12 +00:00
renderer = NULL;
2014-08-27 16:19:48 +00:00
setAcceptedMouseButtons(Qt::AllButtons);
setAcceptHoverEvents(true);
setKeepMouseGrab(true);
2014-08-27 16:19:48 +00:00
connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
startTimer(250);
}
void OpenGLView::handleWindowChanged(QQuickWindow *win)
{
if (win)
{
2014-08-28 08:29:12 +00:00
window = qobject_cast<MainModelerWindow *>(win);
if (window)
{
renderer = window->getRenderer();
2014-08-27 16:19:48 +00:00
2014-08-28 08:29:12 +00:00
connect(win, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
2014-08-28 08:29:12 +00:00
win->setClearBeforeRendering(false);
2014-08-28 08:29:12 +00:00
initialized = false;
}
}
}
void OpenGLView::paint()
{
if (not initialized)
{
2014-08-28 08:29:12 +00:00
renderer->initialize();
initialized = true;
}
2014-08-28 08:29:12 +00:00
renderer->resize(width(), height());
renderer->prepareOpenGLState();
renderer->paint();
2014-08-27 16:19:48 +00:00
if (window)
{
window->resetOpenGLState();
}
}
void OpenGLView::hoverMoveEvent(QHoverEvent *event)
{
2014-08-28 08:29:12 +00:00
if (!renderer)
{
return;
}
2014-08-27 16:19:48 +00:00
CameraDefinition camera;
2014-08-28 08:29:12 +00:00
renderer->getScenery()->getCamera(&camera);
2014-08-27 16:19:48 +00:00
QPointF diff = event->posF() - event->oldPosF();
camera.strafeRight(diff.x() * 0.1);
camera.strafeUp(diff.y() * 0.1);
camera.validate();
2014-08-28 08:29:12 +00:00
camera.copy(renderer->render_camera);
renderer->getScenery()->setCamera(&camera);
renderer->getScenery()->getCamera(&camera);
renderer->cameraChangeEvent(&camera);
2014-08-27 16:19:48 +00:00
if (window)
{
window->update();
}
}
void OpenGLView::timerEvent(QTimerEvent *)
{
if (window)
{
window->update();
}
}