1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2004-2015 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <cfloat> |
---|
9 | #include <cstring> |
---|
10 | #include <cassert> |
---|
11 | #include <cmath> |
---|
12 | #include <cstdlib> |
---|
13 | #include <cerrno> |
---|
14 | |
---|
15 | #include <limits> |
---|
16 | #include <set> |
---|
17 | #include <fstream> |
---|
18 | #include <sstream> |
---|
19 | |
---|
20 | #include <sys/types.h> |
---|
21 | #include <unistd.h> // For getpid() |
---|
22 | |
---|
23 | #include <GL/gl.h> |
---|
24 | |
---|
25 | #ifdef WANT_TRACE |
---|
26 | #include <sys/time.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <osgDB/WriteFile> |
---|
30 | #include <osgDB/FileUtils> |
---|
31 | #include <osgDB/FileNameUtils> |
---|
32 | #include <osgGA/StateSetManipulator> |
---|
33 | #include <osgGA/GUIEventAdapter> |
---|
34 | #include <osgViewer/ViewerEventHandlers> |
---|
35 | |
---|
36 | #include <osgEarth/Version> |
---|
37 | #include <osgEarth/Registry> |
---|
38 | #include <osgEarth/FileUtils> |
---|
39 | #include <osgEarth/Cache> |
---|
40 | #include <osgEarth/CachePolicy> |
---|
41 | #include <osgEarthDrivers/cache_filesystem/FileSystemCache> |
---|
42 | #include <osgEarth/MapNode> |
---|
43 | #include <osgEarth/TerrainEngineNode> |
---|
44 | #include <osgEarth/Bounds> |
---|
45 | #include <osgEarth/Profile> |
---|
46 | #include <osgEarth/Viewpoint> |
---|
47 | #include <osgEarth/GeoMath> |
---|
48 | #include <osgEarth/TerrainLayer> |
---|
49 | #include <osgEarth/ImageLayer> |
---|
50 | #include <osgEarth/ElevationLayer> |
---|
51 | #include <osgEarth/ModelLayer> |
---|
52 | #include <osgEarth/DateTime> |
---|
53 | #include <osgEarth/TimeControl> |
---|
54 | #ifdef USE_RTT_PICKER |
---|
55 | #include <osgEarthUtil/RTTPicker> |
---|
56 | #include <osgEarthFeatures/FeatureIndex> |
---|
57 | #else |
---|
58 | #include <osgEarth/IntersectionPicker> |
---|
59 | #endif |
---|
60 | #include <osgEarthUtil/GraticuleNode> |
---|
61 | #include <osgEarthFeatures/FeatureModelSource> |
---|
62 | #include <osgEarthFeatures/Feature> |
---|
63 | #include <osgEarthSymbology/Color> |
---|
64 | #include <osgEarthSymbology/Geometry> |
---|
65 | #include <osgEarthSymbology/Style> |
---|
66 | #include <osgEarthSymbology/StyleSheet> |
---|
67 | #include <osgEarthSymbology/IconSymbol> |
---|
68 | #include <osgEarthSymbology/LineSymbol> |
---|
69 | |
---|
70 | #include <osgEarthAnnotation/AnnotationNode> |
---|
71 | #include <osgEarthAnnotation/FeatureNode> |
---|
72 | #include <osgEarthAnnotation/PlaceNode> |
---|
73 | #ifdef NEW_ANNOTATION_API |
---|
74 | #include <osgEarth/ScreenSpaceLayout> |
---|
75 | #else |
---|
76 | #include <osgEarth/Decluttering> |
---|
77 | #include <osgEarthAnnotation/HighlightDecoration> |
---|
78 | #include <osgEarthAnnotation/ScaleDecoration> |
---|
79 | #endif |
---|
80 | #include <osgEarthUtil/EarthManipulator> |
---|
81 | #include <osgEarthUtil/Sky> |
---|
82 | #include <osgEarthDrivers/sky_simple/SimpleSkyOptions> |
---|
83 | #include <osgEarthUtil/AutoClipPlaneHandler> |
---|
84 | #include <osgEarthUtil/MouseCoordsTool> |
---|
85 | #include <osgEarthUtil/UTMGraticule> |
---|
86 | #include <osgEarthUtil/MGRSGraticule> |
---|
87 | #include <osgEarthUtil/GeodeticGraticule> |
---|
88 | #include <osgEarthUtil/LatLongFormatter> |
---|
89 | #include <osgEarthUtil/MGRSFormatter> |
---|
90 | #include <osgEarthUtil/GLSLColorFilter> |
---|
91 | #include <osgEarthUtil/VerticalScale> |
---|
92 | #include <osgEarthDrivers/gdal/GDALOptions> |
---|
93 | #include <osgEarthDrivers/engine_mp/MPTerrainEngineOptions> |
---|
94 | #ifdef USE_REX |
---|
95 | #include <osgEarthDrivers/engine_rex/RexTerrainEngineOptions> |
---|
96 | #endif |
---|
97 | |
---|
98 | #include "Renderer.h" |
---|
99 | #ifdef USE_RTT_PICKER |
---|
100 | #include "Picker.h" |
---|
101 | #endif |
---|
102 | #include "IData.h" |
---|
103 | #include "ColorMap.h" |
---|
104 | #if 0 |
---|
105 | #include "SingleWindow.h" |
---|
106 | #endif |
---|
107 | #include "ScaleBar.h" |
---|
108 | #include "FileUtil.h" |
---|
109 | #include "Util.h" |
---|
110 | #include "Icons.h" |
---|
111 | #include "Trace.h" |
---|
112 | |
---|
113 | #define MSECS_ELAPSED(t1, t2) \ |
---|
114 | ((t1).tv_sec == (t2).tv_sec ? (((t2).tv_usec - (t1).tv_usec)/1.0e+3) : \ |
---|
115 | (((t2).tv_sec - (t1).tv_sec))*1.0e+3 + (double)((t2).tv_usec - (t1).tv_usec)/1.0e+3) |
---|
116 | |
---|
117 | #ifndef CACHE_DIR |
---|
118 | #define CACHE_DIR "/var/cache/geovis" |
---|
119 | #endif |
---|
120 | #define BASE_IMAGE "world.tif" |
---|
121 | #define PIN_ICON "placemark32.png" |
---|
122 | |
---|
123 | using namespace GeoVis; |
---|
124 | using namespace IData; |
---|
125 | |
---|
126 | Renderer::Renderer() : |
---|
127 | _needsRedraw(false), |
---|
128 | _windowWidth(500), |
---|
129 | _windowHeight(500), |
---|
130 | _idleTimeout(-1L), |
---|
131 | _pickPending(false), |
---|
132 | _scaleBarUnits(UNITS_METERS) |
---|
133 | { |
---|
134 | TRACE("Enter"); |
---|
135 | |
---|
136 | initIconMap(); |
---|
137 | _bgColor[0] = 0; |
---|
138 | _bgColor[1] = 0; |
---|
139 | _bgColor[2] = 0; |
---|
140 | // 100 Mbps |
---|
141 | setMaximumBitrate(1.0e8); |
---|
142 | _lastFrameTime = _minFrameTime; |
---|
143 | |
---|
144 | iDataInit(getenv("USER")); |
---|
145 | setCacheBaseDirectory(CACHE_DIR); |
---|
146 | char *base = getenv("MAP_BASE_URI"); |
---|
147 | if (base != NULL) { |
---|
148 | _baseURI = base; |
---|
149 | TRACE("Setting base URI: %s", _baseURI.c_str()); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | osgGA::EventQueue *Renderer::getEventQueue() |
---|
154 | { |
---|
155 | if (_viewer.valid()) { |
---|
156 | osgViewer::ViewerBase::Windows windows; |
---|
157 | _viewer->getWindows(windows); |
---|
158 | if (windows.size() > 0) { |
---|
159 | return windows[0]->getEventQueue(); |
---|
160 | } |
---|
161 | } |
---|
162 | return NULL; |
---|
163 | } |
---|
164 | |
---|
165 | Renderer::~Renderer() |
---|
166 | { |
---|
167 | TRACE("Enter"); |
---|
168 | |
---|
169 | // Removes temp files |
---|
170 | deleteColorMap("all"); |
---|
171 | |
---|
172 | iDataCleanup(); |
---|
173 | |
---|
174 | // Cache is deleted in main() to allow ref-counted objects to |
---|
175 | // be deleted and threads to exit first |
---|
176 | |
---|
177 | TRACE("Leave"); |
---|
178 | } |
---|
179 | |
---|
180 | std::string Renderer::getIconFile(const char *name) const |
---|
181 | { |
---|
182 | std::ostringstream oss; |
---|
183 | oss << _resourcePath << "/" << GeoVis::getIconFile(name); |
---|
184 | return oss.str(); |
---|
185 | } |
---|
186 | |
---|
187 | std::string Renderer::getBaseImage() const |
---|
188 | { |
---|
189 | std::ostringstream oss; |
---|
190 | oss << _resourcePath << "/" << BASE_IMAGE; |
---|
191 | return oss.str(); |
---|
192 | } |
---|
193 | |
---|
194 | std::string Renderer::getPinIcon() const |
---|
195 | { |
---|
196 | std::ostringstream oss; |
---|
197 | oss << _resourcePath << "/" << PIN_ICON; |
---|
198 | return oss.str(); |
---|
199 | } |
---|
200 | |
---|
201 | void Renderer::setupCache() |
---|
202 | { |
---|
203 | std::ostringstream dir; |
---|
204 | dir << _cacheBaseDir << "/geovis_cache" << getpid(); |
---|
205 | _cacheDir = dir.str(); |
---|
206 | const char *path = _cacheDir.c_str(); |
---|
207 | TRACE("Cache dir: %s", path); |
---|
208 | removeDirectory(path); |
---|
209 | if (!osgDB::makeDirectory(_cacheDir)) { |
---|
210 | ERROR("Failed to create directory '%s'", path); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | void Renderer::initColorMaps() |
---|
215 | { |
---|
216 | if (!_colorMaps.empty()) |
---|
217 | return; |
---|
218 | #if 0 |
---|
219 | osg::TransferFunction1D *defaultColorMap = new osg::TransferFunction1D; |
---|
220 | defaultColorMap->allocate(256); |
---|
221 | defaultColorMap->setColor(0.00, osg::Vec4f(0,0,1,1), false); |
---|
222 | defaultColorMap->setColor(0.25, osg::Vec4f(0,1,1,1), false); |
---|
223 | defaultColorMap->setColor(0.50, osg::Vec4f(0,1,0,1), false); |
---|
224 | defaultColorMap->setColor(0.75, osg::Vec4f(1,1,0,1), false); |
---|
225 | defaultColorMap->setColor(1.00, osg::Vec4f(1,0,0,1), false); |
---|
226 | defaultColorMap->updateImage(); |
---|
227 | addColorMap("default", defaultColorMap); |
---|
228 | osg::TransferFunction1D *defaultGrayColorMap = new osg::TransferFunction1D; |
---|
229 | defaultGrayColorMap->allocate(256); |
---|
230 | defaultGrayColorMap->setColor(0, osg::Vec4f(0,0,0,1), false); |
---|
231 | defaultGrayColorMap->setColor(1, osg::Vec4f(1,1,1,1), false); |
---|
232 | defaultGrayColorMap->updateImage(); |
---|
233 | addColorMap("grayDefault", defaultGrayColorMap); |
---|
234 | #endif |
---|
235 | } |
---|
236 | |
---|
237 | void Renderer::saveCLRFile(const std::string& path, osg::TransferFunction1D *xfer) |
---|
238 | { |
---|
239 | std::ofstream out(path.c_str()); |
---|
240 | float value; |
---|
241 | unsigned int r, g, b, a; |
---|
242 | for (osg::TransferFunction1D::ColorMap::const_iterator itr = xfer->getColorMap().begin(); |
---|
243 | itr != xfer->getColorMap().end(); ++itr) { |
---|
244 | value = itr->first; |
---|
245 | r = (unsigned int)(255.0 * itr->second.r()); |
---|
246 | g = (unsigned int)(255.0 * itr->second.g()); |
---|
247 | b = (unsigned int)(255.0 * itr->second.b()); |
---|
248 | a = (unsigned int)(255.0 * itr->second.a()); |
---|
249 | out << value << " " << r << " " << g << " " << b << " " << a << std::endl; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | void Renderer::getColorMapRange(const ColorMapId& id, float *min, float *max) const |
---|
254 | { |
---|
255 | ColorMapHashmap::const_iterator itr = _colorMaps.find(id); |
---|
256 | if (itr == _colorMaps.end()) { |
---|
257 | ERROR("Unknown ColorMap %s", id.c_str()); |
---|
258 | return; |
---|
259 | } |
---|
260 | |
---|
261 | *min = itr->second->getMinimum(); |
---|
262 | *max = itr->second->getMaximum(); |
---|
263 | } |
---|
264 | |
---|
265 | std::string Renderer::getColorMapFilePath(const ColorMapId& id) const |
---|
266 | { |
---|
267 | std::ostringstream path; |
---|
268 | path << "/tmp/" << id << "_" << getpid() << ".clr"; |
---|
269 | return path.str(); |
---|
270 | } |
---|
271 | |
---|
272 | void Renderer::addColorMap(const ColorMapId& id, osg::TransferFunction1D *xfer) |
---|
273 | { |
---|
274 | _colorMaps[id] = xfer; |
---|
275 | saveCLRFile(getColorMapFilePath(id), xfer); |
---|
276 | } |
---|
277 | |
---|
278 | void Renderer::deleteColorMap(const ColorMapId& id) |
---|
279 | { |
---|
280 | ColorMapHashmap::iterator itr; |
---|
281 | bool doAll = false; |
---|
282 | |
---|
283 | if (id.compare("all") == 0) { |
---|
284 | itr = _colorMaps.begin(); |
---|
285 | doAll = true; |
---|
286 | } else { |
---|
287 | itr = _colorMaps.find(id); |
---|
288 | } |
---|
289 | |
---|
290 | if (itr == _colorMaps.end()) { |
---|
291 | if (!doAll) { |
---|
292 | ERROR("Unknown ColorMap %s", id.c_str()); |
---|
293 | } |
---|
294 | return; |
---|
295 | } |
---|
296 | |
---|
297 | do { |
---|
298 | TRACE("Deleting ColorMap %s", itr->first.c_str()); |
---|
299 | std::string path = getColorMapFilePath(itr->first); |
---|
300 | if (remove(path.c_str()) < 0) { |
---|
301 | ERROR("Failed to delete colormap file '%s': %s", |
---|
302 | path.c_str(), strerror(errno)); |
---|
303 | } |
---|
304 | itr = _colorMaps.erase(itr); |
---|
305 | } while (doAll && itr != _colorMaps.end()); |
---|
306 | } |
---|
307 | |
---|
308 | void Renderer::setColorMapNumberOfTableEntries(const ColorMapId& id, |
---|
309 | int numEntries) |
---|
310 | { |
---|
311 | ColorMapHashmap::iterator itr; |
---|
312 | bool doAll = false; |
---|
313 | |
---|
314 | if (id.compare("all") == 0) { |
---|
315 | itr = _colorMaps.begin(); |
---|
316 | doAll = true; |
---|
317 | } else { |
---|
318 | itr = _colorMaps.find(id); |
---|
319 | } |
---|
320 | |
---|
321 | if (itr == _colorMaps.end()) { |
---|
322 | ERROR("Unknown ColorMap %s", id.c_str()); |
---|
323 | return; |
---|
324 | } |
---|
325 | |
---|
326 | do { |
---|
327 | itr->second->allocate(numEntries); |
---|
328 | } while (doAll && ++itr != _colorMaps.end()); |
---|
329 | } |
---|
330 | |
---|
331 | bool |
---|
332 | Renderer::renderColorMap(const ColorMapId& id, int width, int height, |
---|
333 | osg::Image *imgData, |
---|
334 | bool opaque, float bgColor[3], |
---|
335 | bool bgr, int bytesPerPixel) const |
---|
336 | { |
---|
337 | ColorMapHashmap::const_iterator itr = _colorMaps.find(id); |
---|
338 | if (itr == _colorMaps.end()) { |
---|
339 | ERROR("Unknown ColorMap %s", id.c_str()); |
---|
340 | return false; |
---|
341 | } |
---|
342 | |
---|
343 | ColorMap::renderColorMap(itr->second, width, height, imgData, opaque, bgColor, bgr, bytesPerPixel); |
---|
344 | return true; |
---|
345 | } |
---|
346 | |
---|
347 | void Renderer::initViewer() { |
---|
348 | if (_viewer.valid()) |
---|
349 | return; |
---|
350 | _viewer = new osgViewer::Viewer(); |
---|
351 | #if 1 |
---|
352 | osg::DisplaySettings *ds = _viewer->getDisplaySettings(); |
---|
353 | if (ds == NULL) { |
---|
354 | ds = osg::DisplaySettings::instance().get(); |
---|
355 | } |
---|
356 | ds->setDoubleBuffer(false); |
---|
357 | ds->setMinimumNumAlphaBits(8); |
---|
358 | ds->setMinimumNumStencilBits(8); |
---|
359 | ds->setNumMultiSamples(0); |
---|
360 | #endif |
---|
361 | _viewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded); |
---|
362 | _viewer->getDatabasePager()->setUnrefImageDataAfterApplyPolicy(false, false); |
---|
363 | _viewer->setReleaseContextAtEndOfFrameHint(false); |
---|
364 | //_viewer->setLightingMode(osg::View::SKY_LIGHT); |
---|
365 | _viewer->getCamera()->setClearColor(osg::Vec4(_bgColor[0], _bgColor[1], _bgColor[2], 1)); |
---|
366 | _viewer->getCamera()->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
367 | _viewer->getCamera()->setNearFarRatio(0.00002); |
---|
368 | _viewer->getCamera()->setSmallFeatureCullingPixelSize(-1.0f); |
---|
369 | _stateManip = new osgGA::StateSetManipulator(_viewer->getCamera()->getOrCreateStateSet()); |
---|
370 | _viewer->addEventHandler(_stateManip); |
---|
371 | //_viewer->addEventHandler(new osgViewer::StatsHandler()); |
---|
372 | |
---|
373 | #ifdef DEBUG |
---|
374 | if (_viewer->getViewerStats() != NULL) { |
---|
375 | TRACE("Enabling stats"); |
---|
376 | _viewer->getViewerStats()->collectStats("scene", true); |
---|
377 | } |
---|
378 | #endif |
---|
379 | #if 0 |
---|
380 | osgViewer::ViewerBase::Windows windows; |
---|
381 | _viewer->getWindows(windows); |
---|
382 | if (windows.size() == 1) { |
---|
383 | windows[0]->setSyncToVBlank(false); |
---|
384 | } else { |
---|
385 | ERROR("Num windows: %lu", windows.size()); |
---|
386 | } |
---|
387 | #endif |
---|
388 | } |
---|
389 | |
---|
390 | void Renderer::finalizeViewer() { |
---|
391 | initViewer(); |
---|
392 | TRACE("Before _viewer->isRealized()"); |
---|
393 | if (!_viewer->isRealized()) { |
---|
394 | int screen = 0; |
---|
395 | const char *displayEnv = getenv("DISPLAY"); |
---|
396 | if (displayEnv != NULL) { |
---|
397 | TRACE("DISPLAY: %s", displayEnv); |
---|
398 | // 3 parts: host, display, screen |
---|
399 | int part = 0; |
---|
400 | for (size_t c = 0; c < strlen(displayEnv); c++) { |
---|
401 | if (displayEnv[c] == ':') { |
---|
402 | part = 1; |
---|
403 | } else if (part == 1 && displayEnv[c] == '.') { |
---|
404 | part = 2; |
---|
405 | } else if (part == 2) { |
---|
406 | screen = atoi(&displayEnv[c]); |
---|
407 | break; |
---|
408 | } |
---|
409 | } |
---|
410 | } |
---|
411 | TRACE("Using screen: %d", screen); |
---|
412 | #ifdef USE_OFFSCREEN_RENDERING |
---|
413 | #ifdef USE_PBUFFER |
---|
414 | osg::ref_ptr<osg::GraphicsContext> pbuffer; |
---|
415 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
---|
416 | traits->x = 0; |
---|
417 | traits->y = 0; |
---|
418 | traits->width = _windowWidth; |
---|
419 | traits->height = _windowHeight; |
---|
420 | traits->red = 8; |
---|
421 | traits->green = 8; |
---|
422 | traits->blue = 8; |
---|
423 | traits->alpha = 8; |
---|
424 | traits->windowDecoration = false; |
---|
425 | traits->pbuffer = true; |
---|
426 | traits->doubleBuffer = true; |
---|
427 | traits->sharedContext = 0; |
---|
428 | |
---|
429 | pbuffer = osg::GraphicsContext::createGraphicsContext(traits.get()); |
---|
430 | if (pbuffer.valid()) { |
---|
431 | TRACE("Pixel buffer has been created successfully."); |
---|
432 | } else { |
---|
433 | ERROR("Pixel buffer has not been created successfully."); |
---|
434 | } |
---|
435 | osg::Camera *camera = new osg::Camera; |
---|
436 | camera->setGraphicsContext(pbuffer.get()); |
---|
437 | //camera->getOrCreateStateSet()->setGlobalDefaults(); |
---|
438 | camera->setViewport(new osg::Viewport(0, 0, _windowWidth, _windowHeight)); |
---|
439 | GLenum buffer = pbuffer->getTraits()->doubleBuffer ? GL_BACK : GL_FRONT; |
---|
440 | camera->setDrawBuffer(buffer); |
---|
441 | camera->setReadBuffer(buffer); |
---|
442 | _captureCallback = new ScreenCaptureCallback(); |
---|
443 | camera->setFinalDrawCallback(_captureCallback.get()); |
---|
444 | _viewer->addSlave(camera, osg::Matrixd(), osg::Matrixd()); |
---|
445 | #else |
---|
446 | _viewer->getCamera()->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); |
---|
447 | osg::Texture2D* texture2D = new osg::Texture2D; |
---|
448 | texture2D->setTextureSize(_windowWidth, _windowHeight); |
---|
449 | texture2D->setInternalFormat(GL_RGBA); |
---|
450 | texture2D->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST); |
---|
451 | texture2D->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST); |
---|
452 | |
---|
453 | _viewer->getCamera()->setImplicitBufferAttachmentMask(0, 0); |
---|
454 | _viewer->getCamera()->attach(osg::Camera::COLOR_BUFFER0, texture2D); |
---|
455 | //_viewer->getCamera()->attach(osg::Camera::DEPTH_BUFFER, GL_DEPTH_COMPONENT24); |
---|
456 | _viewer->getCamera()->attach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER, GL_DEPTH24_STENCIL8_EXT); |
---|
457 | _captureCallback = new ScreenCaptureCallback(texture2D); |
---|
458 | _viewer->getCamera()->setFinalDrawCallback(_captureCallback.get()); |
---|
459 | _viewer->setUpViewInWindow(0, 0, _windowWidth, _windowHeight, screen); |
---|
460 | #endif |
---|
461 | #else |
---|
462 | _captureCallback = new ScreenCaptureCallback(); |
---|
463 | _viewer->getCamera()->setFinalDrawCallback(_captureCallback.get()); |
---|
464 | #if 1 |
---|
465 | _viewer->setUpViewInWindow(0, 0, _windowWidth, _windowHeight, screen); |
---|
466 | #else |
---|
467 | SingleWindow *windowConfig = new SingleWindow(0, 0, _windowWidth, _windowHeight, screen); |
---|
468 | osg::DisplaySettings *ds = windowConfig->getActiveDisplaySetting(*_viewer.get()); |
---|
469 | ds->setDoubleBuffer(false); |
---|
470 | ds->setMinimumNumAlphaBits(8); |
---|
471 | ds->setMinimumNumStencilBits(8); |
---|
472 | ds->setNumMultiSamples(0); |
---|
473 | windowConfig->setWindowDecoration(false); |
---|
474 | windowConfig->setOverrideRedirect(false); |
---|
475 | _viewer->apply(windowConfig); |
---|
476 | #endif |
---|
477 | #endif |
---|
478 | _viewer->realize(); |
---|
479 | initColorMaps(); |
---|
480 | // HACK: This seems to initialize something required for properly |
---|
481 | // mapping mouse coords |
---|
482 | assert(getEventQueue() != NULL); |
---|
483 | getEventQueue()->mouseMotion(_windowWidth/2, _windowHeight/2); |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | void Renderer::setAttribution(const std::string& attrib) |
---|
488 | { |
---|
489 | TRACE("Setting map attribution: '%s'", attrib.c_str()); |
---|
490 | _attribution = attrib; |
---|
491 | if (_copyrightLabel.valid()) { |
---|
492 | _copyrightLabel->setText(_attribution); |
---|
493 | _needsRedraw = true; |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | void Renderer::initControls() |
---|
498 | { |
---|
499 | if (!_debugBox.valid()) { |
---|
500 | _debugBox = |
---|
501 | new osgEarth::Util::Controls::HBox(osgEarth::Util::Controls::Control::ALIGN_RIGHT, |
---|
502 | osgEarth::Util::Controls::Control::ALIGN_TOP, |
---|
503 | osgEarth::Util::Controls::Gutter(0, 2, 2, 0), 2.0f); |
---|
504 | _debugLabel = |
---|
505 | new osgEarth::Util::Controls::LabelControl("Selection: None", 12.0f); |
---|
506 | _debugLabel->setForeColor(osg::Vec4f(0, 0, 0, 1)); |
---|
507 | //_debugLabel->setHaloColor(osg::Vec4f(1, 1, 1, 1)); |
---|
508 | _debugBox->addControl(_debugLabel.get()); |
---|
509 | _debugBox->setVertFill(true); |
---|
510 | _debugBox->setForeColor(osg::Vec4f(0, 0, 0, 1)); |
---|
511 | _debugBox->setBackColor(osg::Vec4f(1, 1, 1, 0.6)); |
---|
512 | osgEarth::Util::Controls::ControlCanvas::getOrCreate(_viewer.get())->addControl(_debugBox.get()); |
---|
513 | } |
---|
514 | if (_copyrightScaleBox.valid()) |
---|
515 | return; |
---|
516 | _copyrightScaleBox = |
---|
517 | new osgEarth::Util::Controls::HBox(osgEarth::Util::Controls::Control::ALIGN_RIGHT, |
---|
518 | osgEarth::Util::Controls::Control::ALIGN_BOTTOM, |
---|
519 | osgEarth::Util::Controls::Gutter(0, 2, 2, 0), 2.0f); |
---|
520 | _attribution = "Map data © OpenStreetMap"; |
---|
521 | _copyrightLabel = |
---|
522 | new osgEarth::Util::Controls::LabelControl(_attribution, 12.0f); |
---|
523 | _copyrightLabel->setForeColor(osg::Vec4f(1, 1, 1, 1)); |
---|
524 | _copyrightLabel->setHaloColor(osg::Vec4f(0, 0, 0, 1)); |
---|
525 | _copyrightLabel->setEncoding(osgText::String::ENCODING_UTF8); |
---|
526 | _scaleLabel = |
---|
527 | new osgEarth::Util::Controls::LabelControl("- km", 12.0f); |
---|
528 | _scaleLabel->setForeColor(osg::Vec4f(1, 1, 1, 1)); |
---|
529 | _scaleLabel->setHaloColor(osg::Vec4f(0, 0, 0, 1)); |
---|
530 | _scaleBar = |
---|
531 | new osgEarth::Util::Controls::Frame(); |
---|
532 | _scaleBar->setVertFill(true); |
---|
533 | _scaleBar->setForeColor(osg::Vec4f(0, 0, 0, 1)); |
---|
534 | _scaleBar->setBackColor(osg::Vec4f(1, 1, 1, 0.6)); |
---|
535 | _scaleBar->setBorderColor(osg::Vec4f(0, 0, 0 ,1)); |
---|
536 | _scaleBar->setBorderWidth(1.0); |
---|
537 | _copyrightScaleBox->addControl(_copyrightLabel.get()); |
---|
538 | _copyrightScaleBox->addControl(_scaleLabel.get()); |
---|
539 | _copyrightScaleBox->addControl(_scaleBar.get()); |
---|
540 | osgEarth::Util::Controls::ControlCanvas::getOrCreate(_viewer.get())->addControl(_copyrightScaleBox.get()); |
---|
541 | // Install an event callback to handle scale bar updates |
---|
542 | // Can't use an update callback since that will trigger |
---|
543 | // constant rendering |
---|
544 | _mapNode->setEventCallback(new MapNodeCallback(this)); |
---|
545 | } |
---|
546 | |
---|
547 | void Renderer::setGraticule(bool enable, GraticuleType type) |
---|
548 | { |
---|
549 | if (!_mapNode.valid() || !_sceneRoot.valid()) |
---|
550 | return; |
---|
551 | if (enable) { |
---|
552 | if (_graticule.valid()) { |
---|
553 | _sceneRoot->removeChild(_graticule.get()); |
---|
554 | _graticule = NULL; |
---|
555 | } |
---|
556 | switch (type) { |
---|
557 | case GRATICULE_UTM: { |
---|
558 | osgEarth::Util::UTMGraticule *gr = new osgEarth::Util::UTMGraticule(_mapNode.get()); |
---|
559 | _sceneRoot->addChild(gr); |
---|
560 | _graticule = gr; |
---|
561 | } |
---|
562 | break; |
---|
563 | case GRATICULE_MGRS: { |
---|
564 | osgEarth::Util::MGRSGraticule *gr = new osgEarth::Util::MGRSGraticule(_mapNode.get()); |
---|
565 | _sceneRoot->addChild(gr); |
---|
566 | _graticule = gr; |
---|
567 | } |
---|
568 | break; |
---|
569 | case GRATICULE_SHADER: { |
---|
570 | osgEarth::Util::GraticuleOptions opt; |
---|
571 | opt.color() = osgEarth::Symbology::Color(1, 1, 0); |
---|
572 | opt.labelColor() = osgEarth::Symbology::Color(1, 1, 0); |
---|
573 | opt.lineWidth() = 1.0; |
---|
574 | osgEarth::Util::GraticuleNode *gr = new osgEarth::Util::GraticuleNode(_mapNode.get(), opt); |
---|
575 | _sceneRoot->addChild(gr); |
---|
576 | _graticule = gr; |
---|
577 | } |
---|
578 | break; |
---|
579 | case GRATICULE_GEODETIC: |
---|
580 | default: |
---|
581 | osgEarth::Util::GeodeticGraticule *gr = new osgEarth::Util::GeodeticGraticule(_mapNode.get()); |
---|
582 | osgEarth::Util::GeodeticGraticuleOptions opt = gr->getOptions(); |
---|
583 | opt.lineStyle()->getOrCreate<osgEarth::Symbology::LineSymbol>()->stroke()->color().set(1,0,0,1); |
---|
584 | gr->setOptions(opt); |
---|
585 | _sceneRoot->addChild(gr); |
---|
586 | _graticule = gr; |
---|
587 | } |
---|
588 | } else if (_graticule.valid()) { |
---|
589 | _sceneRoot->removeChild(_graticule.get()); |
---|
590 | _graticule = NULL; |
---|
591 | } |
---|
592 | _needsRedraw = true; |
---|
593 | } |
---|
594 | |
---|
595 | void Renderer::setReadout(int x, int y) |
---|
596 | { |
---|
597 | if (!_coordsCallback.valid() || !_mapNode.valid() || !_viewer.valid()) |
---|
598 | return; |
---|
599 | |
---|
600 | osgEarth::GeoPoint mapCoord; |
---|
601 | if (mapMouseCoords(x, y, mapCoord)) { |
---|
602 | _coordsCallback->set(mapCoord, _viewer->asView(), _mapNode); |
---|
603 | } else { |
---|
604 | _coordsCallback->reset(_viewer->asView(), _mapNode); |
---|
605 | } |
---|
606 | _needsRedraw = true; |
---|
607 | } |
---|
608 | |
---|
609 | void Renderer::clearReadout() |
---|
610 | { |
---|
611 | if (_coordsCallback.valid()) { |
---|
612 | _coordsCallback->reset(_viewer->asView(), _mapNode); |
---|
613 | } |
---|
614 | _needsRedraw = true; |
---|
615 | } |
---|
616 | |
---|
617 | void Renderer::setCoordinateReadout(bool state, CoordinateDisplayType type, |
---|
618 | int precision) |
---|
619 | { |
---|
620 | if (!state) { |
---|
621 | if (_mouseCoordsTool.valid() && _viewer.valid()) { |
---|
622 | _viewer->removeEventHandler(_mouseCoordsTool.get()); |
---|
623 | _mouseCoordsTool = NULL; |
---|
624 | } |
---|
625 | if (_coordsCallback.valid()) { |
---|
626 | _coordsCallback = NULL; |
---|
627 | } |
---|
628 | if (_coordsBox.valid() && _viewer.valid()) { |
---|
629 | _coordsBox->clearControls(); |
---|
630 | osgEarth::Util::Controls::ControlCanvas::getOrCreate(_viewer.get())->removeControl(_coordsBox.get()); |
---|
631 | } |
---|
632 | _coordsBox = NULL; |
---|
633 | } else { |
---|
634 | initMouseCoordsTool(type, precision); |
---|
635 | } |
---|
636 | _needsRedraw = true; |
---|
637 | } |
---|
638 | |
---|
639 | osgEarth::Util::MGRSFormatter::Precision |
---|
640 | Renderer::getMGRSPrecision(int precisionInMeters) |
---|
641 | { |
---|
642 | switch (precisionInMeters) { |
---|
643 | case 1: |
---|
644 | return osgEarth::Util::MGRSFormatter::PRECISION_1M; |
---|
645 | case 10: |
---|
646 | return osgEarth::Util::MGRSFormatter::PRECISION_10M; |
---|
647 | case 100: |
---|
648 | return osgEarth::Util::MGRSFormatter::PRECISION_100M; |
---|
649 | case 1000: |
---|
650 | return osgEarth::Util::MGRSFormatter::PRECISION_1000M; |
---|
651 | case 10000: |
---|
652 | return osgEarth::Util::MGRSFormatter::PRECISION_10000M; |
---|
653 | case 100000: |
---|
654 | return osgEarth::Util::MGRSFormatter::PRECISION_100000M; |
---|
655 | default: |
---|
656 | ERROR("Invalid precision: %d", precisionInMeters); |
---|
657 | return osgEarth::Util::MGRSFormatter::PRECISION_1M; |
---|
658 | } |
---|
659 | } |
---|
660 | |
---|
661 | void Renderer::initMouseCoordsTool(CoordinateDisplayType type, int precision) |
---|
662 | { |
---|
663 | if (!_viewer.valid()) |
---|
664 | return; |
---|
665 | if (_mouseCoordsTool.valid()) { |
---|
666 | _viewer->removeEventHandler(_mouseCoordsTool.get()); |
---|
667 | } |
---|
668 | _mouseCoordsTool = new MouseCoordsTool(_mapNode.get()); |
---|
669 | osgEarth::Util::Controls::LabelControl *readout; |
---|
670 | if (_coordsCallback.valid()) { |
---|
671 | readout = _coordsCallback->getLabel(); |
---|
672 | _coordsCallback = NULL; |
---|
673 | } else { |
---|
674 | readout = new osgEarth::Util::Controls::LabelControl("Lat/Long: N/A", 12.0f); |
---|
675 | readout->setForeColor(osg::Vec4f(0, 0, 0, 1)); |
---|
676 | _coordsBox = |
---|
677 | new osgEarth::Util::Controls::HBox(osgEarth::Util::Controls::Control::ALIGN_LEFT, |
---|
678 | osgEarth::Util::Controls::Control::ALIGN_TOP, |
---|
679 | osgEarth::Util::Controls::Gutter(0, 2, 2, 0), 2.0f); |
---|
680 | _coordsBox->setVertFill(true); |
---|
681 | _coordsBox->setForeColor(osg::Vec4f(0, 0, 0, 1)); |
---|
682 | _coordsBox->setBackColor(osg::Vec4f(1, 1, 1, 0.6)); |
---|
683 | _coordsBox->addControl(readout); |
---|
684 | osgEarth::Util::Controls::ControlCanvas::getOrCreate(_viewer.get())->addControl(_coordsBox.get()); |
---|
685 | } |
---|
686 | |
---|
687 | osgEarth::Util::Formatter *formatter = NULL; |
---|
688 | if (type == COORDS_MGRS) { |
---|
689 | osgEarth::Util::MGRSFormatter::Precision prec = |
---|
690 | osgEarth::Util::MGRSFormatter::PRECISION_1M; |
---|
691 | if (precision > 0) { |
---|
692 | prec = getMGRSPrecision(precision); |
---|
693 | } |
---|
694 | unsigned int opts = 0u; |
---|
695 | formatter = new osgEarth::Util::MGRSFormatter(prec, NULL, opts); |
---|
696 | } else { |
---|
697 | osgEarth::Util::LatLongFormatter::AngularFormat af; |
---|
698 | unsigned int opts = osgEarth::Util::LatLongFormatter::USE_SYMBOLS; |
---|
699 | switch (type) { |
---|
700 | case COORDS_LATLONG_DEGREES_DECIMAL_MINUTES: |
---|
701 | af = osgEarth::Util::LatLongFormatter::FORMAT_DEGREES_DECIMAL_MINUTES; |
---|
702 | break; |
---|
703 | case COORDS_LATLONG_DEGREES_MINUTES_SECONDS: |
---|
704 | af = osgEarth::Util::LatLongFormatter::FORMAT_DEGREES_MINUTES_SECONDS; |
---|
705 | break; |
---|
706 | default: |
---|
707 | af = osgEarth::Util::LatLongFormatter::FORMAT_DECIMAL_DEGREES; |
---|
708 | } |
---|
709 | osgEarth::Util::LatLongFormatter *latlong = new osgEarth::Util::LatLongFormatter(af, opts); |
---|
710 | if (precision > 0) { |
---|
711 | latlong->setPrecision(precision); |
---|
712 | } |
---|
713 | formatter = latlong; |
---|
714 | } |
---|
715 | _coordsCallback = new MouseCoordsCallback(readout, formatter); |
---|
716 | |
---|
717 | _mouseCoordsTool->addCallback(_coordsCallback.get()); |
---|
718 | _viewer->addEventHandler(_mouseCoordsTool.get()); |
---|
719 | } |
---|
720 | |
---|
721 | void Renderer::initEarthManipulator() |
---|
722 | { |
---|
723 | _manipulator = new osgEarth::Util::EarthManipulator; |
---|
724 | #if 1 |
---|
725 | osgEarth::Util::EarthManipulator::Settings *settings = _manipulator->getSettings(); |
---|
726 | |
---|
727 | settings->bindMouse(osgEarth::Util::EarthManipulator::ACTION_ROTATE, |
---|
728 | osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON, |
---|
729 | osgGA::GUIEventAdapter::MODKEY_ALT); |
---|
730 | osgEarth::Util::EarthManipulator::ActionOptions options; |
---|
731 | options.clear(); |
---|
732 | options.add(osgEarth::Util::EarthManipulator::OPTION_CONTINUOUS, true); |
---|
733 | settings->bindMouse(osgEarth::Util::EarthManipulator::ACTION_ZOOM, |
---|
734 | osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON, |
---|
735 | osgGA::GUIEventAdapter::MODKEY_ALT, options); |
---|
736 | |
---|
737 | if (_map->isGeocentric()) { |
---|
738 | settings->setMinMaxDistance(1.0, 2.0e7); |
---|
739 | } else { |
---|
740 | osgEarth::GeoExtent extent = _map->getProfile()->getExtent(); |
---|
741 | settings->setMinMaxDistance(1.0, getMaxDistanceFromExtent(extent)); |
---|
742 | } |
---|
743 | |
---|
744 | _manipulator->applySettings(settings); |
---|
745 | #endif |
---|
746 | _viewer->setCameraManipulator(_manipulator.get()); |
---|
747 | _manipulator->setNode(NULL); |
---|
748 | _manipulator->setNode(_sceneRoot.get()); |
---|
749 | //_manipulator->computeHomePosition(); |
---|
750 | } |
---|
751 | |
---|
752 | void Renderer::loadEarthFile(const char *path) |
---|
753 | { |
---|
754 | TRACE("Loading %s", path); |
---|
755 | osg::Node *node = osgDB::readNodeFile(path); |
---|
756 | if (node == NULL) { |
---|
757 | ERROR("Couldn't load %s", path); |
---|
758 | return; |
---|
759 | } |
---|
760 | osgEarth::MapNode *mapNode = osgEarth::MapNode::findMapNode(node); |
---|
761 | if (mapNode == NULL) { |
---|
762 | ERROR("Couldn't find MapNode"); |
---|
763 | return; |
---|
764 | } else { |
---|
765 | initViewer(); |
---|
766 | _sceneRoot = new osg::Group; |
---|
767 | _sceneRoot->addChild(node); |
---|
768 | _map = mapNode->getMap(); |
---|
769 | } |
---|
770 | _mapNode = mapNode; |
---|
771 | |
---|
772 | if (_clipPlaneCullCallback.valid()) { |
---|
773 | _viewer->getCamera()->removeCullCallback(_clipPlaneCullCallback.get()); |
---|
774 | _clipPlaneCullCallback = NULL; |
---|
775 | } |
---|
776 | if (_map->isGeocentric()) { |
---|
777 | _clipPlaneCullCallback = new osgEarth::Util::AutoClipPlaneCullCallback(mapNode); |
---|
778 | _viewer->getCamera()->addCullCallback(_clipPlaneCullCallback.get()); |
---|
779 | } |
---|
780 | _viewer->setSceneData(_sceneRoot.get()); |
---|
781 | |
---|
782 | if (_mouseCoordsTool.valid()) { |
---|
783 | initMouseCoordsTool(); |
---|
784 | } |
---|
785 | initControls(); |
---|
786 | initEarthManipulator(); |
---|
787 | |
---|
788 | _viewer->home(); |
---|
789 | finalizeViewer(); |
---|
790 | _needsRedraw = true; |
---|
791 | } |
---|
792 | |
---|
793 | void Renderer::resetMap(osgEarth::MapOptions::CoordinateSystemType type, |
---|
794 | const osg::Vec4f& bgColor, |
---|
795 | const char *profile, |
---|
796 | double bounds[4]) |
---|
797 | { |
---|
798 | TRACE("Restting map with type %d, profile %s", type, profile); |
---|
799 | |
---|
800 | osgEarth::MapOptions mapOpts; |
---|
801 | mapOpts.coordSysType() = type; |
---|
802 | if (profile != NULL) { |
---|
803 | if (bounds != NULL) { |
---|
804 | mapOpts.profile() = osgEarth::ProfileOptions(); |
---|
805 | if (strcmp(profile, "geodetic") == 0) { |
---|
806 | mapOpts.profile()->srsString() = "epsg:4326"; // WGS84 |
---|
807 | } else if (strcmp(profile, "spherical-mercator") == 0) { |
---|
808 | // Projection used by Google/Bing/OSM |
---|
809 | // aka epsg:900913 meters in x/y |
---|
810 | // aka WGS84 Web Mercator (Auxiliary Sphere) |
---|
811 | // X/Y: -20037508.34m to 20037508.34m |
---|
812 | mapOpts.profile()->srsString() = "epsg:3857"; |
---|
813 | } else { |
---|
814 | mapOpts.profile()->srsString() = profile; |
---|
815 | } |
---|
816 | TRACE("Setting profile bounds: %g %g %g %g", |
---|
817 | bounds[0], bounds[1], bounds[2], bounds[3]); |
---|
818 | if (1) { |
---|
819 | osg::ref_ptr<osgEarth::SpatialReference> fromSRS = osgEarth::SpatialReference::create("wgs84"); |
---|
820 | osg::ref_ptr<osgEarth::SpatialReference> toSRS = osgEarth::SpatialReference::create(mapOpts.profile()->srsString().get()); |
---|
821 | osgEarth::Bounds extents(bounds[0], bounds[1], bounds[2], bounds[3]); |
---|
822 | extents.transform(fromSRS, toSRS); |
---|
823 | mapOpts.profile()->bounds() = extents; |
---|
824 | //mapOpts.profile()->numTilesWideAtLod0() = 1; |
---|
825 | //mapOpts.profile()->numTilesHighAtLod0() = 2; |
---|
826 | } else { |
---|
827 | mapOpts.profile()->bounds() = |
---|
828 | osgEarth::Bounds(bounds[0], bounds[1], bounds[2], bounds[3]); |
---|
829 | } |
---|
830 | } else { |
---|
831 | mapOpts.profile() = osgEarth::ProfileOptions(profile); |
---|
832 | } |
---|
833 | } else if (type == osgEarth::MapOptions::CSTYPE_PROJECTED) { |
---|
834 | mapOpts.profile() = osgEarth::ProfileOptions("global-mercator"); |
---|
835 | } |
---|
836 | |
---|
837 | #ifdef USE_CACHE |
---|
838 | setupCache(); |
---|
839 | osgEarth::Drivers::FileSystemCacheOptions cacheOpts; |
---|
840 | cacheOpts.rootPath() = _cacheDir; |
---|
841 | mapOpts.cache() = cacheOpts; |
---|
842 | #endif |
---|
843 | |
---|
844 | initViewer(); |
---|
845 | |
---|
846 | //mapOpts.referenceURI() = _baseURI; |
---|
847 | osgEarth::Map *map = new osgEarth::Map(mapOpts); |
---|
848 | _map = map; |
---|
849 | osgEarth::Drivers::GDALOptions bopts; |
---|
850 | bopts.url() = getBaseImage(); |
---|
851 | addImageLayer("base", bopts); |
---|
852 | #ifdef USE_REX |
---|
853 | osgEarth::Drivers::RexTerrainEngine::RexTerrainEngineOptions engineOpt; |
---|
854 | #else |
---|
855 | osgEarth::Drivers::MPTerrainEngine::MPTerrainEngineOptions engineOpt; |
---|
856 | #endif |
---|
857 | // Set background layer color |
---|
858 | engineOpt.color() = bgColor; |
---|
859 | //engineOpt.minLOD() = 1; |
---|
860 | // Sets shader uniform for terrain renderer (config var defaults to false) |
---|
861 | if (!_map->isGeocentric()) { |
---|
862 | engineOpt.enableLighting() = false; |
---|
863 | } |
---|
864 | osgEarth::MapNodeOptions mapNodeOpts(engineOpt); |
---|
865 | // Sets GL_LIGHTING state in MapNode StateSet (config var defaults to true) |
---|
866 | //mapNodeOpts.enableLighting() = true; |
---|
867 | #if 0 |
---|
868 | osgEarth::TerrainOptions terrOpts; |
---|
869 | //terrOpts.loadingPolicy().mapLoadingThreadsPerCore() = 1; |
---|
870 | //terrOpts.loadingPolicy().numLoadingThreads() = 1; |
---|
871 | //terrOpts.loadingPolicy().numCompileThreadsPerCore() = 1; |
---|
872 | //terrOpts.loadingPolicy().numCompileThreads() = 1; |
---|
873 | if (_map->isGeocentric()) { |
---|
874 | // The default is 17 posts |
---|
875 | terrOpts.tileSize() = 17; |
---|
876 | } else { |
---|
877 | // Can reduce if we don't have elevation data |
---|
878 | terrOpts.tileSize() = 2; |
---|
879 | } |
---|
880 | mapNodeOpts.setTerrainOptions(terrOpts); |
---|
881 | #endif |
---|
882 | osgEarth::MapNode *mapNode = new osgEarth::MapNode(map, mapNodeOpts); |
---|
883 | _mapNode = mapNode; |
---|
884 | if (_map->isGeocentric()) { |
---|
885 | osgEarth::DateTime now; |
---|
886 | TRACE("Creating SkyNode"); |
---|
887 | osgEarth::Drivers::SimpleSky::SimpleSkyOptions skyOpts; |
---|
888 | skyOpts.hours() = now.hours(); |
---|
889 | skyOpts.ambient() = 0.2f; |
---|
890 | skyOpts.atmosphericLighting() = true; |
---|
891 | skyOpts.exposure() = 3.0; |
---|
892 | _skyNode = osgEarth::Util::SkyNode::create(skyOpts, mapNode); |
---|
893 | _skyNode->addChild(mapNode); |
---|
894 | _skyNode->attach(_viewer.get(), 0); |
---|
895 | _sceneRoot = new osg::Group(); |
---|
896 | _sceneRoot->addChild(_skyNode.get()); |
---|
897 | //_sceneRoot = _skyNode; |
---|
898 | } else { |
---|
899 | _sceneRoot = new osg::Group(); |
---|
900 | _sceneRoot->addChild(_mapNode.get()); |
---|
901 | } |
---|
902 | |
---|
903 | if (_clipPlaneCullCallback.valid()) { |
---|
904 | _viewer->getCamera()->removeCullCallback(_clipPlaneCullCallback.get()); |
---|
905 | _clipPlaneCullCallback = NULL; |
---|
906 | } |
---|
907 | if (_map->isGeocentric()) { |
---|
908 | _clipPlaneCullCallback = new osgEarth::Util::AutoClipPlaneCullCallback(mapNode); |
---|
909 | _viewer->getCamera()->addCullCallback(_clipPlaneCullCallback.get()); |
---|
910 | } |
---|
911 | _viewer->setSceneData(_sceneRoot.get()); |
---|
912 | if (_mouseCoordsTool.valid()) { |
---|
913 | initMouseCoordsTool(); |
---|
914 | } |
---|
915 | #ifdef USE_RTT_PICKER |
---|
916 | if (_picker.valid()) { |
---|
917 | _viewer->removeEventHandler(_picker.get()); |
---|
918 | _picker = NULL; |
---|
919 | } |
---|
920 | if (!_picker.valid()) { |
---|
921 | _picker = new osgEarth::Util::RTTPicker; |
---|
922 | _picker->addChild(mapNode); |
---|
923 | //osg::ref_ptr<HoverCallback> callback = new HoverCallback(this); |
---|
924 | osg::ref_ptr<SelectCallback> callback = new SelectCallback(this); |
---|
925 | _picker->setDefaultCallback(callback.get()); |
---|
926 | _viewer->addEventHandler(_picker.get()); |
---|
927 | } |
---|
928 | #endif |
---|
929 | initControls(); |
---|
930 | //_viewer->setSceneData(_sceneRoot.get()); |
---|
931 | initEarthManipulator(); |
---|
932 | _viewer->home(); |
---|
933 | |
---|
934 | finalizeViewer(); |
---|
935 | _needsRedraw = true; |
---|
936 | } |
---|
937 | |
---|
938 | void Renderer::clearMap() |
---|
939 | { |
---|
940 | if (_map.valid()) { |
---|
941 | _map->clear(); |
---|
942 | _needsRedraw = true; |
---|
943 | } |
---|
944 | } |
---|
945 | |
---|
946 | void Renderer::setSkyAmbient(float value) |
---|
947 | { |
---|
948 | #if OSG_MIN_VERSION_REQUIRED(2, 7, 0) |
---|
949 | if (_skyNode.valid()) { |
---|
950 | _skyNode->setMinimumAmbient(osg::Vec4f(value, value, value, 1.0f)); |
---|
951 | _needsRedraw = true; |
---|
952 | } |
---|
953 | #endif |
---|
954 | } |
---|
955 | |
---|
956 | void Renderer::setLighting(bool state) |
---|
957 | { |
---|
958 | if (_mapNode.valid()) { |
---|
959 | TRACE("Setting lighting: %d", state ? 1 : 0); |
---|
960 | _mapNode->getOrCreateStateSet() |
---|
961 | ->setMode(GL_LIGHTING, state ? 1 : 0); |
---|
962 | _needsRedraw = true; |
---|
963 | } |
---|
964 | } |
---|
965 | |
---|
966 | void Renderer::setViewerLightType(osg::View::LightingMode mode) |
---|
967 | { |
---|
968 | if (_viewer.valid()) { |
---|
969 | _viewer->setLightingMode(mode); |
---|
970 | _needsRedraw = true; |
---|
971 | } |
---|
972 | } |
---|
973 | |
---|
974 | void Renderer::setTerrainVerticalScale(double scale) |
---|
975 | { |
---|
976 | if (_mapNode.valid()) { |
---|
977 | if (!_verticalScale.valid()) { |
---|
978 | _verticalScale = new osgEarth::Util::VerticalScale; |
---|
979 | _mapNode->getTerrainEngine()->addEffect(_verticalScale); |
---|
980 | } |
---|
981 | _verticalScale->setScale(scale); |
---|
982 | _needsRedraw = true; |
---|
983 | } |
---|
984 | } |
---|
985 | |
---|
986 | void Renderer::setEphemerisTime(time_t utcTime) |
---|
987 | { |
---|
988 | if (_skyNode.valid()) { |
---|
989 | osgEarth::DateTime time(utcTime); |
---|
990 | _skyNode->setDateTime(time); |
---|
991 | _needsRedraw = true; |
---|
992 | } |
---|
993 | } |
---|
994 | |
---|
995 | void Renderer::setEphemerisTime(int year, int month, int day, double hours) |
---|
996 | { |
---|
997 | if (_skyNode.valid()) { |
---|
998 | osgEarth::DateTime time(year, month, day, hours); |
---|
999 | _skyNode->setDateTime(time); |
---|
1000 | _needsRedraw = true; |
---|
1001 | } |
---|
1002 | } |
---|
1003 | |
---|
1004 | void Renderer::setTerrainColor(const osg::Vec4f& color) |
---|
1005 | { |
---|
1006 | // XXX: Don't know how to change this at runtime without a map reset |
---|
1007 | _needsRedraw = true; |
---|
1008 | } |
---|
1009 | |
---|
1010 | void Renderer::setTerrainLighting(bool state) |
---|
1011 | { |
---|
1012 | if (_skyNode.valid()) { |
---|
1013 | _skyNode->setLighting((state ? osg::StateAttribute::ON : osg::StateAttribute::OFF)); |
---|
1014 | } else { |
---|
1015 | #if 1 |
---|
1016 | if (!_mapNode.valid()) { |
---|
1017 | ERROR("No map node"); |
---|
1018 | return; |
---|
1019 | } |
---|
1020 | // XXX: HACK alert |
---|
1021 | // Find the terrain engine container (might be above one or more decorators) |
---|
1022 | osg::Group *group = _mapNode->getTerrainEngine(); |
---|
1023 | while (group->getParent(0) != NULL && group->getParent(0) != _mapNode.get()) { |
---|
1024 | group = group->getParent(0); |
---|
1025 | } |
---|
1026 | if (group != NULL && group->getParent(0) == _mapNode.get()) { |
---|
1027 | TRACE("Setting terrain lighting: %d", state ? 1 : 0); |
---|
1028 | if (group->getOrCreateStateSet()->getUniform("oe_mode_GL_LIGHTING") != NULL) { |
---|
1029 | group->getStateSet()->getUniform("oe_mode_GL_LIGHTING")->set(state); |
---|
1030 | } else { |
---|
1031 | ERROR("Can't get terrain lighting uniform"); |
---|
1032 | } |
---|
1033 | } else { |
---|
1034 | ERROR("Can't find terrain engine container"); |
---|
1035 | } |
---|
1036 | #else |
---|
1037 | if (_stateManip.valid()) { |
---|
1038 | _stateManip->setLightingEnabled(state); |
---|
1039 | } |
---|
1040 | #endif |
---|
1041 | } |
---|
1042 | _needsRedraw = true; |
---|
1043 | } |
---|
1044 | |
---|
1045 | void Renderer::setTerrainWireframe(bool state) |
---|
1046 | { |
---|
1047 | if (!_map.valid()) { |
---|
1048 | ERROR("No map"); |
---|
1049 | return; |
---|
1050 | } |
---|
1051 | #if 0 |
---|
1052 | if (!_mapNode.valid()) { |
---|
1053 | ERROR("No map node"); |
---|
1054 | return; |
---|
1055 | } |
---|
1056 | TRACE("Setting terrain wireframe: %d", state ? 1 : 0); |
---|
1057 | osg::StateSet *state = _mapNode->getOrCreateStateSet(); |
---|
1058 | osg::PolygonMode *pmode = dynamic_cast< osg::PolygonMode* >(state->getAttribute(osg::StateAttribute::POLYGONMODE)); |
---|
1059 | if (pmode == NULL) { |
---|
1060 | pmode = new osg::PolygonMode; |
---|
1061 | state->setAttribute(pmode); |
---|
1062 | } |
---|
1063 | if (state) { |
---|
1064 | pmode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE); |
---|
1065 | } else { |
---|
1066 | pmode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL); |
---|
1067 | } |
---|
1068 | _needsRedraw = true; |
---|
1069 | #else |
---|
1070 | if (_stateManip.valid()) { |
---|
1071 | _stateManip->setPolygonMode(state ? osg::PolygonMode::LINE : osg::PolygonMode::FILL); |
---|
1072 | _needsRedraw = true; |
---|
1073 | } |
---|
1074 | #endif |
---|
1075 | } |
---|
1076 | |
---|
1077 | void Renderer::saveNamedViewpoint(const char *name) |
---|
1078 | { |
---|
1079 | _viewpoints[name] = getViewpoint(); |
---|
1080 | } |
---|
1081 | |
---|
1082 | bool Renderer::removeNamedViewpoint(const char *name) |
---|
1083 | { |
---|
1084 | ViewpointHashmap::iterator itr = _viewpoints.find(name); |
---|
1085 | if (itr != _viewpoints.end()) { |
---|
1086 | _viewpoints.erase(name); |
---|
1087 | return true; |
---|
1088 | } else { |
---|
1089 | ERROR("Unknown viewpoint: '%s'", name); |
---|
1090 | return false; |
---|
1091 | } |
---|
1092 | } |
---|
1093 | |
---|
1094 | bool Renderer::restoreNamedViewpoint(const char *name, double durationSecs) |
---|
1095 | { |
---|
1096 | ViewpointHashmap::iterator itr = _viewpoints.find(name); |
---|
1097 | if (itr != _viewpoints.end()) { |
---|
1098 | setViewpoint(itr->second, durationSecs); |
---|
1099 | return true; |
---|
1100 | } else { |
---|
1101 | ERROR("Unknown viewpoint: '%s'", name); |
---|
1102 | return false; |
---|
1103 | } |
---|
1104 | } |
---|
1105 | |
---|
1106 | void Renderer::setViewpoint(const osgEarth::Viewpoint& v, double durationSecs) |
---|
1107 | { |
---|
1108 | if (_manipulator.valid()) { |
---|
1109 | TRACE("Setting viewpoint: %g %g %g %g %g %g", |
---|
1110 | v.focalPoint()->x(), v.focalPoint()->y(), v.focalPoint()->z(), |
---|
1111 | v.heading()->as(osgEarth::Units::DEGREES), |
---|
1112 | v.pitch()->as(osgEarth::Units::DEGREES), |
---|
1113 | v.range()->as(osgEarth::Units::METERS)); |
---|
1114 | _manipulator->setViewpoint(v, durationSecs); |
---|
1115 | _needsRedraw = true; |
---|
1116 | } else { |
---|
1117 | ERROR("No manipulator"); |
---|
1118 | } |
---|
1119 | } |
---|
1120 | |
---|
1121 | osgEarth::Viewpoint Renderer::getViewpoint() |
---|
1122 | { |
---|
1123 | if (_manipulator.valid()) { |
---|
1124 | return _manipulator->getViewpoint(); |
---|
1125 | } else { |
---|
1126 | // Uninitialized, invalid viewpoint |
---|
1127 | return osgEarth::Viewpoint(); |
---|
1128 | } |
---|
1129 | } |
---|
1130 | |
---|
1131 | static void srsInfo(const osgEarth::SpatialReference *srs) |
---|
1132 | { |
---|
1133 | TRACE("SRS: %s", srs->getName().c_str()); |
---|
1134 | TRACE("horiz: \"%s\" vert: \"%s\"", srs->getHorizInitString().c_str(), srs->getVertInitString().c_str()); |
---|
1135 | TRACE("geographic: %d geodetic: %d projected: %d ecef: %d mercator: %d spherical mercator: %d northpolar: %d southpolar: %d userdefined: %d contiguous: %d cube: %d ltp: %d plate_carre: %d", |
---|
1136 | srs->isGeographic() ? 1 : 0, |
---|
1137 | srs->isGeodetic() ? 1 : 0, |
---|
1138 | srs->isProjected() ? 1 : 0, |
---|
1139 | srs->isECEF() ? 1 : 0, |
---|
1140 | srs->isMercator() ? 1 : 0, |
---|
1141 | srs->isSphericalMercator() ? 1 : 0, |
---|
1142 | srs->isNorthPolar() ? 1 : 0, |
---|
1143 | srs->isSouthPolar() ? 1 : 0, |
---|
1144 | srs->isUserDefined() ? 1 : 0, |
---|
1145 | srs->isContiguous() ? 1 : 0, |
---|
1146 | srs->isCube() ? 1 : 0, |
---|
1147 | srs->isLTP() ? 1 : 0, |
---|
1148 | srs->isPlateCarre() ? 1 : 0); |
---|
1149 | } |
---|
1150 | |
---|
1151 | bool Renderer::getWorldCoords(const osgEarth::GeoPoint& mapPt, osg::Vec3d *world) |
---|
1152 | { |
---|
1153 | if (!_mapNode.valid() || _mapNode->getTerrain() == NULL) { |
---|
1154 | ERROR("No map"); |
---|
1155 | return false; |
---|
1156 | } |
---|
1157 | TRACE("Input SRS:"); |
---|
1158 | srsInfo(mapPt.getSRS()); |
---|
1159 | TRACE("Map SRS:"); |
---|
1160 | srsInfo(_mapNode->getMapSRS()); |
---|
1161 | bool ret = mapPt.toWorld(*world, _mapNode->getTerrain()); |
---|
1162 | TRACE("In: %g,%g,%g Out: %g,%g,%g", |
---|
1163 | mapPt.x(), mapPt.y(), mapPt.z(), |
---|
1164 | world->x(), world->y(), world->z()); |
---|
1165 | return ret; |
---|
1166 | } |
---|
1167 | |
---|
1168 | bool Renderer::worldToScreen(const osg::Vec3d& world, osg::Vec3d *screen, bool invertY) |
---|
1169 | { |
---|
1170 | if (!_viewer.valid()) { |
---|
1171 | ERROR("No viewer"); |
---|
1172 | return false; |
---|
1173 | } |
---|
1174 | osg::Camera *cam = _viewer->getCamera(); |
---|
1175 | osg::Matrixd MVP = cam->getViewMatrix() * cam->getProjectionMatrix(); |
---|
1176 | // Get clip coords |
---|
1177 | osg::Vec4d pt; |
---|
1178 | pt = osg::Vec4d(world, 1.0) * MVP; |
---|
1179 | // Clip |
---|
1180 | if (pt.x() < -pt.w() || |
---|
1181 | pt.x() > pt.w() || |
---|
1182 | pt.y() < -pt.w() || |
---|
1183 | pt.y() > pt.w() || |
---|
1184 | pt.z() < -pt.w() || |
---|
1185 | pt.z() > pt.w()) { |
---|
1186 | // Outside frustum |
---|
1187 | TRACE("invalid pt: %g,%g,%g,%g", pt.x(), pt.y(), pt.z(), pt.w()); |
---|
1188 | return false; |
---|
1189 | } |
---|
1190 | TRACE("clip pt: %g,%g,%g,%g", pt.x(), pt.y(), pt.z(), pt.w()); |
---|
1191 | // Perspective divide: now NDC |
---|
1192 | pt /= pt.w(); |
---|
1193 | const osg::Viewport *viewport = cam->getViewport(); |
---|
1194 | #if 1 |
---|
1195 | screen->x() = viewport->x() + viewport->width() * 0.5 + pt.x() * viewport->width() * 0.5; |
---|
1196 | screen->y() = viewport->y() + viewport->height() * 0.5 + pt.y() * viewport->height() * 0.5; |
---|
1197 | //double near = 0; |
---|
1198 | //double far = 1; |
---|
1199 | //screen->z() = (far + near) * 0.5 + (far - near) * 0.5 * pt.z(); |
---|
1200 | screen->z() = 0.5 + 0.5 * pt.z(); |
---|
1201 | #else |
---|
1202 | *screen = osg::Vec3d(pt.x(), pt.y(), pt.z()) * cam->getViewport()->computeWindowMatrix(); |
---|
1203 | #endif |
---|
1204 | if (invertY) { |
---|
1205 | screen->y() = viewport->height() - screen->y(); |
---|
1206 | } |
---|
1207 | TRACE("screen: %g,%g,%g", screen->x(), screen->y(), screen->z()); |
---|
1208 | return true; |
---|
1209 | } |
---|
1210 | |
---|
1211 | bool Renderer::worldToScreen(std::vector<osg::Vec3d>& coords, bool invertY) |
---|
1212 | { |
---|
1213 | if (!_viewer.valid()) { |
---|
1214 | ERROR("No viewer"); |
---|
1215 | return false; |
---|
1216 | } |
---|
1217 | bool ret = true; |
---|
1218 | osg::Camera *cam = _viewer->getCamera(); |
---|
1219 | osg::Matrixd MVP = cam->getViewMatrix() * cam->getProjectionMatrix(); |
---|
1220 | const osg::Viewport *viewport = cam->getViewport(); |
---|
1221 | for (std::vector<osg::Vec3d>::iterator itr = coords.begin(); |
---|
1222 | itr != coords.end(); ++itr) { |
---|
1223 | // Get clip coords |
---|
1224 | osg::Vec4d pt; |
---|
1225 | pt = osg::Vec4d(*itr, 1.0) * MVP; |
---|
1226 | // Clip |
---|
1227 | if (pt.x() < -pt.w() || |
---|
1228 | pt.x() > pt.w() || |
---|
1229 | pt.y() < -pt.w() || |
---|
1230 | pt.y() > pt.w() || |
---|
1231 | pt.z() < -pt.w() || |
---|
1232 | pt.z() > pt.w()) { |
---|
1233 | // Outside frustum |
---|
1234 | TRACE("invalid pt: %g,%g,%g,%g", pt.x(), pt.y(), pt.z(), pt.w()); |
---|
1235 | itr->set(std::numeric_limits<double>::quiet_NaN(), |
---|
1236 | std::numeric_limits<double>::quiet_NaN(), |
---|
1237 | std::numeric_limits<double>::quiet_NaN()); |
---|
1238 | ret = false; |
---|
1239 | continue; |
---|
1240 | } |
---|
1241 | TRACE("clip pt: %g,%g,%g,%g", pt.x(), pt.y(), pt.z(), pt.w()); |
---|
1242 | // Perspective divide: now NDC |
---|
1243 | pt /= pt.w(); |
---|
1244 | #if 1 |
---|
1245 | itr->x() = viewport->x() + viewport->width() * 0.5 + pt.x() * viewport->width() * 0.5; |
---|
1246 | itr->y() = viewport->y() + viewport->height() * 0.5 + pt.y() * viewport->height() * 0.5; |
---|
1247 | //double near = 0; |
---|
1248 | //double far = 1; |
---|
1249 | //itr->z() = (far + near) * 0.5 + (far - near) * 0.5 * pt.z(); |
---|
1250 | itr->z() = 0.5 + 0.5 * pt.z(); |
---|
1251 | #else |
---|
1252 | *itr = osg::Vec3d(pt.x(), pt.y(), pt.z()) * viewport->computeWindowMatrix(); |
---|
1253 | #endif |
---|
1254 | if (invertY) { |
---|
1255 | itr->y() = viewport->height() - itr->y(); |
---|
1256 | } |
---|
1257 | TRACE("screen: %g,%g,%g", itr->x(), itr->y(), itr->z()); |
---|
1258 | } |
---|
1259 | return ret; |
---|
1260 | } |
---|
1261 | |
---|
1262 | bool Renderer::mouseToLatLong(int mouseX, int mouseY, |
---|
1263 | double *latitude, double *longitude) |
---|
1264 | { |
---|
1265 | if (getMapSRS() == NULL) |
---|
1266 | return false; |
---|
1267 | const osgEarth::SpatialReference *outSRS = |
---|
1268 | getMapSRS()->getGeographicSRS(); |
---|
1269 | if (outSRS == NULL) |
---|
1270 | return false; |
---|
1271 | osgEarth::GeoPoint mapPoint; |
---|
1272 | if (mapMouseCoords(mouseX, mouseY, mapPoint)) { |
---|
1273 | mapPoint = mapPoint.transform(outSRS); |
---|
1274 | *longitude = mapPoint.x(); |
---|
1275 | *latitude = mapPoint.y(); |
---|
1276 | return true; |
---|
1277 | } else { |
---|
1278 | return false; |
---|
1279 | } |
---|
1280 | } |
---|
1281 | #if 0 |
---|
1282 | bool Renderer::mapToLatLong(osgEarth::GeoPoint& mapPoint) |
---|
1283 | { |
---|
1284 | if (getMapSRS() == NULL) |
---|
1285 | return false; |
---|
1286 | const osgEarth::SpatialReference *outSRS = |
---|
1287 | getMapSRS()->getGeographicSRS(); |
---|
1288 | if (outSRS == NULL) |
---|
1289 | return false; |
---|
1290 | mapPoint = mapPoint.transform(outSRS); |
---|
1291 | return true; |
---|
1292 | } |
---|
1293 | #endif |
---|
1294 | /** |
---|
1295 | * \brief Map screen mouse coordinates to map coordinates |
---|
1296 | * |
---|
1297 | * This method assumes that mouse Y coordinates are 0 at the top |
---|
1298 | * of the screen and increase going down if invertY is set, and |
---|
1299 | * 0 at the bottom and increase going up otherwise. |
---|
1300 | */ |
---|
1301 | bool Renderer::mapMouseCoords(float mouseX, float mouseY, |
---|
1302 | osgEarth::GeoPoint& map, bool invertY) |
---|
1303 | { |
---|
1304 | if (!_mapNode.valid() || _mapNode->getTerrain() == NULL) { |
---|
1305 | ERROR("No map"); |
---|
1306 | return false; |
---|
1307 | } |
---|
1308 | if (!_viewer.valid()) { |
---|
1309 | ERROR("No viewer"); |
---|
1310 | return false; |
---|
1311 | } |
---|
1312 | if (invertY) { |
---|
1313 | mouseY = ((float)_windowHeight - mouseY); |
---|
1314 | } |
---|
1315 | osg::Vec3d world; |
---|
1316 | if (_mapNode->getTerrain()->getWorldCoordsUnderMouse(_viewer->asView(), mouseX, mouseY, world)) { |
---|
1317 | map.fromWorld(getMapSRS(), world); |
---|
1318 | return true; |
---|
1319 | } |
---|
1320 | return false; |
---|
1321 | } |
---|
1322 | |
---|
1323 | bool Renderer::addImageLayer(const char *name, |
---|
1324 | osgEarth::TileSourceOptions& opts, |
---|
1325 | unsigned int pos, |
---|
1326 | bool enableCache, |
---|
1327 | bool coverage, |
---|
1328 | bool makeShared, |
---|
1329 | bool visible, |
---|
1330 | unsigned int minLOD, unsigned int maxLOD) |
---|
1331 | { |
---|
1332 | if (!_map.valid()) { |
---|
1333 | ERROR("No map"); |
---|
1334 | return false; |
---|
1335 | } |
---|
1336 | TRACE("layer: %s", name); |
---|
1337 | if (!opts.tileSize().isSet()) { |
---|
1338 | opts.tileSize() = 256; |
---|
1339 | } |
---|
1340 | osgEarth::ImageLayerOptions layerOpts(name, opts); |
---|
1341 | layerOpts.textureCompression() = osg::Texture::USE_IMAGE_DATA_FORMAT; |
---|
1342 | if (coverage) { |
---|
1343 | layerOpts.coverage() = true; |
---|
1344 | } |
---|
1345 | if (makeShared) { |
---|
1346 | layerOpts.shared() = true; |
---|
1347 | } |
---|
1348 | if (!enableCache) { |
---|
1349 | TRACE("Disabling cache for layer %s", name); |
---|
1350 | layerOpts.cachePolicy() = osgEarth::CachePolicy(osgEarth::CachePolicy::USAGE_NO_CACHE); |
---|
1351 | } |
---|
1352 | if (!visible) { |
---|
1353 | layerOpts.visible() = false; |
---|
1354 | } |
---|
1355 | layerOpts.minLevel() = minLOD; |
---|
1356 | layerOpts.maxLevel() = maxLOD; |
---|
1357 | osg::ref_ptr<osgEarth::ImageLayer> layer = new osgEarth::ImageLayer(layerOpts); |
---|
1358 | if (pos < (unsigned int)_map->getNumImageLayers()) { |
---|
1359 | _map->insertImageLayer(layer.get(), pos); |
---|
1360 | } else { |
---|
1361 | _map->addImageLayer(layer.get()); |
---|
1362 | } |
---|
1363 | if (layer->getTileSource() == NULL || !layer->getTileSource()->isOK()) { |
---|
1364 | ERROR("Failed to add image layer: %s", name); |
---|
1365 | _map->removeImageLayer(layer.get()); |
---|
1366 | return false; |
---|
1367 | } |
---|
1368 | _needsRedraw = true; |
---|
1369 | return true; |
---|
1370 | } |
---|
1371 | |
---|
1372 | void Renderer::addColorFilter(const char *name, |
---|
1373 | const char *shader) |
---|
1374 | { |
---|
1375 | if (!_map.valid()) { |
---|
1376 | ERROR("No map"); |
---|
1377 | return; |
---|
1378 | } |
---|
1379 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1380 | if (layer == NULL) { |
---|
1381 | TRACE("Image layer not found: %s", name); |
---|
1382 | return; |
---|
1383 | } |
---|
1384 | osgEarth::Util::GLSLColorFilter *filter = new osgEarth::Util::GLSLColorFilter; |
---|
1385 | filter->setCode(shader); |
---|
1386 | //filter->setCode("color.rgb = color.r > 0.5 ? vec3(1.0) : vec3(0.0);"); |
---|
1387 | layer->addColorFilter(filter); |
---|
1388 | _needsRedraw = true; |
---|
1389 | } |
---|
1390 | |
---|
1391 | void Renderer::removeColorFilter(const char *name, int idx) |
---|
1392 | { |
---|
1393 | if (!_map.valid()) { |
---|
1394 | ERROR("No map"); |
---|
1395 | return; |
---|
1396 | } |
---|
1397 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1398 | if (layer == NULL) { |
---|
1399 | TRACE("Image layer not found: %s", name); |
---|
1400 | return; |
---|
1401 | } |
---|
1402 | if (idx < 0) { |
---|
1403 | while (!layer->getColorFilters().empty()) { |
---|
1404 | layer->removeColorFilter(layer->getColorFilters()[0]); |
---|
1405 | } |
---|
1406 | } else { |
---|
1407 | layer->removeColorFilter(layer->getColorFilters().at(idx)); |
---|
1408 | } |
---|
1409 | _needsRedraw = true; |
---|
1410 | } |
---|
1411 | |
---|
1412 | void Renderer::removeImageLayer(const char *name) |
---|
1413 | { |
---|
1414 | if (!_map.valid()) { |
---|
1415 | ERROR("No map"); |
---|
1416 | return; |
---|
1417 | } |
---|
1418 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1419 | if (layer != NULL) { |
---|
1420 | _map->removeImageLayer(layer); |
---|
1421 | _needsRedraw = true; |
---|
1422 | } else { |
---|
1423 | TRACE("Image layer not found: %s", name); |
---|
1424 | } |
---|
1425 | } |
---|
1426 | |
---|
1427 | void Renderer::moveImageLayer(const char *name, unsigned int pos) |
---|
1428 | { |
---|
1429 | if (!_map.valid()) { |
---|
1430 | ERROR("No map"); |
---|
1431 | return; |
---|
1432 | } |
---|
1433 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1434 | if (layer != NULL) { |
---|
1435 | _map->moveImageLayer(layer, pos); |
---|
1436 | _needsRedraw = true; |
---|
1437 | } else { |
---|
1438 | TRACE("Image layer not found: %s", name); |
---|
1439 | } |
---|
1440 | } |
---|
1441 | |
---|
1442 | void Renderer::setImageLayerOpacity(const char *name, double opacity) |
---|
1443 | { |
---|
1444 | if (!_map.valid()) { |
---|
1445 | ERROR("No map"); |
---|
1446 | return; |
---|
1447 | } |
---|
1448 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1449 | if (layer != NULL) { |
---|
1450 | layer->setOpacity(opacity); |
---|
1451 | _needsRedraw = true; |
---|
1452 | } else { |
---|
1453 | TRACE("Image layer not found: %s", name); |
---|
1454 | } |
---|
1455 | } |
---|
1456 | |
---|
1457 | void Renderer::setImageLayerVisibleRange(const char *name, float min, float max) |
---|
1458 | { |
---|
1459 | if (!_map.valid()) { |
---|
1460 | ERROR("No map"); |
---|
1461 | return; |
---|
1462 | } |
---|
1463 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1464 | if (layer != NULL) { |
---|
1465 | layer->setMinVisibleRange(min); |
---|
1466 | layer->setMaxVisibleRange(max); |
---|
1467 | _needsRedraw = true; |
---|
1468 | } else { |
---|
1469 | TRACE("Image layer not found: %s", name); |
---|
1470 | } |
---|
1471 | } |
---|
1472 | |
---|
1473 | void Renderer::setImageLayerVisibility(const char *name, bool state) |
---|
1474 | { |
---|
1475 | if (!_map.valid()) { |
---|
1476 | ERROR("No map"); |
---|
1477 | return; |
---|
1478 | } |
---|
1479 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1480 | if (layer != NULL) { |
---|
1481 | layer->setVisible(state); |
---|
1482 | _needsRedraw = true; |
---|
1483 | } else { |
---|
1484 | TRACE("Image layer not found: %s", name); |
---|
1485 | } |
---|
1486 | } |
---|
1487 | |
---|
1488 | bool Renderer::layerHasSequence(const char *name) |
---|
1489 | { |
---|
1490 | if (!_map.valid()) { |
---|
1491 | ERROR("No map"); |
---|
1492 | return false; |
---|
1493 | } |
---|
1494 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1495 | if (layer != NULL) { |
---|
1496 | osgEarth::SequenceControl *seq = layer->getSequenceControl(); |
---|
1497 | if (seq == NULL) { |
---|
1498 | TRACE("Image layer has no SequenceControl: %s", name); |
---|
1499 | return false; |
---|
1500 | } |
---|
1501 | return seq->supportsSequenceControl(); |
---|
1502 | } |
---|
1503 | TRACE("Image layer not found: %s", name); |
---|
1504 | return false; |
---|
1505 | } |
---|
1506 | |
---|
1507 | bool Renderer::sequencePause(const char *name) |
---|
1508 | { |
---|
1509 | if (!_map.valid()) { |
---|
1510 | ERROR("No map"); |
---|
1511 | return false; |
---|
1512 | } |
---|
1513 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1514 | if (layer != NULL) { |
---|
1515 | osgEarth::SequenceControl *seq = layer->getSequenceControl(); |
---|
1516 | if (seq == NULL || !seq->supportsSequenceControl()) { |
---|
1517 | TRACE("Image layer has no SequenceControl: %s", name); |
---|
1518 | return false; |
---|
1519 | } |
---|
1520 | seq->pauseSequence(); |
---|
1521 | _needsRedraw = true; |
---|
1522 | return true; |
---|
1523 | } |
---|
1524 | TRACE("Image layer not found: %s", name); |
---|
1525 | return false; |
---|
1526 | } |
---|
1527 | |
---|
1528 | bool Renderer::sequencePlay(const char *name) |
---|
1529 | { |
---|
1530 | if (!_map.valid()) { |
---|
1531 | ERROR("No map"); |
---|
1532 | return false; |
---|
1533 | } |
---|
1534 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1535 | if (layer != NULL) { |
---|
1536 | osgEarth::SequenceControl *seq = layer->getSequenceControl(); |
---|
1537 | if (seq == NULL || !seq->supportsSequenceControl()) { |
---|
1538 | TRACE("Image layer has no SequenceControl: %s", name); |
---|
1539 | return false; |
---|
1540 | } |
---|
1541 | seq->playSequence(); |
---|
1542 | _needsRedraw = true; |
---|
1543 | return true; |
---|
1544 | } |
---|
1545 | TRACE("Image layer not found: %s", name); |
---|
1546 | return false; |
---|
1547 | } |
---|
1548 | |
---|
1549 | bool Renderer::sequenceSeek(const char *name, unsigned int frame) |
---|
1550 | { |
---|
1551 | if (!_map.valid()) { |
---|
1552 | ERROR("No map"); |
---|
1553 | return false; |
---|
1554 | } |
---|
1555 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
1556 | if (layer != NULL) { |
---|
1557 | osgEarth::SequenceControl *seq = layer->getSequenceControl(); |
---|
1558 | if (seq == NULL || !seq->supportsSequenceControl()) { |
---|
1559 | TRACE("Image layer has no SequenceControl: %s", name); |
---|
1560 | return false; |
---|
1561 | } |
---|
1562 | seq->seekToSequenceFrame(frame); |
---|
1563 | _needsRedraw = true; |
---|
1564 | return true; |
---|
1565 | } |
---|
1566 | TRACE("Image layer not found: %s", name); |
---|
1567 | return false; |
---|
1568 | } |
---|
1569 | |
---|
1570 | void Renderer::addElevationLayer(const char *name, |
---|
1571 | osgEarth::TileSourceOptions& opts, |
---|
1572 | unsigned int pos, |
---|
1573 | bool enableCache, |
---|
1574 | bool visible, |
---|
1575 | const char *verticalDatum, |
---|
1576 | unsigned int minLOD, unsigned int maxLOD) |
---|
1577 | { |
---|
1578 | if (!_map.valid()) { |
---|
1579 | ERROR("No map"); |
---|
1580 | return; |
---|
1581 | } |
---|
1582 | TRACE("layer: %s", name); |
---|
1583 | if (!opts.tileSize().isSet()) { |
---|
1584 | opts.tileSize() = 17; |
---|
1585 | } |
---|
1586 | osgEarth::ElevationLayerOptions layerOpts(name, opts); |
---|
1587 | if (!enableCache) { |
---|
1588 | layerOpts.cachePolicy() = osgEarth::CachePolicy(osgEarth::CachePolicy::USAGE_NO_CACHE); |
---|
1589 | } |
---|
1590 | if (!visible) { |
---|
1591 | layerOpts.visible() = false; |
---|
1592 | } |
---|
1593 | // Check for vertical datum override (required for GDAL driver if not using |
---|
1594 | // geodetic HAE). |
---|
1595 | if (verticalDatum != NULL && strlen(verticalDatum) > 0) { |
---|
1596 | layerOpts.verticalDatum() = verticalDatum; |
---|
1597 | } |
---|
1598 | layerOpts.minLevel() = minLOD; |
---|
1599 | layerOpts.maxLevel() = maxLOD; |
---|
1600 | osgEarth::ElevationLayer *layer = new osgEarth::ElevationLayer(layerOpts); |
---|
1601 | _map->addElevationLayer(layer); |
---|
1602 | // Map API lacks an insertElevationLayer method, so need to move it |
---|
1603 | if (pos < (unsigned int)_map->getNumElevationLayers()) { |
---|
1604 | _map->moveElevationLayer(layer, pos); |
---|
1605 | } |
---|
1606 | _needsRedraw = true; |
---|
1607 | } |
---|
1608 | |
---|
1609 | void Renderer::removeElevationLayer(const char *name) |
---|
1610 | { |
---|
1611 | if (!_map.valid()) { |
---|
1612 | ERROR("No map"); |
---|
1613 | return; |
---|
1614 | } |
---|
1615 | osgEarth::ElevationLayer *layer = _map->getElevationLayerByName(name); |
---|
1616 | if (layer != NULL) { |
---|
1617 | _map->removeElevationLayer(layer); |
---|
1618 | _needsRedraw = true; |
---|
1619 | } else { |
---|
1620 | TRACE("Elevation layer not found: %s", name); |
---|
1621 | } |
---|
1622 | } |
---|
1623 | |
---|
1624 | void Renderer::moveElevationLayer(const char *name, unsigned int pos) |
---|
1625 | { |
---|
1626 | if (!_map.valid()) { |
---|
1627 | ERROR("No map"); |
---|
1628 | return; |
---|
1629 | } |
---|
1630 | osgEarth::ElevationLayer *layer = _map->getElevationLayerByName(name); |
---|
1631 | if (layer != NULL) { |
---|
1632 | _map->moveElevationLayer(layer, pos); |
---|
1633 | _needsRedraw = true; |
---|
1634 | } else { |
---|
1635 | TRACE("Elevation layer not found: %s", name); |
---|
1636 | } |
---|
1637 | } |
---|
1638 | |
---|
1639 | void Renderer::setElevationLayerVisibility(const char *name, bool state) |
---|
1640 | { |
---|
1641 | if (!_map.valid()) { |
---|
1642 | ERROR("No map"); |
---|
1643 | return; |
---|
1644 | } |
---|
1645 | osgEarth::ElevationLayer *layer = _map->getElevationLayerByName(name); |
---|
1646 | if (layer != NULL) { |
---|
1647 | layer->setVisible(state); |
---|
1648 | _needsRedraw = true; |
---|
1649 | } else { |
---|
1650 | TRACE("Elevation layer not found: %s", name); |
---|
1651 | } |
---|
1652 | } |
---|
1653 | |
---|
1654 | void Renderer::addTerrainMaskLayer(const char *name, |
---|
1655 | osgEarth::MaskSourceOptions& opts, |
---|
1656 | unsigned int minLOD) |
---|
1657 | { |
---|
1658 | if (!_map.valid()) { |
---|
1659 | ERROR("No map"); |
---|
1660 | return; |
---|
1661 | } |
---|
1662 | TRACE("layer: %s", name); |
---|
1663 | osgEarth::MaskLayerOptions layerOpts(name, opts); |
---|
1664 | layerOpts.minLevel() = minLOD; |
---|
1665 | osgEarth::MaskLayer *layer = new osgEarth::MaskLayer(layerOpts); |
---|
1666 | _map->addTerrainMaskLayer(layer); |
---|
1667 | _needsRedraw = true; |
---|
1668 | } |
---|
1669 | |
---|
1670 | void Renderer::removeTerrainMaskLayer(const char *name) |
---|
1671 | { |
---|
1672 | if (!_map.valid()) { |
---|
1673 | ERROR("No map"); |
---|
1674 | return; |
---|
1675 | } |
---|
1676 | osgEarth::MaskLayer *layer = getTerrainMaskLayerByName(name); |
---|
1677 | if (layer != NULL) { |
---|
1678 | _map->removeTerrainMaskLayer(layer); |
---|
1679 | _needsRedraw = true; |
---|
1680 | } else { |
---|
1681 | TRACE("Terrain mask layer not found: %s", name); |
---|
1682 | } |
---|
1683 | } |
---|
1684 | |
---|
1685 | void Renderer::initAnnotations() |
---|
1686 | { |
---|
1687 | if (!_annotations.valid()) { |
---|
1688 | _annotations = new osg::Group(); |
---|
1689 | _annotations->setName("Annotations"); |
---|
1690 | _sceneRoot->addChild(_annotations.get()); |
---|
1691 | _placeNodes = new osg::Group(); |
---|
1692 | _placeNodes->setName("Place Nodes"); |
---|
1693 | #ifdef NEW_ANNOTATION_API |
---|
1694 | osgEarth::ScreenSpaceLayout::activate(_placeNodes->getOrCreateStateSet()); |
---|
1695 | #else |
---|
1696 | osgEarth::Decluttering::setEnabled(_placeNodes->getOrCreateStateSet(), true); |
---|
1697 | #endif |
---|
1698 | _annotations->addChild(_placeNodes.get()); |
---|
1699 | if (_picker.valid()) { |
---|
1700 | _picker->addChild(_placeNodes.get()); |
---|
1701 | } |
---|
1702 | } |
---|
1703 | } |
---|
1704 | |
---|
1705 | void Renderer::setSelectMode(SelectMode mode) |
---|
1706 | { |
---|
1707 | _selectMode = mode; |
---|
1708 | switch (_selectMode) { |
---|
1709 | case SELECT_OFF: { |
---|
1710 | #ifdef USE_RTT_PICKER |
---|
1711 | if (_picker.valid()) { |
---|
1712 | _viewer->removeEventHandler(_picker.get()); |
---|
1713 | _picker = NULL; |
---|
1714 | } |
---|
1715 | #endif |
---|
1716 | } |
---|
1717 | break; |
---|
1718 | case SELECT_ON: { |
---|
1719 | #ifdef USE_RTT_PICKER |
---|
1720 | if (!_picker.valid()) { |
---|
1721 | _picker = new osgEarth::Util::RTTPicker; |
---|
1722 | _picker->addChild(_mapNode); |
---|
1723 | osg::ref_ptr<SelectCallback> callback = new SelectCallback(this); |
---|
1724 | _picker->setDefaultCallback(callback.get()); |
---|
1725 | _viewer->addEventHandler(_picker.get()); |
---|
1726 | } |
---|
1727 | #endif |
---|
1728 | } |
---|
1729 | break; |
---|
1730 | default: |
---|
1731 | ERROR("Unknown select mode"); |
---|
1732 | } |
---|
1733 | } |
---|
1734 | |
---|
1735 | void Renderer::enablePlacard(const char *layerName, bool state) |
---|
1736 | { |
---|
1737 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(layerName); |
---|
1738 | if (layer == NULL) { |
---|
1739 | ERROR("Unknown layer '%s'", layerName); |
---|
1740 | return; |
---|
1741 | } |
---|
1742 | _placardConfigs[layerName].setEnabled(state); |
---|
1743 | } |
---|
1744 | |
---|
1745 | void Renderer::setPlacardConfig(const Placard& placardConf, const char *layerName) |
---|
1746 | { |
---|
1747 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(layerName); |
---|
1748 | if (layer == NULL) { |
---|
1749 | ERROR("Unknown layer '%s'", layerName); |
---|
1750 | return; |
---|
1751 | } |
---|
1752 | //PlacardNode *node; |
---|
1753 | //node->setConfig(placardConf); |
---|
1754 | _placardConfigs[layerName] = placardConf; |
---|
1755 | } |
---|
1756 | |
---|
1757 | void Renderer::deselectFeatures(std::vector<unsigned long>& featureIDs, const char *layerName) |
---|
1758 | { |
---|
1759 | // TODO: Take down placard? |
---|
1760 | TRACE("Deselect features layer '%s', num features: %u", layerName, featureIDs.size()); |
---|
1761 | for (unsigned int i = 0; i < featureIDs.size(); i++) { |
---|
1762 | _selectedFeatures[layerName].erase(featureIDs.at(i)); |
---|
1763 | } |
---|
1764 | FeatureSelectionHashmap::iterator itr = _selectedFeatures.find(layerName); |
---|
1765 | if (itr != _selectedFeatures.end()) { |
---|
1766 | for (FeatureSelectionHashmap::mapped_type::iterator fitr = itr->second.begin(); |
---|
1767 | fitr != itr->second.end(); ++fitr) { |
---|
1768 | TRACE("Selection: %lu", *fitr); |
---|
1769 | } |
---|
1770 | } else { |
---|
1771 | TRACE("No selection"); |
---|
1772 | } |
---|
1773 | updateDebugLabel(); |
---|
1774 | } |
---|
1775 | |
---|
1776 | void Renderer::selectFeatures(std::vector<unsigned long>& featureIDs, const char *layerName, bool clear) |
---|
1777 | { |
---|
1778 | bool doPlacard = false; |
---|
1779 | TRACE("Select layer '%s', num features: %u", layerName, featureIDs.size()); |
---|
1780 | if (clear) { |
---|
1781 | clearSelection(); |
---|
1782 | } |
---|
1783 | if (featureIDs.size() == 0) { |
---|
1784 | // clear selection |
---|
1785 | return; |
---|
1786 | } |
---|
1787 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(layerName); |
---|
1788 | if (layer == NULL) { |
---|
1789 | ERROR("Unknown layer '%s'", layerName); |
---|
1790 | return; |
---|
1791 | } |
---|
1792 | for (unsigned int i = 0; i < featureIDs.size(); i++) { |
---|
1793 | TRACE("feature ID: %u", featureIDs.at(i)); |
---|
1794 | _selectedFeatures[layerName].insert(featureIDs.at(i)); |
---|
1795 | } |
---|
1796 | FeatureSelectionHashmap::iterator itr = _selectedFeatures.find(layerName); |
---|
1797 | if (itr != _selectedFeatures.end()) { |
---|
1798 | for (FeatureSelectionHashmap::mapped_type::iterator fitr = itr->second.begin(); |
---|
1799 | fitr != itr->second.end(); ++fitr) { |
---|
1800 | TRACE("Selection: %lu", *fitr); |
---|
1801 | } |
---|
1802 | } else { |
---|
1803 | TRACE("No selection"); |
---|
1804 | } |
---|
1805 | unsigned long fid = featureIDs.at(0); |
---|
1806 | |
---|
1807 | osgEarth::ModelSource *modelSource = layer->getModelSource(); |
---|
1808 | osgEarth::Features::FeatureModelSource *fms = dynamic_cast<osgEarth::Features::FeatureModelSource *>(modelSource); |
---|
1809 | if (fms != NULL) { |
---|
1810 | osgEarth::Features::FeatureSource *fs = fms->getFeatureSource(); |
---|
1811 | FindFeatureSourceIndexNodeVisitor visitor; |
---|
1812 | visitor.source = fs; |
---|
1813 | _sceneRoot->accept(visitor); |
---|
1814 | TRACE("Num FeatureSourceIndexNodes found: %lu", visitor.nodes.size()); |
---|
1815 | for (size_t i = 0; i < visitor.nodes.size(); i++) { |
---|
1816 | osgEarth::Features::FeatureIndex *index = visitor.nodes[i]->getIndex(); |
---|
1817 | #if OSGEARTH_MIN_VERSION_REQUIRED(3, 0, 0) |
---|
1818 | osgEarth::ObjectID id = index->getObjectID(fid); |
---|
1819 | if (doPlacard) { |
---|
1820 | osgEarth::Features::Feature *feature = index->getFeature(id); |
---|
1821 | if (feature) { |
---|
1822 | // Find feature centroid |
---|
1823 | osgEarth::GeoPoint location; |
---|
1824 | if (feature->getGeometry()) { |
---|
1825 | osg::BoundingSphered bound; |
---|
1826 | feature->getWorldBound(getMapSRS(), bound); |
---|
1827 | location.set(getMapSRS(), bound.center(), |
---|
1828 | osgEarth::ALTMODE_ABSOLUTE); |
---|
1829 | } |
---|
1830 | addPlacard(location, feature, layerName); |
---|
1831 | } |
---|
1832 | } |
---|
1833 | setHighlightByObjectID(id); |
---|
1834 | TRACE("FID %lu = OID %d", fid, id); |
---|
1835 | #endif |
---|
1836 | } |
---|
1837 | _needsRedraw = true; |
---|
1838 | } |
---|
1839 | updateDebugLabel(); |
---|
1840 | } |
---|
1841 | |
---|
1842 | void Renderer::updateDebugLabel() |
---|
1843 | { |
---|
1844 | if (!_debugLabel.valid()) return; |
---|
1845 | std::ostringstream oss; |
---|
1846 | oss << "Selection:"; |
---|
1847 | for (FeatureSelectionHashmap::iterator itr = _selectedFeatures.begin(); |
---|
1848 | itr != _selectedFeatures.end(); ++itr) { |
---|
1849 | bool found = false; |
---|
1850 | oss << std::endl << itr->first << ":"; |
---|
1851 | for (FeatureSelectionHashmap::mapped_type::iterator fitr = itr->second.begin(); |
---|
1852 | fitr != itr->second.end(); ++fitr) { |
---|
1853 | oss << " " << *fitr; |
---|
1854 | found = true; |
---|
1855 | } |
---|
1856 | if (!found) { |
---|
1857 | oss << " None"; |
---|
1858 | } |
---|
1859 | } |
---|
1860 | _debugLabel->setText(oss.str()); |
---|
1861 | } |
---|
1862 | |
---|
1863 | void Renderer::addPlacard(const osgEarth::GeoPoint& location, |
---|
1864 | osgEarth::Features::Feature *feature, |
---|
1865 | const char *layerName) |
---|
1866 | { |
---|
1867 | if (feature == NULL) return; |
---|
1868 | Placard placard = getPlacardConfig(layerName); |
---|
1869 | if (!placard.enabled()) |
---|
1870 | return; |
---|
1871 | |
---|
1872 | const osgEarth::Features::AttributeTable &attrs = feature->getAttrs(); |
---|
1873 | if (placard.getNumEntries() == 0) { |
---|
1874 | placard.addAllAttributes(attrs); |
---|
1875 | } |
---|
1876 | PlacardNode *label = |
---|
1877 | new PlacardNode(_mapNode.get(), location, placard, attrs); |
---|
1878 | #ifndef NEW_ANNOTATION_API |
---|
1879 | label->getOrCreateStateSet()->setRenderBinDetails(INT_MAX, "RenderBin"); |
---|
1880 | #endif |
---|
1881 | getAnnotations()->addChild(label); |
---|
1882 | } |
---|
1883 | |
---|
1884 | void Renderer::clearSelection() |
---|
1885 | { |
---|
1886 | _selectedFeatures.clear(); |
---|
1887 | clearHighlight(); |
---|
1888 | clearSelectionAnnotationNodes(); |
---|
1889 | clearBoxSelection(); |
---|
1890 | updateDebugLabel(); |
---|
1891 | _needsRedraw = true; |
---|
1892 | } |
---|
1893 | |
---|
1894 | /** |
---|
1895 | * \brief Remove annotation nodes created during a pick/selection |
---|
1896 | * |
---|
1897 | * The primary purpose of this method is to take down the feature placard |
---|
1898 | */ |
---|
1899 | void Renderer::clearSelectionAnnotationNodes() |
---|
1900 | { |
---|
1901 | osg::Group *nodes = getAnnotations(); |
---|
1902 | std::vector<osg::Node *> toRemove; |
---|
1903 | for (unsigned int i = 0; i < nodes->getNumChildren(); i++) { |
---|
1904 | osg::Node *node = nodes->getChild(i); |
---|
1905 | // This can be Placard, PlacardLabel, Label, Place or Track Node |
---|
1906 | #ifdef NEW_ANNOTATION_API |
---|
1907 | if (dynamic_cast<osgEarth::Annotation::GeoPositionNode *>(node) != NULL) { |
---|
1908 | #else |
---|
1909 | if (dynamic_cast<osgEarth::Annotation::OrthoNode *>(node) != NULL) { |
---|
1910 | #endif |
---|
1911 | toRemove.push_back(node); |
---|
1912 | } |
---|
1913 | } |
---|
1914 | for (std::vector<osg::Node *>::iterator itr = toRemove.begin(); |
---|
1915 | itr != toRemove.end(); ++itr) { |
---|
1916 | osgEarth::Annotation::AnnotationNode *anno = dynamic_cast<osgEarth::Annotation::AnnotationNode *>(*itr); |
---|
1917 | if (anno != NULL) { |
---|
1918 | if (_hovered.find(anno) != _hovered.end()) { |
---|
1919 | _hovered.erase(anno); |
---|
1920 | } |
---|
1921 | if (_selected.find(anno) != _selected.end()) { |
---|
1922 | _selected.erase(anno); |
---|
1923 | } |
---|
1924 | } |
---|
1925 | nodes->removeChild(*itr); |
---|
1926 | } |
---|
1927 | _needsRedraw = true; |
---|
1928 | } |
---|
1929 | |
---|
1930 | void Renderer::initBoxSelection(int x, int y) |
---|
1931 | { |
---|
1932 | double latitude, longitude; |
---|
1933 | if (!mouseToLatLong(x, y, &latitude, &longitude)) { |
---|
1934 | return; |
---|
1935 | } |
---|
1936 | _anchorLat = latitude; |
---|
1937 | _anchorLong = longitude; |
---|
1938 | addRhumbBox(latitude, latitude, longitude, longitude); |
---|
1939 | } |
---|
1940 | |
---|
1941 | void Renderer::updateBoxSelection(int x, int y) |
---|
1942 | { |
---|
1943 | double nlat, nlong; |
---|
1944 | if (!mouseToLatLong(x, y, &nlat, &nlong)) { |
---|
1945 | return; |
---|
1946 | } |
---|
1947 | double latMin, latMax, longMin, longMax; |
---|
1948 | if (nlong >= _anchorLong && nlat >= _anchorLat) { |
---|
1949 | // +x +y |
---|
1950 | longMin = _anchorLong; |
---|
1951 | latMin = _anchorLat; |
---|
1952 | longMax = nlong; |
---|
1953 | latMax = nlat; |
---|
1954 | } else if (nlong < _anchorLong && nlat >= _anchorLat) { |
---|
1955 | // -x +y |
---|
1956 | longMin = nlong; |
---|
1957 | latMin = _anchorLat; |
---|
1958 | longMax = _anchorLong; |
---|
1959 | latMax = nlat; |
---|
1960 | } else if (nlong < _anchorLong && nlat < _anchorLat) { |
---|
1961 | // -x -y |
---|
1962 | longMin = nlong; |
---|
1963 | latMin = nlat; |
---|
1964 | longMax = _anchorLong; |
---|
1965 | latMax = _anchorLat; |
---|
1966 | } else { |
---|
1967 | // +x -y |
---|
1968 | longMin = _anchorLong; |
---|
1969 | latMin = nlat; |
---|
1970 | longMax = nlong; |
---|
1971 | latMax = _anchorLat; |
---|
1972 | } |
---|
1973 | osgEarth::Annotation::FeatureNode *node = _selectionBox.get(); |
---|
1974 | osgEarth::Symbology::Geometry *geom = node->getFeature()->getGeometry(); |
---|
1975 | (*geom)[0] = osg::Vec3d(longMin, latMin, 0); |
---|
1976 | (*geom)[1] = osg::Vec3d(longMax, latMin, 0); |
---|
1977 | (*geom)[2] = osg::Vec3d(longMax, latMax, 0); |
---|
1978 | (*geom)[3] = osg::Vec3d(longMin, latMax, 0); |
---|
1979 | node->init(); |
---|
1980 | #ifndef NEW_ANNOTATION_API |
---|
1981 | for (std::set<osgEarth::Annotation::AnnotationNode *>::iterator itr = _selected.begin(); |
---|
1982 | itr != _selected.end(); ++itr) { |
---|
1983 | (*itr)->clearDecoration(); |
---|
1984 | } |
---|
1985 | #endif |
---|
1986 | _selected.clear(); |
---|
1987 | SelectPlaceNodesVisitor spnv(this, latMin, latMax, longMin, longMax); |
---|
1988 | _placeNodes->accept(spnv); |
---|
1989 | _needsRedraw = true; |
---|
1990 | } |
---|
1991 | |
---|
1992 | void Renderer::getBoxSelection(double *latMin, double *latMax, |
---|
1993 | double *longMin, double *longMax, |
---|
1994 | const osgEarth::SpatialReference *outSRS) |
---|
1995 | { |
---|
1996 | osgEarth::Annotation::FeatureNode *node = _selectionBox.get(); |
---|
1997 | if (node == NULL) |
---|
1998 | return; |
---|
1999 | osgEarth::Symbology::Geometry *geom = node->getFeature()->getGeometry(); |
---|
2000 | if (geom == NULL) |
---|
2001 | return; |
---|
2002 | *latMin = (*geom)[0].y(); |
---|
2003 | *latMax = (*geom)[2].y(); |
---|
2004 | *longMin = (*geom)[0].x(); |
---|
2005 | *longMax = (*geom)[2].x(); |
---|
2006 | if (outSRS == NULL) |
---|
2007 | return; |
---|
2008 | |
---|
2009 | const osgEarth::SpatialReference* fromSRS = _mapNode->getMapSRS()->getGeographicSRS(); |
---|
2010 | osgEarth::GeoPoint pt1(fromSRS, *longMin, *latMin); |
---|
2011 | osgEarth::GeoPoint pt2(fromSRS, *longMax, *latMax); |
---|
2012 | pt1.transform(outSRS); |
---|
2013 | pt2.transform(outSRS); |
---|
2014 | *latMin = pt1.y(); |
---|
2015 | *latMax = pt2.y(); |
---|
2016 | *longMin = pt1.x(); |
---|
2017 | *longMax = pt2.x(); |
---|
2018 | } |
---|
2019 | |
---|
2020 | void SelectPlaceNodesVisitor::apply(osg::Node& node) |
---|
2021 | { |
---|
2022 | osgEarth::Annotation::PlaceNode *placeNode = |
---|
2023 | dynamic_cast<osgEarth::Annotation::PlaceNode*>(&node); |
---|
2024 | if (placeNode != NULL) { |
---|
2025 | const osgEarth::SpatialReference *outSRS = |
---|
2026 | _renderer->getMapSRS()->getGeographicSRS(); |
---|
2027 | osgEarth::GeoPoint pt = placeNode->getPosition(); |
---|
2028 | pt = pt.transform(outSRS); |
---|
2029 | if (pt.x() >= _longMin && pt.x() <= _longMax && |
---|
2030 | pt.y() >= _latMin && pt.y() <= _latMax) { |
---|
2031 | if (_renderer->select(placeNode)) { |
---|
2032 | TRACE("Select PlaceNode: %g %g n:'%s' t:'%s'", |
---|
2033 | pt.x(), pt.y(), placeNode->getName().c_str(), |
---|
2034 | placeNode->getText().c_str()); |
---|
2035 | #ifndef NEW_ANNOTATION_API |
---|
2036 | placeNode->setDecoration("select"); |
---|
2037 | #endif |
---|
2038 | } |
---|
2039 | } |
---|
2040 | } |
---|
2041 | traverse(node); |
---|
2042 | } |
---|
2043 | |
---|
2044 | void Renderer::clearBoxSelection() |
---|
2045 | { |
---|
2046 | if (_annotations.valid() && _selectionBox.valid()) { |
---|
2047 | _annotations->removeChild(_selectionBox.get()); |
---|
2048 | _selectionBox = NULL; |
---|
2049 | } |
---|
2050 | #ifndef NEW_ANNOTATION_API |
---|
2051 | for (std::set<osgEarth::Annotation::AnnotationNode *>::iterator itr = _selected.begin(); |
---|
2052 | itr != _selected.end(); ++itr) { |
---|
2053 | (*itr)->clearDecoration(); |
---|
2054 | } |
---|
2055 | #endif |
---|
2056 | _selected.clear(); |
---|
2057 | _needsRedraw = true; |
---|
2058 | } |
---|
2059 | |
---|
2060 | void Renderer::addRhumbBox(double latMin, double latMax, |
---|
2061 | double longMin, double longMax) |
---|
2062 | { |
---|
2063 | if (!_mapNode.valid()) { |
---|
2064 | ERROR("No map node"); |
---|
2065 | return; |
---|
2066 | } |
---|
2067 | initAnnotations(); |
---|
2068 | |
---|
2069 | if (_selectionBox.valid()) { |
---|
2070 | osgEarth::Symbology::Geometry *geom = _selectionBox->getFeature()->getGeometry(); |
---|
2071 | (*geom)[0] = osg::Vec3d(longMin, latMin, 0); |
---|
2072 | (*geom)[1] = osg::Vec3d(longMax, latMin, 0); |
---|
2073 | (*geom)[2] = osg::Vec3d(longMax, latMax, 0); |
---|
2074 | (*geom)[3] = osg::Vec3d(longMin, latMax, 0); |
---|
2075 | _selectionBox->init(); |
---|
2076 | } else { |
---|
2077 | const osgEarth::SpatialReference* geoSRS = _mapNode->getMapSRS()->getGeographicSRS(); |
---|
2078 | osgEarth::Symbology::Geometry *geom = new osgEarth::Symbology::Polygon(); |
---|
2079 | geom->push_back(osg::Vec3d(longMin, latMin, 0)); |
---|
2080 | geom->push_back(osg::Vec3d(longMax, latMin, 0)); |
---|
2081 | geom->push_back(osg::Vec3d(longMax, latMax, 0)); |
---|
2082 | geom->push_back(osg::Vec3d(longMin, latMax, 0)); |
---|
2083 | osgEarth::Symbology::Style boxStyle; |
---|
2084 | #if 1 |
---|
2085 | osgEarth::Symbology::PolygonSymbol *poly = boxStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>(); |
---|
2086 | poly->fill()->color() = osgEarth::Symbology::Color::Cyan; |
---|
2087 | poly->fill()->color().a() = 0.5; |
---|
2088 | #else |
---|
2089 | osgEarth::Symbology::LineSymbol *line = boxStyle.getOrCreate<osgEarth::Symbology::LineSymbol>(); |
---|
2090 | line->stroke()->color() = osgEarth::Symbology::Color::Yellow; |
---|
2091 | line->stroke()->width() = 2.0f; |
---|
2092 | //line->creaseAngle() = 45.0f; |
---|
2093 | line->tessellation() = 10; |
---|
2094 | #endif |
---|
2095 | osgEarth::Symbology::AltitudeSymbol *alt = boxStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>(); |
---|
2096 | alt->clamping() = osgEarth::Symbology::AltitudeSymbol::CLAMP_TO_TERRAIN; |
---|
2097 | //alt->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_GPU; |
---|
2098 | alt->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_DRAPE; |
---|
2099 | //alt->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_SCENE; |
---|
2100 | #if 0 |
---|
2101 | osgEarth::Symbology::RenderSymbol* rs = boxStyle.getOrCreateSymbol<osgEarth::Symbology::RenderSymbol>(); |
---|
2102 | rs->depthOffset()->enabled() = true; |
---|
2103 | rs->depthOffset()->minBias() = 1000; |
---|
2104 | #endif |
---|
2105 | osgEarth::Features::Feature *feature = new osgEarth::Features::Feature(geom, geoSRS, boxStyle); |
---|
2106 | //feature->geoInterp() = osgEarth::GEOINTERP_GREAT_CIRCLE; |
---|
2107 | feature->geoInterp() = osgEarth::GEOINTERP_RHUMB_LINE; |
---|
2108 | _selectionBox = |
---|
2109 | new osgEarth::Annotation::FeatureNode(_mapNode, feature); |
---|
2110 | _annotations->addChild(_selectionBox.get()); |
---|
2111 | } |
---|
2112 | |
---|
2113 | _needsRedraw = true; |
---|
2114 | } |
---|
2115 | |
---|
2116 | void Renderer::addPlaceNode(double latitude, double longitude, char *labelText) |
---|
2117 | { |
---|
2118 | if (!_mapNode.valid()) { |
---|
2119 | ERROR("No map node"); |
---|
2120 | return; |
---|
2121 | } |
---|
2122 | initAnnotations(); |
---|
2123 | |
---|
2124 | const osgEarth::SpatialReference *geoSRS = _mapNode->getMapSRS()->getGeographicSRS(); |
---|
2125 | |
---|
2126 | osgEarth::Symbology::Style pin; |
---|
2127 | pin.getOrCreate<osgEarth::Symbology::IconSymbol>()->url()->setLiteral(getPinIcon()); |
---|
2128 | osgEarth::Annotation::AnnotationNode *anno = |
---|
2129 | new osgEarth::Annotation::PlaceNode(_mapNode, osgEarth::GeoPoint(geoSRS, longitude, latitude), labelText, pin); |
---|
2130 | anno->setName(labelText); |
---|
2131 | _placeNodes->addChild(anno); |
---|
2132 | #ifdef USE_RTT_PICKER |
---|
2133 | osgEarth::Registry::objectIndex()->tagNode(anno, anno); |
---|
2134 | #else |
---|
2135 | osgEarth::Annotation::DecorationInstaller |
---|
2136 | highlightInstaller("select", new osgEarth::Annotation::HighlightDecoration(osg::Vec4f(1,1,0,0.5))); |
---|
2137 | _placeNodes->accept(highlightInstaller); |
---|
2138 | |
---|
2139 | // scale labels when hovering: |
---|
2140 | osgEarth::Annotation::DecorationInstaller |
---|
2141 | scaleInstaller("hover", new osgEarth::Annotation::ScaleDecoration(1.1f)); |
---|
2142 | _placeNodes->accept(scaleInstaller); |
---|
2143 | #endif |
---|
2144 | #if 0 |
---|
2145 | writeScene("/tmp/test.osg"); |
---|
2146 | #endif |
---|
2147 | _needsRedraw = true; |
---|
2148 | } |
---|
2149 | #if 0 |
---|
2150 | void Renderer::hoverFeatureNodes(int x, int y, bool invertY) |
---|
2151 | { |
---|
2152 | if (!_mapNode.valid()) { |
---|
2153 | ERROR("No map node"); |
---|
2154 | return; |
---|
2155 | } |
---|
2156 | osgEarth::IntersectionPicker picker(_viewer.get(), _mapNode.get()); |
---|
2157 | osgEarth::IntersectionPicker::Hits hits; |
---|
2158 | float mouseX = (float)x; |
---|
2159 | float mouseY = (float)y; |
---|
2160 | if (invertY) { |
---|
2161 | mouseY = ((float)_windowHeight - mouseY); |
---|
2162 | } |
---|
2163 | std::set<osgEarth::Annotation::FeatureNode*> toUnHover; |
---|
2164 | for (std::set<osgEarth::Annotation::FeatureNode*>::iterator itr = _hovered.begin(); |
---|
2165 | itr != _hovered.end(); ++itr) { |
---|
2166 | toUnHover.insert(*itr); |
---|
2167 | } |
---|
2168 | if (picker.pick(mouseX, mouseY, hits)) { |
---|
2169 | TRACE("Picker hits: %d", hits.size()); |
---|
2170 | for (osgEarth::IntersectionPicker::Hits::const_iterator hitr = hits.begin(); |
---|
2171 | hitr != hits.end(); ++hitr) { |
---|
2172 | osgEarth::Annotation::FeatureNode *anno = |
---|
2173 | picker.getNode<osgEarth::Annotation::FeatureNode>(*hitr); |
---|
2174 | if (anno != NULL) { |
---|
2175 | TRACE("Hit FeatureNode: %p", anno); |
---|
2176 | if (_hovered.find(anno) == _hovered.end()) { |
---|
2177 | _hovered.insert(anno); |
---|
2178 | anno->setDecoration("hover"); |
---|
2179 | _needsRedraw = true; |
---|
2180 | } |
---|
2181 | toUnHover.erase(anno); |
---|
2182 | } |
---|
2183 | } |
---|
2184 | #if 0 |
---|
2185 | writeScene("/tmp/test.osgt"); |
---|
2186 | #endif |
---|
2187 | } |
---|
2188 | for (std::set<osgEarth::Annotation::FeatureNode *>::iterator itr = toUnHover.begin(); |
---|
2189 | itr != toUnHover.end(); ++itr) { |
---|
2190 | _hovered.erase(*itr); |
---|
2191 | (*itr)->clearDecoration(); |
---|
2192 | _needsRedraw = true; |
---|
2193 | } |
---|
2194 | } |
---|
2195 | #endif |
---|
2196 | |
---|
2197 | #ifdef USE_RTT_PICKER |
---|
2198 | void Renderer::hoverPlaceNode(int x, int y, bool invertY) |
---|
2199 | { |
---|
2200 | if (!_placeNodes.valid()) { |
---|
2201 | TRACE("No place nodes"); |
---|
2202 | return; |
---|
2203 | } |
---|
2204 | return; |
---|
2205 | |
---|
2206 | float mouseX = (float)x; |
---|
2207 | float mouseY = (float)y; |
---|
2208 | if (invertY) { |
---|
2209 | mouseY = ((float)_windowHeight - mouseY); |
---|
2210 | } |
---|
2211 | if (_picker->pick(_viewer.get(), mouseX, mouseY)) { |
---|
2212 | TRACE("Hover pick queued: %g %g", mouseX, mouseY); |
---|
2213 | } else { |
---|
2214 | TRACE("Failed to queue pick: %g %g", mouseX, mouseY); |
---|
2215 | } |
---|
2216 | } |
---|
2217 | |
---|
2218 | void Renderer::deletePlaceNode(int x, int y, bool invertY) |
---|
2219 | { |
---|
2220 | if (!_placeNodes.valid()) { |
---|
2221 | TRACE("No place nodes"); |
---|
2222 | return; |
---|
2223 | } |
---|
2224 | return; |
---|
2225 | |
---|
2226 | osg::ref_ptr<osgEarth::Util::RTTPicker> picker = new osgEarth::Util::RTTPicker; |
---|
2227 | picker->addChild(_placeNodes.get()); |
---|
2228 | osg::ref_ptr<DeleteCallback> callback = new DeleteCallback(this); |
---|
2229 | float mouseX = (float)x; |
---|
2230 | float mouseY = (float)y; |
---|
2231 | if (invertY) { |
---|
2232 | mouseY = ((float)_windowHeight - mouseY); |
---|
2233 | } |
---|
2234 | if (picker->pick(_viewer.get(), mouseX, mouseY, callback)) { |
---|
2235 | TRACE("Delete pick queued: %g %g", mouseX, mouseY); |
---|
2236 | } |
---|
2237 | } |
---|
2238 | #else |
---|
2239 | void Renderer::hoverPlaceNode(int x, int y, bool invertY) |
---|
2240 | { |
---|
2241 | if (!_placeNodes.valid()) { |
---|
2242 | TRACE("No place nodes"); |
---|
2243 | return; |
---|
2244 | } |
---|
2245 | |
---|
2246 | osgEarth::IntersectionPicker picker(_viewer.get(), _placeNodes.get()); |
---|
2247 | osgEarth::IntersectionPicker::Hits hits; |
---|
2248 | float mouseX = (float)x; |
---|
2249 | float mouseY = (float)y; |
---|
2250 | if (invertY) { |
---|
2251 | mouseY = ((float)_windowHeight - mouseY); |
---|
2252 | } |
---|
2253 | std::set<osgEarth::Annotation::AnnotationNode*> toUnHover; |
---|
2254 | for (std::set<osgEarth::Annotation::AnnotationNode*>::iterator itr = _hovered.begin(); |
---|
2255 | itr != _hovered.end(); ++itr) { |
---|
2256 | toUnHover.insert(*itr); |
---|
2257 | } |
---|
2258 | if (picker.pick(mouseX, mouseY, hits)) { |
---|
2259 | TRACE("Picker hits: %d", hits.size()); |
---|
2260 | for (osgEarth::IntersectionPicker::Hits::const_iterator hitr = hits.begin(); |
---|
2261 | hitr != hits.end(); ++hitr) { |
---|
2262 | TRACE("Hit: node %p drawable %p idx %d", picker.getNode<osg::Node>(*hitr), hitr->drawable.get(), hitr->primitiveIndex); |
---|
2263 | osgEarth::Annotation::AnnotationNode *anno = |
---|
2264 | picker.getNode<osgEarth::Annotation::AnnotationNode>(*hitr); |
---|
2265 | if (anno != NULL && anno->getDecoration().empty()) { |
---|
2266 | TRACE("Hit AnnotationNode: %p", anno); |
---|
2267 | if (_hovered.find(anno) == _hovered.end()) { |
---|
2268 | _hovered.insert(anno); |
---|
2269 | anno->setDecoration("hover"); |
---|
2270 | _needsRedraw = true; |
---|
2271 | } |
---|
2272 | toUnHover.erase(anno); |
---|
2273 | } |
---|
2274 | } |
---|
2275 | #if 0 |
---|
2276 | writeScene("/tmp/test.osgt"); |
---|
2277 | #endif |
---|
2278 | } |
---|
2279 | for (std::set<osgEarth::Annotation::AnnotationNode *>::iterator itr = toUnHover.begin(); |
---|
2280 | itr != toUnHover.end(); ++itr) { |
---|
2281 | _hovered.erase(*itr); |
---|
2282 | (*itr)->clearDecoration(); |
---|
2283 | _needsRedraw = true; |
---|
2284 | } |
---|
2285 | } |
---|
2286 | |
---|
2287 | void Renderer::deletePlaceNode(int x, int y, bool invertY) |
---|
2288 | { |
---|
2289 | if (!_placeNodes.valid()) { |
---|
2290 | TRACE("No place nodes"); |
---|
2291 | return; |
---|
2292 | } |
---|
2293 | osgEarth::IntersectionPicker picker(_viewer.get(), _placeNodes.get()); |
---|
2294 | osgEarth::IntersectionPicker::Hits hits; |
---|
2295 | float mouseX = (float)x; |
---|
2296 | float mouseY = (float)y; |
---|
2297 | if (invertY) { |
---|
2298 | mouseY = ((float)_windowHeight - mouseY); |
---|
2299 | } |
---|
2300 | if (picker.pick(mouseX, mouseY, hits)) { |
---|
2301 | TRACE("Picker hit!"); |
---|
2302 | // prevent multiple hits on the same instance |
---|
2303 | std::set<osgEarth::Annotation::AnnotationNode *> fired; |
---|
2304 | for (osgEarth::IntersectionPicker::Hits::const_iterator hitr = hits.begin(); |
---|
2305 | hitr != hits.end(); ++hitr) { |
---|
2306 | osgEarth::Annotation::AnnotationNode *anno = |
---|
2307 | picker.getNode<osgEarth::Annotation::AnnotationNode>(*hitr); |
---|
2308 | if (anno != NULL && fired.find(anno) == fired.end()) { |
---|
2309 | fired.insert(anno); |
---|
2310 | _needsRedraw = true; |
---|
2311 | } |
---|
2312 | } |
---|
2313 | for (std::set<osgEarth::Annotation::AnnotationNode *>::iterator itr = fired.begin(); |
---|
2314 | itr != fired.end(); ++itr) { |
---|
2315 | (*itr)->clearDecoration(); |
---|
2316 | if (_hovered.find(*itr) != _hovered.end()) { |
---|
2317 | _hovered.erase(*itr); |
---|
2318 | } |
---|
2319 | if (_selected.find(*itr) != _selected.end()) { |
---|
2320 | _selected.erase(*itr); |
---|
2321 | } |
---|
2322 | _placeNodes->removeChild(*itr); |
---|
2323 | } |
---|
2324 | } else { |
---|
2325 | TRACE("NO Picker hits"); |
---|
2326 | } |
---|
2327 | #if 0 |
---|
2328 | writeScene("/tmp/test.osg"); |
---|
2329 | #endif |
---|
2330 | } |
---|
2331 | #endif |
---|
2332 | |
---|
2333 | void Renderer::addModelLayer(const char *name, |
---|
2334 | osgEarth::ModelSourceOptions& opts, |
---|
2335 | unsigned int pos, |
---|
2336 | bool enableCache, |
---|
2337 | bool lighting, |
---|
2338 | bool visible, |
---|
2339 | bool terrainPatch) |
---|
2340 | { |
---|
2341 | if (!_map.valid()) { |
---|
2342 | ERROR("No map"); |
---|
2343 | return; |
---|
2344 | } |
---|
2345 | TRACE("layer: %s", name); |
---|
2346 | osgEarth::ModelLayerOptions layerOpts(name, opts); |
---|
2347 | if (!enableCache) { |
---|
2348 | TRACE("Disabling cache for layer %s", name); |
---|
2349 | layerOpts.cachePolicy() = osgEarth::CachePolicy(osgEarth::CachePolicy::USAGE_NO_CACHE); |
---|
2350 | } |
---|
2351 | if (!visible) { |
---|
2352 | layerOpts.visible() = false; |
---|
2353 | } |
---|
2354 | layerOpts.lightingEnabled() = lighting; |
---|
2355 | if (terrainPatch) { |
---|
2356 | layerOpts.terrainPatch() = true; |
---|
2357 | } |
---|
2358 | osgEarth::ModelLayer *layer = new osgEarth::ModelLayer(layerOpts); |
---|
2359 | if (pos < (unsigned int)_map->getNumModelLayers()) { |
---|
2360 | _map->insertModelLayer(layer, pos); |
---|
2361 | } else { |
---|
2362 | _map->addModelLayer(layer); |
---|
2363 | } |
---|
2364 | _needsRedraw = true; |
---|
2365 | } |
---|
2366 | |
---|
2367 | void Renderer::removeModelLayer(const char *name) |
---|
2368 | { |
---|
2369 | if (!_map.valid()) { |
---|
2370 | ERROR("No map"); |
---|
2371 | return; |
---|
2372 | } |
---|
2373 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(name); |
---|
2374 | if (layer != NULL) { |
---|
2375 | _map->removeModelLayer(layer); |
---|
2376 | _needsRedraw = true; |
---|
2377 | } else { |
---|
2378 | TRACE("Model layer not found: %s", name); |
---|
2379 | } |
---|
2380 | } |
---|
2381 | |
---|
2382 | void Renderer::moveModelLayer(const char *name, unsigned int pos) |
---|
2383 | { |
---|
2384 | if (!_map.valid()) { |
---|
2385 | ERROR("No map"); |
---|
2386 | return; |
---|
2387 | } |
---|
2388 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(name); |
---|
2389 | if (layer != NULL) { |
---|
2390 | _map->moveModelLayer(layer, pos); |
---|
2391 | _needsRedraw = true; |
---|
2392 | } else { |
---|
2393 | TRACE("Model layer not found: %s", name); |
---|
2394 | } |
---|
2395 | } |
---|
2396 | |
---|
2397 | void Renderer::setModelLayerOpacity(const char *name, double opacity) |
---|
2398 | { |
---|
2399 | if (!_map.valid()) { |
---|
2400 | ERROR("No map"); |
---|
2401 | return; |
---|
2402 | } |
---|
2403 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(name); |
---|
2404 | if (layer != NULL) { |
---|
2405 | layer->setOpacity(opacity); |
---|
2406 | _needsRedraw = true; |
---|
2407 | } else { |
---|
2408 | TRACE("Model layer not found: %s", name); |
---|
2409 | } |
---|
2410 | } |
---|
2411 | #if 0 |
---|
2412 | void Renderer::setModelLayerVisibleRange(const char *name, float min, float max) |
---|
2413 | { |
---|
2414 | if (!_map.valid()) { |
---|
2415 | ERROR("No map"); |
---|
2416 | return; |
---|
2417 | } |
---|
2418 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(name); |
---|
2419 | if (layer != NULL) { |
---|
2420 | layer->minVisibleRange(min); |
---|
2421 | layer->maxVisibleRange(max); |
---|
2422 | _needsRedraw = true; |
---|
2423 | } else { |
---|
2424 | TRACE("Model layer not found: %s", name); |
---|
2425 | } |
---|
2426 | } |
---|
2427 | #endif |
---|
2428 | void Renderer::setModelLayerVisibility(const char *name, bool state) |
---|
2429 | { |
---|
2430 | if (!_map.valid()) { |
---|
2431 | ERROR("No map"); |
---|
2432 | return; |
---|
2433 | } |
---|
2434 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(name); |
---|
2435 | if (layer != NULL) { |
---|
2436 | layer->setVisible(state); |
---|
2437 | _needsRedraw = true; |
---|
2438 | } else { |
---|
2439 | TRACE("Model layer not found: %s", name); |
---|
2440 | } |
---|
2441 | } |
---|
2442 | |
---|
2443 | void Renderer::setLODScale(float scale) |
---|
2444 | { |
---|
2445 | if (!_viewer.valid()) |
---|
2446 | return; |
---|
2447 | |
---|
2448 | TRACE("Setting LOD scale to %g", scale); |
---|
2449 | _viewer->getCamera()->setLODScale(scale); |
---|
2450 | _needsRedraw = true; |
---|
2451 | } |
---|
2452 | |
---|
2453 | /** |
---|
2454 | * \brief Resize the render window (image size for renderings) |
---|
2455 | */ |
---|
2456 | void Renderer::setWindowSize(int width, int height) |
---|
2457 | { |
---|
2458 | if (_windowWidth == width && |
---|
2459 | _windowHeight == height) { |
---|
2460 | TRACE("No change"); |
---|
2461 | return; |
---|
2462 | } |
---|
2463 | |
---|
2464 | TRACE("Setting window size to %dx%d", width, height); |
---|
2465 | |
---|
2466 | double origBitrate = getMaximumBitrate(); |
---|
2467 | |
---|
2468 | _windowWidth = width; |
---|
2469 | _windowHeight = height; |
---|
2470 | |
---|
2471 | setMaximumBitrate(origBitrate); |
---|
2472 | |
---|
2473 | if (_viewer.valid()) { |
---|
2474 | #ifdef USE_OFFSCREEN_RENDERING |
---|
2475 | #ifdef USE_PBUFFER |
---|
2476 | osg::ref_ptr<osg::GraphicsContext> pbuffer; |
---|
2477 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
---|
2478 | traits->x = 0; |
---|
2479 | traits->y = 0; |
---|
2480 | traits->width = _windowWidth; |
---|
2481 | traits->height = _windowHeight; |
---|
2482 | traits->red = 8; |
---|
2483 | traits->green = 8; |
---|
2484 | traits->blue = 8; |
---|
2485 | traits->alpha = 8; |
---|
2486 | traits->windowDecoration = false; |
---|
2487 | traits->pbuffer = true; |
---|
2488 | traits->doubleBuffer = true; |
---|
2489 | traits->sharedContext = 0; |
---|
2490 | |
---|
2491 | pbuffer = osg::GraphicsContext::createGraphicsContext(traits.get()); |
---|
2492 | if (pbuffer.valid()) { |
---|
2493 | TRACE("Pixel buffer has been created successfully."); |
---|
2494 | } else { |
---|
2495 | ERROR("Pixel buffer has not been created successfully."); |
---|
2496 | } |
---|
2497 | osg::Camera *camera = new osg::Camera; |
---|
2498 | camera->setGraphicsContext(pbuffer.get()); |
---|
2499 | //camera->getOrCreateStateSet()->setGlobalDefaults(); |
---|
2500 | camera->setViewport(new osg::Viewport(0, 0, _windowWidth, _windowHeight)); |
---|
2501 | GLenum buffer = pbuffer->getTraits()->doubleBuffer ? GL_BACK : GL_FRONT; |
---|
2502 | camera->setDrawBuffer(buffer); |
---|
2503 | camera->setReadBuffer(buffer); |
---|
2504 | camera->setFinalDrawCallback(_captureCallback.get()); |
---|
2505 | _viewer->addSlave(camera, osg::Matrixd(), osg::Matrixd()); |
---|
2506 | _viewer->realize(); |
---|
2507 | #else |
---|
2508 | if (_captureCallback.valid()) { |
---|
2509 | _captureCallback->getTexture()->setTextureSize(_windowWidth, _windowHeight); |
---|
2510 | } |
---|
2511 | osgViewer::ViewerBase::Windows windows; |
---|
2512 | _viewer->getWindows(windows); |
---|
2513 | if (windows.size() == 1) { |
---|
2514 | windows[0]->setWindowRectangle(0, 0, _windowWidth, _windowHeight); |
---|
2515 | } else { |
---|
2516 | ERROR("Num windows: %lu", windows.size()); |
---|
2517 | } |
---|
2518 | #endif |
---|
2519 | #else |
---|
2520 | osgViewer::ViewerBase::Windows windows; |
---|
2521 | _viewer->getWindows(windows); |
---|
2522 | #if 1 |
---|
2523 | for (osgViewer::Viewer::Windows::iterator itr = windows.begin(); |
---|
2524 | itr != windows.end(); ++itr) { |
---|
2525 | osgViewer::GraphicsWindow *window = *itr; |
---|
2526 | window->setWindowRectangle(0, 0, _windowWidth, _windowHeight); |
---|
2527 | //window->grabFocusIfPointerInWindow(); |
---|
2528 | } |
---|
2529 | //_viewer->getCamera()->setViewport(new osg::Viewport(0, 0, _windowWidth, _windowHeight)); |
---|
2530 | #else |
---|
2531 | if (windows.size() == 1) { |
---|
2532 | windows[0]->setWindowRectangle(0, 0, _windowWidth, _windowHeight); |
---|
2533 | } else { |
---|
2534 | ERROR("Num windows: %lu", windows.size()); |
---|
2535 | } |
---|
2536 | #endif |
---|
2537 | #endif |
---|
2538 | // HACK: Without this, the mouse coordinate mapping uses the old size |
---|
2539 | // for 1 frame. |
---|
2540 | assert(_viewer->getEventQueue() != NULL); |
---|
2541 | //TRACE("Window EventQueue: %p", getEventQueue()); |
---|
2542 | //TRACE("Viewer EventQueue: %p", _viewer->getEventQueue()); |
---|
2543 | _viewer->getEventQueue()->windowResize(0, 0, _windowWidth, _windowHeight); |
---|
2544 | _needsRedraw = true; |
---|
2545 | } |
---|
2546 | } |
---|
2547 | |
---|
2548 | /** |
---|
2549 | * \brief Set the orientation of the camera from a quaternion |
---|
2550 | * rotation |
---|
2551 | * |
---|
2552 | * \param[in] quat A quaternion with scalar part first: w,x,y,z |
---|
2553 | * \param[in] absolute Is rotation absolute or relative? |
---|
2554 | */ |
---|
2555 | void Renderer::setCameraOrientation(const double quat[4], bool absolute) |
---|
2556 | { |
---|
2557 | if (_manipulator.valid()) { |
---|
2558 | _manipulator->setRotation(osg::Quat(quat[1], quat[2], quat[3], quat[0])); |
---|
2559 | _needsRedraw = true; |
---|
2560 | } |
---|
2561 | } |
---|
2562 | |
---|
2563 | /** |
---|
2564 | * \brief Reset pan, zoom, clipping planes and optionally rotation |
---|
2565 | * |
---|
2566 | * \param[in] resetOrientation Reset the camera rotation/orientation also |
---|
2567 | */ |
---|
2568 | void Renderer::resetCamera(bool resetOrientation) |
---|
2569 | { |
---|
2570 | TRACE("Enter: resetOrientation=%d", resetOrientation ? 1 : 0); |
---|
2571 | if (_viewer.valid()) { |
---|
2572 | _viewer->home(); |
---|
2573 | _needsRedraw = true; |
---|
2574 | } |
---|
2575 | } |
---|
2576 | |
---|
2577 | /** |
---|
2578 | * \brief Perform a 2D translation of the camera |
---|
2579 | * |
---|
2580 | * x,y pan amount are specified as signed absolute pan amount in viewport |
---|
2581 | * units -- i.e. 0 is no pan, .5 is half the viewport, 2 is twice the viewport, |
---|
2582 | * etc. |
---|
2583 | * |
---|
2584 | * \param[in] x Viewport coordinate horizontal panning (positive number pans |
---|
2585 | * camera left, object right) |
---|
2586 | * \param[in] y Viewport coordinate vertical panning (positive number pans |
---|
2587 | * camera up, object down) |
---|
2588 | */ |
---|
2589 | void Renderer::panCamera(double x, double y) |
---|
2590 | { |
---|
2591 | TRACE("Enter: %g %g", x, y); |
---|
2592 | |
---|
2593 | if (_manipulator.valid()) { |
---|
2594 | // Wants mouse delta x,y in normalized screen coords |
---|
2595 | _manipulator->pan(x, y); |
---|
2596 | _needsRedraw = true; |
---|
2597 | } |
---|
2598 | } |
---|
2599 | |
---|
2600 | void Renderer::rotateCamera(double x, double y) |
---|
2601 | { |
---|
2602 | TRACE("Enter: %g %g", x, y); |
---|
2603 | |
---|
2604 | if (_manipulator.valid()) { |
---|
2605 | _manipulator->rotate(x, y); |
---|
2606 | _needsRedraw = true; |
---|
2607 | } |
---|
2608 | } |
---|
2609 | |
---|
2610 | /** |
---|
2611 | * \brief Dolly camera or set orthographic scaling based on camera type |
---|
2612 | * |
---|
2613 | * \param[in] y Mouse y coordinate in normalized screen coords |
---|
2614 | */ |
---|
2615 | void Renderer::zoomCamera(double y) |
---|
2616 | { |
---|
2617 | TRACE("Enter: y: %g", y); |
---|
2618 | |
---|
2619 | if (_manipulator.valid()) { |
---|
2620 | // +y = zoom out, -y = zoom in |
---|
2621 | TRACE("camDist: %g", _manipulator->getDistance()); |
---|
2622 | #ifdef LIMIT_ZOOM_BY_MAP_SCALE |
---|
2623 | if ((_mapScale < 0.0 && y > 0.0) || |
---|
2624 | (_mapScale < 0.1 && y < 0.0) || |
---|
2625 | (_mapScale > 40000.0 && y > 0.0)) |
---|
2626 | return; |
---|
2627 | #endif |
---|
2628 | #if 1 |
---|
2629 | _manipulator->zoom(0, y); |
---|
2630 | #else |
---|
2631 | double dist = _manipulator->getDistance(); |
---|
2632 | dist *= (1.0 + y); |
---|
2633 | _manipulator->setDistance(dist); |
---|
2634 | #endif |
---|
2635 | _needsRedraw = true; |
---|
2636 | } |
---|
2637 | } |
---|
2638 | |
---|
2639 | bool Renderer::getImageLayerExtent(const char *name, osgEarth::GeoExtent &ext) |
---|
2640 | { |
---|
2641 | if (!_map.valid()) { |
---|
2642 | ERROR("No map"); |
---|
2643 | return false; |
---|
2644 | } |
---|
2645 | osgEarth::ImageLayer *layer = _map->getImageLayerByName(name); |
---|
2646 | if (layer != NULL) { |
---|
2647 | osgEarth::TileSource *ts = layer->getTileSource(); |
---|
2648 | if (ts != NULL) { |
---|
2649 | ext = ts->getDataExtentsUnion(); |
---|
2650 | if (!ext.isValid() && ts->getProfile() != NULL) { |
---|
2651 | ext = ts->getProfile()->getExtent(); |
---|
2652 | } |
---|
2653 | TRACE("Image layer %s srs: %s extent: %g %g %g %g", |
---|
2654 | name, ext.getSRS() ? ext.getSRS()->getName().c_str() : "nil", |
---|
2655 | ext.xMin(), ext.yMin(), ext.xMax(), ext.yMax()); |
---|
2656 | return true; |
---|
2657 | } |
---|
2658 | } else { |
---|
2659 | TRACE("Image layer not found: %s", name); |
---|
2660 | } |
---|
2661 | return false; |
---|
2662 | } |
---|
2663 | |
---|
2664 | bool Renderer::getElevationLayerExtent(const char *name, osgEarth::GeoExtent &ext) |
---|
2665 | { |
---|
2666 | if (!_map.valid()) { |
---|
2667 | ERROR("No map"); |
---|
2668 | return false; |
---|
2669 | } |
---|
2670 | osgEarth::ElevationLayer *layer = _map->getElevationLayerByName(name); |
---|
2671 | if (layer != NULL) { |
---|
2672 | osgEarth::TileSource *ts = layer->getTileSource(); |
---|
2673 | if (ts != NULL) { |
---|
2674 | ext = ts->getDataExtentsUnion(); |
---|
2675 | if (!ext.isValid() && ts->getProfile() != NULL) { |
---|
2676 | ext = ts->getProfile()->getExtent(); |
---|
2677 | } |
---|
2678 | TRACE("Elevation Layer %s srs: %s extent: %g %g %g %g", |
---|
2679 | name, ext.getSRS() ? ext.getSRS()->getName().c_str() : "nil", |
---|
2680 | ext.xMin(), ext.yMin(), ext.xMax(), ext.yMax()); |
---|
2681 | return true; |
---|
2682 | } |
---|
2683 | } else { |
---|
2684 | TRACE("Elevation layer not found: %s", name); |
---|
2685 | } |
---|
2686 | return false; |
---|
2687 | } |
---|
2688 | |
---|
2689 | bool Renderer::getTerrainMaskLayerExtent(const char *name, osgEarth::GeoExtent &ext) |
---|
2690 | { |
---|
2691 | if (!_map.valid()) { |
---|
2692 | ERROR("No map"); |
---|
2693 | return false; |
---|
2694 | } |
---|
2695 | osgEarth::MaskLayer *layer = getTerrainMaskLayerByName(name); |
---|
2696 | if (layer != NULL) { |
---|
2697 | const osgEarth::SpatialReference *srs = getMapSRS(); |
---|
2698 | osgEarth::GeoExtent newExt(srs); |
---|
2699 | osg::Vec3dArray *polypts = layer->getOrCreateMaskBoundary(1.0f, srs, NULL); |
---|
2700 | if (polypts) { |
---|
2701 | for (size_t i = 0; i < polypts->size(); i++) { |
---|
2702 | osg::Vec3d pt = (*polypts)[i]; |
---|
2703 | newExt.expandToInclude(pt); |
---|
2704 | } |
---|
2705 | } |
---|
2706 | if (!newExt.isValid()) { |
---|
2707 | ERROR("Couldn't find extent of layer %s", name); |
---|
2708 | return false; |
---|
2709 | } |
---|
2710 | ext = newExt; |
---|
2711 | TRACE("Mask Layer %s srs: %s extent: %g %g %g %g", |
---|
2712 | name, ext.getSRS() ? ext.getSRS()->getName().c_str() : "nil", |
---|
2713 | ext.xMin(), ext.yMin(), ext.xMax(), ext.yMax()); |
---|
2714 | return true; |
---|
2715 | } else { |
---|
2716 | TRACE("Terrain mask layer not found: %s", name); |
---|
2717 | } |
---|
2718 | return false; |
---|
2719 | } |
---|
2720 | |
---|
2721 | bool Renderer::getModelLayerExtent(const char *name, osgEarth::GeoExtent &ext) |
---|
2722 | { |
---|
2723 | if (!_map.valid()) { |
---|
2724 | ERROR("No map"); |
---|
2725 | return false; |
---|
2726 | } |
---|
2727 | osgEarth::ModelLayer *layer = _map->getModelLayerByName(name); |
---|
2728 | if (layer != NULL) { |
---|
2729 | osgEarth::ModelSource *ms = layer->getModelSource(); |
---|
2730 | if (ms != NULL) { |
---|
2731 | osgEarth::DataExtentList& dataExtents = ms->getDataExtents(); |
---|
2732 | if (dataExtents.size() > 0) { |
---|
2733 | ext = dataExtents[0]; |
---|
2734 | for (unsigned int i = 1; i < dataExtents.size(); i++) { |
---|
2735 | ext.expandToInclude(dataExtents[i]); |
---|
2736 | } |
---|
2737 | } |
---|
2738 | if (!ext.isValid()) { |
---|
2739 | ERROR("Couldn't find extent of layer %s", name); |
---|
2740 | return false; |
---|
2741 | } |
---|
2742 | TRACE("Model Layer %s srs: %s extent: %g %g %g %g", |
---|
2743 | name, ext.getSRS() ? ext.getSRS()->getName().c_str() : "nil", |
---|
2744 | ext.xMin(), ext.yMin(), ext.xMax(), ext.yMax()); |
---|
2745 | return true; |
---|
2746 | } |
---|
2747 | } else { |
---|
2748 | TRACE("Model layer not found: %s", name); |
---|
2749 | } |
---|
2750 | return false; |
---|
2751 | } |
---|
2752 | |
---|
2753 | /** |
---|
2754 | * \brief Set view to fit a bounding rectangle |
---|
2755 | * |
---|
2756 | * A camera distance is chosen such that the extent rectangle |
---|
2757 | * will fit in the vertical field of view. |
---|
2758 | * |
---|
2759 | * \param ext input GeoExtent |
---|
2760 | * \param durationSecs Animation duration for the view change. |
---|
2761 | */ |
---|
2762 | void Renderer::setViewpointFromExtent(const osgEarth::GeoExtent& ext, |
---|
2763 | double durationSecs) |
---|
2764 | { |
---|
2765 | setViewpointFromRect(ext.xMin(), ext.yMin(), ext.xMax(), ext.yMax(), |
---|
2766 | ext.getSRS(), durationSecs); |
---|
2767 | } |
---|
2768 | |
---|
2769 | /** |
---|
2770 | * \brief Set view to fit a bounding rectangle |
---|
2771 | * |
---|
2772 | * A camera distance is chosen such that the extent rectangle |
---|
2773 | * will fit in the vertical field of view. |
---|
2774 | * |
---|
2775 | * \param xmin Minimum X coord in the given SRS |
---|
2776 | * \param ymin Minimum Y coord in the given SRS |
---|
2777 | * \param xmax Maximum X coord in the given SRS |
---|
2778 | * \param ymax Maximum Y coord in the given SRS |
---|
2779 | * \param srs Optional SRS of bounding points. If omitted, wgs84 is assumed. |
---|
2780 | * \param durationSecs Animation duration for the view change. |
---|
2781 | */ |
---|
2782 | void Renderer::setViewpointFromRect(double xmin, double ymin, |
---|
2783 | double xmax, double ymax, |
---|
2784 | const osgEarth::SpatialReference *srs, |
---|
2785 | double durationSecs) |
---|
2786 | { |
---|
2787 | if (!_viewer.valid() || !_manipulator.valid() || getMapSRS() == NULL) |
---|
2788 | return; |
---|
2789 | |
---|
2790 | double x = 0.0, y = 0.0, distance = 0.0; |
---|
2791 | double x1 = xmin, y1 = ymin, x2 = xmax, y2 = ymax; |
---|
2792 | osgEarth::Units distanceUnits = getMapSRS()->getUnits(); |
---|
2793 | if (getMapSRS()->isProjected() && !getMapSRS()->isPlateCarre()) { |
---|
2794 | TRACE("Projected"); |
---|
2795 | osg::ref_ptr<const osgEarth::SpatialReference> fromSRS = |
---|
2796 | srs ? srs : osgEarth::SpatialReference::create("wgs84"); |
---|
2797 | fromSRS->transformExtentToMBR(getMapSRS(), x1, y1, x2, y2); |
---|
2798 | double height = y2 - y1; |
---|
2799 | double fovy, aspect, near, far; |
---|
2800 | _viewer->getCamera()->getProjectionMatrixAsPerspective(fovy, aspect, near, far); |
---|
2801 | distance = height / (2. * tan(osg::DegreesToRadians(fovy)/2.)); |
---|
2802 | TRACE("fov: %g a: %g n: %g f: %g (%g, %g, %g, %g)", fovy, aspect, near, far, x1, y1, x2, y2); |
---|
2803 | x = x1 + (x2 - x1)/2.0; |
---|
2804 | y = y1 + (y2 - y1)/2.0; |
---|
2805 | TRACE("x: %g y: %g, dist: %g", x, y, distance); |
---|
2806 | } else if (getMapSRS()->isGeographic() && !getMapSRS()->isPlateCarre()) { |
---|
2807 | // World coords are ECEF |
---|
2808 | TRACE("Geocentric"); |
---|
2809 | osg::ref_ptr<const osgEarth::SpatialReference> fromSRS = |
---|
2810 | srs ? srs : osgEarth::SpatialReference::create("wgs84"); |
---|
2811 | fromSRS->transformExtentToMBR(getMapSRS(), x1, y1, x2, y2); |
---|
2812 | TRACE("(%g, %g, %g, %g)", x1, y1, x2, y2); |
---|
2813 | // These are angular units |
---|
2814 | x = x1 + (x2 - x1)/2.0; |
---|
2815 | y = y1 + (y2 - y1)/2.0; |
---|
2816 | // These will be ECEF |
---|
2817 | osg::Vec3d world1, world2, world3, world4; |
---|
2818 | // bottom |
---|
2819 | getMapSRS()->transformToWorld(osg::Vec3d(x, y1, 0), world1); |
---|
2820 | // top |
---|
2821 | getMapSRS()->transformToWorld(osg::Vec3d(x, y2, 0), world2); |
---|
2822 | // Focal point on surface |
---|
2823 | getMapSRS()->transformToWorld(osg::Vec3d(x, y, 0), world3); |
---|
2824 | // point on line between top and bottom points |
---|
2825 | world4 = world1 + (world2 - world1)/2.0; |
---|
2826 | TRACE("world1: %g,%g,%g world2: %g,%g,%g", |
---|
2827 | world1.x(), world1.y(), world1.z(), |
---|
2828 | world2.x(), world2.y(), world2.z()); |
---|
2829 | TRACE("world3: %g,%g,%g world4: %g,%g,%g", |
---|
2830 | world3.x(), world3.y(), world3.z(), |
---|
2831 | world4.x(), world4.y(), world4.z()); |
---|
2832 | double height = (world2 - world1).length(); |
---|
2833 | double fovy, aspect, near, far; |
---|
2834 | _viewer->getCamera()->getProjectionMatrixAsPerspective(fovy, aspect, near, far); |
---|
2835 | distance = height / (2. * tan(osg::DegreesToRadians(fovy)/2.)); |
---|
2836 | distance -= (world4 - world3).length(); |
---|
2837 | if (distance < 0.0) distance = 0.0; |
---|
2838 | distanceUnits = osgEarth::Units::METERS; |
---|
2839 | TRACE("fov: %g a: %g n: %g f: %g (%g, %g, %g, %g)", fovy, aspect, near, far, x1, y1, x2, y2); |
---|
2840 | TRACE("x: %g y: %g, dist: %g", x, y, distance); |
---|
2841 | } else { |
---|
2842 | assert(getMapSRS()->isPlateCarre()); |
---|
2843 | ERROR("Plate Carree not supported"); |
---|
2844 | return; |
---|
2845 | } |
---|
2846 | TRACE("Map units: %d", getMapSRS()->getUnits().getType()); |
---|
2847 | osgEarth::Viewpoint vpt; |
---|
2848 | vpt.focalPoint()->set(getMapSRS(), x, y, 0.0, osgEarth::ALTMODE_ABSOLUTE); |
---|
2849 | vpt.range()->set(distance, distanceUnits); |
---|
2850 | vpt.heading()->set(0.0, osgEarth::Units::DEGREES); |
---|
2851 | vpt.pitch()->set(-90.0, osgEarth::Units::DEGREES); |
---|
2852 | |
---|
2853 | _manipulator->setViewpoint(vpt, durationSecs); |
---|
2854 | _needsRedraw = true; |
---|
2855 | } |
---|
2856 | |
---|
2857 | double Renderer::getMaxDistanceFromExtent(const osgEarth::GeoExtent& extent) |
---|
2858 | { |
---|
2859 | if (!_viewer.valid() || extent.getSRS() == NULL) |
---|
2860 | return 2.0e7; |
---|
2861 | |
---|
2862 | double x = 0.0, y = 0.0, distance = 0.0; |
---|
2863 | double x1 = extent.xMin(), y1 = extent.yMin(), x2 = extent.xMax(), y2 = extent.yMax(); |
---|
2864 | osgEarth::Units distanceUnits = extent.getSRS()->getUnits(); |
---|
2865 | if (extent.getSRS()->isProjected() && !extent.getSRS()->isPlateCarre()) { |
---|
2866 | TRACE("Projected"); |
---|
2867 | double height = y2 - y1; |
---|
2868 | double fovy, aspect, near, far; |
---|
2869 | _viewer->getCamera()->getProjectionMatrixAsPerspective(fovy, aspect, near, far); |
---|
2870 | distance = height / (2. * tan(osg::DegreesToRadians(fovy)/2.)); |
---|
2871 | TRACE("fov: %g a: %g n: %g f: %g (%g, %g, %g, %g)", fovy, aspect, near, far, x1, y1, x2, y2); |
---|
2872 | x = x1 + (x2 - x1)/2.0; |
---|
2873 | y = y1 + (y2 - y1)/2.0; |
---|
2874 | TRACE("x: %g y: %g, dist: %g", x, y, distance); |
---|
2875 | } else if (extent.getSRS()->isGeographic() && !extent.getSRS()->isPlateCarre()) { |
---|
2876 | // World coords are ECEF |
---|
2877 | TRACE("Geocentric"); |
---|
2878 | TRACE("(%g, %g, %g, %g)", x1, y1, x2, y2); |
---|
2879 | // These are angular units |
---|
2880 | x = x1 + (x2 - x1)/2.0; |
---|
2881 | y = y1 + (y2 - y1)/2.0; |
---|
2882 | // These will be ECEF |
---|
2883 | osg::Vec3d world1, world2, world3, world4; |
---|
2884 | // bottom |
---|
2885 | getMapSRS()->transformToWorld(osg::Vec3d(x, y1, 0), world1); |
---|
2886 | // top |
---|
2887 | getMapSRS()->transformToWorld(osg::Vec3d(x, y2, 0), world2); |
---|
2888 | // Focal point on surface |
---|
2889 | getMapSRS()->transformToWorld(osg::Vec3d(x, y, 0), world3); |
---|
2890 | // point on line between top and bottom points |
---|
2891 | world4 = world1 + (world2 - world1)/2.0; |
---|
2892 | double height = (world2 - world1).length(); |
---|
2893 | double fovy, aspect, near, far; |
---|
2894 | _viewer->getCamera()->getProjectionMatrixAsPerspective(fovy, aspect, near, far); |
---|
2895 | distance = height / (2. * tan(osg::DegreesToRadians(fovy)/2.)); |
---|
2896 | distance -= (world4 - world3).length(); |
---|
2897 | if (distance < 0.0) distance = 0.0; |
---|
2898 | distanceUnits = osgEarth::Units::METERS; |
---|
2899 | TRACE("fov: %g a: %g n: %g f: %g (%g, %g, %g, %g)", fovy, aspect, near, far, x1, y1, x2, y2); |
---|
2900 | TRACE("x: %g y: %g, dist: %g", x, y, distance); |
---|
2901 | } else { |
---|
2902 | assert(extent.getSRS()->isPlateCarre()); |
---|
2903 | ERROR("Plate Carree not supported"); |
---|
2904 | } |
---|
2905 | |
---|
2906 | return distance; |
---|
2907 | } |
---|
2908 | |
---|
2909 | /** |
---|
2910 | * \brief Dolly camera to set distance from focal point |
---|
2911 | * |
---|
2912 | * \param[in] dist distance in world coordinate units (typically meters) |
---|
2913 | */ |
---|
2914 | void Renderer::setCameraDistance(double dist) |
---|
2915 | { |
---|
2916 | TRACE("Enter: dist: %g", dist); |
---|
2917 | |
---|
2918 | if (_manipulator.valid()) { |
---|
2919 | TRACE("camDist: %g", _manipulator->getDistance()); |
---|
2920 | |
---|
2921 | _manipulator->setDistance(dist); |
---|
2922 | |
---|
2923 | _needsRedraw = true; |
---|
2924 | } |
---|
2925 | } |
---|
2926 | |
---|
2927 | void Renderer::keyPress(int key) |
---|
2928 | { |
---|
2929 | osgGA::EventQueue *queue = getEventQueue(); |
---|
2930 | if (queue != NULL) { |
---|
2931 | queue->keyPress(key); |
---|
2932 | _needsRedraw = true; |
---|
2933 | } |
---|
2934 | } |
---|
2935 | |
---|
2936 | void Renderer::keyRelease(int key) |
---|
2937 | { |
---|
2938 | osgGA::EventQueue *queue = getEventQueue(); |
---|
2939 | if (queue != NULL) { |
---|
2940 | queue->keyRelease(key); |
---|
2941 | _needsRedraw = true; |
---|
2942 | } |
---|
2943 | } |
---|
2944 | |
---|
2945 | void Renderer::setThrowingEnabled(bool state) |
---|
2946 | { |
---|
2947 | if (_manipulator.valid()) { |
---|
2948 | _manipulator->getSettings()->setThrowingEnabled(state); |
---|
2949 | } |
---|
2950 | } |
---|
2951 | |
---|
2952 | void Renderer::mouseDoubleClick(int button, double x, double y) |
---|
2953 | { |
---|
2954 | osgGA::EventQueue *queue = getEventQueue(); |
---|
2955 | if (queue != NULL) { |
---|
2956 | queue->mouseDoubleButtonPress((float)x, (float)y, button); |
---|
2957 | _needsRedraw = true; |
---|
2958 | } |
---|
2959 | } |
---|
2960 | |
---|
2961 | void Renderer::mouseClick(int button, double x, double y) |
---|
2962 | { |
---|
2963 | osgGA::EventQueue *queue = getEventQueue(); |
---|
2964 | if (queue != NULL) { |
---|
2965 | queue->mouseButtonPress((float)x, (float)y, button); |
---|
2966 | _needsRedraw = true; |
---|
2967 | } |
---|
2968 | } |
---|
2969 | |
---|
2970 | void Renderer::mouseDrag(int button, double x, double y) |
---|
2971 | { |
---|
2972 | osgGA::EventQueue *queue = getEventQueue(); |
---|
2973 | if (queue != NULL) { |
---|
2974 | queue->mouseMotion((float)x, (float)y); |
---|
2975 | _needsRedraw = true; |
---|
2976 | } |
---|
2977 | } |
---|
2978 | |
---|
2979 | void Renderer::mouseRelease(int button, double x, double y) |
---|
2980 | { |
---|
2981 | osgGA::EventQueue *queue = getEventQueue(); |
---|
2982 | if (queue != NULL) { |
---|
2983 | queue->mouseButtonRelease((float)x, (float)y, button); |
---|
2984 | _needsRedraw = true; |
---|
2985 | } |
---|
2986 | } |
---|
2987 | |
---|
2988 | void Renderer::mouseMotion(double x, double y) |
---|
2989 | { |
---|
2990 | #if 1 |
---|
2991 | osgGA::EventQueue *queue = getEventQueue(); |
---|
2992 | if (queue != NULL) { |
---|
2993 | queue->mouseMotion((float)x, (float)y); |
---|
2994 | _needsRedraw = true; |
---|
2995 | } |
---|
2996 | #else |
---|
2997 | if (_mouseCoordsTool.valid()) { |
---|
2998 | if (_viewer.valid() && _coordsCallback.valid()) { |
---|
2999 | osgEarth::GeoPoint mapPt; |
---|
3000 | if (mapMouseCoords((float)x, (float)y, mapPt)) { |
---|
3001 | _coordsCallback->set(mapPt, _viewer->asView(), _mapNode); |
---|
3002 | } else { |
---|
3003 | _coordsCallback->reset(_viewer->asView(), _mapNode); |
---|
3004 | } |
---|
3005 | _needsRedraw = true; |
---|
3006 | } |
---|
3007 | } |
---|
3008 | #endif |
---|
3009 | } |
---|
3010 | |
---|
3011 | void Renderer::mouseScroll(int direction) |
---|
3012 | { |
---|
3013 | osgGA::EventQueue *queue = getEventQueue(); |
---|
3014 | if (queue != NULL) { |
---|
3015 | queue->mouseScroll((direction > 0 ? osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN)); |
---|
3016 | _needsRedraw = true; |
---|
3017 | } |
---|
3018 | } |
---|
3019 | |
---|
3020 | /** |
---|
3021 | * \brief Set the RGB background color to render into the image |
---|
3022 | */ |
---|
3023 | void Renderer::setBackgroundColor(float color[3]) |
---|
3024 | { |
---|
3025 | _bgColor[0] = color[0]; |
---|
3026 | _bgColor[1] = color[1]; |
---|
3027 | _bgColor[2] = color[2]; |
---|
3028 | |
---|
3029 | if (_viewer.valid()) { |
---|
3030 | _viewer->getCamera()->setClearColor(osg::Vec4(color[0], color[1], color[2], 1)); |
---|
3031 | |
---|
3032 | _needsRedraw = true; |
---|
3033 | } |
---|
3034 | } |
---|
3035 | |
---|
3036 | /** |
---|
3037 | * \brief Sets flag to trigger rendering next time render() is called |
---|
3038 | */ |
---|
3039 | void Renderer::eventuallyRender() |
---|
3040 | { |
---|
3041 | _needsRedraw = true; |
---|
3042 | } |
---|
3043 | |
---|
3044 | /** |
---|
3045 | * \brief Get a timeout for select() |
---|
3046 | * |
---|
3047 | * If the paging thread is idle, returns <0 indicating that the |
---|
3048 | * select call can block until data is available. Otherwise, |
---|
3049 | * if the frame render time was faster than the target frame |
---|
3050 | * rate, return the remaining frame time. |
---|
3051 | */ |
---|
3052 | void Renderer::getTimeout(struct timeval *tv) |
---|
3053 | { |
---|
3054 | if (!checkNeedToDoFrame()) { |
---|
3055 | // <0 means no timeout, block until socket has data |
---|
3056 | // If _idleTimeout is positive, server will disconnect after timeout |
---|
3057 | tv->tv_sec = _idleTimeout; |
---|
3058 | tv->tv_usec = 0L; |
---|
3059 | } else if (_lastFrameTime < _minFrameTime) { |
---|
3060 | tv->tv_sec = 0L; |
---|
3061 | tv->tv_usec = (long)1.0e6*(_minFrameTime - _lastFrameTime); |
---|
3062 | } else { |
---|
3063 | // No timeout (poll) |
---|
3064 | tv->tv_sec = tv->tv_usec = 0L; |
---|
3065 | } |
---|
3066 | } |
---|
3067 | |
---|
3068 | /** |
---|
3069 | * \brief Check if paging thread is quiescent |
---|
3070 | */ |
---|
3071 | bool Renderer::isPagerIdle() |
---|
3072 | { |
---|
3073 | if (!_viewer.valid()) |
---|
3074 | return true; |
---|
3075 | else |
---|
3076 | return (!_viewer->getDatabasePager()->requiresUpdateSceneGraph() && |
---|
3077 | !_viewer->getDatabasePager()->getRequestsInProgress()); |
---|
3078 | } |
---|
3079 | |
---|
3080 | /** |
---|
3081 | * \brief Check is frame call is necessary to render and/or update |
---|
3082 | * in response to events or timed actions |
---|
3083 | */ |
---|
3084 | bool Renderer::checkNeedToDoFrame() |
---|
3085 | { |
---|
3086 | return (_needsRedraw || _pickPending || |
---|
3087 | (_viewer.valid() && _viewer->checkNeedToDoFrame())); |
---|
3088 | } |
---|
3089 | |
---|
3090 | /** |
---|
3091 | * \brief MapNode event phase |
---|
3092 | * |
---|
3093 | * This is called by the MapNode's event callback during the event |
---|
3094 | * traversal of the viewer |
---|
3095 | */ |
---|
3096 | void Renderer::mapNodeUpdate() |
---|
3097 | { |
---|
3098 | computeMapScale(); |
---|
3099 | } |
---|
3100 | |
---|
3101 | void Renderer::markFrameStart() |
---|
3102 | { |
---|
3103 | _startFrameTime = osg::Timer::instance()->tick(); |
---|
3104 | } |
---|
3105 | |
---|
3106 | void Renderer::markFrameEnd() |
---|
3107 | { |
---|
3108 | osg::Timer_t endFrameTick = osg::Timer::instance()->tick(); |
---|
3109 | _lastFrameTime = osg::Timer::instance()->delta_s(_startFrameTime, endFrameTick); |
---|
3110 | if (_lastFrameTime > _minFrameTime) { |
---|
3111 | FRAME("BROKE FRAME by %.2f msec", (_lastFrameTime - _minFrameTime)*1000.0f); |
---|
3112 | } else { |
---|
3113 | FRAME("Frame time: %.2f msec", _lastFrameTime*1000.0f); |
---|
3114 | } |
---|
3115 | #ifdef USE_THROTTLING_SLEEP |
---|
3116 | if (_lastFrameTime < _minFrameTime) { |
---|
3117 | FRAME("Sleeping for %.2f msec", (_minFrameTime - _lastFrameTime)*1000.0f); |
---|
3118 | OpenThreads::Thread::microSleep(static_cast<unsigned int>(1000000.0*(_minFrameTime - _lastFrameTime))); |
---|
3119 | } |
---|
3120 | #endif |
---|
3121 | } |
---|
3122 | |
---|
3123 | /** |
---|
3124 | * \brief Cause the rendering to render a new image if needed |
---|
3125 | * |
---|
3126 | * The _needsRedraw flag indicates if a state change has occured since |
---|
3127 | * the last rendered frame |
---|
3128 | */ |
---|
3129 | bool Renderer::render() |
---|
3130 | { |
---|
3131 | if (_viewer.valid() && checkNeedToDoFrame()) { |
---|
3132 | FRAME("Enter needsRedraw=%d", _needsRedraw ? 1 : 0); |
---|
3133 | _renderStartTime = osg::Timer::instance()->tick(); |
---|
3134 | FRAME("Before frame()"); |
---|
3135 | _viewer->frame(); |
---|
3136 | FRAME("After frame()"); |
---|
3137 | _renderStopTime = osg::Timer::instance()->tick(); |
---|
3138 | _renderTime = osg::Timer::instance()->delta_s(_renderStartTime, _renderStopTime); |
---|
3139 | FRAME("Render time: %g msec", _renderTime * 1000.0); |
---|
3140 | #ifndef SLEEP_AFTER_QUEUE_FRAME |
---|
3141 | _lastFrameTime = _renderTime; |
---|
3142 | #ifdef USE_THROTTLING_SLEEP |
---|
3143 | if (_lastFrameTime < _minFrameTime) { |
---|
3144 | FRAME("Sleeping for %.2f msec", (_minFrameTime - _lastFrameTime)*1000.0f); |
---|
3145 | OpenThreads::Thread::microSleep(static_cast<unsigned int>(1.0e6*(_minFrameTime - _lastFrameTime))); |
---|
3146 | } |
---|
3147 | #endif |
---|
3148 | #endif |
---|
3149 | #ifdef WANT_FRAME |
---|
3150 | if (_viewer->getViewerStats() != NULL) { |
---|
3151 | _viewer->getViewerStats()->report(std::cerr, _viewer->getViewerStats()->getLatestFrameNumber()); |
---|
3152 | } |
---|
3153 | #endif |
---|
3154 | _needsRedraw = false; |
---|
3155 | return true; |
---|
3156 | } else { |
---|
3157 | _renderStartTime = _renderStopTime = osg::Timer::instance()->tick(); |
---|
3158 | _renderTime = 0; |
---|
3159 | return false; |
---|
3160 | } |
---|
3161 | } |
---|
3162 | |
---|
3163 | /** |
---|
3164 | * \brief Read back the rendered framebuffer image |
---|
3165 | */ |
---|
3166 | osg::Image *Renderer::getRenderedFrame() |
---|
3167 | { |
---|
3168 | if (_captureCallback.valid()) |
---|
3169 | return _captureCallback->getImage(); |
---|
3170 | else |
---|
3171 | return NULL; |
---|
3172 | } |
---|
3173 | |
---|
3174 | void Renderer::setScaleBar(bool state) |
---|
3175 | { |
---|
3176 | if (_scaleLabel.valid()) { |
---|
3177 | _scaleLabel->setVisible(state); |
---|
3178 | } |
---|
3179 | if (_scaleBar.valid()) { |
---|
3180 | _scaleBar->setVisible(state); |
---|
3181 | } |
---|
3182 | _needsRedraw = true; |
---|
3183 | } |
---|
3184 | |
---|
3185 | void Renderer::setScaleBarUnits(ScaleBarUnits units) |
---|
3186 | { |
---|
3187 | _scaleBarUnits = units; |
---|
3188 | _needsRedraw = true; |
---|
3189 | } |
---|
3190 | |
---|
3191 | /** |
---|
3192 | * \brief Compute the scale ratio of the map based on a horizontal center line |
---|
3193 | * |
---|
3194 | * The idea here is to take 2 screen points on a horizontal line in the center |
---|
3195 | * of the screen and convert to lat/long. The lat/long coordinates are then |
---|
3196 | * used to compute the great circle distance (assuming spherical earth) between |
---|
3197 | * the points. |
---|
3198 | * |
---|
3199 | * We could use local projected map coordinates for the distance computation, |
---|
3200 | * which would be faster; however, this would not show e.g. the change in |
---|
3201 | * scale at different latitudes |
---|
3202 | */ |
---|
3203 | double Renderer::computeMapScale() |
---|
3204 | { |
---|
3205 | if (!_scaleLabel.valid() || !_scaleLabel->visible()) { |
---|
3206 | return -1.0; |
---|
3207 | } |
---|
3208 | if (!_mapNode.valid() || _mapNode->getTerrain() == NULL) { |
---|
3209 | ERROR("No map"); |
---|
3210 | return -1.0; |
---|
3211 | } |
---|
3212 | if (!_viewer.valid()) { |
---|
3213 | ERROR("No viewer"); |
---|
3214 | return -1.0; |
---|
3215 | } |
---|
3216 | |
---|
3217 | double x, y; |
---|
3218 | double pixelWidth = _windowWidth * 0.1 * 2.0; |
---|
3219 | if (pixelWidth < 10) |
---|
3220 | pixelWidth = 10; |
---|
3221 | if (pixelWidth > 150) |
---|
3222 | pixelWidth = 150; |
---|
3223 | x = (double)(_windowWidth -1)/2.0 - pixelWidth / 2.0; |
---|
3224 | y = (double)(_windowHeight-1)/2.0; |
---|
3225 | |
---|
3226 | osg::Vec3d world1, world2; |
---|
3227 | if (!_mapNode->getTerrain()->getWorldCoordsUnderMouse(_viewer->asView(), x, y, world1)) { |
---|
3228 | // off map |
---|
3229 | TRACE("Off map coords: %g %g", x, y); |
---|
3230 | _scaleLabel->setText(""); |
---|
3231 | _scaleBar->setWidth(0); |
---|
3232 | return -1.0; |
---|
3233 | } |
---|
3234 | x += pixelWidth; |
---|
3235 | if (!_mapNode->getTerrain()->getWorldCoordsUnderMouse(_viewer->asView(), x, y, world2)) { |
---|
3236 | // off map |
---|
3237 | TRACE("Off map coords: %g %g", x, y); |
---|
3238 | _scaleLabel->setText(""); |
---|
3239 | _scaleBar->setWidth(0); |
---|
3240 | return -1.0; |
---|
3241 | } |
---|
3242 | |
---|
3243 | #if 0 |
---|
3244 | TRACE("w1: %g %g %g w2: %g %g %g", |
---|
3245 | world1.x(), world1.y(), world1.z(), |
---|
3246 | world2.x(), world2.y(), world2.z()); |
---|
3247 | #endif |
---|
3248 | |
---|
3249 | double meters; |
---|
3250 | double radius = 6378137.0; |
---|
3251 | if (_mapNode->getMapSRS() && |
---|
3252 | _mapNode->getMapSRS()->getEllipsoid()) { |
---|
3253 | radius = _mapNode->getMapSRS()->getEllipsoid()->getRadiusEquator(); |
---|
3254 | } |
---|
3255 | if (!_map->isGeocentric() && |
---|
3256 | _mapNode->getMapSRS() && |
---|
3257 | _mapNode->getMapSRS()->isGeographic()) { |
---|
3258 | TRACE("Map is geographic"); |
---|
3259 | // World cords are already lat/long |
---|
3260 | // Compute great circle distance |
---|
3261 | meters = |
---|
3262 | osgEarth::GeoMath::distance(world1, world2, _mapNode->getMapSRS()); |
---|
3263 | } else if (_mapNode->getMapSRS()) { |
---|
3264 | // Get map coords in lat/long |
---|
3265 | osgEarth::GeoPoint mapPoint1, mapPoint2; |
---|
3266 | mapPoint1.fromWorld(_mapNode->getMapSRS(), world1); |
---|
3267 | mapPoint1.makeGeographic(); |
---|
3268 | mapPoint2.fromWorld(_mapNode->getMapSRS(), world2); |
---|
3269 | mapPoint2.makeGeographic(); |
---|
3270 | // Compute great circle distance |
---|
3271 | meters = |
---|
3272 | osgEarth::GeoMath::distance(osg::DegreesToRadians(mapPoint1.y()), |
---|
3273 | osg::DegreesToRadians(mapPoint1.x()), |
---|
3274 | osg::DegreesToRadians(mapPoint2.y()), |
---|
3275 | osg::DegreesToRadians(mapPoint2.x()), |
---|
3276 | radius); |
---|
3277 | } else { |
---|
3278 | // Assume geocentric? |
---|
3279 | ERROR("No map SRS"); |
---|
3280 | _scaleLabel->setText(""); |
---|
3281 | _scaleBar->setWidth(0); |
---|
3282 | return -1.0; |
---|
3283 | } |
---|
3284 | |
---|
3285 | double scale = meters / pixelWidth; |
---|
3286 | // 1mi = 5280 feet |
---|
3287 | //double scaleMiles = scale / 1609.344; // International mile = 1609.344m |
---|
3288 | //double scaleNauticalMiles = scale / 1852.0; // nautical mile = 1852m |
---|
3289 | //double scaleUSSurveyMiles = scale / 1609.347218694; // US survey mile = 5280 US survey feet |
---|
3290 | //double scaleUSSurveyFeet = scale * 3937.0/1200.0; // US survey foot = 1200/3937 m |
---|
3291 | #if 0 |
---|
3292 | TRACE("m: %g px: %g m/px: %g", meters, pixelWidth, scale); |
---|
3293 | #endif |
---|
3294 | _mapScale = scale; |
---|
3295 | switch (_scaleBarUnits) { |
---|
3296 | case UNITS_NAUTICAL_MILES: { |
---|
3297 | double nmi = meters / 1852.0; |
---|
3298 | scale = nmi / pixelWidth; |
---|
3299 | nmi = normalizeScaleNauticalMiles(nmi); |
---|
3300 | pixelWidth = nmi / scale; |
---|
3301 | if (_scaleLabel.valid()) { |
---|
3302 | _scaleLabel->setText(osgEarth::Stringify() |
---|
3303 | << nmi |
---|
3304 | << " nmi"); |
---|
3305 | } |
---|
3306 | } |
---|
3307 | break; |
---|
3308 | case UNITS_US_SURVEY_FEET: { |
---|
3309 | double feet = meters * 3937.0/1200.0; |
---|
3310 | scale = feet / pixelWidth; |
---|
3311 | feet = normalizeScaleFeet(feet); |
---|
3312 | pixelWidth = feet / scale; |
---|
3313 | if (_scaleLabel.valid()) { |
---|
3314 | if (feet >= 5280) { |
---|
3315 | _scaleLabel->setText(osgEarth::Stringify() |
---|
3316 | << feet / 5280.0 |
---|
3317 | << " miUS"); |
---|
3318 | } else { |
---|
3319 | _scaleLabel->setText(osgEarth::Stringify() |
---|
3320 | << feet |
---|
3321 | << " ftUS"); |
---|
3322 | } |
---|
3323 | } |
---|
3324 | } |
---|
3325 | break; |
---|
3326 | case UNITS_INTL_FEET: { |
---|
3327 | double feet = 5280.0 * meters / 1609.344; |
---|
3328 | scale = feet / pixelWidth; |
---|
3329 | feet = normalizeScaleFeet(feet); |
---|
3330 | pixelWidth = feet / scale; |
---|
3331 | if (_scaleLabel.valid()) { |
---|
3332 | if (feet >= 5280) { |
---|
3333 | _scaleLabel->setText(osgEarth::Stringify() |
---|
3334 | << feet / 5280.0 |
---|
3335 | << " mi"); |
---|
3336 | } else { |
---|
3337 | _scaleLabel->setText(osgEarth::Stringify() |
---|
3338 | << feet |
---|
3339 | << " ft"); |
---|
3340 | } |
---|
3341 | } |
---|
3342 | } |
---|
3343 | break; |
---|
3344 | case UNITS_METERS: |
---|
3345 | default: { |
---|
3346 | meters = normalizeScaleMeters(meters); |
---|
3347 | pixelWidth = meters / scale; |
---|
3348 | if (_scaleLabel.valid()) { |
---|
3349 | if (meters >= 1000) { |
---|
3350 | _scaleLabel->setText(osgEarth::Stringify() |
---|
3351 | << meters / 1000.0 |
---|
3352 | << " km"); |
---|
3353 | } else { |
---|
3354 | _scaleLabel->setText(osgEarth::Stringify() |
---|
3355 | << meters |
---|
3356 | << " m"); |
---|
3357 | } |
---|
3358 | } |
---|
3359 | } |
---|
3360 | break; |
---|
3361 | } |
---|
3362 | if (_scaleBar.valid()) { |
---|
3363 | _scaleBar->setWidth(pixelWidth); |
---|
3364 | } |
---|
3365 | return scale; |
---|
3366 | } |
---|
3367 | |
---|
3368 | std::string Renderer::getCanonicalPath(const std::string& url) const |
---|
3369 | { |
---|
3370 | std::string retStr; |
---|
3371 | std::string proto = osgDB::getServerProtocol(url); |
---|
3372 | if (proto.empty()) { |
---|
3373 | retStr = osgDB::getRealPath(url); |
---|
3374 | if (!osgDB::fileExists(retStr)) { |
---|
3375 | if (!url.empty() && url[0] != '/') { |
---|
3376 | // Relative URL, assume local:// |
---|
3377 | std::ostringstream oss; |
---|
3378 | oss << getCacheDirectory() << "/" << url; |
---|
3379 | retStr = oss.str(); |
---|
3380 | } else { |
---|
3381 | retStr = ""; |
---|
3382 | } |
---|
3383 | } |
---|
3384 | } else if (proto == "local") { |
---|
3385 | TRACE("Local protocol: '%s'", getLocalFilePath(url).c_str()); |
---|
3386 | std::ostringstream oss; |
---|
3387 | oss << getCacheDirectory() << "/" << getLocalFilePath(url); |
---|
3388 | retStr = oss.str(); |
---|
3389 | } else if (proto == "file") { |
---|
3390 | TRACE("File: '/%s'", osgDB::getServerFileName(url).c_str()); |
---|
3391 | std::ostringstream oss; |
---|
3392 | oss << "/" << osgDB::getServerFileName(url); |
---|
3393 | retStr = oss.str(); |
---|
3394 | } else if (proto == "idata") { |
---|
3395 | std::string fileName = osgDB::getServerFileName(url); |
---|
3396 | TRACE("IData protocol: coll: '%s', '%s'", osgDB::getServerAddress(url).c_str(), fileName.c_str()); |
---|
3397 | int collection = atoi(osgDB::getServerAddress(url).c_str()); |
---|
3398 | { |
---|
3399 | std::ostringstream oss; |
---|
3400 | oss << getCacheDirectory() << "/" << fileName; |
---|
3401 | retStr = oss.str(); |
---|
3402 | std::string dirPath = osgDB::getFilePath(retStr); |
---|
3403 | osgDB::makeDirectory(dirPath); |
---|
3404 | } |
---|
3405 | // If this is a shape file, get auxiliary files (e.g. .dbf, .shx, etc) |
---|
3406 | if (osgDB::getFileExtension(fileName) == "shp") { |
---|
3407 | // Check for files with same basename |
---|
3408 | std::vector<DirectoryItem> items; |
---|
3409 | std::string path = osgDB::getFilePath(fileName); |
---|
3410 | getContents(collection, path.c_str(), items); |
---|
3411 | // Get name without extension |
---|
3412 | std::string basename = osgDB::getStrippedName(fileName); |
---|
3413 | for (std::vector<DirectoryItem>::const_iterator itr = items.begin(); |
---|
3414 | itr != items.end(); ++itr) { |
---|
3415 | if (!itr->isDir && |
---|
3416 | osgDB::getStrippedName(itr->name) == basename) { |
---|
3417 | std::string localName; |
---|
3418 | { |
---|
3419 | std::ostringstream oss; |
---|
3420 | oss << getCacheDirectory() << "/" << itr->name; |
---|
3421 | localName = oss.str(); |
---|
3422 | } |
---|
3423 | if (!localName.empty() && !osgDB::fileExists(localName)) { |
---|
3424 | IData::Buffer buf; |
---|
3425 | { |
---|
3426 | std::ostringstream oss; |
---|
3427 | oss << "//" << itr->name; |
---|
3428 | IData::getFile(collection, oss.str().c_str(), &buf); |
---|
3429 | } |
---|
3430 | std::ofstream file(localName.c_str()); |
---|
3431 | file.write((char *)buf.data, buf.size); |
---|
3432 | file.close(); |
---|
3433 | } |
---|
3434 | } |
---|
3435 | } |
---|
3436 | } |
---|
3437 | if (!osgDB::fileExists(retStr)) { |
---|
3438 | std::ostringstream oss; |
---|
3439 | oss << "//" << fileName; |
---|
3440 | IData::Buffer buf; |
---|
3441 | IData::getFile(collection, oss.str().c_str(), &buf); |
---|
3442 | std::ofstream file(retStr.c_str()); |
---|
3443 | file.write((char *)buf.data, buf.size); |
---|
3444 | file.close(); |
---|
3445 | } |
---|
3446 | } else { |
---|
3447 | TRACE("Protocol: '%s' url: '%s'", proto.c_str(), url.c_str()); |
---|
3448 | retStr = url; |
---|
3449 | } |
---|
3450 | return retStr; |
---|
3451 | } |
---|
3452 | |
---|
3453 | void Renderer::writeScene(const std::string& file) |
---|
3454 | { |
---|
3455 | if (_sceneRoot.valid()) { |
---|
3456 | GraphPrintVisitor gpv; |
---|
3457 | _sceneRoot->accept(gpv); |
---|
3458 | //osgDB::writeNodeFile(*_sceneRoot.get(), file); |
---|
3459 | } |
---|
3460 | } |
---|