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>
31 #include <QMouseEvent>
37 * Constructors + destructor
40 /// Primary constructor.
41 tcViewportWidget::tcViewportWidget(QWidget
* parent
)
43 , m_adaptiveQuality(true)
44 , m_polygonMode(GL_FILL
)
45 , m_observer(new tcObserver())
47 , m_mouseInteracting(false)
49 , m_mouseIntersecting(false)
51 , m_mouseSlicing(false)
54 setMouseTracking(true);
55 setMinimumSize(400,300);
59 tcViewportWidget::~tcViewportWidget()
68 /// Set the globe object.
69 void tcViewportWidget::setGlobe(tcGlobe
* globe
)
72 tcGeo
pos( 7.03227 * M_PI
/180, 45.92093 * M_PI
/180);
73 m_observer
->setFocus(pos
, globe
->radiusAt(pos
));
80 /// Find the coordinates under the mouse.
81 tcGeo
tcViewportWidget::geoAt(float x
, float y
, bool* ok
)
83 maths::Vector
<2,double> screenXy(x
/ width(), 1.0 - y
/ height());
84 maths::Vector
<3,double> obs
= m_observer
->position();
85 maths::Vector
<3,double> ray
= m_observer
->ray(screenXy
, (double)width() / height());
86 double r
= m_observer
->focusAltitude();
89 bool intersecting
= false;
90 // find intersection P of ray and globe
91 // given globe radius r, observer O, ray R, where |R|=1
94 // (O+t*R)*(O+t*R) = r*r
95 // O*O + 2*O*t*R + t*t*R*R = r*r
96 // O*O + 2*O*t*R + t*t - r*r = 0
97 // quadratic in parameter t
98 // a = 1, b = 2*RO c = O*O-r*r RO = O*R
99 // t = (-b +- sqrt(b*b - 4ac)) / 2a
100 // = (-2RO +- sqrt(4*RO*RO - 4*O*O + 4*r*r)) / 2
101 // = (-2*RO +- 2*sqrt(RO*RO - O*O + r*r)) / 2
102 // = RO += sqrt(RO*RO - O*O + r*r)
103 double discriminant
= roro
- obs
.sqr() + r
*r
;
104 if (discriminant
>= 0.0)
106 discriminant
= sqrt(discriminant
);
107 double t
= -ro
- discriminant
;
111 obs
/= r
; // should now be normalized
119 // mouse is outside of globe
120 // use closest point on outline circle of globe
122 // tangential circle: P*(P-O) = 0
125 // line: P/s = O + tR
126 // P = s(O + tR) (s>0)
127 // r*r - s(O + tR)O = 0
128 // r*r - s(OO + tRO) = 0
131 // t = (r*r/s - OO)/RO
133 // (sO + tsR)(sO + tsR - O) = 0
134 // (O + tR)(sO + tsR - O) = 0
135 // sOO + tsRO + tsOR + ttsRR - OO - tRO = 0
136 // sOO + 2tsRO + tts - OO - tRO = 0
137 // s(OO + 2tRO + tt) = OO - tRO
138 // substitute s=rr/(OO+tRO)
139 // rr/(OO+tRO)(OO + 2tRO + tt) = OO - tRO
140 // rr(OO + 2tRO + tt) = (OO - tRO)(OO+tRO)
141 // rrOO + 2rrtRO + rrtt = OO*OO - ttRO*RO
142 // tt(rr + RO*RO) + t(2rrRO) + (rrOO - OO*OO)
143 // a = rr + RO*RO, b = 2rrRO, c = rr00 - OO*OO
144 // t = (-2rrRO +- sqrt(4rrrrRO*RO - 4(rr + RO*RO)(rr00 - OO*OO))) / 2(rr + RO*RO)
145 // t = (-rrRO +- sqrt(rrrrRO*RO - (rr + RO*RO)(rr00 - OO*OO))) / (rr + RO*RO)
149 double discriminant
= rr
*rr
*roro
- (rr
+ roro
)*(rr
*oo
- oo
*oo
);
150 if (discriminant
>= 0)
152 discriminant
= sqrt(discriminant
);
153 double t
= (-rr
*ro
+ discriminant
) / (rr
+ roro
);
154 double s
= rr
/(oo
+ t
*ro
);
159 obs
/= r
; // should now be normalized
172 return tcGeo(obs
, true);
181 * General rendering slots
184 /// Change the quality to adaptive.
185 void tcViewportWidget::setQualityAdaptive()
187 m_adaptiveQuality
= true;
191 /// Change the quality to full resolution.
192 void tcViewportWidget::setQualityFull()
194 m_adaptiveQuality
= false;
198 /// Change the polygon mode to normal.
199 void tcViewportWidget::setPolygonModeNormal()
201 m_polygonMode
= GL_FILL
;
205 /// Change the polygon mode to wireframe.
206 void tcViewportWidget::setPolygonModeWireframe()
208 m_polygonMode
= GL_LINE
;
212 /// Remove colour coding.
213 void tcViewportWidget::setColourCodingNone()
215 m_globe
->setColourCoding(tcGlobe::NoColourCoding
);
219 /// Set colour coding to elevation sample alignment.
220 void tcViewportWidget::setColourCodingElevationSampleAlignment()
222 m_globe
->setColourCoding(tcGlobe::ElevationSampleAlignment
);
226 /// Set colour mapping for an output channel to an input band.
227 void tcViewportWidget::setColourMapping(int output
, int input
)
229 m_globe
->setColourMapping(output
, input
);
234 * Elevation modification slots
237 /// Go to flat elevation mode.
238 void tcViewportWidget::setElevationFlat()
242 m_globe
->setElevationMode(tcGlobe::NoElevation
);
243 m_observer
->setFocusAltitude(m_globe
->radiusAt(m_observer
->focus()));
248 /// Go to raw SRTM elevation mode.
249 void tcViewportWidget::setElevationRaw()
253 m_globe
->setElevationMode(tcGlobe::RawElevation
);
254 m_observer
->setFocusAltitude(m_globe
->radiusAt(m_observer
->focus()));
259 /// Go to corrected elevation mode.
260 void tcViewportWidget::setElevationCorrected()
264 m_globe
->setElevationMode(tcGlobe::CorrectedElevation
);
265 m_observer
->setFocusAltitude(m_globe
->radiusAt(m_observer
->focus()));
270 /// Set the level of correction applied to the elevation data.
271 void tcViewportWidget::setElevationCorrection(int correction
)
275 m_globe
->setElevationCorrection((float)correction
/100);
276 m_observer
->setFocusAltitude(m_globe
->radiusAt(m_observer
->focus()));
281 /// Set the elevation data set name.
282 void tcViewportWidget::setElevationDataSet(const QString
& name
)
286 m_globe
->setElevationDataSet(name
);
287 m_observer
->setFocusAltitude(m_globe
->radiusAt(m_observer
->focus()));
296 void tcViewportWidget::initializeGL()
298 glClearColor(0.0, 0.3, 0.7, 0.0);
301 void tcViewportWidget::resizeGL(int w
, int h
)
303 glViewport(0, 0, (GLint
)w
, (GLint
)h
);
305 double aspect
= (double)w
/ h
;
306 m_observer
->setupProjection(aspect
);
309 void tcViewportWidget::paintGL()
311 static int maxTexUnits
= 0;
312 if (maxTexUnits
== 0)
314 glGetIntegerv(GL_MAX_TEXTURE_UNITS
, &maxTexUnits
);
317 qDebug() << tr("Maximum texture units is %1, but Tecorrec needs at least 3").arg(maxTexUnits
);
322 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
323 glEnable(GL_DEPTH_TEST
);
324 glEnable(GL_CULL_FACE
);
327 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
329 glPolygonMode(GL_FRONT_AND_BACK
, m_polygonMode
);
331 m_observer
->setupModelView();
335 m_globe
->render(m_observer
, m_adaptiveQuality
, m_slice
);
339 m_globe
->render(m_observer
);
342 float meanRadius
= m_globe
->meanRadius();
345 double spotlightExtent
= m_observer
->range()*0.4;
346 // Draw a spotlight for the mouse
347 if (m_mouseIntersecting
)
349 maths::Vector
<3,double> vec
= m_mouseCoord
;
350 float altitude
= m_globe
->altitudeAt(m_mouseCoord
);
353 glColor4f(1.0f
, 1.0f
, 0.0f
, 1.0f
);
354 glVertex3(vec
*meanRadius
);
355 glVertex3(vec
*(meanRadius
+ altitude
));
358 glColor4f(1.0f
, 0.0f
, 1.0f
, 1.0f
);
359 glVertex3(vec
*(meanRadius
+ altitude
));
360 glColor4f(1.0f
, 1.0f
, 1.0f
, 0.0f
);
361 glVertex3(vec
*(meanRadius
+ altitude
+ spotlightExtent
));
365 // Draw a spotlight for the focus
367 maths::Vector
<3,double> vec
= m_observer
->focus();
368 float altitude
= m_globe
->altitudeAt(m_observer
->focus());
371 glColor4f(1.0f
, 1.0f
, 0.0f
, 1.0f
);
372 glVertex3(vec
*meanRadius
);
373 glVertex3(vec
*(meanRadius
+ altitude
));
376 glColor4f(0.0f
, 1.0f
, 1.0f
, 1.0f
);
377 glVertex3(vec
*(meanRadius
+ altitude
));
378 glColor4f(1.0f
, 1.0f
, 1.0f
, 0.0f
);
379 glVertex3(vec
*(meanRadius
+ altitude
+ spotlightExtent
));
385 // Draw a fence around the selected region
386 for (int i
= 0; i
< 2; ++i
)
390 float colour
[4] = {1.0f
, 0.5f
, 1.0f
, 1.0f
};
391 if (0 == i
&& m_mouseSlicing
&& m_mouseIntersecting
)
393 coord
= m_mouseCoord
;
394 ending
= m_mouseStartCoord
;
396 else if (1 == i
&& m_sliced
)
408 tcGeo delta
= ending
- coord
;
409 int nlon
= 2 + fabs(delta
.lon())*10*meanRadius
/ m_observer
->range();
410 int nlat
= 2 + fabs(delta
.lat())*10*meanRadius
/ m_observer
->range();
411 double dlon
= delta
.lon() / nlon
;
412 double dlat
= delta
.lat() / nlat
;
414 glDisable(GL_CULL_FACE
);
415 glBegin(GL_TRIANGLE_STRIP
);
416 for (int i
= 0; i
<= nlon
*2 + nlat
*2; ++i
)
418 float radius
= m_globe
->radiusAt(coord
);
419 maths::Vector
<3,double> coordVec
= coord
;
421 glVertex3(coordVec
*radius
);
422 glColor4f(1.0f
, 1.0f
, 1.0f
, 0.0f
);
423 glVertex3(coordVec
*(radius
+ m_observer
->range()*0.1));
427 coord
.setLon(coord
.lon() + dlon
);
429 else if (i
< nlon
+ nlat
)
431 coord
.setLat(coord
.lat() + dlat
);
433 else if (i
< nlon
+ nlat
+ nlon
)
435 coord
.setLon(coord
.lon() - dlon
);
439 coord
.setLat(coord
.lat() - dlat
);
443 glEnable(GL_CULL_FACE
);
452 void tcViewportWidget::mouseMoveEvent(QMouseEvent
* event
)
454 if (m_mouseInteracting
)
456 QPointF dpos
= event
->posF() - m_mousePos
;
457 m_mousePos
= event
->posF();
459 // Pan the scene, adjust the observer focus
460 m_observer
->moveFocusRelative(dpos
.x() / height(), dpos
.y() / width());
461 m_observer
->setFocusAltitude(m_globe
->radiusAt(m_observer
->focus()));
464 QString mouseDescription
;
466 m_mouseCoord
= geoAt(event
->posF().x(), event
->posF().y(), &ok
);
467 m_mouseIntersecting
= ok
;
470 mouseDescription
= m_mouseCoord
.describe() + " " + tr("%1 m","metres")
471 .arg(m_globe
->altitudeAt(m_mouseCoord
));
472 emit
mouseGeoChanged(m_mouseCoord
);
476 mouseDescription
= tr("--");
478 emit
mouseGeoTextChanged(tr("Focus: %1 %2, Mouse: %3")
479 .arg(m_observer
->focus().describe())
480 .arg(tr("%1 m","metres")
481 .arg(m_globe
->altitudeAt(m_observer
->focus())))
482 .arg(mouseDescription
));
484 if (m_mouseInteracting
|| m_mouseSlicing
|| m_sliced
)
490 void tcViewportWidget::mousePressEvent(QMouseEvent
* event
)
492 if (event
->button() == Qt::LeftButton
)
494 m_mouseInteracting
= true;
495 m_mousePos
= event
->posF();
497 else if (event
->button() == Qt::RightButton
)
500 m_mouseStartCoord
= geoAt(event
->posF().x(), event
->posF().y(), &ok
);
501 m_mouseIntersecting
= false;
504 m_mouseSlicing
= true;
509 void tcViewportWidget::mouseReleaseEvent(QMouseEvent
* event
)
511 if (m_mouseInteracting
)
513 m_mouseInteracting
= false;
515 else if (m_mouseSlicing
)
517 m_mouseSlicing
= false;
518 m_sliced
= m_mouseIntersecting
;
519 if (m_mouseIntersecting
)
521 m_slice
[0] = m_mouseStartCoord
;
522 m_slice
[1] = m_mouseCoord
;
528 void tcViewportWidget::wheelEvent(QWheelEvent
* event
)
530 float delta
= M_PI
/180.0 * event
->delta()/8;
532 if (event
->orientation() == Qt::Vertical
)
534 if (event
->modifiers().testFlag(Qt::ControlModifier
))
536 m_observer
->adjustElevation(delta
);
540 m_observer
->adjustRange(-delta
);
545 m_observer
->adjustAzimuth(-delta
);
547 m_observer
->setFocusAltitude(m_globe
->radiusAt(m_observer
->focus()));