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