2014-08-19 13:49:08 +00:00
|
|
|
#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"
|
2014-08-19 13:49:08 +00:00
|
|
|
#include "OpenGLRenderer.h"
|
2014-09-26 15:46:39 +00:00
|
|
|
#include "ModelerCameras.h"
|
2014-08-19 13:49:08 +00:00
|
|
|
|
|
|
|
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-19 13:49:08 +00:00
|
|
|
|
2014-08-27 16:19:48 +00:00
|
|
|
setAcceptedMouseButtons(Qt::AllButtons);
|
2014-09-26 15:46:39 +00:00
|
|
|
|
|
|
|
mouse_button = Qt::NoButton;
|
2014-08-19 13:49:08 +00:00
|
|
|
|
2014-08-27 16:19:48 +00:00
|
|
|
connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
|
2014-09-18 15:38:37 +00:00
|
|
|
startTimer(50);
|
2014-08-19 13:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-19 13:49:08 +00:00
|
|
|
|
2014-08-28 08:29:12 +00:00
|
|
|
win->setClearBeforeRendering(false);
|
2014-08-19 13:49:08 +00:00
|
|
|
|
2014-08-28 08:29:12 +00:00
|
|
|
initialized = false;
|
|
|
|
}
|
2014-08-19 13:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLView::paint()
|
|
|
|
{
|
2014-09-26 15:46:39 +00:00
|
|
|
if (not initialized or not renderer)
|
2014-08-19 13:49:08 +00:00
|
|
|
{
|
2014-08-28 08:29:12 +00:00
|
|
|
renderer->initialize();
|
2014-08-19 13:49:08 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2014-08-28 08:29:12 +00:00
|
|
|
renderer->resize(width(), height());
|
|
|
|
renderer->prepareOpenGLState();
|
|
|
|
renderer->paint();
|
2014-08-19 15:01:42 +00:00
|
|
|
|
2014-08-27 16:19:48 +00:00
|
|
|
if (window)
|
|
|
|
{
|
|
|
|
window->resetOpenGLState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:39 +00:00
|
|
|
void OpenGLView::wheelEvent(QWheelEvent *event)
|
2014-08-27 16:19:48 +00:00
|
|
|
{
|
2014-09-26 15:46:39 +00:00
|
|
|
window->getCamera()->processZoom(0.1 * (double)event->angleDelta().y());
|
|
|
|
}
|
2014-08-28 08:29:12 +00:00
|
|
|
|
2014-09-26 15:46:39 +00:00
|
|
|
void OpenGLView::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
mouse_button = event->button();
|
|
|
|
mouse_pos = event->windowPos();
|
|
|
|
}
|
2014-08-27 16:19:48 +00:00
|
|
|
|
2014-09-26 15:46:39 +00:00
|
|
|
void OpenGLView::mouseReleaseEvent(QMouseEvent *)
|
|
|
|
{
|
|
|
|
mouse_button = Qt::NoButton;
|
|
|
|
}
|
2014-08-27 16:19:48 +00:00
|
|
|
|
2014-09-26 15:46:39 +00:00
|
|
|
void OpenGLView::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QPointF diff = event->windowPos() - mouse_pos;
|
|
|
|
if (mouse_button == Qt::MidButton)
|
2014-08-27 16:19:48 +00:00
|
|
|
{
|
2014-09-26 15:46:39 +00:00
|
|
|
window->getCamera()->processScroll(-0.1 * diff.x(), 0.1 * diff.y());
|
|
|
|
}
|
|
|
|
mouse_pos = event->windowPos();
|
2014-08-27 16:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLView::timerEvent(QTimerEvent *)
|
|
|
|
{
|
|
|
|
if (window)
|
|
|
|
{
|
|
|
|
window->update();
|
|
|
|
}
|
2014-08-19 13:49:08 +00:00
|
|
|
}
|