Equalizer
1.2.1
|
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 "node.h" 00034 00035 #include "config.h" 00036 #include "sceneReader.h" 00037 #include "quad.h" 00038 00039 #include <osg/MatrixTransform> 00040 #include <osg/Texture2D> 00041 #include <osgUtil/UpdateVisitor> 00042 00043 namespace osgScaleViewer 00044 { 00045 00046 Node::Node( eq::Config* parent ) 00047 : eq::Node( parent ) 00048 { 00049 } 00050 00051 bool Node::configInit( const eq::uint128_t& initID ) 00052 { 00053 if( !eq::Node::configInit( initID )) 00054 return false; 00055 00056 Config* config = static_cast<Config*>( getConfig( )); 00057 if( !config->mapData( initID )) 00058 return false; 00059 00060 _frameStamp = new osg::FrameStamp; 00061 _updateVisitor = new osgUtil::UpdateVisitor; 00062 _updateVisitor->setFrameStamp( _frameStamp ); 00063 _contextID = 0; 00064 00065 // load model at first config run 00066 if( !_model ) 00067 { 00068 const InitData& initData = config->getInitData(); 00069 const std::string& modelFile = initData.getModelFileName(); 00070 if( !modelFile.empty( )) 00071 { 00072 SceneReader sceneReader; 00073 _model = sceneReader.readModel( modelFile ); 00074 00075 if( _model.valid( )) 00076 { 00077 osg::Matrix matrix; 00078 matrix.makeRotate( -osg::PI_2, osg::Vec3( 1., 0., 0. )); 00079 00080 osg::ref_ptr<osg::MatrixTransform> transform = 00081 new osg::MatrixTransform(); 00082 transform->setMatrix( matrix ); 00083 transform->addChild( _model ); 00084 transform->setDataVariance( osg::Object::STATIC ); 00085 00086 _model = transform; 00087 } 00088 } 00089 } 00090 00091 if( !_model ) 00092 { 00093 const InitData& initData = config->getInitData(); 00094 std::string imageFile = initData.getImageFileName(); 00095 if( !imageFile.empty( )) 00096 { 00097 SceneReader sceneReader; 00098 osg::ref_ptr<osg::Image> image = sceneReader.readImage( imageFile ); 00099 if( image.valid( )) 00100 _model = _createSceneGraph( image ); 00101 } 00102 } 00103 00104 if( !_model ) 00105 _model = _createSceneGraph(); 00106 return true; 00107 } 00108 00109 bool Node::configExit() 00110 { 00111 _contextID = 0; 00112 _frameStamp = 0; 00113 _updateVisitor = 0; 00114 return eq::Node::configExit(); 00115 } 00116 00117 00118 void Node::frameStart( const eq::uint128_t& frameID, 00119 const uint32_t frameNumber ) 00120 { 00121 _frameStamp->setFrameNumber( frameNumber ); 00122 00123 // TODO use global time saved in FrameData, use one FrameData per node 00124 const double time = static_cast< double >( getConfig()->getTime( )) / 1000.; 00125 _frameStamp->setReferenceTime( time ); 00126 _frameStamp->setSimulationTime( time ); 00127 _updateVisitor->setTraversalNumber( frameNumber ); 00128 _model->accept( *_updateVisitor ); 00129 _model->getBound(); 00130 00131 eq::Node::frameStart( frameID, frameNumber ); 00132 } 00133 00134 osg::ref_ptr< osg::Node > Node::_createSceneGraph() 00135 { 00136 // init scene graph 00137 osg::ref_ptr<osg::Group> root = _initSceneGraph(); 00138 00139 // draw a red quad 00140 Quad quad; 00141 osg::ref_ptr<osg::Node> geometryChild = quad.createQuad(); 00142 root->addChild( geometryChild ); 00143 00144 return root.get(); 00145 } 00146 00147 osg::ref_ptr< osg::Node > Node::_createSceneGraph( 00148 osg::ref_ptr< osg::Image > image ) 00149 { 00150 // init scene graph 00151 osg::ref_ptr<osg::Group> root = _initSceneGraph(); 00152 00153 // det the image as a texture 00154 osg::Texture2D* texture = new osg::Texture2D(); 00155 texture->setImage( image ); 00156 00157 // draw a textured quad 00158 Quad quad; 00159 osg::ref_ptr<osg::Node> geometryChild = quad.createQuad( image->s(), 00160 image->t( )); 00161 00162 osg::StateSet* stateOne = new osg::StateSet(); 00163 stateOne->setTextureAttributeAndModes( 0, texture, osg::StateAttribute::ON); 00164 geometryChild->setStateSet( stateOne ); 00165 00166 root->addChild( geometryChild ); 00167 00168 return root.get(); 00169 } 00170 00171 osg::ref_ptr< osg::Group > Node::_initSceneGraph() 00172 { 00173 osg::ref_ptr<osg::Group> root = new osg::Group(); 00174 root->setDataVariance( osg::Object::STATIC ); 00175 00176 // enable lighting and LIGHT0 in root node 00177 osg::StateSet* state = root->getOrCreateStateSet(); 00178 state->setMode( GL_LIGHTING, osg::StateAttribute::ON ); 00179 state->setMode( GL_LIGHT0, osg::StateAttribute::ON ); 00180 00181 // lightsource 00182 osg::ref_ptr<osg::LightSource> ls0 = _createLightSource(); 00183 00184 // translation of lightsource (with light0) 00185 osg::Matrix matrix; 00186 osg::ref_ptr<osg::MatrixTransform> lightTranslateNode = 00187 new osg::MatrixTransform(); 00188 00189 matrix.makeTranslate( 0.f, -5.f, 0.f ); 00190 lightTranslateNode->setMatrix( matrix ); 00191 lightTranslateNode->addChild( ls0.get( )); 00192 lightTranslateNode->setDataVariance( osg::Object::STATIC ); 00193 00194 root->addChild( lightTranslateNode.get( )); 00195 00196 return root; 00197 } 00198 00199 osg::ref_ptr< osg::LightSource > Node::_createLightSource() 00200 { 00201 osg::ref_ptr<osg::Light> light0 = new osg::Light(); 00202 light0->setLightNum( 0 ); //light0 is now GL-Light0 00203 light0->setPosition( osg::Vec4( 0.f, -50.f, 0.f, 1.f )); 00204 light0->setAmbient( osg::Vec4( 1.0f, 1.0f, 1.0f, 0.0f )); 00205 light0->setDiffuse( osg::Vec4( 1.0f, 1.0f, 1.0f, 0.0f )); 00206 light0->setDirection( osg::Vec3( 0.f, 1.f, 0.f )); 00207 light0->setSpotCutoff( 30.f ); 00208 light0->setSpotExponent( 50.0f ); 00209 00210 osg::ref_ptr< osg::LightSource > ls0 = new osg::LightSource(); 00211 ls0->setLight( light0.get( )); 00212 ls0->setDataVariance( osg::Object::STATIC ); 00213 00214 return ls0; 00215 } 00216 00217 00218 }