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

Last change on this file since 4282 was 4282, checked in by ldelgass, 10 years ago

Add some tests for NULL pointers, fix argument indices in map terain linecolor
(still not implemented)

File size: 7.7 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/Map>
24#include <osgEarth/Viewpoint>
25#include <osgEarth/ImageLayer>
26#include <osgEarth/ElevationLayer>
27#include <osgEarth/ModelLayer>
28#include <osgEarth/TileSource>
29#include <osgEarth/ModelSource>
30#include <osgEarth/GeoData>
31#include <osgEarthUtil/EarthManipulator>
32#include <osgEarthUtil/MouseCoordsTool>
33#include <osgEarthUtil/AutoClipPlaneHandler>
34
35#include "Types.h"
36#include "Trace.h"
37
38// Controls if TGA format is sent to client
39//#define RENDER_TARGA
40#define TARGA_BYTES_PER_PIXEL 3
41
42namespace GeoVis {
43
44class ScreenCaptureCallback : public osg::Camera::DrawCallback
45{
46public:
47    ScreenCaptureCallback() :
48        osg::Camera::DrawCallback()
49    {
50        _image = new osg::Image;
51    }
52
53    virtual void operator()(osg::RenderInfo &renderInfo) const
54    {
55        TRACE("Enter ScreenCaptureCallback");
56        int width, height;
57        if (renderInfo.getCurrentCamera() == NULL) {
58            ERROR("No camera");
59            return;
60        }
61        if (renderInfo.getCurrentCamera()->getViewport() == NULL) {
62            ERROR("No viewport");
63            return;
64        }
65        width = (int)renderInfo.getCurrentCamera()->getViewport()->width();
66        height = (int)renderInfo.getCurrentCamera()->getViewport()->height();
67        TRACE("readPixels: %d x %d", width, height);
68#ifdef RENDER_TARGA
69        _image->readPixels(0, 0, width, height,
70                           GL_BGR, GL_UNSIGNED_BYTE);
71#else
72        _image->readPixels(0, 0, width, height,
73                           GL_RGB, GL_UNSIGNED_BYTE);
74#endif
75    }
76
77    osg::Image *getImage()
78    {
79        return _image.get();
80    }
81
82private:
83    osg::ref_ptr<osg::Image> _image;
84};
85
86class MouseCoordsCallback : public osgEarth::Util::MouseCoordsTool::Callback
87{
88public:
89    MouseCoordsCallback() :
90        osgEarth::Util::MouseCoordsTool::Callback(),
91        _havePoint(false)
92    {}
93
94    void set(const osgEarth::GeoPoint& p, osg::View *view, osgEarth::MapNode *mapNode)
95    {
96        TRACE("%g %g %g", p.x(), p.y(), p.z());
97        _pt = p;
98        _havePoint = true;
99    }
100
101    void reset(osg::View *view, osgEarth::MapNode *mapNode)
102    {
103        TRACE("Out of range");
104        // Out of range click
105        _havePoint = false;
106    }
107
108    bool report(double *x, double *y, double *z)
109    {
110        if (_havePoint) {
111            *x = _pt.x();
112            *y = _pt.y();
113            *z = _pt.z();
114            _havePoint = false;
115            return true;
116        }
117        return false;
118    }
119
120private:
121    bool _havePoint;
122    osgEarth::GeoPoint _pt;
123};
124
125/**
126 * \brief GIS Renderer
127 */
128class Renderer
129{
130public:
131    typedef std::string ColorMapId;
132
133    Renderer();
134    virtual ~Renderer();
135
136    // Colormaps
137
138    void addColorMap(const ColorMapId& id, osg::TransferFunction1D *xfer);
139
140    void deleteColorMap(const ColorMapId& id);
141
142    void setColorMapNumberOfTableEntries(const ColorMapId& id, int numEntries);
143
144    // Scene
145
146    void loadEarthFile(const char *path);
147
148    void resetMap(osgEarth::MapOptions::CoordinateSystemType type,
149                  const char *profile = NULL,
150                  double bounds[4] = NULL);
151
152    void clearMap();
153
154    // Map options
155
156    void setLighting(bool state);
157
158    void setTerrainLighting(bool state);
159
160    void setTerrainVerticalScale(double scale);
161
162    void setTerrainWireframe(bool state);
163
164    // Image raster layers
165
166    int getNumImageLayers() const
167    {
168        return (_map.valid() ? _map->getNumImageLayers() : 0);
169    }
170
171    void addImageLayer(const char *name, const osgEarth::TileSourceOptions& opts,
172                       bool makeShared = false, bool visible = true);
173
174    void removeImageLayer(const char *name);
175
176    void moveImageLayer(const char *name, unsigned int pos);
177
178    void setImageLayerOpacity(const char *name, double opacity);
179
180    void setImageLayerVisibility(const char *name, bool state);
181
182    void addColorFilter(const char *name, const char *shader);
183
184    void removeColorFilter(const char *name, int idx = -1);
185
186    // Elevation raster layers
187
188    int getNumElevationLayers() const
189    {
190        return (_map.valid() ? _map->getNumElevationLayers() : 0);
191    }
192
193    void addElevationLayer(const char *name, const osgEarth::TileSourceOptions& opts);
194
195    void removeElevationLayer(const char *name);
196
197    void moveElevationLayer(const char *name, unsigned int pos);
198
199    void setElevationLayerVisibility(const char *name, bool state);
200
201    // Model layers
202
203    int getNumModelLayers() const
204    {
205        return (_map.valid() ? _map->getNumModelLayers() : 0);
206    }
207
208    void addModelLayer(const char *name, const osgEarth::ModelSourceOptions& opts);
209
210    void removeModelLayer(const char *name);
211
212    void moveModelLayer(const char *name, unsigned int pos);
213
214    void setModelLayerOpacity(const char *name, double opacity);
215
216    void setModelLayerVisibility(const char *name, bool state);
217
218    // Render window
219
220    void setWindowSize(int width, int height);
221
222    int getWindowWidth() const
223    {
224        return _windowWidth;
225    }
226
227    int getWindowHeight() const
228    {
229        return _windowHeight;
230    }
231
232    // Camera
233
234    osgEarth::Viewpoint getViewpoint();
235
236    void setViewpoint(const osgEarth::Viewpoint& v, double durationSecs = 0.0);
237
238    void resetCamera(bool resetOrientation = true);
239
240    void setCameraOrientation(const double quat[4], bool absolute = true);
241
242    void panCamera(double x, double y, bool absolute = false);
243
244    void rotateCamera(double x, double y, bool absolute = false);
245
246    void zoomCamera(double z, bool absolute = false);
247
248    // Keyboard events
249
250    void keyPress(int key);
251
252    void keyRelease(int key);
253
254    // Mouse events
255
256    void setThrowingEnabled(bool state);
257
258    void mouseClick(int button, double x, double y);
259
260    void mouseDoubleClick(int button, double x, double y);
261
262    void mouseDrag(int button, double x, double y);
263
264    void mouseRelease(int button, double x, double y);
265
266    void mouseMotion(double x, double y);
267
268    void mouseScroll(int direction);
269
270    // Rendering an image
271
272    void setBackgroundColor(float color[3]);
273
274    void eventuallyRender();
275
276    bool render();
277
278    osg::Image *getRenderedFrame();
279
280    bool mapMouseCoords(float mouseX, float mouseY, osgEarth::GeoPoint &pt);
281
282    bool getMousePoint(double *x, double *y, double *z)
283    {
284        return (_coordsCallback.valid() && _coordsCallback->report(x, y, z));
285    }
286
287    long getTimeout();
288
289private:
290    typedef std::tr1::unordered_map<ColorMapId, osg::ref_ptr<osg::TransferFunction1D> > ColorMapHashmap;
291
292    void initColorMaps();
293
294    void initCamera();
295
296    bool isPagerIdle();
297
298    bool checkNeedToDoFrame();
299
300    osgGA::EventQueue *getEventQueue();
301
302    bool _needsRedraw;
303    int _windowWidth, _windowHeight;
304    float _bgColor[3];
305
306    double _minFrameTime;
307    double _lastFrameTime;
308
309    ColorMapHashmap _colorMaps;
310
311    osg::ref_ptr<osg::Node> _sceneRoot;
312    osg::ref_ptr<osgEarth::MapNode> _mapNode;
313    osg::ref_ptr<osgEarth::Map> _map;
314    osg::ref_ptr<osgViewer::Viewer> _viewer;
315    osg::ref_ptr<ScreenCaptureCallback> _captureCallback;
316    osg::ref_ptr<osgEarth::Util::AutoClipPlaneCullCallback> _clipPlaneCullCallback;
317    osg::ref_ptr<osgEarth::Util::MouseCoordsTool> _mouseCoordsTool;
318    osg::ref_ptr<MouseCoordsCallback> _coordsCallback;
319    osg::ref_ptr<osgEarth::Util::EarthManipulator> _manipulator;
320    osg::ref_ptr<osgGA::StateSetManipulator> _stateManip;
321};
322
323}
324
325#endif
Note: See TracBrowser for help on using the repository browser.