1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
21 * @file tcViewportWidget.cpp
22 * @brief OpenGL viewport widget.
25 #include "tcViewportWidget.h"
26 #include <tcObserver.h>
29 #include <QMouseEvent>
34 * Constructors + destructor
37 /// Primary constructor.
38 tcViewportWidget::tcViewportWidget(QWidget
* parent
)
40 , m_observer(new tcObserver())
42 , m_mouseInteracting(false)
48 tcViewportWidget::~tcViewportWidget()
57 /// Set the globe object.
58 void tcViewportWidget::setGlobe(tcGlobe
* globe
)
61 m_observer
->setFocus(tcGeo( 7.0 * M_PI
/180, 46.0 * M_PI
/180), globe
->meanRadius());
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
);
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());
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
);
131 m_observer
->adjustRange(delta
);
136 m_observer
->adjustAzimuth(delta
);