source: osgVisual/trunk/src/draw2D/visual_hud.cpp @ 311

Last change on this file since 311 was 221, checked in by Torben Dannhauer, 14 years ago

Updated copyright NOTICE
(No code changes)

File size: 3.7 KB
Line 
1/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 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_hud.h>
18
19using namespace osgVisual;
20
21visual_hud::visual_hud(void)
22{
23}
24
25visual_hud::~visual_hud(void)
26{
27}
28
29bool visual_hud::init( osgViewer::Viewer *viewer_ )
30{       
31    screen_width = viewer_->getCamera()->getViewport()->width();
32    screen_height = viewer_->getCamera()->getViewport()->height();
33       
34        visual_draw2D::getInstance()->addDrawContent( addContent(), "HUD" );
35
36        return true;
37}
38
39osg::ref_ptr<osg::Geode> visual_hud::addContent()
40{
41        osg::ref_ptr<osg::Geode> hudGeode = this;
42
43        // turn lighting off
44    osg::StateSet* stateset = hudGeode->getOrCreateStateSet();
45    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
46
47    // create background
48        {
49                // configuring background shape
50                float depth = -0.1; // Paint background behind text.
51                osg::Vec4f bgColor_rgba = osg::Vec4(1.0f,1.0,0.8f,0.2f);
52                osg::Vec3 hudCenter = osg::Vec3((float)(screen_width/2), (float)(screen_height/2), 0.0);
53                float baseSize = 200;
54                float addSize = 70;
55
56
57                osg::Geometry* hudBackground = new osg::Geometry;
58
59                osg::Vec3Array* vertices = new osg::Vec3Array;
60                vertices->push_back(hudCenter+osg::Vec3(baseSize+addSize, baseSize, depth));
61                vertices->push_back(hudCenter+osg::Vec3(baseSize, baseSize+addSize, depth));
62                vertices->push_back(hudCenter+osg::Vec3(-baseSize , baseSize+addSize, depth));
63                vertices->push_back(hudCenter+osg::Vec3(-baseSize-addSize, baseSize,depth));
64                vertices->push_back(hudCenter+osg::Vec3(-baseSize-addSize, -baseSize,depth));
65                vertices->push_back(hudCenter+osg::Vec3(-baseSize , -baseSize-addSize, depth));
66                vertices->push_back(hudCenter+osg::Vec3(baseSize, -baseSize-addSize, depth));
67                vertices->push_back(hudCenter+osg::Vec3(baseSize+addSize, -baseSize, depth));
68                hudBackground->setVertexArray(vertices);
69               
70                osg::Vec3Array* normals = new osg::Vec3Array;
71                normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
72                hudBackground->setNormalArray(normals);
73                hudBackground->setNormalBinding(osg::Geometry::BIND_OVERALL);
74               
75                osg::Vec4Array* colors = new osg::Vec4Array;
76                colors->push_back( bgColor_rgba );
77                hudBackground->setColorArray(colors);
78                hudBackground->setColorBinding(osg::Geometry::BIND_OVERALL);
79               
80                hudBackground->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON,0,8));
81               
82                osg::StateSet* stateset = hudBackground->getOrCreateStateSet();
83                stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
84                stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
85
86                hudGeode->addDrawable(hudBackground);   
87        }
88   
89        // Add Text:
90        //osg::Vec3 position(550.0f,450.0f,0.0f);
91        osg::Vec3 position(100.0f,100.0f,0.0f);
92        osg::Vec3 delta(0.0f,-120.0f,0.0f);
93        std::string timesFont("fonts/arial.ttf");
94        {
95                osgText::Text* text = new  osgText::Text;
96                hudGeode->addDrawable( text );
97
98                text->setFont(timesFont);
99                text->setPosition(position);
100                text->setText("Head Up Displays\nare simple ;-)");
101
102                position += delta;
103        }
104
105        return hudGeode.get();
106}
Note: See TracBrowser for help on using the repository browser.