1 // Window for placing a Canvas onto it (for dialogs, menus, etc.)
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <Canvas/canvas.hxx>
22 #include <osg/BlendFunc>
23 #include <osg/Geometry>
24 #include <osg/Texture2D>
25 #include <osgGA/GUIEventHandler>
28 * Callback to enable/disable rendering of canvas displayed inside windows
31 public osg::Drawable::CullCallback
34 CullCallback(Canvas::CameraCullCallback* camera_cull);
37 Canvas::CameraCullCallback *_camera_cull;
39 virtual bool cull( osg::NodeVisitor* nv,
40 osg::Drawable* drawable,
41 osg::RenderInfo* renderInfo ) const;
44 //------------------------------------------------------------------------------
45 CullCallback::CullCallback(Canvas::CameraCullCallback* camera_cull):
46 _camera_cull( camera_cull )
51 //------------------------------------------------------------------------------
52 bool CullCallback::cull( osg::NodeVisitor* nv,
53 osg::Drawable* drawable,
54 osg::RenderInfo* renderInfo ) const
56 _camera_cull->enableRendering();
62 //----------------------------------------------------------------------------
63 Window::Window(SGPropertyNode* node):
64 PropertyBasedElement(node),
66 _geometry( new osg::Geometry ),
67 _vertices( new osg::Vec3Array(4) ),
68 _tex_coords( new osg::Vec2Array(4) ),
71 _width(node, "size[0]"),
72 _height(node, "size[1]")
79 _geometry->setVertexArray(_vertices);
80 _geometry->setTexCoordArray(0,_tex_coords);
82 osg::Vec4Array* colors = new osg::Vec4Array(1);
83 (*colors)[0].set(1.0f,1.0f,1.0,1.0f);
84 _geometry->setColorArray(colors);
85 _geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
87 _geometry->addPrimitiveSet(
88 new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)
90 _geometry->setDataVariance(osg::Object::DYNAMIC);
92 osg::StateSet* stateSet = _geometry->getOrCreateStateSet();
93 stateSet->setRenderBinDetails(1000, "RenderBin");
95 // speed optimization?
96 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
97 stateSet->setAttribute(new osg::BlendFunc(
98 osg::BlendFunc::SRC_ALPHA,
99 osg::BlendFunc::ONE_MINUS_SRC_ALPHA)
101 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
102 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
103 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
104 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
105 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
108 //----------------------------------------------------------------------------
114 //----------------------------------------------------------------------------
115 void Window::update(double delta_time_sec)
121 _region.set(_x, _y, _width, _height);
123 int z = 0; // TODO do we need to use z for depth ordering?
125 (*_vertices)[0].set(_region.l(), _region.t(), z);
126 (*_vertices)[1].set(_region.r(), _region.t(), z);
127 (*_vertices)[2].set(_region.r(), _region.b(), z);
128 (*_vertices)[3].set(_region.l(), _region.b(), z);
130 float l = 0, t = 1, b = 0, r = 1;
131 (*_tex_coords)[0].set(l,t);
132 (*_tex_coords)[1].set(r,t);
133 (*_tex_coords)[2].set(r,b);
134 (*_tex_coords)[3].set(l,b);
136 _geometry->dirtyDisplayList();
139 //----------------------------------------------------------------------------
140 void Window::valueChanged (SGPropertyNode * node)
142 if( node->getParent() != _node )
145 const std::string& name = node->getNameString();
146 if( name == "x" || name == "y" || name == "size" )
150 //----------------------------------------------------------------------------
151 void Window::setCanvas(CanvasPtr canvas)
154 _geometry->getOrCreateStateSet()
155 ->setTextureAttribute(0, canvas ? canvas->getTexture() : 0);
156 _geometry->dirtyDisplayList();
157 _geometry->setCullCallback(
158 canvas ? new CullCallback(canvas->getCameraCullCallback()) : 0
162 //----------------------------------------------------------------------------
163 CanvasWeakPtr Window::getCanvas() const
168 //----------------------------------------------------------------------------
169 bool Window::handleMouseEvent(const MouseEvent& event)
171 if( !_canvas.expired() )
172 return _canvas.lock()->handleMouseEvent(event);
177 } // namespace canvas