summaryrefslogtreecommitdiff
path: root/indra/newview/lltracker.cpp
blob: b1bee0f2c7f3b3ea1e650ed2fdf3b4657c8abf11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/**
 * @file lltracker.cpp
 * @brief Container for objects user is tracking.
 *
 * $LicenseInfo:firstyear=2003&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

// library includes
#include "llcoord.h"
#include "llfontgl.h"
#include "llgl.h"
#include "llrender.h"
#include "llinventory.h"
#include "llinventorydefines.h"
#include "llpointer.h"
#include "llstring.h"
#include "lluuid.h"
#include "v3math.h"
#include "v3dmath.h"
#include "v4color.h"

// viewer includes
#include "llappviewer.h"
#include "lltracker.h"
#include "llagent.h"
#include "llagentcamera.h"
#include "llcallingcard.h"
#include "llfloaterworldmap.h"
#include "llhudtext.h"
#include "llhudview.h"
#include "llinventorymodel.h"
#include "llinventoryobserver.h"
#include "lllandmarklist.h"
#include "llprogressview.h"
#include "llsky.h"
#include "llui.h"
#include "llviewercamera.h"
#include "llviewerinventory.h"
#include "llviewerwindow.h"
#include "llworld.h"
#include "llworldmapview.h"
#include "llviewercontrol.h"

const F32 DESTINATION_REACHED_RADIUS    = 3.0f;
const F32 DESTINATION_VISITED_RADIUS    = 6.0f;

// this last one is useful for when the landmark is
// very close to agent when tracking is turned on
const F32 DESTINATION_UNVISITED_RADIUS = 12.0f;

const S32 ARROW_OFF_RADIUS_SQRD = 100;

const S32 HUD_ARROW_SIZE = 32;

// static
LLTracker *LLTracker::sTrackerp = NULL;
bool LLTracker::sCheesyBeacon = false;

LLTracker::LLTracker()
:   mTrackingStatus(TRACKING_NOTHING),
    mTrackingLocationType(LOCATION_NOTHING),
    mHUDArrowCenterX(0),
    mHUDArrowCenterY(0),
    mToolTip( "" ),
    mTrackedLandmarkName(""),
    mHasReachedLandmark(false),
    mHasLandmarkPosition(false),
    mLandmarkHasBeenVisited(false),
    mTrackedLocationName( "" ),
    mIsTrackingLocation(false),
    mHasReachedLocation(false)
{ }


LLTracker::~LLTracker()
{
    purgeBeaconText();
}


// static
void LLTracker::stopTracking(bool clear_ui)
{
    instance()->stopTrackingAll(clear_ui);
}


// static virtual
void LLTracker::drawHUDArrow()
{
    if (!LLWorld::instanceExists())
    {
        return;
    }

    static LLCachedControl<bool> render_beacon(gSavedSettings, "RenderTrackerBeacon", true);
    if (!render_beacon)
    {
        return;
    }

    if (gViewerWindow->getProgressView()->getVisible()) return;

    static LLUIColor map_track_color = LLUIColorTable::instance().getColor("MapTrackColor", LLColor4::white);

    /* tracking autopilot destination has been disabled
       -- 2004.01.09, Leviathan
    // Draw dot for autopilot target
    if (gAgent.getAutoPilot())
    {
        instance()->drawMarker( gAgent.getAutoPilotTargetGlobal(), map_track_color );
        return;
    }
    */
    switch (getTrackingStatus())
    {
    case TRACKING_AVATAR:
        // Tracked avatar
        if(LLAvatarTracker::instance().haveTrackingInfo())
        {
            instance()->drawMarker( LLAvatarTracker::instance().getGlobalPos(), map_track_color );
        }
        break;

    case TRACKING_LANDMARK:
        instance()->drawMarker( getTrackedPositionGlobal(), map_track_color );
        break;

    case TRACKING_LOCATION:
        // HACK -- try to keep the location just above the terrain
#if 0
        // UNHACKED by CRO - keep location where the location is
        instance()->mTrackedPositionGlobal.mdV[VZ] =
                0.9f * instance()->mTrackedPositionGlobal.mdV[VZ]
                + 0.1f * (LLWorld::getInstance()->resolveLandHeightGlobal(getTrackedPositionGlobal()) + 1.5f);
#endif
        instance()->mTrackedPositionGlobal.mdV[VZ] = llclamp((F32)instance()->mTrackedPositionGlobal.mdV[VZ], LLWorld::getInstance()->resolveLandHeightGlobal(getTrackedPositionGlobal()) + 1.5f, (F32)instance()->getTrackedPositionGlobal().mdV[VZ]);
        instance()->drawMarker( getTrackedPositionGlobal(), map_track_color );
        break;

    default:
        break;
    }
}


