root/OpenSceneGraph/trunk/examples/osgfxbrowser/Frame.cpp @ 5187

Revision 5187, 3.1 kB (checked in by robert, 8 years ago)

Replaced ref_ptr<>:take with ref_ptr<>:release, and added handling of removeal
of ref_ptr<> operator >.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include "Frame.h"
2
3#include <osgText/Text>
4
5namespace osgfxbrowser {
6
7Frame::Frame()
8:    osg::Geode(),
9    bgcolor_(0.5f, 0.5f, 0.5f, 1.0f),
10    rect_(0, 0, 100, 100),
11    caption_("Frame")
12{
13}
14
15Frame::Frame(const Frame &copy, const osg::CopyOp &copyop)
16:    osg::Geode(copy, copyop),
17    bgcolor_(copy.bgcolor_),
18    rect_(copy.rect_),
19    caption_(copy.caption_)
20{
21}
22
23void Frame::rebuild()
24{
25    float zPos = -0.1f;
26
27    removeDrawables(0, getNumDrawables());
28    addDrawable(build_quad(rect_, bgcolor_));
29    addDrawable(build_quad(Rect(rect_.x0 + 4, rect_.y1 - 24, rect_.x1 - 4, rect_.y1 - 4), osg::Vec4(0, 0, 0, bgcolor_.w()), false, zPos));
30
31    osg::ref_ptr<osgText::Text> caption_text = new osgText::Text;
32    caption_text->setText(caption_);
33    caption_text->setColor(osg::Vec4(1, 1, 1, 1));
34    caption_text->setAlignment(osgText::Text::CENTER_CENTER);
35    caption_text->setFont("fonts/arial.ttf");
36    caption_text->setCharacterSize(16);
37    caption_text->setFontResolution(16, 16);
38    caption_text->setPosition(osg::Vec3((rect_.x0 + rect_.x1) / 2, rect_.y1 - 15, zPos*2.0f));
39    addDrawable(caption_text.get());
40
41    rebuild_client_area(Rect(rect_.x0 + 4, rect_.y0 + 4, rect_.x1 - 4, rect_.y1 - 28));
42}
43
44osg::Geometry *Frame::build_quad(const Rect &rect, const osg::Vec4 &color, bool shadow, float z)
45{
46    const float shadow_space = 8;
47    const float shadow_size = 10;
48
49    osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
50    osg::ref_ptr<osg::Vec3Array> vx = new osg::Vec3Array;
51
52    vx->push_back(osg::Vec3(rect.x0, rect.y0, z));
53    vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
54    vx->push_back(osg::Vec3(rect.x1, rect.y1, z));
55    vx->push_back(osg::Vec3(rect.x0, rect.y1, z));
56
57    if (shadow) {
58        vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0-shadow_size, z));
59        vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
60        vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
61        vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0, z));
62
63        vx->push_back(osg::Vec3(rect.x1, rect.y1-shadow_space, z));
64        vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
65        vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
66        vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y1-shadow_space, z));
67    }
68
69    geo->setVertexArray(vx.get());
70
71    osg::ref_ptr<osg::Vec4Array> clr = new osg::Vec4Array;
72    clr->push_back(color);
73    clr->push_back(color);
74    clr->push_back(color);
75    clr->push_back(color);
76
77    if (shadow) {
78
79        float alpha = color.w() * 0.5f;
80        const osg::Vec3 black(0, 0, 0);
81
82        clr->push_back(osg::Vec4(black, 0));
83        clr->push_back(osg::Vec4(black, 0));
84        clr->push_back(osg::Vec4(black, alpha));
85        clr->push_back(osg::Vec4(black, alpha));
86
87        clr->push_back(osg::Vec4(black, alpha));
88        clr->push_back(osg::Vec4(black, alpha));
89        clr->push_back(osg::Vec4(black, 0));
90        clr->push_back(osg::Vec4(black, 0));
91    }
92
93    geo->setColorArray(clr.get());
94    geo->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
95
96    geo->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, shadow? 12: 4));
97
98    return geo.release();
99}
100
101}
Note: See TracBrowser for help on using the browser.