source: trunk/packages/vizservers/geovis/Renderer.h @ 4319

Last change on this file since 4319 was 4319, checked in by ldelgass, 11 years ago

Add protocol to delete named viewpoint

File size: 10.6 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2004-2013  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 <string>
12#include <vector>
13#include <tr1/unordered_map>
14#include <typeinfo>
15
16#include <osg/ref_ptr>
17#include <osg/Node>
18#include <osg/Image>
19#include <osg/TransferFunction>
20#include <osgViewer/Viewer>
21#include <osgGA/StateSetManipulator>
22
23#include <osgEarth/StringUtils>
24#include <osgEarth/Map>
25#include <osgEarth/Viewpoint>
26#include <osgEarth/ImageLayer>
27#include <osgEarth/ElevationLayer>
28#include <osgEarth/ModelLayer>
29#include <osgEarth/TileSource>
30#include <osgEarth/ModelSource>
31#include <osgEarth/GeoData>
32#include <osgEarthUtil/EarthManipulator>
33#include <osgEarthUtil/MouseCoordsTool>
34#include <osgEarthUtil/Controls>
35#include <osgEarthUtil/Formatter>
36#include <osgEarthUtil/AutoClipPlaneHandler>
37#include <osgEarthUtil/VerticalScale>
38
39#include "Types.h"
40#include "Trace.h"
41
42// Controls if TGA format is sent to client
43//#define RENDER_TARGA
44#define TARGA_BYTES_PER_PIXEL 3
45
46namespace GeoVis {
47
48class ScreenCaptureCallback : public osg::Camera::DrawCallback
49{
50public:
51    ScreenCaptureCallback() :
52        osg::Camera::DrawCallback()
53    {
54        _image = new osg::Image;
55    }
56
57    virtual void operator()(osg::RenderInfo &renderInfo) const
58    {
59        TRACE("Enter ScreenCaptureCallback");
60        int width, height;
61        if (renderInfo.getCurrentCamera() == NULL) {
62            ERROR("No camera");
63            return;
64        }
65        if (renderInfo.getCurrentCamera()->getViewport() == NULL) {
66            ERROR("No viewport");
67            return;
68        }
69        width = (int)renderInfo.getCurrentCamera()->getViewport()->width();
70        height = (int)renderInfo.getCurrentCamera()->getViewport()->height();
71        TRACE("readPixels: %d x %d", width, height);
72#ifdef RENDER_TARGA
73        _image->readPixels(0, 0, width, height,
74                           GL_BGR, GL_UNSIGNED_BYTE);
75#else
76        _image->readPixels(0, 0, width, height,
77                           GL_RGB, GL_UNSIGNED_BYTE);
78#endif
79    }
80
81    osg::Image *getImage()
82    {
83        return _image.get();
84    }
85
86private:
87    osg::ref_ptr<osg::Image> _image;
88};
89
90class MouseCoordsCallback : public osgEarth::Util::MouseCoordsTool::Callback
91{
92public:
93    MouseCoordsCallback(osgEarth::Util::Controls::LabelControl *label,
94                        osgEarth::Util::Formatter *formatter) :
95        osgEarth::Util::MouseCoordsTool::Callback(),
96        _label(label),
97        _formatter(formatter),
98        _havePoint(false)
99    {
100    }
101
102    void set(const osgEarth::GeoPoint& p, osg::View *view, osgEarth::MapNode *mapNode)
103    {
104        TRACE("%g %g %g", p.x(), p.y(), p.z());
105        if (_label.valid()) {
106            _label->setText(osgEarth::Stringify()
107                            << "Lat/Long: "
108                            <<  _formatter->format(p));
109                            //<< ", " << p.z());
110        }
111        _pt = p;
112        _havePoint = true;
113    }
114
115    void reset(osg::View *view, osgEarth::MapNode *mapNode)
116    {
117        TRACE("Out of range");
118        // Out of range of map extents
119        if (_label.valid()) {
120            _label->setText("");
121        }
122        _havePoint = false;
123    }
124
125    bool report(double *x, double *y, double *z)
126    {
127        if (_havePoint) {
128            *x = _pt.x();
129            *y = _pt.y();
130            *z = _pt.z();
131            _havePoint = false;
132            return true;
133        }
134        return false;
135    }
136
137private:
138    osg::observer_ptr<osgEarth::Util::Controls::LabelControl> _label;
139    osg::ref_ptr<osgEarth::Util::Formatter> _formatter;
140    bool _havePoint;
141    osgEarth::GeoPoint _pt;
142};
143
144/**
145 * \brief GIS Renderer
146 */
147class Renderer
148{
149public:
150    typedef std::string ColorMapId;
151    typedef std::string ViewpointId;
152
153    enum GraticuleType {
154        GRATICULE_UTM,
155        GRATICULE_MGRS,
156        GRATICULE_GEODETIC
157    };
158
159    Renderer();
160    virtual ~Renderer();
161
162    // Colormaps
163
164    void addColorMap(const ColorMapId& id, osg::TransferFunction1D *xfer);
165
166    void deleteColorMap(const ColorMapId& id);
167
168    void setColorMapNumberOfTableEntries(const ColorMapId& id, int numEntries);
169
170    // Scene
171
172    void loadEarthFile(const char *path);
173
174    void resetMap(osgEarth::MapOptions::CoordinateSystemType type,
175                  const char *profile = NULL,
176                  double bounds[4] = NULL);
177
178    void clearMap();
179
180    // Map options
181
182    void setGraticule(bool enable, GraticuleType type = GRATICULE_GEODETIC);
183
184    void setViewerLightType(osg::View::LightingMode mode);
185
186    void setLighting(bool state);
187
188    void setTerrainLighting(bool state);
189
190    void setTerrainVerticalScale(double scale);
191
192    void setTerrainWireframe(bool state);
193
194    // Image raster layers
195
196    int getNumImageLayers() const
197    {
198        return (_map.valid() ? _map->getNumImageLayers() : 0);
199    }
200
201    void getImageLayerNames(std::vector<std::string>& names)
202    {
203        if (_map.valid()) {
204            osgEarth::ImageLayerVector layerVector;
205            _map->getImageLayers(layerVector);
206            osgEarth::ImageLayerVector::const_iterator itr;
207            for (itr = layerVector.begin(); itr != layerVector.end(); ++itr) {
208                //osgEarth::UID uid = (*itr)->getUID();
209                names.push_back((*itr)->getName());
210            }
211        }
212    }
213
214    void addImageLayer(const char *name, osgEarth::TileSourceOptions& opts,
215                       bool makeShared = false, bool visible = true);
216
217    void removeImageLayer(const char *name);
218
219    void moveImageLayer(const char *name, unsigned int pos);
220
221    void setImageLayerOpacity(const char *name, double opacity);
222
223    void setImageLayerVisibility(const char *name, bool state);
224
225    void addColorFilter(const char *name, const char *shader);
226
227    void removeColorFilter(const char *name, int idx = -1);
228
229    // Elevation raster layers
230
231    int getNumElevationLayers() const
232    {
233        return (_map.valid() ? _map->getNumElevationLayers() : 0);
234    }
235
236    void getElevationLayerNames(std::vector<std::string>& names)
237    {
238        if (_map.valid()) {
239            osgEarth::ElevationLayerVector layerVector;
240            _map->getElevationLayers(layerVector);
241            osgEarth::ElevationLayerVector::const_iterator itr;
242            for (itr = layerVector.begin(); itr != layerVector.end(); ++itr) {
243                //osgEarth::UID uid = (*itr)->getUID();
244                names.push_back((*itr)->getName());
245            }
246        }
247    }
248
249    void addElevationLayer(const char *name, osgEarth::TileSourceOptions& opts);
250
251    void removeElevationLayer(const char *name);
252
253    void moveElevationLayer(const char *name, unsigned int pos);
254
255    void setElevationLayerVisibility(const char *name, bool state);
256
257    // Model layers
258
259    int getNumModelLayers() const
260    {
261        return (_map.valid() ? _map->getNumModelLayers() : 0);
262    }
263
264    void getModelLayerNames(std::vector<std::string>& names)
265    {
266        if (_map.valid()) {
267            osgEarth::ModelLayerVector layerVector;
268            _map->getModelLayers(layerVector);
269            osgEarth::ModelLayerVector::const_iterator itr;
270            for (itr = layerVector.begin(); itr != layerVector.end(); ++itr) {
271                //osgEarth::UID uid = (*itr)->getUID();
272                names.push_back((*itr)->getName());
273            }
274        }
275    }
276
277    void addModelLayer(const char *name, osgEarth::ModelSourceOptions& opts);
278
279    void removeModelLayer(const char *name);
280
281    void moveModelLayer(const char *name, unsigned int pos);
282
283    void setModelLayerOpacity(const char *name, double opacity);
284
285    void setModelLayerVisibility(const char *name, bool state);
286
287    // Render window
288
289    void setWindowSize(int width, int height);
290
291    int getWindowWidth() const
292    {
293        return _windowWidth;
294    }
295
296    int getWindowHeight() const
297    {
298        return _windowHeight;
299    }
300
301    // Camera
302
303    void saveNamedViewpoint(const char *name);
304
305    bool restoreNamedViewpoint(const char *name, double durationSecs);
306
307    bool removeNamedViewpoint(const char *name);
308
309    osgEarth::Viewpoint getViewpoint();
310
311    void setViewpoint(const osgEarth::Viewpoint& v, double durationSecs = 0.0);
312
313    void resetCamera(bool resetOrientation = true);
314
315    void setCameraOrientation(const double quat[4], bool absolute = true);
316
317    void panCamera(double x, double y);
318
319    void rotateCamera(double x, double y);
320
321    void zoomCamera(double z);
322
323    void setCameraDistance(double dist);
324
325    // Keyboard events
326
327    void keyPress(int key);
328
329    void keyRelease(int key);
330
331    // Mouse events
332
333    void setThrowingEnabled(bool state);
334
335    void mouseClick(int button, double x, double y);
336
337    void mouseDoubleClick(int button, double x, double y);
338
339    void mouseDrag(int button, double x, double y);
340
341    void mouseRelease(int button, double x, double y);
342
343    void mouseMotion(double x, double y);
344
345    void mouseScroll(int direction);
346
347    // Rendering an image
348
349    void setBackgroundColor(float color[3]);
350
351    void eventuallyRender();
352
353    bool render();
354
355    osg::Image *getRenderedFrame();
356
357    bool mapMouseCoords(float mouseX, float mouseY, osgEarth::GeoPoint &pt);
358
359    bool getMousePoint(double *x, double *y, double *z)
360    {
361        return (_coordsCallback.valid() && _coordsCallback->report(x, y, z));
362    }
363
364    long getTimeout();
365
366private:
367    typedef std::tr1::unordered_map<ColorMapId, osg::ref_ptr<osg::TransferFunction1D> > ColorMapHashmap;
368    typedef std::tr1::unordered_map<ViewpointId, osgEarth::Viewpoint> ViewpointHashmap;
369
370    void initViewer();
371
372    void finalizeViewer();
373
374    void initEarthManipulator();
375
376    void initMouseCoordsTool();
377
378    void initColorMaps();
379
380    void initCamera();
381
382    bool isPagerIdle();
383
384    bool checkNeedToDoFrame();
385
386    osgGA::EventQueue *getEventQueue();
387
388    bool _needsRedraw;
389    int _windowWidth, _windowHeight;
390    float _bgColor[3];
391
392    double _minFrameTime;
393    double _lastFrameTime;
394
395    ColorMapHashmap _colorMaps;
396
397    std::string _baseURI;
398
399    osg::ref_ptr<osg::Group> _sceneRoot;
400    osg::ref_ptr<osg::Group> _graticule;
401    osg::ref_ptr<osgEarth::MapNode> _mapNode;
402    osg::ref_ptr<osgEarth::Map> _map;
403    osg::ref_ptr<osgViewer::Viewer> _viewer;
404    osg::ref_ptr<ScreenCaptureCallback> _captureCallback;
405    osg::ref_ptr<osgEarth::Util::AutoClipPlaneCullCallback> _clipPlaneCullCallback;
406    osg::ref_ptr<osgEarth::Util::MouseCoordsTool> _mouseCoordsTool;
407    osg::ref_ptr<MouseCoordsCallback> _coordsCallback;
408    osg::ref_ptr<osgEarth::Util::EarthManipulator> _manipulator;
409    osg::ref_ptr<osgGA::StateSetManipulator> _stateManip;
410    osg::ref_ptr<osgEarth::Util::VerticalScale> _verticalScale;
411    ViewpointHashmap _viewpoints;
412};
413
414}
415
416#endif
Note: See TracBrowser for help on using the repository browser.