// static
void LLTracker::render3D()
{
    if (!gFloaterWorldMap || !gSavedSettings.getBOOL("RenderTrackerBeacon"))
    {
        return;
    }

    static LLUIColor map_track_color = LLUIColorTable::instance().getColor("MapTrackColor", LLColor4::white);
    static LLUIColor map_track_color_under = LLUIColorTable::instance().getColor("MapTrackColorUnder", LLColor4::white);

    // Arbitary location beacon
    if( instance()->mIsTrackingLocation )
    {
        if (!instance()->mBeaconText)
        {
            instance()->mBeaconText = (LLHUDText *)LLHUDObject::addHUDObject(LLHUDObject::LL_HUD_TEXT);
            instance()->mBeaconText->setDoFade(false);
        }

        LLVector3d pos_global = instance()->mTrackedPositionGlobal;
        // (z-attenuation < 1) means compute "shorter" distance in z-axis,
        // so cancel tracking even if avatar is a little above or below.
        F32 dist = gFloaterWorldMap->getDistanceToDestination(pos_global, 0.5f);
        if (dist < DESTINATION_REACHED_RADIUS)
        {
            instance()->stopTrackingLocation(false,true);
        }
        else
        {
            renderBeacon( instance()->mTrackedPositionGlobal, map_track_color, map_track_color_under,
                        instance()->mBeaconText, instance()->mTrackedLocationName );
        }
    }

    // Landmark beacon
    else if( !instance()->mTrackedLandmarkAssetID.isNull() )
    {
        if (!instance()->mBeaconText)
        {
            instance()->mBeaconText = (LLHUDText *)LLHUDObject::addHUDObject(LLHUDObject::LL_HUD_TEXT);
            instance()->mBeaconText->setDoFade(false);
        }

        if (instance()->mHasLandmarkPosition)
        {
            F32 dist = gFloaterWorldMap->getDistanceToDestination(instance()->mTrackedPositionGlobal, 1.0f);

            if (   !instance()->mLandmarkHasBeenVisited
                && dist < DESTINATION_VISITED_RADIUS )
            {
                // its close enough ==> flag as visited
                instance()->setLandmarkVisited();
            }

            if (   !instance()->mHasReachedLandmark
                && dist < DESTINATION_REACHED_RADIUS )
            {
                // its VERY CLOSE ==> automatically stop tracking
                instance()->stopTrackingLandmark();
            }
            else
            {
                if (    instance()->mHasReachedLandmark
                     && dist > DESTINATION_UNVISITED_RADIUS )
                {
                    // this is so that landmark beacons don't immediately
                    // disappear when they're created only a few meters
                    // away, yet disappear when the agent wanders away
                    // and back again
                    instance()->mHasReachedLandmark = false;
                }
                renderBeacon( instance()->mTrackedPositionGlobal, map_track_color, map_track_color_under,
                              instance()->mBeaconText, instance()->mTrackedLandmarkName );
            }
        }
        else
        {
            // probably just finished downloading the asset
            instance()->cacheLandmarkPosition();
        }
    }
    else
    {
        // Avatar beacon
        LLAvatarTracker& av_tracker = LLAvatarTracker::instance();
        if(av_tracker.haveTrackingInfo())
        {
            if (!instance()->mBeaconText)
            {
                instance()->mBeaconText = (LLHUDText *)LLHUDObject::addHUDObject(LLHUDObject::LL_HUD_TEXT);
                instance()->mBeaconText->setDoFade(false);
            }

            F32 dist = gFloaterWorldMap->getDistanceToDestination(instance()->getTrackedPositionGlobal(), 0.0f);
            if (dist < DESTINATION_REACHED_RADIUS)
            {
                instance()->stopTrackingAvatar();
            }
            else
            {
                renderBeacon( av_tracker.getGlobalPos(), map_track_color, map_track_color_under,
                            instance()->mBeaconText, av_tracker.getName() );
            }
        }
        else
        {
            bool stop_tracking = false;
            const LLUUID& avatar_id = av_tracker.getAvatarID();
            if(avatar_id.isNull())
            {
                stop_tracking = true;
            }
            else
            {
                const LLRelationship* buddy = av_tracker.getBuddyInfo(avatar_id);
                if(buddy && !buddy->isOnline() && !gAgent.isGodlike())
                {
                    stop_tracking = true;
                }
                if(!buddy && !gAgent.isGodlike())
                {
                    stop_tracking = true;
                }
            }
            if(stop_tracking)
            {
                instance()->stopTrackingAvatar();
            }
        }
    }
}


