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
);
85 glEnable(GL_CULL_FACE
);
88 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
91 m_observer
->setupModelView();
93 m_globe
->render(m_observer
);
100 void tcViewportWidget::mouseMoveEvent(QMouseEvent
* event
)
102 if (m_mouseInteracting
)
104 QPointF dpos
= event
->posF() - m_mousePos
;
105 m_mousePos
= event
->posF();
107 // Pan the scene, adjust the observer focus
108 m_observer
->moveFocusRelative(dpos
.x() / height(), dpos
.y() / width());
114 void tcViewportWidget::mousePressEvent(QMouseEvent
* event
)
116 m_mouseInteracting
= true;
117 m_mousePos
= event
->posF();
120 void tcViewportWidget::mouseReleaseEvent(QMouseEvent
* event
)
122 m_mouseInteracting
= false;
125 void tcViewportWidget::wheelEvent(QWheelEvent
* event
)
127 float delta
= M_PI
/180.0 * event
->delta()/8;
129 if (event
->orientation() == Qt::Vertical
)
131 if (event
->modifiers().testFlag(Qt::ControlModifier
))
133 m_observer
->adjustElevation(-delta
);
137 m_observer
->adjustRange(delta
);
142 m_observer
->adjustAzimuth(delta
);