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