1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2015 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | #include <vector> |
---|
8 | #include <cstdio> |
---|
9 | #include <cassert> |
---|
10 | |
---|
11 | #include <osgEarth/Registry> |
---|
12 | #include <osgEarth/ShaderGenerator> |
---|
13 | #include <osgEarth/ObjectIndex> |
---|
14 | #include <osgEarthFeatures/Feature> |
---|
15 | #include <osgEarthFeatures/FeatureIndex> |
---|
16 | #include <osgEarthFeatures/FeatureSourceIndexNode> |
---|
17 | #include <osgEarthAnnotation/AnnotationNode> |
---|
18 | #include <osgEarthAnnotation/LabelNode> |
---|
19 | #include <osgEarthSymbology/Color> |
---|
20 | #include <osgEarthSymbology/Style> |
---|
21 | #include <osgEarthSymbology/TextSymbol> |
---|
22 | |
---|
23 | #include "Picker.h" |
---|
24 | #include "Placard.h" |
---|
25 | #include "Renderer.h" |
---|
26 | #include "RendererCmd.h" |
---|
27 | #include "Trace.h" |
---|
28 | |
---|
29 | using namespace GeoVis; |
---|
30 | |
---|
31 | static const char *highlightVert = |
---|
32 | "#version 130\n" |
---|
33 | "uniform uint objectid_to_highlight; \n" |
---|
34 | "uint oe_index_objectid; // Stage global containing object id \n" |
---|
35 | "flat out int selected; \n" |
---|
36 | "void checkForHighlight(inout vec4 vertex) \n" |
---|
37 | "{ \n" |
---|
38 | " selected = (objectid_to_highlight > 1u && objectid_to_highlight == oe_index_objectid) ? 1 : 0; \n" |
---|
39 | "} \n"; |
---|
40 | |
---|
41 | static const char *highlightFrag = |
---|
42 | "#version 130\n" |
---|
43 | "flat in int selected; \n" |
---|
44 | "void highlightFragment(inout vec4 color) \n" |
---|
45 | "{ \n" |
---|
46 | " if ( selected == 1 ) \n" |
---|
47 | " color.rgb = mix(color.rgb, clamp(vec3(0.5,0.5,2.0)*(1.0-color.rgb), 0.0, 1.0), 0.5); \n" |
---|
48 | "} \n"; |
---|
49 | |
---|
50 | static osg::Uniform *s_highlightUniform = NULL; |
---|
51 | |
---|
52 | static void installHighlighter(osg::StateSet *stateSet, int attrLocation) |
---|
53 | { |
---|
54 | TRACE("GLSL Version: %s", GLSL_VERSION_STR); |
---|
55 | // This shader program will highlight the selected object. |
---|
56 | osgEarth::VirtualProgram *vp = osgEarth::VirtualProgram::getOrCreate(stateSet); |
---|
57 | vp->setFunction("checkForHighlight", highlightVert, osgEarth::ShaderComp::LOCATION_VERTEX_CLIP); |
---|
58 | vp->setFunction("highlightFragment", highlightFrag, osgEarth::ShaderComp::LOCATION_FRAGMENT_COLORING); |
---|
59 | |
---|
60 | // Since we're accessing object IDs, we need to load the indexing shader as well: |
---|
61 | osgEarth::Registry::objectIndex()->loadShaders(vp); |
---|
62 | |
---|
63 | // A uniform that will tell the shader which object to highlight: |
---|
64 | if (s_highlightUniform == NULL) { |
---|
65 | s_highlightUniform = new osg::Uniform("objectid_to_highlight", 0u); |
---|
66 | } |
---|
67 | stateSet->addUniform(s_highlightUniform); |
---|
68 | } |
---|
69 | |
---|
70 | void GeoVis::setHighlightByObjectID(osgEarth::ObjectID id) |
---|
71 | { |
---|
72 | if (s_highlightUniform != NULL) { |
---|
73 | s_highlightUniform->set(id); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void GeoVis::clearHighlight() |
---|
78 | { |
---|
79 | if (s_highlightUniform != NULL) { |
---|
80 | s_highlightUniform->set(0U); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | HoverCallback::HoverCallback(Renderer *renderer) : |
---|
85 | _renderer(renderer) |
---|
86 | { |
---|
87 | installHighlighter(renderer->getMapNode()->getOrCreateStateSet(), |
---|
88 | osgEarth::Registry::objectIndex()->getObjectIDAttribLocation()); |
---|
89 | } |
---|
90 | |
---|
91 | void HoverCallback::onHit(osgEarth::ObjectID id) |
---|
92 | { |
---|
93 | // First see whether it's a feature: |
---|
94 | osgEarth::Features::FeatureIndex *index = osgEarth::Registry::objectIndex()->get<osgEarth::Features::FeatureIndex>(id); |
---|
95 | osgEarth::Features::Feature *feature = index ? index->getFeature(id) : 0L; |
---|
96 | TRACE("Hover hit"); |
---|
97 | if (feature) { |
---|
98 | TRACE("Hit feature ID: %lu (of %d), oid: %lu", feature->getFID(), index->size(), id); |
---|
99 | const osgEarth::Features::AttributeTable &attrs = feature->getAttrs(); |
---|
100 | for (osgEarth::Features::AttributeTable::const_iterator itr = attrs.begin(); itr != attrs.end(); ++itr) { |
---|
101 | TRACE(" attr: %s", itr->first.c_str()); |
---|
102 | switch (itr->second.first) { |
---|
103 | case osgEarth::Features::ATTRTYPE_STRING: |
---|
104 | TRACE(" value: %s", itr->second.getString().c_str()); |
---|
105 | break; |
---|
106 | case osgEarth::Features::ATTRTYPE_INT: |
---|
107 | TRACE(" value: %d", itr->second.getInt()); |
---|
108 | break; |
---|
109 | case osgEarth::Features::ATTRTYPE_DOUBLE: |
---|
110 | TRACE(" value: %g", itr->second.getDouble()); |
---|
111 | break; |
---|
112 | case osgEarth::Features::ATTRTYPE_BOOL: |
---|
113 | TRACE(" value: %s", itr->second.getBool() ? "true" : "false"); |
---|
114 | break; |
---|
115 | default: |
---|
116 | TRACE(" value: unknown type"); |
---|
117 | } |
---|
118 | } |
---|
119 | //TRACE("Feature name: %s", feature->getString("name").c_str()); |
---|
120 | } else { |
---|
121 | osgEarth::Annotation::AnnotationNode* anno = |
---|
122 | osgEarth::Registry::objectIndex()->get<osgEarth::Annotation::AnnotationNode>(id); |
---|
123 | |
---|
124 | std::set<osgEarth::Annotation::AnnotationNode*> toUnHover; |
---|
125 | std::set<osgEarth::Annotation::AnnotationNode*>& hovered = _renderer->getHovered(); |
---|
126 | for (std::set<osgEarth::Annotation::AnnotationNode*>::iterator itr = hovered.begin(); |
---|
127 | itr != hovered.end(); ++itr) { |
---|
128 | toUnHover.insert(*itr); |
---|
129 | } |
---|
130 | if (anno != NULL) { |
---|
131 | TRACE("Hit AnnotationNode: %p", anno); |
---|
132 | #if 0 |
---|
133 | if (anno->getDecoration().empty() && |
---|
134 | hovered.find(anno) == hovered.end()) { |
---|
135 | hovered.insert(anno); |
---|
136 | anno->setDecoration("hover"); |
---|
137 | _renderer->eventuallyRender(); |
---|
138 | } |
---|
139 | toUnHover.erase(anno); |
---|
140 | #endif |
---|
141 | } |
---|
142 | #if 0 |
---|
143 | for (std::set<osgEarth::Annotation::AnnotationNode *>::iterator itr = toUnHover.begin(); |
---|
144 | itr != toUnHover.end(); ++itr) { |
---|
145 | hovered.erase(*itr); |
---|
146 | (*itr)->clearDecoration(); |
---|
147 | _renderer->eventuallyRender(); |
---|
148 | } |
---|
149 | #endif |
---|
150 | } |
---|
151 | setHighlightByObjectID(id); |
---|
152 | } |
---|
153 | |
---|
154 | void HoverCallback::onMiss() |
---|
155 | { |
---|
156 | TRACE("Hover miss"); |
---|
157 | std::set<osgEarth::Annotation::AnnotationNode*> toUnHover; |
---|
158 | std::set<osgEarth::Annotation::AnnotationNode*>& hovered = _renderer->getHovered(); |
---|
159 | for (std::set<osgEarth::Annotation::AnnotationNode*>::iterator itr = hovered.begin(); |
---|
160 | itr != hovered.end(); ++itr) { |
---|
161 | toUnHover.insert(*itr); |
---|
162 | } |
---|
163 | for (std::set<osgEarth::Annotation::AnnotationNode *>::iterator itr = toUnHover.begin(); |
---|
164 | itr != toUnHover.end(); ++itr) { |
---|
165 | hovered.erase(*itr); |
---|
166 | #ifndef NEW_ANNOTATION_API |
---|
167 | (*itr)->clearDecoration(); |
---|
168 | #endif |
---|
169 | _renderer->eventuallyRender(); |
---|
170 | } |
---|
171 | clearHighlight(); |
---|
172 | } |
---|
173 | |
---|
174 | bool HoverCallback::accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa) |
---|
175 | { |
---|
176 | // Pick whenever mouse moves |
---|
177 | return ea.getEventType() == ea.MOVE; |
---|
178 | } |
---|
179 | |
---|
180 | SelectCallback::SelectCallback(Renderer *renderer) : |
---|
181 | _renderer(renderer) |
---|
182 | { |
---|
183 | installHighlighter(renderer->getMapNode()->getOrCreateStateSet(), |
---|
184 | osgEarth::Registry::objectIndex()->getObjectIDAttribLocation()); |
---|
185 | installHighlighter(renderer->getPlaceNodes()->getOrCreateStateSet(), |
---|
186 | osgEarth::Registry::objectIndex()->getObjectIDAttribLocation()); |
---|
187 | } |
---|
188 | |
---|
189 | void SelectCallback::onHit(osgEarth::ObjectID id) |
---|
190 | { |
---|
191 | // First see whether it's a feature: |
---|
192 | osgEarth::Features::FeatureIndex *index = osgEarth::Registry::objectIndex()->get<osgEarth::Features::FeatureIndex>(id); |
---|
193 | osgEarth::Features::Feature *feature = index ? index->getFeature(id) : 0L; |
---|
194 | TRACE("Select hit oid: %lu at %g,%g", id, _pickPoint.x(), _pickPoint.y()); |
---|
195 | if (feature) { |
---|
196 | osgEarth::Features::FeatureSourceIndex *fsi = dynamic_cast<osgEarth::Features::FeatureSourceIndex *>(index); |
---|
197 | std::string layerName; |
---|
198 | if (fsi) { |
---|
199 | osgEarth::Features::FeatureSource *source = fsi->getFeatureSource(); |
---|
200 | if (source) { |
---|
201 | layerName = source->getFeatureSourceOptions().name().get(); |
---|
202 | } |
---|
203 | } |
---|
204 | TRACE("Hit feature ID: %lu (of %d) layer: %s", feature->getFID(), index->size(), layerName.c_str()); |
---|
205 | |
---|
206 | _renderer->clearSelection(); |
---|
207 | _renderer->addPlacard(_pickPoint, feature, layerName.c_str()); |
---|
208 | |
---|
209 | char mesg[256]; |
---|
210 | snprintf(mesg, sizeof(mesg), "nv>select feature %u %lu %d {%s}\n", id, feature->getFID(), index->size(), layerName.c_str()); |
---|
211 | size_t length = strlen(mesg); |
---|
212 | queueResponse(mesg, length, Response::VOLATILE, Response::DATA); |
---|
213 | } else { |
---|
214 | _renderer->clearSelection(); |
---|
215 | osgEarth::Annotation::AnnotationNode* anno = |
---|
216 | osgEarth::Registry::objectIndex()->get<osgEarth::Annotation::AnnotationNode>(id); |
---|
217 | if (anno != NULL) { |
---|
218 | TRACE("Hit AnnotationNode: %p, \"%s\"", anno, anno->getName().c_str()); |
---|
219 | char mesg[256]; |
---|
220 | snprintf(mesg, sizeof(mesg), "nv>select annotation %u {%s}\n", id, anno->getName().c_str()); |
---|
221 | size_t length = strlen(mesg); |
---|
222 | queueResponse(mesg, length, Response::VOLATILE, Response::DATA); |
---|
223 | } |
---|
224 | } |
---|
225 | setHighlightByObjectID(id); |
---|
226 | _renderer->pickPending(false); |
---|
227 | _renderer->eventuallyRender(); |
---|
228 | } |
---|
229 | |
---|
230 | void SelectCallback::onMiss() |
---|
231 | { |
---|
232 | TRACE("Select miss"); |
---|
233 | _renderer->clearSelection(); |
---|
234 | |
---|
235 | static const char *mesg = "nv>select clear\n"; |
---|
236 | static const size_t length = strlen(mesg); |
---|
237 | queueResponse(mesg, length, Response::STATIC, Response::DATA); |
---|
238 | |
---|
239 | _renderer->pickPending(false); |
---|
240 | _renderer->eventuallyRender(); |
---|
241 | } |
---|
242 | |
---|
243 | bool SelectCallback::accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa) |
---|
244 | { |
---|
245 | if (ea.getEventType() == ea.PUSH) { |
---|
246 | _x = ea.getX(); |
---|
247 | _y = ea.getY(); |
---|
248 | return false; |
---|
249 | } else if (ea.getEventType() == ea.RELEASE) { |
---|
250 | // Don't pick if mouse was dragged more than 2 pixels |
---|
251 | if (fabsf(ea.getX()-_x) > 2 || fabsf(ea.getY()-_y) > 2) { |
---|
252 | return false; |
---|
253 | } else { |
---|
254 | _renderer->mapMouseCoords(ea.getX(), ea.getY(), _pickPoint, false); |
---|
255 | _renderer->pickPending(true); |
---|
256 | TRACE("Pending pick at %g %g", ea.getX(), ea.getY()); |
---|
257 | return true; |
---|
258 | } |
---|
259 | } |
---|
260 | return false; |
---|
261 | } |
---|
262 | |
---|
263 | void DeleteCallback::onHit(osgEarth::ObjectID id) |
---|
264 | { |
---|
265 | // First see whether it's a feature: |
---|
266 | osgEarth::Features::FeatureIndex *index = osgEarth::Registry::objectIndex()->get<osgEarth::Features::FeatureIndex>(id); |
---|
267 | osgEarth::Features::Feature *feature = index ? index->getFeature(id) : 0L; |
---|
268 | |
---|
269 | if (feature) { |
---|
270 | TRACE("Hit feature ID: %lu, oid: %lu name: %s", feature->getFID(), id); |
---|
271 | TRACE("Feature name: %s", feature->getString("name").c_str()); |
---|
272 | } else { |
---|
273 | TRACE("Picker hit!"); |
---|
274 | osgEarth::Annotation::AnnotationNode* anno = |
---|
275 | osgEarth::Registry::objectIndex()->get<osgEarth::Annotation::AnnotationNode>(id); |
---|
276 | std::set<osgEarth::Annotation::AnnotationNode*>& hovered = _renderer->getHovered(); |
---|
277 | #ifndef NEW_ANNOTATION_API |
---|
278 | anno->clearDecoration(); |
---|
279 | #endif |
---|
280 | _renderer->getPlaceNodes()->removeChild(anno); |
---|
281 | if (hovered.find(anno) != hovered.end()) { |
---|
282 | hovered.erase(anno); |
---|
283 | } |
---|
284 | } |
---|
285 | } |
---|