// static
void LLTracker::trackAvatar( const LLUUID& avatar_id, const std::string& name )
{
    instance()->stopTrackingLandmark();
    instance()->stopTrackingLocation();

    LLAvatarTracker::instance().track( avatar_id, name );
    instance()->mTrackingStatus = TRACKING_AVATAR;
    instance()->mLabel = name;
    instance()->mToolTip = "";
}


// static
void LLTracker::trackLandmark( const LLUUID& asset_id, const LLUUID& item_id, const std::string& name)
{
    instance()->stopTrackingAvatar();
    instance()->stopTrackingLocation();

    instance()->mTrackedLandmarkAssetID = asset_id;
    instance()->mTrackedLandmarkItemID = item_id;
    instance()->mTrackedLandmarkName = name;
    instance()->cacheLandmarkPosition();
    instance()->mTrackingStatus = TRACKING_LANDMARK;
    instance()->mLabel = name;
    instance()->mToolTip = "";
}


// static
void LLTracker::trackLocation(const LLVector3d& pos_global, const std::string& full_name, const std::string& tooltip, ETrackingLocationType location_type)
{
    instance()->stopTrackingAvatar();
    instance()->stopTrackingLandmark();

    instance()->mTrackedPositionGlobal = pos_global;
    instance()->mTrackedLocationName = full_name;
    instance()->mIsTrackingLocation = true;
    instance()->mTrackingStatus = TRACKING_LOCATION;
    instance()->mTrackingLocationType = location_type;
    instance()->mLabel = full_name;
    instance()->mToolTip = tooltip;
}


// static
bool LLTracker::handleMouseDown(S32 x, S32 y)
{
    bool eat_mouse_click = false;
    // fortunately, we can always compute the tracking arrow center
    S32 dist_sqrd = (x - instance()->mHUDArrowCenterX) * (x - instance()->mHUDArrowCenterX) +
                    (y - instance()->mHUDArrowCenterY) * (y - instance()->mHUDArrowCenterY);
    if (dist_sqrd < ARROW_OFF_RADIUS_SQRD)
    {
        /* tracking autopilot destination has been disabled
           -- 2004.01.09, Leviathan
        // turn off tracking
        if (gAgent.getAutoPilot())
        {
            gAgent.stopAutoPilot(true); // true because cancelled by user
            eat_mouse_click = true;
        }
        */
        if (getTrackingStatus())
        {
            instance()->stopTrackingAll();
            eat_mouse_click = true;
        }
    }
    return eat_mouse_click;
}


// static
LLVector3d LLTracker::getTrackedPositionGlobal()
{
    LLVector3d pos_global;
    switch (getTrackingStatus())
    {
    case TRACKING_AVATAR:
    {
        LLAvatarTracker& av_tracker = LLAvatarTracker::instance();
        if (av_tracker.haveTrackingInfo())
        {
            pos_global = av_tracker.getGlobalPos(); }
        break;
    }
    case TRACKING_LANDMARK:
        if( instance()->mHasLandmarkPosition )
        {
            pos_global = instance()->mTrackedPositionGlobal;
        }
        break;
    case TRACKING_LOCATION:
        pos_global = instance()->mTrackedPositionGlobal;
        break;
    default:
        break;
    }
    return pos_global;
}


// static
bool LLTracker::hasLandmarkPosition()
{
    if (!instance()->mHasLandmarkPosition)
    {
        // maybe we just received the landmark position info
        instance()->cacheLandmarkPosition();
    }
    return instance()->mHasLandmarkPosition;
}


