1 /* OpenSceneGraph example, osgfxbrowser.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16 * THE SOFTWARE.
17 */
18
19 #include "Frame.h"
20
21 #include <osgText/Text>
22
23 namespace osgfxbrowser {
24
Frame()25 Frame::Frame()
26 : osg::Geode(),
27 bgcolor_(0.5f, 0.5f, 0.5f, 1.0f),
28 rect_(0, 0, 100, 100),
29 caption_("Frame")
30 {
31 }
32
Frame(const Frame & copy,const osg::CopyOp & copyop)33 Frame::Frame(const Frame ©, const osg::CopyOp ©op)
34 : osg::Geode(copy, copyop),
35 bgcolor_(copy.bgcolor_),
36 rect_(copy.rect_),
37 caption_(copy.caption_)
38 {
39 }
40
rebuild()41 void Frame::rebuild()
42 {
43 float zPos = -0.1f;
44
45 removeDrawables(0, getNumDrawables());
46 addDrawable(build_quad(rect_, bgcolor_));
47 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));
48
49 osg::ref_ptr<osgText::Text> caption_text = new osgText::Text;
50 caption_text->setText(caption_);
51 caption_text->setColor(osg::Vec4(1, 1, 1, 1));
52 caption_text->setAlignment(osgText::Text::CENTER_CENTER);
53 caption_text->setFont("fonts/arial.ttf");
54 caption_text->setCharacterSize(16);
55 caption_text->setFontResolution(16, 16);
56 caption_text->setPosition(osg::Vec3((rect_.x0 + rect_.x1) / 2, rect_.y1 - 15, zPos*2.0f));
57 addDrawable(caption_text.get());
58
59 rebuild_client_area(Rect(rect_.x0 + 4, rect_.y0 + 4, rect_.x1 - 4, rect_.y1 - 28));
60 }
61
build_quad(const Rect & rect,const osg::Vec4 & color,bool shadow,float z)62 osg::Geometry *Frame::build_quad(const Rect &rect, const osg::Vec4 &color, bool shadow, float z)
63 {
64 const float shadow_space = 8;
65 const float shadow_size = 10;
66
67 osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
68 osg::ref_ptr<osg::Vec3Array> vx = new osg::Vec3Array;
69
70 vx->push_back(osg::Vec3(rect.x0, rect.y0, z));
71 vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
72 vx->push_back(osg::Vec3(rect.x1, rect.y1, z));
73 vx->push_back(osg::Vec3(rect.x0, rect.y1, z));
74
75 if (shadow) {
76 vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0-shadow_size, z));
77 vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
78 vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
79 vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0, z));
80
81 vx->push_back(osg::Vec3(rect.x1, rect.y1-shadow_space, z));
82 vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
83 vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
84 vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y1-shadow_space, z));
85 }
86
87 geo->setVertexArray(vx.get());
88
89 osg::ref_ptr<osg::Vec4Array> clr = new osg::Vec4Array;
90 clr->push_back(color);
91 clr->push_back(color);
92 clr->push_back(color);
93 clr->push_back(color);
94
95 if (shadow) {
96
97 float alpha = color.w() * 0.5f;
98 const osg::Vec3 black(0, 0, 0);
99
100 clr->push_back(osg::Vec4(black, 0));
101 clr->push_back(osg::Vec4(black, 0));
102 clr->push_back(osg::Vec4(black, alpha));
103 clr->push_back(osg::Vec4(black, alpha));
104
105 clr->push_back(osg::Vec4(black, alpha));
106 clr->push_back(osg::Vec4(black, alpha));
107 clr->push_back(osg::Vec4(black, 0));
108 clr->push_back(osg::Vec4(black, 0));
109 }
110
111 geo->setColorArray(clr.get(), osg::Array::BIND_PER_VERTEX);
112
113 geo->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, shadow? 12: 4));
114
115 return geo.release();
116 }
117
118 }
119