void SpotView::paintGL() { QGLPainter painter(this); //painter.begin(); painter.clear(); // Set perspective transformation and position model-view matrix QGLCamera camera; QVector3D sceneOrigin = floor->boundingBox().center(); camera.setCenter(sceneOrigin); camera.setEye(sceneOrigin + QVector3D(0.0f, 5.0f, 20.0f)); camera.apply(&painter); paintGL(&painter); }
void ViewportWidget::initializeGL(QGLPainter *painter) { QGLCamera *camera = new QGLCamera(); camera->setNearPlane(5.0f); camera->setFarPlane(1000.0f); camera->setFieldOfView(34.0f); setCamera(camera); _lp.setPosition(QVector3D(-1.0, -2.0, 1.0)); _lp.setSpotAngle(180); _lp.setSpotExponent(24); _lp.setSpotDirection(QVector3D(1.0, 4.0, 4.0)); _lm.setAmbientSceneColor(QColor(200, 200, 200)); painter->setLightModel(&_lm); painter->setMainLight(&_lp); painter->setClearColor(Qt::black); }
// Convert deltas in the X and Y directions into percentages of // the view width and height. QPointF QGLGraphicsNavigationItemPrivate::viewDelta(qreal deltax, qreal deltay) { QRectF rect = viewportItem->rect(); qreal w = rect.width(); qreal h = rect.height(); QGLCamera *camera = viewportItem->camera(); bool scaleToWidth; qreal scaleFactor, scaleX, scaleY; QSizeF viewSize = camera->viewSize(); if (w >= h) { if (viewSize.width() >= viewSize.height()) scaleToWidth = true; else scaleToWidth = false; } else { if (viewSize.width() >= viewSize.height()) scaleToWidth = false; else scaleToWidth = true; } int rotation = camera->screenRotation(); if (rotation == 90 || rotation == 270) { scaleToWidth = !scaleToWidth; qSwap(deltax, deltay); } if (rotation == 90 || rotation == 180) { deltax = -deltax; } if (rotation == 180 || rotation == 270) { deltay = -deltay; } if (scaleToWidth) { scaleFactor = 2.0f / viewSize.width(); scaleX = scaleFactor * h / w; scaleY = scaleFactor; } else { scaleFactor = 2.0f / viewSize.height(); scaleX = scaleFactor; scaleY = scaleFactor * w / h; } return QPointF(deltax * scaleX / w, deltay * scaleY / h); }
// Rotate about the object being viewed. void QGLGraphicsNavigationItemPrivate::rotate(qreal deltax, qreal deltay) { QGLCamera *camera = viewportItem->camera(); QRectF rect = viewportItem->rect(); int rotation = camera->screenRotation(); if (rotation == 90 || rotation == 270) { qSwap(deltax, deltay); } if (rotation == 90 || rotation == 180) { deltax = -deltax; } if (rotation == 180 || rotation == 270) { deltay = -deltay; } qreal anglex = deltax * 90.0f / rect.width(); qreal angley = deltay * 90.0f / rect.height(); QQuaternion q = camera->pan(-anglex); q *= camera->tilt(-angley); camera->rotateCenter(q); }
/*! Create a copy of this QGLCamera, parented onto the \a parent given, by default 0 (which results in no parenting). The copy returned has exactly the same state as the original. */ QGLCamera *QGLCamera::clone(QObject *parent) const { Q_D(const QGLCamera); QGLCamera *copy = new QGLCamera(parent); copy->d_func()->projectionType = d->projectionType; copy->d_func()->fieldOfView = d->fieldOfView; copy->d_func()->nearPlane = d->nearPlane; copy->d_func()->farPlane = d->farPlane; copy->d_func()->viewSize = d->viewSize; copy->d_func()->minViewSize = d->minViewSize; copy->d_func()->screenRotation = d->screenRotation; copy->d_func()->eye = d->eye; copy->d_func()->upVector = d->upVector; copy->d_func()->center = d->center; copy->d_func()->viewVector = d->viewVector; copy->d_func()->eyeSeparation = d->eyeSeparation; copy->d_func()->motionAdjustment = d->motionAdjustment; copy->d_func()->adjustForAspectRatio = d->adjustForAspectRatio; return copy; }
// Pan left/right/up/down without rotating about the object. void QGLGraphicsNavigationItemPrivate::pan(qreal deltax, qreal deltay) { QGLCamera *camera = viewportItem->camera(); QPointF delta = viewDelta(deltax, deltay); QVector3D t = camera->translation(delta.x(), -delta.y(), 0.0f); // Technically panning the eye left should make the object appear to // move off to the right, but this looks weird on-screen where the user // actually thinks they are picking up the object and dragging it rather // than moving the eye. We therefore apply the inverse of the translation // to make it "look right". camera->setEye(camera->eye() - t); camera->setCenter(camera->center() - t); }
// Zoom in and out according to the change in wheel delta. void QGLGraphicsNavigationItemPrivate::wheel(qreal delta) { QGLCamera *camera = viewportItem->camera(); qreal scale = qAbs(viewDelta(delta, delta).x()); if (delta < 0) scale = -scale; if (scale >= 0.0f) scale += 1.0f; else scale = 1.0f / (1.0f - scale); qreal fov = camera->fieldOfView(); if (fov != 0.0f) camera->setFieldOfView(camera->fieldOfView() / scale); else camera->setViewSize(camera->viewSize() / scale); }
void CubeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if (navigating) { QPoint delta = event->pos().toPoint() - pressedPos; int deltax = delta.x(); int deltay = delta.y(); QGLCamera *camera = this->camera(); int rotation = camera->screenRotation(); if (rotation == 90 || rotation == 270) { qSwap(deltax, deltay); } if (rotation == 90 || rotation == 180) { deltax = -deltax; } if (rotation == 180 || rotation == 270) { deltay = -deltay; } qreal anglex = deltax * 90.0f / rect().width(); qreal angley = deltay * 90.0f / rect().height(); QQuaternion q = startNavCamera->pan(-anglex); q *= startNavCamera->tilt(-angley); camera->setEye(startNavCamera->eye()); camera->setCenter(startNavCamera->center()); camera->setUpVector(startNavCamera->upVector()); camera->rotateCenter(q); } else if (pressedFace != -1) { int face; QPoint pos = cubeIntersection (event->widget(), event->pos().toPoint(), &face); if (face != pressedFace) pos = QPoint(-1, -1); deliverSceneEvent(pos, event); return; } QGraphicsItem::mouseMoveEvent(event); }
/*! Processes the key press event \a e. */ void QGLView::keyPressEvent(QKeyEvent *e) { QGLCamera *camera; qreal sep; if ((d->options & QGLView::CameraNavigation) == 0) { QGLWidget::keyPressEvent(e); return; } switch (e->key()) { case Qt::Key_Escape: case Qt::Key_Q: { if (parentWidget() == 0) close(); } case Qt::Key_Left: { if ((e->modifiers() & Qt::ShiftModifier) != 0) { pan(-10, 0); } else if ((e->modifiers() & Qt::ControlModifier) != 0) { camera = this->camera(); sep = camera->eyeSeparation(); sep -= (sep / 10.0f); if (sep < 0.0f) sep = 0.0f; camera->setEyeSeparation(sep); e->accept(); return; } else { rotate(-10, 0); } } break; case Qt::Key_Right: { if ((e->modifiers() & Qt::ShiftModifier) != 0) { pan(10, 0); } else if ((e->modifiers() & Qt::ControlModifier) != 0) { camera = this->camera(); sep = camera->eyeSeparation(); sep += (sep / 10.0f); camera->setEyeSeparation(sep); e->accept(); return; } else { rotate(10, 0); } } break; case Qt::Key_Up: { if ((e->modifiers() & Qt::ControlModifier) != 0) wheel(120); else if ((e->modifiers() & Qt::ShiftModifier) != 0) pan(0, -10); else rotate(0, -10); } break; case Qt::Key_Down: { if ((e->modifiers() & Qt::ControlModifier) != 0) wheel(-120); else if ((e->modifiers() & Qt::ShiftModifier) != 0) pan(0, 10); else rotate(0, 10); } break; } QGLWidget::keyPressEvent(e); }
bool QGLGraphicsNavigationItemPrivate::handleKeyPress(QKeyEvent *e) { QGLCamera *camera; qreal sep; if ((viewportItem->options() & QGLGraphicsViewportItem::CameraNavigation) == 0) { return false; } switch (e->key()) { case Qt::Key_Left: { if ((e->modifiers() & Qt::ShiftModifier) != 0) { pan(-10, 0); } else if ((e->modifiers() & Qt::ControlModifier) != 0) { camera = viewportItem->camera(); sep = camera->eyeSeparation(); sep -= (sep / 10.0f); if (sep < 0.0f) sep = 0.0f; camera->setEyeSeparation(sep); e->accept(); } else { rotate(-10, 0); } } break; case Qt::Key_Right: { if ((e->modifiers() & Qt::ShiftModifier) != 0) { pan(10, 0); } else if ((e->modifiers() & Qt::ControlModifier) != 0) { camera = viewportItem->camera(); sep = camera->eyeSeparation(); sep += (sep / 10.0f); camera->setEyeSeparation(sep); e->accept(); return true; } else { rotate(10, 0); } } break; case Qt::Key_Up: { if ((e->modifiers() & Qt::ControlModifier) != 0) wheel(120); else if ((e->modifiers() & Qt::ShiftModifier) != 0) pan(0, -10); else rotate(0, -10); } break; case Qt::Key_Down: { if ((e->modifiers() & Qt::ControlModifier) != 0) wheel(-120); else if ((e->modifiers() & Qt::ShiftModifier) != 0) pan(0, 10); else rotate(0, 10); } break; default: return false; } return true; }
QDebug operator<<(QDebug dbg, const QGLCamera &cam) { dbg << "QGLCamera"; if (!cam.objectName().isEmpty()) dbg << cam.objectName(); dbg << "\n"; dbg << " projection:" << ( cam.projectionType() == QGLCamera::Perspective ? "Perspective" : "Orthographic" ); dbg << "-- viewsize:" << cam.viewSize().width() << "x" << cam.viewSize().height() << "\n"; dbg << " near-plane:" << cam.nearPlane() << "-- far-plane:" << cam.farPlane(); dbg << "-- field-of-view:" << cam.fieldOfView() << "\n"; dbg << " rotation:" << cam.screenRotation() << " -- motion adjust:" << cam.motionAdjustment() << " -- aspect adjust:" << cam.adjustForAspectRatio() << "\n"; dbg << " eye:" << cam.eye() << "-- center:" << cam.center(); dbg << "-- up:" << cam.upVector() << "\n"; return dbg; }