// static
const std::string& LLTracker::getTrackedLocationName()
{
    return instance()->mTrackedLocationName;
}

F32 pulse_func(F32 t, F32 z, bool tracking_avatar, std::string direction)
{
    if (!LLTracker::sCheesyBeacon)
    {
        return 0.f;
    }

    t *= F_PI;
    if ("DOWN" == direction)
    {
        z += t*64.f - 256.f;
    }
    else
    {
    z -= t*64.f - 256.f;
    }

    F32 a = cosf(z*F_PI/512.f)*10.0f;
    a = llmax(a, 9.9f);
    a -= 9.9f;
    a *= 10.f;
    return a;
}

void draw_shockwave(F32 center_z, F32 t, S32 steps, LLColor4 color)
{
    if (!LLTracker::sCheesyBeacon)
    {
        return;
    }

    t *= 0.6284f/F_PI;

    t -= (F32) (S32) t;

    t = llmax(t, 0.5f);
    t -= 0.5f;
    t *= 2.0f;

    F32 radius = t*16536.f;

    // Inexact, but reasonably fast.
    F32 delta = F_TWO_PI / steps;
    F32 sin_delta = sin( delta );
    F32 cos_delta = cos( delta );
    F32 x = radius;
    F32 y = 0.f;

    LLColor4 ccol = LLColor4(1,1,1,(1.f-t)*0.25f);
    gGL.begin(LLRender::TRIANGLE_FAN);
    gGL.color4fv(ccol.mV);
    gGL.vertex3f(0.f, 0.f, center_z);
    // make sure circle is complete
    steps += 1;

    color.mV[3] = (1.f-t*t);

    gGL.color4fv(color.mV);
    while( steps-- )
    {
        // Successive rotations
        gGL.vertex3f( x, y, center_z );
        F32 x_new = x * cos_delta - y * sin_delta;
        y = x * sin_delta +  y * cos_delta;
        x = x_new;
    }
    gGL.end();
}


void LLTracker::drawBeacon(LLVector3 pos_agent, std::string direction, LLColor4 fogged_color, F32 dist)
{
    const F32 MAX_HEIGHT = 5020.f;
    const U32 BEACON_ROWS = 128;

    U32 nRows;
    F32 height;
    F32 rowHeight;

    LLColor4 c_col, col_next, col_edge, col_edge_next;

    gGL.matrixMode(LLRender::MM_MODELVIEW);
    gGL.pushMatrix();

    if ("DOWN" == direction)
    {
        gGL.translatef(pos_agent.mV[0], pos_agent.mV[1], pos_agent.mV[2]);
        draw_shockwave(1024.f, gRenderStartTime.getElapsedTimeF32(), 32, fogged_color);
        height = MAX_HEIGHT - pos_agent.mV[2];
    }
    else
    {
        gGL.translatef(pos_agent.mV[0], pos_agent.mV[1], 0);
        height = pos_agent.mV[2];
    }

    nRows = (U32)ceil((BEACON_ROWS * height) / MAX_HEIGHT);
    if(nRows<2) nRows=2;
    rowHeight = height / nRows;

    LLVector3 x_axis = LLViewerCamera::getInstance()->getLeftAxis();
    F32 t = gRenderStartTime.getElapsedTimeF32();

    F32 x = x_axis.mV[0];
    F32 y = x_axis.mV[1];
    F32 z = 0.f;
    F32 z_next;

    F32 a,an;
    F32 xa,xan;
    F32 ya,yan;

    bool tracking_avatar = getTrackingStatus() == TRACKING_AVATAR;

    gGL.color4fv(fogged_color.mV);

    gGL.begin(LLRender::TRIANGLES);

    for (U32 i = 0; i < nRows; i++)
    {
        z_next = z + rowHeight;

        a = pulse_func(t, z, tracking_avatar, direction);
        an = pulse_func(t, z_next, tracking_avatar, direction);

        c_col = fogged_color + LLColor4(a, a, a, a);
        col_next = fogged_color + LLColor4(an, an, an, an);
        col_edge = fogged_color * LLColor4(a, a, a, 0.0f);
        col_edge_next = fogged_color * LLColor4(an, an, an, 0.0f);

        a = a + a + 1.f;
        an = an + an + 1.f;

        xa = x*a;
        ya = y*a;
        xan = x*an;
        yan = y*an;

        gGL.color4fv(col_edge.mV);
        gGL.vertex3f(-xa, -ya, z);

        gGL.color4fv(col_next.mV);
        gGL.vertex3f(0, 0, z_next);

        gGL.color4fv(col_edge_next.mV);
        gGL.vertex3f(-xan, -yan, z_next);


        gGL.color4fv(col_edge.mV);
        gGL.vertex3f(-xa, -ya, z);

        gGL.color4fv(c_col.mV);
        gGL.vertex3f(0, 0, z);

        gGL.color4fv(col_next.mV);
        gGL.vertex3f(0, 0, z_next);


        gGL.color4fv(c_col.mV);
        gGL.vertex3f(0, 0, z);

        gGL.color4fv(col_edge_next.mV);
        gGL.vertex3f(xan, yan, z_next);

        gGL.color4fv(col_next.mV);
        gGL.vertex3f(0, 0, z_next);


        gGL.color4fv(c_col.mV);
        gGL.vertex3f(0, 0, z);

        gGL.color4fv(col_edge.mV);
        gGL.vertex3f(xa, ya, z);

        gGL.color4fv(col_edge_next.mV);
        gGL.vertex3f(xan, yan, z_next);

        z += rowHeight;
    }

    gGL.end();
    gGL.popMatrix();
}

