93 lines
2.2 KiB
C++
93 lines
2.2 KiB
C++
#include "ViewportWidget.h"
|
|
#include "ViewCube.h"
|
|
#include <QMouseEvent>
|
|
#include <QWheelEvent>
|
|
#include <QApplication>
|
|
|
|
ViewportWidget::ViewportWidget(QWidget *parent)
|
|
: QOpenGLWidget(parent)
|
|
{
|
|
m_viewCube = new ViewCube();
|
|
}
|
|
|
|
ViewportWidget::~ViewportWidget()
|
|
{
|
|
delete m_viewCube;
|
|
}
|
|
|
|
void ViewportWidget::initializeGL()
|
|
{
|
|
initializeOpenGLFunctions();
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glEnable(GL_DEPTH_TEST);
|
|
m_viewCube->initializeGL();
|
|
}
|
|
|
|
void ViewportWidget::paintGL()
|
|
{
|
|
// Main scene rendering
|
|
glViewport(0, 0, width(), height());
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
QMatrix4x4 model;
|
|
model.translate(panX, panY, zoom);
|
|
model.rotate(xRot / 16.0f, 1, 0, 0);
|
|
model.rotate(yRot / 16.0f, 0, 1, 0);
|
|
|
|
// For simplicity, we'll use a fixed-function pipeline style for drawing.
|
|
// In a real app, this would use shaders.
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadMatrixf(projection.constData());
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadMatrixf(model.constData());
|
|
|
|
// View cube rendering
|
|
QMatrix4x4 viewCubeModel;
|
|
viewCubeModel.rotate(xRot / 16.0f, 1, 0, 0);
|
|
viewCubeModel.rotate(yRot / 16.0f, 0, 1, 0);
|
|
m_viewCube->paintGL(viewCubeModel, width(), height());
|
|
|
|
glViewport(0, 0, width(), height());
|
|
}
|
|
|
|
void ViewportWidget::resizeGL(int w, int h)
|
|
{
|
|
projection.setToIdentity();
|
|
projection.perspective(45.0f, w / float(h), 0.01f, 100.0f);
|
|
}
|
|
|
|
void ViewportWidget::mousePressEvent(QMouseEvent *event)
|
|
{
|
|
lastPos = event->pos();
|
|
}
|
|
|
|
void ViewportWidget::mouseMoveEvent(QMouseEvent *event)
|
|
{
|
|
int dx = event->pos().x() - lastPos.x();
|
|
int dy = event->pos().y() - lastPos.y();
|
|
|
|
if (event->buttons() & Qt::MiddleButton) {
|
|
if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
|
|
// Pan
|
|
panX += dx / 100.0f;
|
|
panY -= dy / 100.0f;
|
|
} else {
|
|
// Rotate
|
|
xRot += 8 * dy;
|
|
yRot += 8 * dx;
|
|
}
|
|
}
|
|
lastPos = event->pos();
|
|
update();
|
|
}
|
|
|
|
void ViewportWidget::wheelEvent(QWheelEvent *event)
|
|
{
|
|
QPoint numDegrees = event->angleDelta() / 8;
|
|
if (!numDegrees.isNull()) {
|
|
zoom += numDegrees.y() / 5.0f;
|
|
}
|
|
update();
|
|
}
|