source: geovis/trunk/Picker.cpp @ 6083

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

Prevent renderer from waiting in select() if a pick is pending.

  • 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    INFO("Hover hit");
97    if (feature) {
98        INFO("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            INFO(" attr: %s", itr->first.c_str());
102            switch (itr->second.first) {
103            case osgEarth::Features::ATTRTYPE_STRING:
104                INFO(" value: %s", itr->second.getString().c_str());
105                break;
106            case osgEarth::Features::ATTRTYPE_INT:
107                INFO(" value: %d", itr->second.getInt());
108                break;
109            case osgEarth::Features::ATTRTYPE_DOUBLE:
110                INFO(" value: %g", itr->second.getDouble());
111                break;
112            case osgEarth::Features::ATTRTYPE_BOOL:
113                INFO(" value: %s", itr->second.getBool() ? "true" : "false");
114                break;
115            default:
116                INFO(" value: unknown type");
117            }
118        }
119        //INFO("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 && anno->getDecoration().empty()) {
131            INFO("Hit AnnotationNode: %p", anno);
132#if 0
133            if (hovered.find(anno) == hovered.end()) {
134                hovered.insert(anno);
135                anno->setDecoration("hover");
136                _renderer->eventuallyRender();
137            }
138            toUnHover.erase(anno);
139#endif
140        }
141#if 0
142        for (std::set<osgEarth::Annotation::AnnotationNode *>::iterator itr = toUnHover.begin();
143             itr != toUnHover.end(); ++itr) {
144            hovered.erase(*itr);
145            (*itr)->clearDecoration();
146            _renderer->eventuallyRender();
147        }
148#endif
149    }
150    setHighlightByObjectID(id);
151}
152
153void HoverCallback::onMiss()
154{
155    INFO("Hover miss");
156    std::set<osgEarth::Annotation::AnnotationNode*> toUnHover;
157    std::set<osgEarth::Annotation::AnnotationNode*>& hovered = _renderer->getHovered();
158    for (std::set<osgEarth::Annotation::AnnotationNode*>::iterator itr = hovered.begin();
159         itr != hovered.end(); ++itr) {
160        toUnHover.insert(*itr);
161    }
162    for (std::set<osgEarth::Annotation::AnnotationNode *>::iterator itr = toUnHover.begin();
163         itr != toUnHover.end(); ++itr) {
164        hovered.erase(*itr);
165        (*itr)->clearDecoration();
166        _renderer->eventuallyRender();
167    }
168    clearHighlight();
169}
170
171bool HoverCallback::accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa)
172{
173    // Pick whenever mouse moves
174    return ea.getEventType() == ea.MOVE;
175}
176
177SelectCallback::SelectCallback(Renderer *renderer) :
178    _renderer(renderer)
179{
180    installHighlighter(renderer->getMapNode()->getOrCreateStateSet(),
181                       osgEarth::Registry::objectIndex()->getObjectIDAttribLocation());
182    installHighlighter(renderer->getPlaceNodes()->getOrCreateStateSet(),
183                       osgEarth::Registry::objectIndex()->getObjectIDAttribLocation());
184}
185
186void SelectCallback::onHit(osgEarth::ObjectID id)
187{
188    // First see whether it's a feature:
189    osgEarth::Features::FeatureIndex *index = osgEarth::Registry::objectIndex()->get<osgEarth::Features::FeatureIndex>(id);
190    osgEarth::Features::Feature *feature = index ? index->getFeature(id) : 0L;
191    INFO("Select hit oid: %lu at %g,%g", id, _pickPoint.x(), _pickPoint.y());
192    if (feature) {
193        osgEarth::Features::FeatureSourceIndex *fsi = dynamic_cast<osgEarth::Features::FeatureSourceIndex *>(index);
194        std::string layerName;
195        if (fsi) {
196            osgEarth::Features::FeatureSource *source = fsi->getFeatureSource();
197            if (source) {
198                layerName = source->getFeatureSourceOptions().name().get();
199            }
200        }
201        INFO("Hit feature ID: %lu (of %d) layer: %s", feature->getFID(), index->size(), layerName.c_str());
202
203        _renderer->clearSelection();
204        _renderer->addPlacard(_pickPoint, feature, layerName.c_str());
205
206        char mesg[256];
207        snprintf(mesg, sizeof(mesg), "nv>select feature %u %lu %d {%s}\n", id, feature->getFID(), index->size(), layerName.c_str());
208        size_t length = strlen(mesg);
209        queueResponse(mesg, length, Response::VOLATILE, Response::DATA);
210    } else {
211        _renderer->clearSelection();
212        osgEarth::Annotation::AnnotationNode* anno =
213            osgEarth::Registry::objectIndex()->get<osgEarth::Annotation::AnnotationNode>(id);
214        if (anno != NULL) {
215            INFO("Hit AnnotationNode: %p, \"%s\"", anno, anno->getName().c_str());
216            char mesg[256];
217            snprintf(mesg, sizeof(mesg), "nv>select annotation %u {%s}\n", id, anno->getName().c_str());
218            size_t length = strlen(mesg);
219            queueResponse(mesg, length, Response::VOLATILE, Response::DATA);
220        }
221    }
222    setHighlightByObjectID(id);
223    _renderer->pickPending(false);
224    _renderer->eventuallyRender();
225}
226
227void SelectCallback::onMiss()
228{
229    INFO("Select miss");
230    _renderer->clearSelection();
231
232    static const char *mesg = "nv>select clear\n";
233    static const size_t length = strlen(mesg);
234    queueResponse(mesg, length, Response::STATIC, Response::DATA);
235
236    _renderer->pickPending(false);
237    _renderer->eventuallyRender();
238}
239
240bool SelectCallback::accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa)
241{
242    if (ea.getEventType() == ea.PUSH) {
243        _x = ea.getX();
244        _y = ea.getY();
245        return false;
246    } else if (ea.getEventType() == ea.RELEASE) {
247        // Don't pick if mouse was dragged more than 2 pixels
248        if (fabsf(ea.getX()-_x) > 2 || fabsf(ea.getY()-_y) > 2) {
249            return false;
250        } else {
251            _renderer->mapMouseCoords(ea.getX(), ea.getY(), _pickPoint, false);
252            _renderer->pickPending(true);
253            TRACE("Pending pick at %g %g", ea.getX(), ea.getY());
254            return true;
255        }
256    }
257    return false;
258}
259
260void DeleteCallback::onHit(osgEarth::ObjectID id)
261{
262    // First see whether it's a feature:
263    osgEarth::Features::FeatureIndex *index = osgEarth::Registry::objectIndex()->get<osgEarth::Features::FeatureIndex>(id);
264    osgEarth::Features::Feature *feature = index ? index->getFeature(id) : 0L;
265
266    if (feature) {
267        TRACE("Hit feature ID: %lu, oid: %lu name: %s", feature->getFID(), id);
268        TRACE("Feature name: %s", feature->getString("name").c_str());
269    } else {
270        TRACE("Picker hit!");
271        osgEarth::Annotation::AnnotationNode* anno =
272            osgEarth::Registry::objectIndex()->get<osgEarth::Annotation::AnnotationNode>(id);
273        std::set<osgEarth::Annotation::AnnotationNode*>& hovered = _renderer->getHovered();
274        anno->clearDecoration();
275        _renderer->getPlaceNodes()->removeChild(anno);
276        if (hovered.find(anno) != hovered.end()) {
277            hovered.erase(anno);
278        }
279    }
280}
Note: See TracBrowser for help on using the repository browser.