// static
void LLTracker::renderBeacon(LLVector3d pos_global,
                             const LLColor4& color,
                             const LLColor4& color_under,
                             LLHUDText* hud_textp,
                             const std::string& label )
{
    sCheesyBeacon = gSavedSettings.getBOOL("CheesyBeacon");
    LLVector3d to_vec = pos_global - gAgentCamera.getCameraPositionGlobal();

    F32 dist = (F32)to_vec.magVec();
    F32 color_frac = 1.f;
    if (dist > 0.99f * LLViewerCamera::getInstance()->getFar())
    {
        color_frac = 0.4f;
    //  pos_global = gAgentCamera.getCameraPositionGlobal() + 0.99f*(LLViewerCamera::getInstance()->getFar()/dist)*to_vec;
        }
    else
    {
        color_frac = 1.f - 0.6f*(dist/LLViewerCamera::getInstance()->getFar());
    }

    LLColor4 fogged_color = color_frac * color + (1 - color_frac)*gSky.getSkyFogColor();
    LLColor4 fogged_color_under = color_frac * color_under + (1 - color_frac) * gSky.getSkyFogColor();

    F32 FADE_DIST = 3.f;
    fogged_color.mV[3] = llmax(0.2f, llmin(0.5f,(dist-FADE_DIST)/FADE_DIST));
    fogged_color_under.mV[3] = llmax(0.2f, llmin(0.5f,(dist-FADE_DIST)/FADE_DIST));

    LLVector3 pos_agent = gAgent.getPosAgentFromGlobal(pos_global);

    LLGLSTracker gls_tracker;
    gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
    LLGLDisable cull_face(GL_CULL_FACE);
    LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);

    LLTracker::drawBeacon(pos_agent, "DOWN", fogged_color, dist);
    LLTracker::drawBeacon(pos_agent, "UP", fogged_color_under, dist);

    std::string text;
    text = llformat( "%.0f m", to_vec.magVec());

    std::string str;
    str += label;
    str += '\n';
    str += text;

    hud_textp->setFont(LLFontGL::getFontSansSerif());
    hud_textp->setZCompare(false);
    hud_textp->setColor(LLColor4(1.f, 1.f, 1.f, llmax(0.2f, llmin(1.f,(dist-FADE_DIST)/FADE_DIST))));

    hud_textp->setString(str);
    hud_textp->setVertAlignment(LLHUDText::ALIGN_VERT_CENTER);
    hud_textp->setPositionAgent(pos_agent);
}


void LLTracker::stopTrackingAll(bool clear_ui)
{
    switch (mTrackingStatus)
    {
    case TRACKING_AVATAR :
        stopTrackingAvatar(clear_ui);
        break;
    case TRACKING_LANDMARK :
        stopTrackingLandmark(clear_ui);
        break;
    case TRACKING_LOCATION :
        stopTrackingLocation(clear_ui);
        break;
    default:
        mTrackingStatus = TRACKING_NOTHING;
        break;
    }
}


