source: geovis/trunk/Picker.cpp @ 6116

Last change on this file since 6116 was 6116, checked in by ldelgass, 8 years ago

minor build fix

  • Property svn:eol-style set to native
File size: 10.4 KB
Line 
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
29using namespace GeoVis;
30
31static 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
41static 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
50static osg::Uniform *s_highlightUniform = NULL;
51
52static 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
70void GeoVis::setHighlightByObjectID(osgEarth::ObjectID id)
71{
72    if (s_highlightUniform != NULL) {
73        s_highlightUniform->set(id);
74    }
75}
76
77void GeoVis::clearHighlight()
78{
79    if (s_highlightUniform != NULL) {
80        s_highlightUniform->set(0U);
81    }
82}
83
84HoverCallback::HoverCallback(Renderer *renderer) :
85    _renderer(renderer)
86{
87    installHighlighter(renderer->getMapNode()->getOrCreateStateSet(),
88                       osgEarth::Registry::objectIndex()->getObjectIDAttribLocation());
89}
90
91void 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
154void 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        (*itr)->clearDecoration();
167        _renderer->eventuallyRender();
168    }
169    clearHighlight();
170}
171
172bool HoverCallback::accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa)
173{
174    // Pick whenever mouse moves
175    return ea.getEventType() == ea.MOVE;
176}
177
178SelectCallback::SelectCallback(Renderer *renderer) :
179    _renderer(renderer)
180{
181    installHighlighter(renderer->getMapNode()->getOrCreateStateSet(),
182                       osgEarth::Registry::objectIndex()->getObjectIDAttribLocation());
183    installHighlighter(renderer->getPlaceNodes()->getOrCreateStateSet(),
184                       osgEarth::Registry::objectIndex()->getObjectIDAttribLocation());
185}
186
187void SelectCallback::onHit(osgEarth::ObjectID id)
188{
189    // First see whether it's a feature:
190    osgEarth::Features::FeatureIndex *index = osgEarth::Registry::objectIndex()->get<osgEarth::Features::FeatureIndex>(id);
191    osgEarth::Features::Feature *feature = index ? index->getFeature(id) : 0L;
192    TRACE("Select hit oid: %lu at %g,%g", id, _pickPoint.x(), _pickPoint.y());
193    if (feature) {
194        osgEarth::Features::FeatureSourceIndex *fsi = dynamic_cast<osgEarth::Features::FeatureSourceIndex *>(index);
195        std::string layerName;
196        if (fsi) {
197            osgEarth::Features::FeatureSource *source = fsi->getFeatureSource();
198            if (source) {
199                layerName = source->getFeatureSourceOptions().name().get();
200            }
201        }
202        TRACE("Hit feature ID: %lu (of %d) layer: %s", feature->getFID(), index->size(), layerName.c_str());
203
204        _renderer->clearSelection();
205        _renderer->addPlacard(_pickPoint, feature, layerName.c_str());
206
207        char mesg[256];
208        snprintf(mesg, sizeof(mesg), "nv>select feature %u %lu %d {%s}\n", id, feature->getFID(), index->size(), layerName.c_str());
209        size_t length = strlen(mesg);
210        queueResponse(mesg, length, Response::VOLATILE, Response::DATA);
211    } else {
212        _renderer->clearSelection();
213        osgEarth::Annotation::AnnotationNode* anno =
214            osgEarth::Registry::objectIndex()->get<osgEarth::Annotation::AnnotationNode>(id);
215        if (anno != NULL) {
216            TRACE("Hit AnnotationNode: %p, \"%s\"", anno, anno->getName().c_str());
217            char mesg[256];
218            snprintf(mesg, sizeof(mesg), "nv>select annotation %u {%s}\n", id, anno->getName().c_str());
219            size_t length = strlen(mesg);
220            queueResponse(mesg, length, Response::VOLATILE, Response::DATA);
221        }
222    }
223    setHighlightByObjectID(id);
224    _renderer->pickPending(false);
225    _renderer->eventuallyRender();
226}
227
228void SelectCallback::onMiss()
229{
230    TRACE("Select miss");
231    _renderer->clearSelection();
232
233    static const char *mesg = "nv>select clear\n";
234    static const size_t length = strlen(mesg);
235    queueResponse(mesg, length, Response::STATIC, Response::DATA);
236
237    _renderer->pickPending(false);
238    _renderer->eventuallyRender();
239}
240
241bool SelectCallback::accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa)
242{
243    if (ea.getEventType() == ea.PUSH) {
244        _x = ea.getX();
245        _y = ea.getY();
246        return false;
247    } else if (ea.getEventType() == ea.RELEASE) {
248        // Don't pick if mouse was dragged more than 2 pixels
249        if (fabsf(ea.getX()-_x) > 2 || fabsf(ea.getY()-_y) > 2) {
250            return false;
251        } else {
252            _renderer->mapMouseCoords(ea.getX(), ea.getY(), _pickPoint, false);
253            _renderer->pickPending(true);
254            TRACE("Pending pick at %g %g", ea.getX(), ea.getY());
255            return true;
256        }
257    }
258    return false;
259}
260
261void DeleteCallback::onHit(osgEarth::ObjectID id)
262{
263    // First see whether it's a feature:
264    osgEarth::Features::FeatureIndex *index = osgEarth::Registry::objectIndex()->get<osgEarth::Features::FeatureIndex>(id);
265    osgEarth::Features::Feature *feature = index ? index->getFeature(id) : 0L;
266
267    if (feature) {
268        TRACE("Hit feature ID: %lu, oid: %lu name: %s", feature->getFID(), id);
269        TRACE("Feature name: %s", feature->getString("name").c_str());
270    } else {
271        TRACE("Picker hit!");
272        osgEarth::Annotation::AnnotationNode* anno =
273            osgEarth::Registry::objectIndex()->get<osgEarth::Annotation::AnnotationNode>(id);
274        std::set<osgEarth::Annotation::AnnotationNode*>& hovered = _renderer->getHovered();
275        anno->clearDecoration();
276        _renderer->getPlaceNodes()->removeChild(anno);
277        if (hovered.find(anno) != hovered.end()) {
278            hovered.erase(anno);
279        }
280    }
281}
Note: See TracBrowser for help on using the repository browser.