Equalizer  1.2.1
quad.cpp
00001 
00002 /*
00003  * Copyright (c)
00004  *   2008-2009, Thomas McGuire <thomas.mcguire@student.uni-siegen.de>
00005  *   2010, Stefan Eilemann <eile@eyescale.ch>
00006  *   2010, Sarah Amsellem <sarah.amsellem@gmail.com>
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice, this
00012  *   list of conditions and the following disclaimer.
00013  * - Redistributions in binary form must reproduce the above copyright notice,
00014  *   this list of conditions and the following disclaimer in the documentation
00015  *   and/or other materials provided with the distribution.
00016  * - Neither the name of Eyescale Software GmbH nor the names of its
00017  *   contributors may be used to endorse or promote products derived from this
00018  *   software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #include "quad.h"
00034 
00035 #include <osg/Geode>
00036 #include <osg/Geometry>
00037 
00038 namespace osgScaleViewer
00039 {
00040 osg::ref_ptr<osg::Node> Quad::createQuad() const
00041 {
00042     osg::ref_ptr<osg::Geode> geode= new osg::Geode();
00043 
00044     geode->addDrawable( createDrawable().get( ));
00045     geode->setDataVariance( osg::Object::STATIC );
00046 
00047     return geode.get();
00048 }
00049 
00050 osg::ref_ptr<osg::Node> Quad::createQuad( int width, int height ) const
00051 {
00052     osg::ref_ptr<osg::Geode> geode= new osg::Geode();
00053 
00054     geode->addDrawable( createDrawable( width, height ).get( ));
00055     geode->setDataVariance( osg::Object::STATIC );
00056 
00057     return geode.get();
00058 }
00059 
00060 osg::ref_ptr<osg::Drawable> Quad::createDrawable( int width, int height ) const
00061 {
00062     osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
00063 
00064     // vertices
00065     float heightf = 5.f;
00066     float widthf =  heightf * width / static_cast<float>( height );
00067     
00068     osg::ref_ptr<osg::Vec3Array> data = new osg::Vec3Array();
00069     data->push_back( osg::Vec3(  widthf/2.f, -heightf/2.f, 0.f ));
00070     data->push_back( osg::Vec3(  widthf/2.f,  heightf/2.f, 0.f ));
00071     data->push_back( osg::Vec3( -widthf/2.f,  heightf/2.f, 0.f ));
00072     data->push_back( osg::Vec3( -widthf/2.f, -heightf/2.f, 0.f ));
00073 
00074     // color
00075     osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array();
00076     color->push_back( osg::Vec4( 1.f, 1.f, 1.f, 1.f ));
00077 
00078     // normals
00079     osg::ref_ptr<osg::Vec3Array> normals= new osg::Vec3Array();
00080     normals->push_back( osg::Vec3( 0.f, -1.f, 0.f ));
00081 
00082     // texture coordinates
00083     osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(4);
00084     (*texcoords)[0].set( 1.f, 0.f );
00085     (*texcoords)[1].set( 1.f, 1.f );
00086     (*texcoords)[2].set( 0.f, 1.f );
00087     (*texcoords)[3].set( 0.f, 0.f );
00088 
00089     // assign data
00090     geom->setVertexArray( data.get( ));
00091     geom->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ));
00092 
00093     geom->setColorBinding( osg::Geometry::BIND_OVERALL );
00094     geom->setColorArray( color.get( ));
00095 
00096     geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
00097     geom->setNormalArray( normals.get( ));
00098 
00099     geom->setTexCoordArray( 0, texcoords );
00100     geom->setDataVariance( osg::Object::STATIC );
00101 
00102     return geom.get();
00103 }
00104 
00105 osg::ref_ptr<osg::Drawable> Quad::createDrawable() const
00106 {
00107     osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
00108 
00109     // vertices
00110     osg::ref_ptr<osg::Vec3Array> data = new osg::Vec3Array();
00111     data->push_back( osg::Vec3(  1.f, -1.f, 0.f ));
00112     data->push_back( osg::Vec3(  1.f,  1.f, 0.f ));
00113     data->push_back( osg::Vec3( -1.f,  1.f, 0.f ));
00114     data->push_back( osg::Vec3( -1.f, -1.f, 0.f ));
00115 
00116     // color
00117     osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array();
00118     color->push_back( osg::Vec4( 1.f, 0.f, 0.f, 0.f ));
00119 
00120     // normals
00121     osg::ref_ptr<osg::Vec3Array> normals= new osg::Vec3Array();
00122     normals->push_back( osg::Vec3( 0.f, -1.f, 0.f ));
00123 
00124     // assign data
00125     geom->setVertexArray( data.get( ));
00126     geom->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ));
00127 
00128     geom->setColorBinding( osg::Geometry::BIND_OVERALL );
00129     geom->setColorArray( color.get( ));
00130 
00131     geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
00132     geom->setNormalArray( normals.get( ));
00133 
00134     return geom.get();
00135 }
00136 
00137 }
Generated on Fri Jun 8 2012 15:44:32 for Equalizer 1.2.1 by  doxygen 1.8.0