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 | #ifndef GEOVIS_RENDERER_H |
---|
9 | #define GEOVIS_RENDERER_H |
---|
10 | |
---|
11 | #include <sys/time.h> // For struct timeval |
---|
12 | #include <ctime> // For time_t |
---|
13 | #include <string> |
---|
14 | #include <vector> |
---|
15 | #include <tr1/unordered_map> |
---|
16 | #include <typeinfo> |
---|
17 | |
---|
18 | #include <osg/ref_ptr> |
---|
19 | #include <osg/Node> |
---|
20 | #include <osg/Image> |
---|
21 | #include <osg/TransferFunction> |
---|
22 | #include <osgText/String> |
---|
23 | #include <osgViewer/Viewer> |
---|
24 | #include <osgGA/StateSetManipulator> |
---|
25 | #include <osgUtil/IncrementalCompileOperation> |
---|
26 | |
---|
27 | #include <osgEarth/Version> |
---|
28 | #include <osgEarth/StringUtils> |
---|
29 | #include <osgEarth/Map> |
---|
30 | #include <osgEarth/Viewpoint> |
---|
31 | #include <osgEarth/ImageLayer> |
---|
32 | #include <osgEarth/ElevationLayer> |
---|
33 | #include <osgEarth/ModelLayer> |
---|
34 | #include <osgEarth/MaskLayer> |
---|
35 | #include <osgEarth/TileSource> |
---|
36 | #include <osgEarth/ModelSource> |
---|
37 | #include <osgEarth/GeoData> |
---|
38 | #include <osgEarthAnnotation/AnnotationNode> |
---|
39 | #include <osgEarthAnnotation/FeatureNode> |
---|
40 | #include <osgEarthUtil/Sky> |
---|
41 | #include <osgEarthUtil/RTTPicker> |
---|
42 | #include <osgEarthUtil/EarthManipulator> |
---|
43 | #include <osgEarthUtil/MouseCoordsTool> |
---|
44 | #include <osgEarthUtil/Controls> |
---|
45 | #include <osgEarthUtil/Formatter> |
---|
46 | #include <osgEarthUtil/MGRSFormatter> |
---|
47 | #include <osgEarthUtil/AutoClipPlaneHandler> |
---|
48 | #include <osgEarthUtil/VerticalScale> |
---|
49 | |
---|
50 | #define USE_RTT_PICKER |
---|
51 | |
---|
52 | #include "Types.h" |
---|
53 | #include "Trace.h" |
---|
54 | #include "MouseCoordsTool.h" |
---|
55 | #include "ScaleBar.h" |
---|
56 | #include "Placard.h" |
---|
57 | |
---|
58 | // Controls if TGA format is sent to client |
---|
59 | //#define RENDER_TARGA |
---|
60 | #define TARGA_BYTES_PER_PIXEL 3 |
---|
61 | |
---|
62 | namespace GeoVis { |
---|
63 | |
---|
64 | class ScreenCaptureCallback : public osg::Camera::DrawCallback |
---|
65 | { |
---|
66 | public: |
---|
67 | ScreenCaptureCallback(osg::Texture2D *texture = NULL) : |
---|
68 | osg::Camera::DrawCallback(), |
---|
69 | _texture(texture) |
---|
70 | { |
---|
71 | _image = new osg::Image; |
---|
72 | } |
---|
73 | |
---|
74 | virtual void operator()(osg::RenderInfo &renderInfo) const |
---|
75 | { |
---|
76 | FRAME("Enter ScreenCaptureCallback"); |
---|
77 | int width, height; |
---|
78 | if (renderInfo.getCurrentCamera() == NULL) { |
---|
79 | ERROR("No camera"); |
---|
80 | return; |
---|
81 | } |
---|
82 | if (renderInfo.getCurrentCamera()->getViewport() == NULL) { |
---|
83 | ERROR("No viewport"); |
---|
84 | return; |
---|
85 | } |
---|
86 | width = (int)renderInfo.getCurrentCamera()->getViewport()->width(); |
---|
87 | height = (int)renderInfo.getCurrentCamera()->getViewport()->height(); |
---|
88 | FRAME("readPixels: %d x %d", width, height); |
---|
89 | #if 0 //USE_OFFSCREEN_FRAMEBUFFER |
---|
90 | _image = _texture->getImage(); |
---|
91 | #else |
---|
92 | #ifdef RENDER_TARGA |
---|
93 | _image->readPixels(0, 0, width, height, |
---|
94 | GL_BGR, GL_UNSIGNED_BYTE); |
---|
95 | #else |
---|
96 | _image->readPixels(0, 0, width, height, |
---|
97 | GL_RGB, GL_UNSIGNED_BYTE); |
---|
98 | #endif |
---|
99 | #endif |
---|
100 | } |
---|
101 | |
---|
102 | osg::Image *getImage() |
---|
103 | { |
---|
104 | return _image.get(); |
---|
105 | } |
---|
106 | |
---|
107 | osg::Texture2D *getTexture() |
---|
108 | { |
---|
109 | return _texture.get(); |
---|
110 | } |
---|
111 | |
---|
112 | private: |
---|
113 | osg::ref_ptr<osg::Texture2D> _texture; |
---|
114 | osg::ref_ptr<osg::Image> _image; |
---|
115 | }; |
---|
116 | |
---|
117 | /** |
---|
118 | * \brief GIS Renderer |
---|
119 | */ |
---|
120 | class Renderer |
---|
121 | { |
---|
122 | public: |
---|
123 | typedef std::string ColorMapId; |
---|
124 | typedef std::string ViewpointId; |
---|
125 | |
---|
126 | enum GraticuleType { |
---|
127 | GRATICULE_UTM, |
---|
128 | GRATICULE_MGRS, |
---|
129 | GRATICULE_GEODETIC, |
---|
130 | GRATICULE_SHADER |
---|
131 | }; |
---|
132 | |
---|
133 | enum CoordinateDisplayType { |
---|
134 | COORDS_LATLONG_DECIMAL_DEGREES, |
---|
135 | COORDS_LATLONG_DEGREES_DECIMAL_MINUTES, |
---|
136 | COORDS_LATLONG_DEGREES_MINUTES_SECONDS, |
---|
137 | COORDS_MGRS |
---|
138 | }; |
---|
139 | |
---|
140 | enum SelectMode { |
---|
141 | SELECT_OFF, |
---|
142 | SELECT_ON |
---|
143 | }; |
---|
144 | |
---|
145 | Renderer(); |
---|
146 | virtual ~Renderer(); |
---|
147 | |
---|
148 | void setResourcePath(const std::string& path) |
---|
149 | { |
---|
150 | TRACE("Set resource path to %s", path.c_str()); |
---|
151 | _resourcePath = path; |
---|
152 | } |
---|
153 | |
---|
154 | void setCacheBaseDirectory(const std::string& path) |
---|
155 | { |
---|
156 | TRACE("Set cache base dir to %s", path.c_str()); |
---|
157 | _cacheBaseDir = path; |
---|
158 | } |
---|
159 | |
---|
160 | std::string getCacheDirectory() const |
---|
161 | { |
---|
162 | return _cacheDir; |
---|
163 | } |
---|
164 | |
---|
165 | void setupCache(); |
---|
166 | |
---|
167 | std::string getIconFile(const char *name) const; |
---|
168 | |
---|
169 | std::string getBaseImage() const; |
---|
170 | |
---|
171 | std::string getPinIcon() const; |
---|
172 | |
---|
173 | void setAttribution(const std::string& attrib); |
---|
174 | |
---|
175 | // Colormaps |
---|
176 | |
---|
177 | void addColorMap(const ColorMapId& id, osg::TransferFunction1D *xfer); |
---|
178 | |
---|
179 | void deleteColorMap(const ColorMapId& id); |
---|
180 | |
---|
181 | void setColorMapNumberOfTableEntries(const ColorMapId& id, int numEntries); |
---|
182 | |
---|
183 | bool renderColorMap(const ColorMapId& id, int width, int height, |
---|
184 | osg::Image *imgData, |
---|
185 | bool opaque, float bgColor[3], |
---|
186 | bool bgr = false, |
---|
187 | int bytesPerPixel = 3) const; |
---|
188 | |
---|
189 | void getColorMapRange(const ColorMapId& id, float *min, float *max) const; |
---|
190 | |
---|
191 | std::string getColorMapFilePath(const ColorMapId& id) const; |
---|
192 | |
---|
193 | void saveCLRFile(const std::string& path, osg::TransferFunction1D *xfer); |
---|
194 | |
---|
195 | // Scene |
---|
196 | |
---|
197 | void loadEarthFile(const char *path); |
---|
198 | |
---|
199 | void resetMap(osgEarth::MapOptions::CoordinateSystemType type, |
---|
200 | const osg::Vec4f& bgColor = osg::Vec4f(1,1,1,1), |
---|
201 | const char *profile = NULL, |
---|
202 | double bounds[4] = NULL); |
---|
203 | |
---|
204 | void clearMap(); |
---|
205 | |
---|
206 | // Map options |
---|
207 | |
---|
208 | void setCoordinateReadout(bool state, |
---|
209 | CoordinateDisplayType type = COORDS_LATLONG_DECIMAL_DEGREES, |
---|
210 | int precision = -1); |
---|
211 | |
---|
212 | void setReadout(int mouseX, int mouseY); |
---|
213 | |
---|
214 | void clearReadout(); |
---|
215 | |
---|
216 | void setScaleBar(bool state); |
---|
217 | |
---|
218 | void setScaleBarUnits(ScaleBarUnits units); |
---|
219 | |
---|
220 | void setGraticule(bool enable, GraticuleType type = GRATICULE_SHADER); |
---|
221 | |
---|
222 | void setViewerLightType(osg::View::LightingMode mode); |
---|
223 | |
---|
224 | void setLighting(bool state); |
---|
225 | |
---|
226 | void setTerrainColor(const osg::Vec4f& color); |
---|
227 | |
---|
228 | void setTerrainEdges(bool state) {} |
---|
229 | |
---|
230 | void setTerrainLineColor(const osg::Vec4f& color) {} |
---|
231 | |
---|
232 | void setTerrainLineWidth(float width) {} |
---|
233 | |
---|
234 | void setTerrainLighting(bool state); |
---|
235 | |
---|
236 | void setTerrainVerticalScale(double scale); |
---|
237 | |
---|
238 | void setTerrainWireframe(bool state); |
---|
239 | |
---|
240 | void setEphemerisTime(time_t utcTime); |
---|
241 | |
---|
242 | void setEphemerisTime(int year, int month, int day, double hours); |
---|
243 | |
---|
244 | void setSkyAmbient(float ambientLevel); |
---|
245 | |
---|
246 | // Image raster layers |
---|
247 | |
---|
248 | int getNumImageLayers() const |
---|
249 | { |
---|
250 | return (_map.valid() ? _map->getNumImageLayers() : 0); |
---|
251 | } |
---|
252 | |
---|
253 | void getImageLayerNames(std::vector<std::string>& names) const |
---|
254 | { |
---|
255 | if (_map.valid()) { |
---|
256 | osgEarth::ImageLayerVector layerVector; |
---|
257 | _map->getImageLayers(layerVector); |
---|
258 | osgEarth::ImageLayerVector::const_iterator itr; |
---|
259 | for (itr = layerVector.begin(); itr != layerVector.end(); ++itr) { |
---|
260 | //osgEarth::UID uid = (*itr)->getUID(); |
---|
261 | names.push_back((*itr)->getName()); |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | bool addImageLayer(const char *name, |
---|
267 | osgEarth::TileSourceOptions& opts, |
---|
268 | unsigned int pos = UINT_MAX, |
---|
269 | bool enableCache = true, |
---|
270 | bool coverage = false, |
---|
271 | bool makeShared = false, |
---|
272 | bool visible = true, |
---|
273 | unsigned int minLOD = 0, |
---|
274 | unsigned int maxLOD = 23); |
---|
275 | |
---|
276 | void removeImageLayer(const char *name); |
---|
277 | |
---|
278 | void moveImageLayer(const char *name, unsigned int pos); |
---|
279 | |
---|
280 | bool getImageLayerExtent(const char *name, osgEarth::GeoExtent &ext); |
---|
281 | |
---|
282 | void setImageLayerVisibleRange(const char *name, float min, float max); |
---|
283 | |
---|
284 | void setImageLayerLODRange(const char *name, int min, int max); |
---|
285 | |
---|
286 | void setImageLayerOpacity(const char *name, double opacity); |
---|
287 | |
---|
288 | void setImageLayerVisibility(const char *name, bool state); |
---|
289 | |
---|
290 | void addColorFilter(const char *name, const char *shader); |
---|
291 | |
---|
292 | void removeColorFilter(const char *name, int idx = -1); |
---|
293 | |
---|
294 | // Sequence control |
---|
295 | |
---|
296 | bool layerHasSequence(const char *name); |
---|
297 | |
---|
298 | bool sequencePause(const char *name); |
---|
299 | |
---|
300 | bool sequencePlay(const char *name); |
---|
301 | |
---|
302 | bool sequenceSeek(const char *name, unsigned int frame); |
---|
303 | |
---|
304 | // Elevation raster layers |
---|
305 | |
---|
306 | int getNumElevationLayers() const |
---|
307 | { |
---|
308 | return (_map.valid() ? _map->getNumElevationLayers() : 0); |
---|
309 | } |
---|
310 | |
---|
311 | void getElevationLayerNames(std::vector<std::string>& names) const |
---|
312 | { |
---|
313 | if (_map.valid()) { |
---|
314 | osgEarth::ElevationLayerVector layerVector; |
---|
315 | _map->getElevationLayers(layerVector); |
---|
316 | osgEarth::ElevationLayerVector::const_iterator itr; |
---|
317 | for (itr = layerVector.begin(); itr != layerVector.end(); ++itr) { |
---|
318 | //osgEarth::UID uid = (*itr)->getUID(); |
---|
319 | names.push_back((*itr)->getName()); |
---|
320 | } |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | void addElevationLayer(const char *name, |
---|
325 | osgEarth::TileSourceOptions& opts, |
---|
326 | unsigned int pos = UINT_MAX, |
---|
327 | bool enableCache = true, |
---|
328 | bool visible = true, |
---|
329 | const char *verticalDatum = NULL, |
---|
330 | unsigned int minLOD = 0, |
---|
331 | unsigned int maxLOD = 23); |
---|
332 | |
---|
333 | void removeElevationLayer(const char *name); |
---|
334 | |
---|
335 | void moveElevationLayer(const char *name, unsigned int pos); |
---|
336 | |
---|
337 | bool getElevationLayerExtent(const char *name, osgEarth::GeoExtent &ext); |
---|
338 | |
---|
339 | void setElevationLayerVisibleRange(const char *name, float min, float max); |
---|
340 | |
---|
341 | void setElevationLayerVisibility(const char *name, bool state); |
---|
342 | |
---|
343 | // Terrain mask layers |
---|
344 | |
---|
345 | int getNumTerrainMaskLayers() const |
---|
346 | { |
---|
347 | if (!_map.valid()) return 0; |
---|
348 | |
---|
349 | osgEarth::MaskLayerVector layers; |
---|
350 | _map->getTerrainMaskLayers(layers); |
---|
351 | return (int)layers.size(); |
---|
352 | } |
---|
353 | |
---|
354 | void getTerrainMaskLayerNames(std::vector<std::string>& names) const |
---|
355 | { |
---|
356 | if (_map.valid()) { |
---|
357 | osgEarth::MaskLayerVector layerVector; |
---|
358 | _map->getTerrainMaskLayers(layerVector); |
---|
359 | osgEarth::MaskLayerVector::const_iterator itr; |
---|
360 | for (itr = layerVector.begin(); itr != layerVector.end(); ++itr) { |
---|
361 | //osgEarth::UID uid = (*itr)->getUID(); |
---|
362 | names.push_back((*itr)->getName()); |
---|
363 | } |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | osgEarth::MaskLayer *getTerrainMaskLayerByName(const std::string& name) |
---|
368 | { |
---|
369 | if (!_map.valid()) |
---|
370 | return NULL; |
---|
371 | |
---|
372 | osgEarth::MaskLayerVector layerVector; |
---|
373 | _map->getTerrainMaskLayers(layerVector); |
---|
374 | osgEarth::MaskLayerVector::iterator itr; |
---|
375 | for (itr = layerVector.begin(); itr != layerVector.end(); ++itr) { |
---|
376 | if ((*itr)->getName() == name) { |
---|
377 | return (*itr).get(); |
---|
378 | } |
---|
379 | } |
---|
380 | return NULL; |
---|
381 | } |
---|
382 | |
---|
383 | void addTerrainMaskLayer(const char *name, |
---|
384 | osgEarth::MaskSourceOptions& opts, |
---|
385 | unsigned int minLOD = 0); |
---|
386 | |
---|
387 | void removeTerrainMaskLayer(const char *name); |
---|
388 | |
---|
389 | bool getTerrainMaskLayerExtent(const char *name, osgEarth::GeoExtent &ext); |
---|
390 | |
---|
391 | // Model layers |
---|
392 | |
---|
393 | int getNumModelLayers() const |
---|
394 | { |
---|
395 | return (_map.valid() ? _map->getNumModelLayers() : 0); |
---|
396 | } |
---|
397 | |
---|
398 | void getModelLayerNames(std::vector<std::string>& names) const |
---|
399 | { |
---|
400 | if (_map.valid()) { |
---|
401 | osgEarth::ModelLayerVector layerVector; |
---|
402 | _map->getModelLayers(layerVector); |
---|
403 | osgEarth::ModelLayerVector::const_iterator itr; |
---|
404 | for (itr = layerVector.begin(); itr != layerVector.end(); ++itr) { |
---|
405 | //osgEarth::UID uid = (*itr)->getUID(); |
---|
406 | names.push_back((*itr)->getName()); |
---|
407 | } |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | void addModelLayer(const char *name, |
---|
412 | osgEarth::ModelSourceOptions& opts, |
---|
413 | unsigned int pos = UINT_MAX, |
---|
414 | bool enableCache = true, |
---|
415 | bool lighting = true, |
---|
416 | bool visible = true, |
---|
417 | bool terrainPatch = false); |
---|
418 | |
---|
419 | void removeModelLayer(const char *name); |
---|
420 | |
---|
421 | void moveModelLayer(const char *name, unsigned int pos); |
---|
422 | |
---|
423 | bool getModelLayerExtent(const char *name, osgEarth::GeoExtent &ext); |
---|
424 | |
---|
425 | //void setModelLayerVisibleRange(const char *name, float min, float max); |
---|
426 | |
---|
427 | void setModelLayerOpacity(const char *name, double opacity); |
---|
428 | |
---|
429 | void setModelLayerVisibility(const char *name, bool state); |
---|
430 | |
---|
431 | // Render window |
---|
432 | |
---|
433 | void setWindowSize(int width, int height); |
---|
434 | |
---|
435 | int getWindowWidth() const |
---|
436 | { |
---|
437 | return _windowWidth; |
---|
438 | } |
---|
439 | |
---|
440 | int getWindowHeight() const |
---|
441 | { |
---|
442 | return _windowHeight; |
---|
443 | } |
---|
444 | |
---|
445 | // Camera |
---|
446 | |
---|
447 | void saveNamedViewpoint(const char *name); |
---|
448 | |
---|
449 | bool restoreNamedViewpoint(const char *name, double durationSecs); |
---|
450 | |
---|
451 | bool removeNamedViewpoint(const char *name); |
---|
452 | |
---|
453 | osgEarth::Viewpoint getViewpoint(); |
---|
454 | |
---|
455 | void setViewpoint(const osgEarth::Viewpoint& v, double durationSecs = 0.0); |
---|
456 | |
---|
457 | double getMaxDistanceFromExtent(const osgEarth::GeoExtent& extent); |
---|
458 | |
---|
459 | void setViewpointFromExtent(const osgEarth::GeoExtent& ext, |
---|
460 | double durationSecs = 0.0); |
---|
461 | |
---|
462 | void setViewpointFromRect(double xmin, double ymin, |
---|
463 | double xmax, double ymax, |
---|
464 | const osgEarth::SpatialReference *srs = NULL, |
---|
465 | double durationSecs = 0.0); |
---|
466 | |
---|
467 | void resetCamera(bool resetOrientation = true); |
---|
468 | |
---|
469 | void setCameraOrientation(const double quat[4], bool absolute = true); |
---|
470 | |
---|
471 | void panCamera(double x, double y); |
---|
472 | |
---|
473 | void rotateCamera(double x, double y); |
---|
474 | |
---|
475 | void zoomCamera(double z); |
---|
476 | |
---|
477 | void setCameraDistance(double dist); |
---|
478 | |
---|
479 | void setLODScale(float scale); |
---|
480 | |
---|
481 | // Keyboard events |
---|
482 | |
---|
483 | void keyPress(int key); |
---|
484 | |
---|
485 | void keyRelease(int key); |
---|
486 | |
---|
487 | // Mouse events |
---|
488 | |
---|
489 | void setThrowingEnabled(bool state); |
---|
490 | |
---|
491 | void mouseClick(int button, double x, double y); |
---|
492 | |
---|
493 | void mouseDoubleClick(int button, double x, double y); |
---|
494 | |
---|
495 | void mouseDrag(int button, double x, double y); |
---|
496 | |
---|
497 | void mouseRelease(int button, double x, double y); |
---|
498 | |
---|
499 | void mouseMotion(double x, double y); |
---|
500 | |
---|
501 | void mouseScroll(int direction); |
---|
502 | |
---|
503 | void pickPending(bool value) |
---|
504 | { _pickPending = value; } |
---|
505 | |
---|
506 | // Rendering an image |
---|
507 | |
---|
508 | void setBackgroundColor(float color[3]); |
---|
509 | |
---|
510 | void eventuallyRender(); |
---|
511 | |
---|
512 | bool render(); |
---|
513 | |
---|
514 | osg::Image *getRenderedFrame(); |
---|
515 | |
---|
516 | bool mapMouseCoords(float mouseX, float mouseY, |
---|
517 | osgEarth::GeoPoint &pt, bool invertY = true); |
---|
518 | |
---|
519 | double computeMapScale(); |
---|
520 | |
---|
521 | const osgEarth::SpatialReference *getMapSRS() |
---|
522 | { |
---|
523 | if (_mapNode.valid()) { |
---|
524 | return _mapNode->getMapSRS(); |
---|
525 | } else { |
---|
526 | return NULL; |
---|
527 | } |
---|
528 | } |
---|
529 | |
---|
530 | void addPlaceNode(double latitude, double longitude, char *labelText); |
---|
531 | |
---|
532 | void hoverPlaceNode(int x, int y, bool invertY = true); |
---|
533 | |
---|
534 | void deletePlaceNode(int x, int y, bool invertY = true); |
---|
535 | |
---|
536 | bool getMousePoint(double *x, double *y, double *z) |
---|
537 | { |
---|
538 | return (_coordsCallback.valid() && _coordsCallback->report(x, y, z)); |
---|
539 | } |
---|
540 | |
---|
541 | bool mouseToLatLong(int mouseX, int mouseY, |
---|
542 | double *latitude, double *longitude); |
---|
543 | |
---|
544 | bool getWorldCoords(const osgEarth::GeoPoint& mapPt, osg::Vec3d *world); |
---|
545 | |
---|
546 | bool worldToScreen(const osg::Vec3d& world, osg::Vec3d *screen, |
---|
547 | bool invertY = true); |
---|
548 | |
---|
549 | bool worldToScreen(std::vector<osg::Vec3d>& coords, bool invertY = true); |
---|
550 | |
---|
551 | void setMaximumFrameRateInHertz(double rate) |
---|
552 | { |
---|
553 | if (rate > 60.0) |
---|
554 | rate = 60.0; |
---|
555 | if (rate < 0.25) |
---|
556 | rate = 0.25; |
---|
557 | _minFrameTime = 1.0/rate; |
---|
558 | if (_viewer.valid()) { |
---|
559 | osgUtil::IncrementalCompileOperation *op = |
---|
560 | _viewer->getDatabasePager()->getIncrementalCompileOperation(); |
---|
561 | if (op != NULL) { |
---|
562 | TRACE("Setting DB Pager target frame rate to %g", rate); |
---|
563 | op->setTargetFrameRate(rate); |
---|
564 | } |
---|
565 | } |
---|
566 | TRACE("Frame rate target: %.2f Hz", (float)getMaximumFrameRateInHertz()); |
---|
567 | TRACE("Frame time target: %.2f msec", _minFrameTime * 1000.0f); |
---|
568 | } |
---|
569 | |
---|
570 | void setMaximumBitrate(double bitsPerSecond) |
---|
571 | { |
---|
572 | TRACE("Setting max bitrate to %g", bitsPerSecond); |
---|
573 | unsigned long bitsPerFrame = (_windowWidth * _windowHeight * 3 + 16) * 8; |
---|
574 | double fps = bitsPerSecond / ((double)bitsPerFrame); |
---|
575 | setMaximumFrameRateInHertz(fps); |
---|
576 | TRACE("Bandwidth target: %.2f Mbps", (float)(getMaximumBitrate()/1.0e6)); |
---|
577 | } |
---|
578 | |
---|
579 | double getMaximumFrameRateInHertz() |
---|
580 | { |
---|
581 | return (1.0/_minFrameTime); |
---|
582 | } |
---|
583 | |
---|
584 | double getMaximumBitrate() |
---|
585 | { |
---|
586 | unsigned long bitsPerFrame = (_windowWidth * _windowHeight * 3 + 16) * 8; |
---|
587 | return ((double)bitsPerFrame * getMaximumFrameRateInHertz()); |
---|
588 | } |
---|
589 | |
---|
590 | void markFrameStart(); |
---|
591 | |
---|
592 | void markFrameEnd(); |
---|
593 | |
---|
594 | void setIdleTimeout(long timeout) |
---|
595 | { |
---|
596 | if (timeout != -1L && |
---|
597 | (double)timeout < _minFrameTime) { |
---|
598 | ERROR("Timeout must be more than %g sec", _minFrameTime); |
---|
599 | return; |
---|
600 | } |
---|
601 | TRACE("Setting idle timeout to %ld sec", timeout); |
---|
602 | _idleTimeout = timeout; |
---|
603 | } |
---|
604 | |
---|
605 | long getIdleTimeout() |
---|
606 | { |
---|
607 | return _idleTimeout; |
---|
608 | } |
---|
609 | |
---|
610 | void getTimeout(struct timeval *tv); |
---|
611 | |
---|
612 | void mapNodeUpdate(); |
---|
613 | |
---|
614 | std::string getCanonicalPath(const std::string& url) const; |
---|
615 | |
---|
616 | void writeScene(const std::string& file); |
---|
617 | |
---|
618 | // Placard info window |
---|
619 | |
---|
620 | void enablePlacard(const char *layerName, bool state); |
---|
621 | |
---|
622 | void setPlacardConfig(const Placard& placardConf, const char *layerName); |
---|
623 | |
---|
624 | Placard getPlacardConfig(const char *layerName) |
---|
625 | { |
---|
626 | Placard ret; |
---|
627 | PlacardHashmap::iterator itr = _placardConfigs.find(layerName); |
---|
628 | if (itr != _placardConfigs.end()) { |
---|
629 | ret = itr->second; |
---|
630 | } |
---|
631 | return ret; |
---|
632 | } |
---|
633 | |
---|
634 | // Selection |
---|
635 | |
---|
636 | void setSelectMode(SelectMode mode); |
---|
637 | |
---|
638 | void selectFeatures(std::vector<unsigned long>& featureIDs, |
---|
639 | const char *layerName, bool clear = true); |
---|
640 | |
---|
641 | void deselectFeatures(std::vector<unsigned long>& featureIDs, |
---|
642 | const char *layerName); |
---|
643 | |
---|
644 | void addPlacard(const osgEarth::GeoPoint& location, |
---|
645 | osgEarth::Features::Feature *feature, |
---|
646 | const char *layerName); |
---|
647 | |
---|
648 | void clearSelection(); |
---|
649 | |
---|
650 | void addRhumbBox(double latMin, double latMax, |
---|
651 | double longMin, double longMax); |
---|
652 | |
---|
653 | void initBoxSelection(int x, int y); |
---|
654 | void updateBoxSelection(int x, int y); |
---|
655 | void getBoxSelection(double *latMin, double *latMax, |
---|
656 | double *longMin, double *longMax, |
---|
657 | const osgEarth::SpatialReference *outSRS = NULL); |
---|
658 | void clearBoxSelection(); |
---|
659 | |
---|
660 | // These methods are based on the deprecated annotation API |
---|
661 | bool select(osgEarth::Annotation::AnnotationNode *node) |
---|
662 | { |
---|
663 | if (_selected.find(node) == _selected.end()) { |
---|
664 | _selected.insert(node); |
---|
665 | return true; |
---|
666 | } else { |
---|
667 | return false; |
---|
668 | } |
---|
669 | } |
---|
670 | std::set<osgEarth::Annotation::AnnotationNode*>& getHovered() |
---|
671 | { return _hovered; } |
---|
672 | std::set<osgEarth::Annotation::AnnotationNode*>& getSelected() |
---|
673 | { return _selected; } |
---|
674 | |
---|
675 | // Scene Graph |
---|
676 | |
---|
677 | osg::Group *getSceneRoot() |
---|
678 | { return _sceneRoot.get(); } |
---|
679 | osgEarth::MapNode *getMapNode() |
---|
680 | { return _mapNode.get(); } |
---|
681 | osg::Group *getAnnotations() |
---|
682 | { |
---|
683 | initAnnotations(); |
---|
684 | return _annotations.get(); |
---|
685 | } |
---|
686 | osg::Group *getPlaceNodes() |
---|
687 | { |
---|
688 | initAnnotations(); |
---|
689 | return _placeNodes.get(); |
---|
690 | } |
---|
691 | |
---|
692 | private: |
---|
693 | typedef std::tr1::unordered_map<ColorMapId, osg::ref_ptr<osg::TransferFunction1D> > ColorMapHashmap; |
---|
694 | typedef std::tr1::unordered_map<ViewpointId, osgEarth::Viewpoint> ViewpointHashmap; |
---|
695 | typedef std::tr1::unordered_map<std::string, Placard> PlacardHashmap; |
---|
696 | typedef std::tr1::unordered_map<std::string, std::set<unsigned long> > FeatureSelectionHashmap; |
---|
697 | |
---|
698 | void initAnnotations(); |
---|
699 | |
---|
700 | void clearSelectionAnnotationNodes(); |
---|
701 | |
---|
702 | void initViewer(); |
---|
703 | |
---|
704 | void finalizeViewer(); |
---|
705 | |
---|
706 | void initControls(); |
---|
707 | |
---|
708 | void updateDebugLabel(); |
---|
709 | |
---|
710 | void initEarthManipulator(); |
---|
711 | |
---|
712 | void initMouseCoordsTool(CoordinateDisplayType type = COORDS_LATLONG_DECIMAL_DEGREES, |
---|
713 | int precision = -1); |
---|
714 | |
---|
715 | osgEarth::Util::MGRSFormatter::Precision getMGRSPrecision(int precisionInMeters); |
---|
716 | |
---|
717 | void initColorMaps(); |
---|
718 | |
---|
719 | void initCamera(); |
---|
720 | |
---|
721 | bool isPagerIdle(); |
---|
722 | |
---|
723 | bool checkNeedToDoFrame(); |
---|
724 | |
---|
725 | osgGA::EventQueue *getEventQueue(); |
---|
726 | |
---|
727 | bool _needsRedraw; |
---|
728 | int _windowWidth, _windowHeight; |
---|
729 | double _mapScale; |
---|
730 | float _bgColor[3]; |
---|
731 | |
---|
732 | long _idleTimeout; |
---|
733 | double _minFrameTime; |
---|
734 | double _lastFrameTime; |
---|
735 | double _renderTime; |
---|
736 | osg::Timer_t _startFrameTime; |
---|
737 | osg::Timer_t _renderStartTime; |
---|
738 | osg::Timer_t _renderStopTime; |
---|
739 | |
---|
740 | ColorMapHashmap _colorMaps; |
---|
741 | |
---|
742 | std::string _resourcePath; |
---|
743 | std::string _cacheBaseDir; |
---|
744 | std::string _cacheDir; |
---|
745 | std::string _baseURI; |
---|
746 | std::string _attribution; |
---|
747 | |
---|
748 | osg::ref_ptr<osg::Group> _sceneRoot; |
---|
749 | osg::ref_ptr<osg::Group> _graticule; |
---|
750 | osg::ref_ptr<osg::Group> _annotations; |
---|
751 | PlacardHashmap _placardConfigs; |
---|
752 | double _anchorLat, _anchorLong; |
---|
753 | SelectMode _selectMode; |
---|
754 | osg::ref_ptr<osgEarth::Annotation::FeatureNode> _selectionBox; |
---|
755 | #ifdef USE_RTT_PICKER |
---|
756 | osg::ref_ptr<osgEarth::Util::RTTPicker> _picker; |
---|
757 | #endif |
---|
758 | bool _pickPending; |
---|
759 | osg::ref_ptr<osg::Group> _placeNodes; |
---|
760 | FeatureSelectionHashmap _selectedFeatures; |
---|
761 | std::set<osgEarth::Annotation::AnnotationNode *> _hovered; |
---|
762 | std::set<osgEarth::Annotation::AnnotationNode *> _selected; |
---|
763 | osg::ref_ptr<osgEarth::MapNode> _mapNode; |
---|
764 | osg::ref_ptr<osgEarth::Map> _map; |
---|
765 | osg::ref_ptr<osgEarth::Util::SkyNode> _skyNode; |
---|
766 | osg::ref_ptr<osgViewer::Viewer> _viewer; |
---|
767 | osg::ref_ptr<ScreenCaptureCallback> _captureCallback; |
---|
768 | osg::ref_ptr<osgEarth::Util::AutoClipPlaneCullCallback> _clipPlaneCullCallback; |
---|
769 | osg::ref_ptr<osgEarth::Util::Controls::HBox> _coordsBox; |
---|
770 | osg::ref_ptr<MouseCoordsTool> _mouseCoordsTool; |
---|
771 | osg::ref_ptr<MouseCoordsCallback> _coordsCallback; |
---|
772 | osg::ref_ptr<osgEarth::Util::Controls::HBox> _debugBox; |
---|
773 | osg::ref_ptr<osgEarth::Util::Controls::LabelControl> _debugLabel; |
---|
774 | osg::ref_ptr<osgEarth::Util::Controls::HBox> _copyrightScaleBox; |
---|
775 | osg::ref_ptr<osgEarth::Util::Controls::LabelControl> _copyrightLabel; |
---|
776 | osg::ref_ptr<osgEarth::Util::Controls::LabelControl> _scaleLabel; |
---|
777 | osg::ref_ptr<osgEarth::Util::Controls::Frame> _scaleBar; |
---|
778 | ScaleBarUnits _scaleBarUnits; |
---|
779 | osg::ref_ptr<osgEarth::Util::EarthManipulator> _manipulator; |
---|
780 | osg::ref_ptr<osgGA::StateSetManipulator> _stateManip; |
---|
781 | osg::ref_ptr<osgEarth::Util::VerticalScale> _verticalScale; |
---|
782 | ViewpointHashmap _viewpoints; |
---|
783 | }; |
---|
784 | |
---|
785 | class MapNodeCallback : public osg::NodeCallback |
---|
786 | { |
---|
787 | public: |
---|
788 | MapNodeCallback(Renderer *renderer) : |
---|
789 | _renderer(renderer) |
---|
790 | {} |
---|
791 | |
---|
792 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
793 | { |
---|
794 | _renderer->mapNodeUpdate(); |
---|
795 | traverse(node, nv); |
---|
796 | } |
---|
797 | private: |
---|
798 | Renderer *_renderer; |
---|
799 | }; |
---|
800 | |
---|
801 | class SelectPlaceNodesVisitor : public osg::NodeVisitor |
---|
802 | { |
---|
803 | public: |
---|
804 | SelectPlaceNodesVisitor(Renderer *renderer, |
---|
805 | double latMin, double latMax, |
---|
806 | double longMin, double longMax) : |
---|
807 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
---|
808 | _renderer(renderer), |
---|
809 | _latMin(latMin), _latMax(latMax), |
---|
810 | _longMin(longMin), _longMax(longMax) |
---|
811 | {} |
---|
812 | |
---|
813 | virtual void apply(osg::Node& node); |
---|
814 | |
---|
815 | private: |
---|
816 | Renderer *_renderer; |
---|
817 | double _latMin, _latMax, _longMin, _longMax; |
---|
818 | }; |
---|
819 | |
---|
820 | } |
---|
821 | |
---|
822 | #endif |
---|