[31] | 1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
| 2 | * |
---|
| 3 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
---|
| 7 | * |
---|
| 8 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
| 9 | * You have to aquire licenses for all used proprietary modules. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * OpenSceneGraph Public License for more details. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #include "visual_vista2D.h" |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | using namespace osgVisual; |
---|
| 21 | |
---|
| 22 | visual_vista2D::visual_vista2D(void) |
---|
| 23 | { |
---|
| 24 | // Create a Vista2D View |
---|
| 25 | view = new Vista2D::VistaView(); |
---|
[153] | 26 | |
---|
| 27 | filename = ""; |
---|
| 28 | paintBackground = false;; |
---|
| 29 | position_x = 600; |
---|
| 30 | position_y = 400; |
---|
| 31 | zoom = 0.5; |
---|
[31] | 32 | } |
---|
| 33 | |
---|
| 34 | visual_vista2D::~visual_vista2D(void) |
---|
| 35 | { |
---|
| 36 | } |
---|
| 37 | |
---|
[153] | 38 | void visual_vista2D::startVista2D(std::string vista2DFilename_) |
---|
| 39 | { |
---|
[31] | 40 | /*************** |
---|
| 41 | load view |
---|
| 42 | ***************/ |
---|
[153] | 43 | view->load( filename ); |
---|
| 44 | view->setBackgroundMode(paintBackground); // don't paint background |
---|
| 45 | view->setPosition( position_x,position_y); |
---|
| 46 | view->setZoom( zoom ); |
---|
[31] | 47 | |
---|
| 48 | /*************** |
---|
| 49 | start animation with |
---|
| 50 | self running datasource |
---|
| 51 | ***************/ |
---|
| 52 | view->startView (true); |
---|
| 53 | |
---|
| 54 | } |
---|
| 55 | |
---|
[153] | 56 | bool visual_vista2D::processXMLConfiguration() |
---|
[31] | 57 | { |
---|
[153] | 58 | // Init XML |
---|
| 59 | xmlDoc* tmpDoc; |
---|
| 60 | bool disabled; |
---|
| 61 | xmlNode* config = util::getModuleXMLConfig( configFileName, "vista2D", tmpDoc, disabled ); |
---|
| 62 | |
---|
| 63 | if( disabled) |
---|
| 64 | { |
---|
| 65 | OSG_NOTIFY( osg::ALWAYS ) << "..disabled by XML configuration file." << std::endl; |
---|
| 66 | return false; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | // extract configuration values |
---|
| 70 | if(config) |
---|
| 71 | { |
---|
| 72 | xmlNode* a_node = config->children; |
---|
| 73 | |
---|
| 74 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
| 75 | { |
---|
| 76 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
| 77 | //OSG_ALWAYS << "----visual_vista2D::processXMLConfiguration() - node type="<< cur_node->type <<", name=" << cur_node->name << std::endl; |
---|
| 78 | |
---|
| 79 | // Check for vista2d node |
---|
| 80 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "vista2d") |
---|
| 81 | { |
---|
| 82 | // Check attributes |
---|
| 83 | xmlAttr *attr = cur_node->properties; |
---|
| 84 | while ( attr ) |
---|
| 85 | { |
---|
| 86 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 87 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 88 | |
---|
| 89 | if( attr_name == "filename" ) |
---|
| 90 | { |
---|
| 91 | filename=attr_value |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | if( attr_name == "paintBackground" ) |
---|
| 95 | { |
---|
| 96 | paintBackground = (attr_value == "yes") ? true : false; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | if( attr_name == "position_x" ) |
---|
| 100 | { |
---|
| 101 | std::stringstream sstr(attr_value); |
---|
| 102 | sstr >> position_x; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | if( attr_name == "position_y" ) |
---|
| 106 | { |
---|
| 107 | std::stringstream sstr(attr_value); |
---|
| 108 | sstr >> position_y; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | if( attr_name == "zoom" ) |
---|
| 112 | { |
---|
| 113 | std::stringstream sstr(attr_value); |
---|
| 114 | sstr >> zoom; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | attr = attr->next; |
---|
| 118 | } // WHILE attr END |
---|
| 119 | } // IF node == vista2d END |
---|
| 120 | } // FOR end |
---|
| 121 | |
---|
| 122 | // clean up |
---|
| 123 | xmlFreeDoc(tmpDoc); xmlCleanupParser(); |
---|
| 124 | return true; |
---|
| 125 | } // IF Config valid END |
---|
| 126 | else |
---|
| 127 | { |
---|
| 128 | OSG_WARN << "ERROR: visual_vista2D::processXMLConfiguration() - Module configuration not found" << std::endl; |
---|
| 129 | return false; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return true; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | bool visual_vista2D::init( osg::CoordinateSystemNode *sceneGraphRoot_, std::string configFileName ) |
---|
| 136 | { |
---|
| 137 | OSG_NOTIFY (osg::ALWAYS ) << "visual_vista2D initialize.."; // The sentence is finished by the init result... |
---|
| 138 | |
---|
| 139 | this->configFileName = configFileName; |
---|
| 140 | |
---|
| 141 | // Analyse XML configuration file |
---|
| 142 | // Process XML configuration |
---|
| 143 | if(!processXMLConfiguration()) |
---|
| 144 | return false; // Abort vista2D initialization. |
---|
| 145 | |
---|
[31] | 146 | // Check if Vista2D file exist |
---|
[153] | 147 | if (! osgDB::fileExists( filename ) ) |
---|
[31] | 148 | { |
---|
[153] | 149 | osg::notify( osg::WARN ) << "WARNING: visual_vista2D::createVistaOverlay - Could not find specified Vista2D projectfile. Skip adding Vista2D project." << std::endl; |
---|
[31] | 150 | return false; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | osg::Geode* vista2Dgeode = new osg::Geode(); |
---|
| 154 | // Projection node for defining view frustrum for the vista2D-SceneOverlay: |
---|
| 155 | osg::Projection* vista2DProjectionMatrix = new osg::Projection; |
---|
| 156 | |
---|
| 157 | // Initialize the projection matrix for viewing everything we |
---|
| 158 | // will add as descendants of this node. Use screen coordinates |
---|
| 159 | // to define the horizontal and vertical extent of the projection |
---|
| 160 | // matrix. Positions described under this node will equate to |
---|
| 161 | // pixel coordinates. |
---|
[145] | 162 | vista2DProjectionMatrix->setMatrix(osg::Matrix::ortho2D(0,1600,0,900)); /** todo: use screen size dynamically. */ |
---|
[31] | 163 | |
---|
| 164 | // For the HUD model view matrix use an identity matrix: |
---|
| 165 | osg::MatrixTransform* vista2DModelViewMatrix = new osg::MatrixTransform; |
---|
| 166 | vista2DModelViewMatrix->setMatrix(osg::Matrix::identity()); |
---|
| 167 | |
---|
| 168 | // Make sure the model view matrix is not affected by any transforms |
---|
| 169 | // above it in the scene graph: |
---|
| 170 | vista2DModelViewMatrix->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
---|
| 171 | |
---|
| 172 | // Set Stateset of vista2DGeode Renderbin to render quite late.. |
---|
| 173 | osg::StateSet* vista2DoverlayStateSet = vista2Dgeode->getOrCreateStateSet(); |
---|
| 174 | vista2DoverlayStateSet->setRenderBinDetails( 99, "RenderBin"); |
---|
| 175 | |
---|
| 176 | // Add the HUD projection matrix as a child of the root node |
---|
| 177 | // and the HUD model view matrix as a child of the projection matrix |
---|
| 178 | // Anything under this node will be viewed using this projection matrix |
---|
| 179 | // and positioned with this model view matrix. |
---|
| 180 | vista2DProjectionMatrix->addChild(vista2DModelViewMatrix); |
---|
| 181 | |
---|
| 182 | // Add the Geometry node to contain HUD geometry as a child of the |
---|
| 183 | // HUD model view matrix. |
---|
| 184 | vista2DModelViewMatrix->addChild( vista2Dgeode ); |
---|
| 185 | |
---|
| 186 | // Add Payload: Vista2D |
---|
| 187 | osg::ref_ptr<visual_vista2D> vista2D = new visual_vista2D(); |
---|
[153] | 188 | vista2D->startVista2D( vista2DFilename_ ); |
---|
[31] | 189 | |
---|
| 190 | vista2D.get()->setUseDisplayList( false ); |
---|
| 191 | vista2Dgeode->addDrawable( vista2D.get() ); |
---|
| 192 | |
---|
| 193 | sceneGraphRoot_->addChild( vista2DProjectionMatrix ); |
---|
| 194 | return true; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | void visual_vista2D::drawImplementation(osg::RenderInfo& renderInfo) const |
---|
| 198 | { |
---|
| 199 | // save OSG-State |
---|
| 200 | osg::ref_ptr<osg::StateSet> clonedStateSet = reinterpret_cast<osg::StateSet*>( renderInfo.getCurrentCamera()->getOrCreateStateSet()->clone(osg::CopyOp::DEEP_COPY_ALL) ); |
---|
| 201 | |
---|
| 202 | // Draw View |
---|
| 203 | view->draw(); |
---|
| 204 | |
---|
| 205 | // restore OSG-State |
---|
| 206 | renderInfo.getCurrentCamera()->setStateSet( clonedStateSet ); |
---|
| 207 | |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | osg::Object* visual_vista2D::cloneType() const |
---|
| 211 | { |
---|
| 212 | return NULL; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | osg::Object* visual_vista2D::clone(const osg::CopyOp& copyop) const |
---|
| 216 | { |
---|
| 217 | return NULL; |
---|
| 218 | } |
---|