void LLTracker::stopTrackingAvatar(bool clear_ui)
{
    LLAvatarTracker& av_tracker = LLAvatarTracker::instance();
    if( !av_tracker.getAvatarID().isNull() )
    {
        av_tracker.untrack( av_tracker.getAvatarID() );
    }

    purgeBeaconText();
    gFloaterWorldMap->clearAvatarSelection(clear_ui);
    mTrackingStatus = TRACKING_NOTHING;
}


void LLTracker::stopTrackingLandmark(bool clear_ui)
{
    purgeBeaconText();
    mTrackedLandmarkAssetID.setNull();
    mTrackedLandmarkItemID.setNull();
    mTrackedLandmarkName.assign("");
    mTrackedPositionGlobal.zeroVec();
    mHasLandmarkPosition = false;
    mHasReachedLandmark = false;
    mLandmarkHasBeenVisited = true;
    gFloaterWorldMap->clearLandmarkSelection(clear_ui);
    mTrackingStatus = TRACKING_NOTHING;
}


void LLTracker::stopTrackingLocation(bool clear_ui, bool dest_reached)
{
    purgeBeaconText();
    mTrackedLocationName.assign("");
    mIsTrackingLocation = false;
    mTrackedPositionGlobal.zeroVec();
    gFloaterWorldMap->clearLocationSelection(clear_ui, dest_reached);
    mTrackingStatus = TRACKING_NOTHING;
    mTrackingLocationType = LOCATION_NOTHING;
}

void LLTracker::clearFocus()
{
    instance()->mTrackingStatus = TRACKING_NOTHING;
}

void LLTracker::drawMarker(const LLVector3d& pos_global, const LLColor4& color)
{
    // get position
    LLVector3 pos_local = gAgent.getPosAgentFromGlobal(pos_global);

    // check in frustum
    LLCoordGL screen;
    S32 x = 0;
    S32 y = 0;
    const bool CLAMP = true;

    if (LLViewerCamera::getInstance()->projectPosAgentToScreen(pos_local, screen, CLAMP)
        || LLViewerCamera::getInstance()->projectPosAgentToScreenEdge(pos_local, screen) )
    {
        gHUDView->screenPointToLocal(screen.mX, screen.mY, &x, &y);

        // the center of the rendered position of the arrow obeys
        // the following rules:
        // (1) it lies on an ellipse centered on the target position
        // (2) it lies on the line between the target and the window center
        // (3) right now the radii of the ellipse are fixed, but eventually
        //     they will be a function of the target text
        //
        // from those rules we can compute the position of the
        // lower left corner of the image
        LLRect rect = gHUDView->getRect();
        S32 x_center = lltrunc(0.5f * (F32)rect.getWidth());
        S32 y_center = lltrunc(0.5f * (F32)rect.getHeight());
        x = x - x_center;   // x and y relative to center
        y = y - y_center;
        F32 dist = sqrt((F32)(x*x + y*y));
        S32 half_arrow_size = lltrunc(0.5f * HUD_ARROW_SIZE);
        if (dist > 0.f)
        {
            const F32 ARROW_ELLIPSE_RADIUS_X = 2 * HUD_ARROW_SIZE;
            const F32 ARROW_ELLIPSE_RADIUS_Y = HUD_ARROW_SIZE;

            // compute where the arrow should be
            F32 x_target = (F32)(x + x_center) - (ARROW_ELLIPSE_RADIUS_X * ((F32)x / dist) );
            F32 y_target = (F32)(y + y_center) - (ARROW_ELLIPSE_RADIUS_Y * ((F32)y / dist) );

            // keep the arrow within the window
            F32 x_clamped = llclamp( x_target, (F32)half_arrow_size, (F32)(rect.getWidth() - half_arrow_size));
            F32 y_clamped = llclamp( y_target, (F32)half_arrow_size, (F32)(rect.getHeight() - half_arrow_size));

            F32 slope = (F32)(y) / (F32)(x);
            F32 window_ratio = (F32)(rect.getHeight() - HUD_ARROW_SIZE) / (F32)(rect.getWidth() - HUD_ARROW_SIZE);

            // if the arrow has been clamped on one axis
            // then we need to compute the other axis
            if (llabs(slope) > window_ratio)
            {
                if (y_clamped != (F32)y_target)
                {
                    // clamp by y
                    x_clamped = (y_clamped - (F32)y_center) / slope + (F32)x_center;
                }
            }
            else if (x_clamped != (F32)x_target)
            {
                // clamp by x
                y_clamped = (x_clamped - (F32)x_center) * slope + (F32)y_center;
            }
            mHUDArrowCenterX = lltrunc(x_clamped);
            mHUDArrowCenterY = lltrunc(y_clamped);
        }
        else
        {
            // recycle the old values
            x = mHUDArrowCenterX - x_center;
            y = mHUDArrowCenterY - y_center;
        }

        F32 angle = atan2( (F32)y, (F32)x );

        gl_draw_scaled_rotated_image(mHUDArrowCenterX - half_arrow_size,
                                     mHUDArrowCenterY - half_arrow_size,
                                     HUD_ARROW_SIZE, HUD_ARROW_SIZE,
                                     RAD_TO_DEG * angle,
                                     LLWorldMapView::sTrackArrowImage->getImage(),
                                     color);
    }
}


