Mouse control
[tecorrec.git] / tcViewportWidget.cpp
blob4ec842246a9ee320101daae92da55e04b70f24f7
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file tcViewportWidget.cpp
22 * @brief OpenGL viewport widget.
25 #include "tcViewportWidget.h"
26 #include <tcObserver.h>
27 #include <tcGlobe.h>
29 #include <QMouseEvent>
31 #include <cmath>
34 * Constructors + destructor
37 /// Primary constructor.
38 tcViewportWidget::tcViewportWidget(QWidget* parent)
39 : QGLWidget(parent)
40 , m_observer(new tcObserver())
41 , m_globe(0)
42 , m_mouseInteracting(false)
43 , m_mousePos()
47 /// Destructor.
48 tcViewportWidget::~tcViewportWidget()
50 delete m_observer;
54 * Modifiers
57 /// Set the globe object.
58 void tcViewportWidget::setGlobe(tcGlobe* globe)
60 m_globe = globe;
61 m_observer->setFocus(tcGeo( 7.0 * M_PI/180, 46.0 * M_PI/180), globe->meanRadius());
65 * Rendering
68 void tcViewportWidget::initializeGL()
70 glClearColor(0.0, 0.0, 0.0, 0.0);
73 void tcViewportWidget::resizeGL(int w, int h)
75 glViewport(0, 0, (GLint)w, (GLint)h);
77 double aspect = (double)w / h;
78 m_observer->setupProjection(aspect);
81 void tcViewportWidget::paintGL()
83 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84 glEnable(GL_DEPTH_TEST);
86 m_observer->setupModelView();
88 m_globe->render(m_observer);
92 * Mouse events
95 void tcViewportWidget::mouseMoveEvent(QMouseEvent* event)
97 if (m_mouseInteracting)
99 QPointF dpos = event->posF() - m_mousePos;
100 m_mousePos = event->posF();
102 // Pan the scene, adjust the observer focus
103 m_observer->moveFocusRelative(dpos.x() / height(), dpos.y() / width());
105 updateGL();
109 void tcViewportWidget::mousePressEvent(QMouseEvent* event)
111 m_mouseInteracting = true;
112 m_mousePos = event->posF();
115 void tcViewportWidget::mouseReleaseEvent(QMouseEvent* event)
117 m_mouseInteracting = false;
120 void tcViewportWidget::wheelEvent(QWheelEvent* event)
122 float delta = M_PI/180.0 * event->delta()/8;
123 if (event->orientation() == Qt::Vertical)
125 if (event->modifiers().testFlag(Qt::ControlModifier))
127 m_observer->adjustElevation(-delta);
129 else
131 m_observer->adjustRange(delta);
134 else
136 m_observer->adjustAzimuth(delta);
139 updateGL();