1 | |
---|
2 | /* |
---|
3 | HeadsUpDisplay class for orthographic foreground display of text |
---|
4 | |
---|
5 | An OpenSceneGraph viewer for pyVolution SWW files. |
---|
6 | copyright (C) 2004 Geoscience Australia |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | #include <osg/Geode> |
---|
11 | #include <osg/Geometry> |
---|
12 | #include <osg/Group> |
---|
13 | #include <osg/MatrixTransform> |
---|
14 | #include <osg/StateSet> |
---|
15 | #include <hud.h> |
---|
16 | |
---|
17 | #define ORTHO2D_WIDTH 1280 |
---|
18 | #define ORTHO2D_HEIGHT 1024 |
---|
19 | #define DEF_HUD_COLOUR 0.8, 0.8, 0.8, 1.0 |
---|
20 | |
---|
21 | |
---|
22 | // constructor |
---|
23 | HeadsUpDisplay::HeadsUpDisplay() |
---|
24 | { |
---|
25 | // a heads-up display requires an orthographic projection |
---|
26 | _projection = new osg::Projection; |
---|
27 | _projection->setMatrix( osg::Matrix::ortho2D(0,ORTHO2D_WIDTH,0,ORTHO2D_HEIGHT) ); |
---|
28 | |
---|
29 | // font |
---|
30 | osgText::Font* font = osgText::readFontFile("fonts/arial.ttf"); |
---|
31 | |
---|
32 | // title text |
---|
33 | _titletext = new osgText::Text; |
---|
34 | _titletext->setFont(font); |
---|
35 | _titletext->setColor(osg::Vec4(DEF_HUD_COLOUR) ); |
---|
36 | _titletext->setCharacterSize(20); |
---|
37 | _titletext->setPosition(osg::Vec3(20,20,0)); |
---|
38 | _titletext->setFontResolution(40,40); |
---|
39 | _dirtytime = false; |
---|
40 | |
---|
41 | // timer text |
---|
42 | _timetext = new osgText::Text; |
---|
43 | _timetext->setFont(font); |
---|
44 | _timetext->setColor(osg::Vec4(DEF_HUD_COLOUR) ); |
---|
45 | _timetext->setCharacterSize(30); |
---|
46 | _timetext->setPosition(osg::Vec3(1100,20,0)); |
---|
47 | _timetext->setFontResolution(40,40); |
---|
48 | _timevalue = 0.0; |
---|
49 | _dirtytime = true; |
---|
50 | |
---|
51 | // recording mode text |
---|
52 | _modetext = new osgText::Text; |
---|
53 | _modetext->setFont(font); |
---|
54 | _modetext->setColor(osg::Vec4(DEF_HUD_COLOUR) ); |
---|
55 | _modetext->setCharacterSize(30); |
---|
56 | _modetext->setPosition(osg::Vec3(600,20,0)); |
---|
57 | _modetext->setFontResolution(40,40); |
---|
58 | _modestring = std::string(""); |
---|
59 | _dirtymode = true; |
---|
60 | |
---|
61 | |
---|
62 | // state |
---|
63 | osg::StateSet *state = _projection->getOrCreateStateSet(); |
---|
64 | state->setRenderBinDetails(12, "RenderBin"); |
---|
65 | state->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
---|
66 | state->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
67 | |
---|
68 | // scenegraph |
---|
69 | osg::MatrixTransform* xfm = new osg::MatrixTransform; |
---|
70 | xfm->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
---|
71 | xfm->setMatrix(osg::Matrix::identity()); |
---|
72 | osg::Geode* textnode = new osg::Geode; |
---|
73 | textnode->addDrawable( _titletext ); |
---|
74 | textnode->addDrawable( _timetext ); |
---|
75 | textnode->addDrawable( _modetext ); |
---|
76 | xfm->addChild( textnode ); |
---|
77 | _projection->addChild( xfm ); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | HeadsUpDisplay::~HeadsUpDisplay() |
---|
82 | { |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | void HeadsUpDisplay::setTime( float t ) |
---|
88 | { |
---|
89 | if( t != _timevalue ) |
---|
90 | { |
---|
91 | _timevalue = t; |
---|
92 | _dirtytime = true; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | void HeadsUpDisplay::setTitle( char* s ) |
---|
98 | { |
---|
99 | std::string tmp = std::string(s); |
---|
100 | if( tmp != _titlestring ) |
---|
101 | { |
---|
102 | _titlestring = tmp; |
---|
103 | _dirtytitle = true; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | void HeadsUpDisplay::setMode( char* s ) |
---|
109 | { |
---|
110 | std::string tmp = std::string(s); |
---|
111 | if( tmp != _modestring ) |
---|
112 | { |
---|
113 | _modestring = tmp; |
---|
114 | _dirtymode = true; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | void HeadsUpDisplay::update() |
---|
121 | { |
---|
122 | |
---|
123 | if( _dirtytime ) |
---|
124 | { |
---|
125 | char timestr[20]; |
---|
126 | sprintf(timestr, "t = %-7.2f", _timevalue); |
---|
127 | _timetext->setText(timestr); |
---|
128 | |
---|
129 | // hud time now updated ... |
---|
130 | _dirtytime = false; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | if( _dirtymode ) |
---|
135 | { |
---|
136 | _modetext->setText(_modestring.c_str()); |
---|
137 | |
---|
138 | // hud "recording/playback" text now updated ... |
---|
139 | _dirtymode = false; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | if( _dirtytitle ) |
---|
144 | { |
---|
145 | _titletext->setText(_titlestring.c_str()); |
---|
146 | |
---|
147 | // hud title text now updated ... |
---|
148 | _dirtytitle = false; |
---|
149 | } |
---|
150 | } |
---|