void LLTracker::setLandmarkVisited()
{
    // poke the inventory item
    if (!mTrackedLandmarkItemID.isNull())
    {
        LLInventoryItem* i = gInventory.getItem( mTrackedLandmarkItemID );
        LLViewerInventoryItem* item = (LLViewerInventoryItem*)i;
        if (   item
            && !(item->getFlags()&LLInventoryItemFlags::II_FLAGS_LANDMARK_VISITED))
        {
            U32 flags = item->getFlags();
            flags |= LLInventoryItemFlags::II_FLAGS_LANDMARK_VISITED;
            item->setFlags(flags);
            LLMessageSystem* msg = gMessageSystem;
            msg->newMessage("ChangeInventoryItemFlags");
            msg->nextBlock("AgentData");
            msg->addUUID("AgentID", gAgent.getID());
            msg->addUUID("SessionID", gAgent.getSessionID());
            msg->nextBlock("InventoryData");
            msg->addUUID("ItemID", mTrackedLandmarkItemID);
            msg->addU32("Flags", flags);
            gAgent.sendReliableMessage();

            LLInventoryModel::LLCategoryUpdate up(item->getParentUUID(), 0);
            gInventory.accountForUpdate(up);

            // need to communicate that the icon needs to change...
            gInventory.addChangedMask(LLInventoryObserver::INTERNAL, item->getUUID());
            gInventory.notifyObservers();
        }
    }
}


void LLTracker::cacheLandmarkPosition()
{
    // the landmark asset download may have finished, in which case
    // we'll now be able to figure out where we're trying to go
    bool found_landmark = false;
    if( mTrackedLandmarkAssetID == LLFloaterWorldMap::getHomeID())
    {
        LLVector3d pos_global;
        if ( gAgent.getHomePosGlobal( &mTrackedPositionGlobal ))
        {
            found_landmark = true;
        }
        else
        {
            LL_WARNS() << "LLTracker couldn't find home pos" << LL_ENDL;
            mTrackedLandmarkAssetID.setNull();
            mTrackedLandmarkItemID.setNull();
        }
    }
    else
    {
        LLLandmark* landmark = gLandmarkList.getAsset(mTrackedLandmarkAssetID);
        if(landmark && landmark->getGlobalPos(mTrackedPositionGlobal))
        {
            found_landmark = true;

            // cache the object's visitation status
            mLandmarkHasBeenVisited = false;
            LLInventoryItem* item = gInventory.getItem(mTrackedLandmarkItemID);
            if (   item
                && item->getFlags()&LLInventoryItemFlags::II_FLAGS_LANDMARK_VISITED)
            {
                mLandmarkHasBeenVisited = true;
            }
        }
    }
    if ( found_landmark && gFloaterWorldMap )
    {
        mHasReachedLandmark = false;
        F32 dist = gFloaterWorldMap->getDistanceToDestination(mTrackedPositionGlobal, 1.0f);
        if ( dist < DESTINATION_UNVISITED_RADIUS )
        {
            mHasReachedLandmark = true;
        }
        mHasLandmarkPosition = true;
    }
    mHasLandmarkPosition = found_landmark;
}


void LLTracker::purgeBeaconText()
{
    if(!mBeaconText.isNull())
    {
        mBeaconText->markDead();
        mBeaconText = NULL;
    }
}