1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
2 | * |
---|
3 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
5 | * (at your option) any later version. The full license is in LICENSE file |
---|
6 | * included with this distribution, and on the openscenegraph.org website. |
---|
7 | * |
---|
8 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
9 | * You have to aquire licenses for all used proprietary modules. |
---|
10 | * |
---|
11 | * This library is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * OpenSceneGraph Public License for more details. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <visual_debug_hud.h> |
---|
18 | |
---|
19 | using namespace osgVisual; |
---|
20 | |
---|
21 | visual_debug_hud::visual_debug_hud(void) |
---|
22 | { |
---|
23 | |
---|
24 | } |
---|
25 | |
---|
26 | visual_debug_hud::~visual_debug_hud(void) |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | bool visual_debug_hud::init( osgViewer::Viewer *viewer_, osg::CoordinateSystemNode* rootNode_ ) |
---|
31 | { |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | screen_width = viewer_->getCamera()->getViewport()->width(); |
---|
36 | screen_height = viewer_->getCamera()->getViewport()->height(); |
---|
37 | |
---|
38 | visual_draw2D::getInstance()->addDrawContent( addContent(), "HUD" ); |
---|
39 | |
---|
40 | // Set callback. |
---|
41 | updateCallback = new HudUpdateCallback( rootNode_, viewer_->getCamera() ); |
---|
42 | this->setEventCallback( updateCallback ); |
---|
43 | |
---|
44 | isInitialized = true; |
---|
45 | |
---|
46 | return true; |
---|
47 | } |
---|
48 | |
---|
49 | void visual_debug_hud::shutdown() |
---|
50 | { |
---|
51 | if (isInitialized) |
---|
52 | { |
---|
53 | // Remove updatecallback |
---|
54 | this->removeUpdateCallback( updateCallback ); |
---|
55 | updateCallback = NULL; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | osg::ref_ptr<osg::Geode> visual_debug_hud::addContent() |
---|
60 | { |
---|
61 | osg::ref_ptr<osg::Geode> hudGeode = this; |
---|
62 | |
---|
63 | // turn lighting off |
---|
64 | osg::ref_ptr<osg::StateSet> stateset = hudGeode->getOrCreateStateSet(); |
---|
65 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | // Add Text: |
---|
70 | osg::Vec3 position(screen_width-180.0f,120.0f,0.0f); |
---|
71 | osg::Vec3 delta(0.0f,-25.0f,0.0f); |
---|
72 | std::string timesFont("fonts/arial.ttf"); |
---|
73 | { |
---|
74 | textLat = new osgText::Text; |
---|
75 | hudGeode->addDrawable( textLat ); |
---|
76 | textLat->setFont(timesFont); |
---|
77 | textLat->setPosition(position); |
---|
78 | textLat->setCharacterSize(21); |
---|
79 | textLat->setDataVariance(osg::Object::DYNAMIC); |
---|
80 | |
---|
81 | position += delta; |
---|
82 | |
---|
83 | textLon = new osgText::Text; |
---|
84 | hudGeode->addDrawable( textLon ); |
---|
85 | textLon->setFont(timesFont); |
---|
86 | textLon->setPosition(position); |
---|
87 | textLon->setCharacterSize(21); |
---|
88 | textLon->setDataVariance(osg::Object::DYNAMIC); |
---|
89 | |
---|
90 | position += delta; |
---|
91 | |
---|
92 | textAlt = new osgText::Text; |
---|
93 | hudGeode->addDrawable( textAlt ); |
---|
94 | textAlt->setFont(timesFont); |
---|
95 | textAlt->setPosition(position); |
---|
96 | textAlt->setCharacterSize(21); |
---|
97 | textAlt->setDataVariance(osg::Object::DYNAMIC); |
---|
98 | |
---|
99 | position += delta; |
---|
100 | |
---|
101 | textHat = new osgText::Text; |
---|
102 | hudGeode->addDrawable( textHat ); |
---|
103 | textHat->setFont(timesFont); |
---|
104 | textHat->setPosition(position); |
---|
105 | textHat->setCharacterSize(21); |
---|
106 | textHat->setDataVariance(osg::Object::DYNAMIC); |
---|
107 | |
---|
108 | position += delta; |
---|
109 | |
---|
110 | textHot = new osgText::Text; |
---|
111 | hudGeode->addDrawable( textHot ); |
---|
112 | textHot->setFont(timesFont); |
---|
113 | textHot->setPosition(position); |
---|
114 | textHot->setCharacterSize(21); |
---|
115 | textHot->setDataVariance(osg::Object::DYNAMIC); |
---|
116 | |
---|
117 | position += delta; |
---|
118 | } |
---|
119 | |
---|
120 | return hudGeode.get(); |
---|
121 | } |
---|
122 | |
---|
123 | void visual_debug_hud::HudUpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
124 | { |
---|
125 | visual_debug_hud* hud = dynamic_cast<visual_debug_hud*>(node); |
---|
126 | if ( !hud ) |
---|
127 | { |
---|
128 | OSG_NOTIFY(osg::FATAL) << "ERROR : No object found. Unable to apply this callback!" << std::endl; |
---|
129 | return; |
---|
130 | } |
---|
131 | |
---|
132 | // Retrieving new HUD values |
---|
133 | |
---|
134 | double lat = 0; |
---|
135 | double lon = 0; |
---|
136 | double hat = 0; |
---|
137 | double hot = 0; |
---|
138 | double alt = 0; |
---|
139 | |
---|
140 | util::getWGS84ofCamera( sceneCamera, csn, lat, lon, alt ); |
---|
141 | util::queryHeightAboveTerrainInWGS84(hat, csn, lat, lon, alt); |
---|
142 | util::queryHeightOfTerrain(hot, csn, lat, lon); |
---|
143 | |
---|
144 | |
---|
145 | // Updating Display Elements: |
---|
146 | std::ostringstream valuestring; |
---|
147 | |
---|
148 | valuestring.str(""); |
---|
149 | valuestring << osg::RadiansToDegrees( lat ); |
---|
150 | hud->textLat->setText("LAT: "+valuestring.str()); |
---|
151 | |
---|
152 | valuestring.str(""); |
---|
153 | valuestring << osg::RadiansToDegrees( lon ); |
---|
154 | hud->textLon->setText("LON: "+valuestring.str()); |
---|
155 | |
---|
156 | valuestring << std::fixed; |
---|
157 | valuestring.precision(2); |
---|
158 | valuestring.fill('0'); |
---|
159 | |
---|
160 | valuestring.width(8); |
---|
161 | valuestring.str(""); |
---|
162 | valuestring << alt; |
---|
163 | hud->textAlt->setText("ALT: "+valuestring.str()); |
---|
164 | |
---|
165 | |
---|
166 | valuestring.width(8); |
---|
167 | valuestring.str(""); |
---|
168 | valuestring << hat; |
---|
169 | hud->textHat->setText("HAT: "+valuestring.str()); |
---|
170 | |
---|
171 | valuestring.width(8); |
---|
172 | valuestring.str(""); |
---|
173 | valuestring << hot; |
---|
174 | hud->textHot->setText("HOT: "+valuestring.str()); |
---|
175 | } |
---|