[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 | { |
---|
[155] | 24 | OSG_NOTIFY (osg::ALWAYS ) << "visual_vista2D instantiated." << std::endl; |
---|
| 25 | |
---|
[31] | 26 | // Create a Vista2D View |
---|
| 27 | view = new Vista2D::VistaView(); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | visual_vista2D::~visual_vista2D(void) |
---|
| 31 | { |
---|
| 32 | } |
---|
| 33 | |
---|
[155] | 34 | void visual_vista2D::startVista2D(std::string vistaProjectfile, bool paintBackground, int posX, int posY, float zoom) |
---|
[153] | 35 | { |
---|
[31] | 36 | /*************** |
---|
| 37 | load view |
---|
| 38 | ***************/ |
---|
[155] | 39 | view->load( vistaProjectfile ); |
---|
[153] | 40 | view->setBackgroundMode(paintBackground); // don't paint background |
---|
[155] | 41 | view->setPosition( posX, posY); |
---|
[153] | 42 | view->setZoom( zoom ); |
---|
[31] | 43 | |
---|
| 44 | /*************** |
---|
| 45 | start animation with |
---|
| 46 | self running datasource |
---|
| 47 | ***************/ |
---|
| 48 | view->startView (true); |
---|
| 49 | |
---|
| 50 | } |
---|
| 51 | |
---|
[155] | 52 | bool visual_vista2D::processXMLConfiguration(std::string& configFileName, std::string& vistaProjectfile, bool& paintBackground, int& position_x, int& position_y, float& zoom) |
---|
[31] | 53 | { |
---|
[153] | 54 | // Init XML |
---|
| 55 | xmlDoc* tmpDoc; |
---|
| 56 | bool disabled; |
---|
[155] | 57 | xmlNode* config = util::getModuleXMLConfig( configFileName, "vista2d", tmpDoc, disabled ); |
---|
[153] | 58 | |
---|
| 59 | if( disabled) |
---|
| 60 | { |
---|
| 61 | OSG_NOTIFY( osg::ALWAYS ) << "..disabled by XML configuration file." << std::endl; |
---|
| 62 | return false; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | // extract configuration values |
---|
| 66 | if(config) |
---|
| 67 | { |
---|
| 68 | xmlNode* a_node = config->children; |
---|
| 69 | |
---|
| 70 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
| 71 | { |
---|
| 72 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
| 73 | //OSG_ALWAYS << "----visual_vista2D::processXMLConfiguration() - node type="<< cur_node->type <<", name=" << cur_node->name << std::endl; |
---|
| 74 | |
---|
| 75 | // Check for vista2d node |
---|
| 76 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "vista2d") |
---|
| 77 | { |
---|
| 78 | // Check attributes |
---|
| 79 | xmlAttr *attr = cur_node->properties; |
---|
| 80 | while ( attr ) |
---|
| 81 | { |
---|
| 82 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 83 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 84 | |
---|
| 85 | if( attr_name == "filename" ) |
---|
| 86 | { |
---|
[155] | 87 | vistaProjectfile=attr_value; |
---|
[153] | 88 | } |
---|
| 89 | |
---|
| 90 | if( attr_name == "paintBackground" ) |
---|
| 91 | { |
---|
| 92 | paintBackground = (attr_value == "yes") ? true : false; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | if( attr_name == "position_x" ) |
---|
| 96 | { |
---|
| 97 | std::stringstream sstr(attr_value); |
---|
| 98 | sstr >> position_x; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | if( attr_name == "position_y" ) |
---|
| 102 | { |
---|
| 103 | std::stringstream sstr(attr_value); |
---|
| 104 | sstr >> position_y; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if( attr_name == "zoom" ) |
---|
| 108 | { |
---|
| 109 | std::stringstream sstr(attr_value); |
---|
| 110 | sstr >> zoom; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | attr = attr->next; |
---|
| 114 | } // WHILE attr END |
---|
| 115 | } // IF node == vista2d END |
---|
| 116 | } // FOR end |
---|
| 117 | |
---|
| 118 | // clean up |
---|
| 119 | xmlFreeDoc(tmpDoc); xmlCleanupParser(); |
---|
| 120 | return true; |
---|
| 121 | } // IF Config valid END |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | OSG_WARN << "ERROR: visual_vista2D::processXMLConfiguration() - Module configuration not found" << std::endl; |
---|
| 125 | return false; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return true; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | bool visual_vista2D::init( osg::CoordinateSystemNode *sceneGraphRoot_, std::string configFileName ) |
---|
| 132 | { |
---|
| 133 | OSG_NOTIFY (osg::ALWAYS ) << "visual_vista2D initialize.."; // The sentence is finished by the init result... |
---|
| 134 | |
---|
[155] | 135 | std::string vistaProjectfile = ""; |
---|
| 136 | bool paintBackground = false; |
---|
| 137 | int position_x = 0; |
---|
| 138 | int position_y = 0; |
---|
| 139 | float zoom = 1; |
---|
[153] | 140 | |
---|
[155] | 141 | // Process XML configuration |
---|
| 142 | if(!processXMLConfiguration(configFileName, vistaProjectfile, paintBackground, position_x, position_y, zoom)) |
---|
[153] | 143 | return false; // Abort vista2D initialization. |
---|
| 144 | |
---|
[155] | 145 | // Check if Vista2D project file exists |
---|
| 146 | if ( !osgDB::fileExists(vistaProjectfile) ) |
---|
[31] | 147 | { |
---|
[155] | 148 | OSG_ALWAYS << "WARNING: visual_vista2D::init() - Specified vista2D projectfile '"<< vistaProjectfile <<"' does not exist! Skip using Vista2D!" << std::endl; |
---|
[31] | 149 | return false; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | osg::Geode* vista2Dgeode = new osg::Geode(); |
---|
| 153 | // Projection node for defining view frustrum for the vista2D-SceneOverlay: |
---|
| 154 | osg::Projection* vista2DProjectionMatrix = new osg::Projection; |
---|
| 155 | |
---|
| 156 | // Initialize the projection matrix for viewing everything we |
---|
| 157 | // will add as descendants of this node. Use screen coordinates |
---|
| 158 | // to define the horizontal and vertical extent of the projection |
---|
| 159 | // matrix. Positions described under this node will equate to |
---|
| 160 | // pixel coordinates. |
---|
[145] | 161 | vista2DProjectionMatrix->setMatrix(osg::Matrix::ortho2D(0,1600,0,900)); /** todo: use screen size dynamically. */ |
---|
[31] | 162 | |
---|
| 163 | // For the HUD model view matrix use an identity matrix: |
---|
| 164 | osg::MatrixTransform* vista2DModelViewMatrix = new osg::MatrixTransform; |
---|
| 165 | vista2DModelViewMatrix->setMatrix(osg::Matrix::identity()); |
---|
| 166 | |
---|
| 167 | // Make sure the model view matrix is not affected by any transforms |
---|
| 168 | // above it in the scene graph: |
---|
| 169 | vista2DModelViewMatrix->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
---|
| 170 | |
---|
| 171 | // Set Stateset of vista2DGeode Renderbin to render quite late.. |
---|
| 172 | osg::StateSet* vista2DoverlayStateSet = vista2Dgeode->getOrCreateStateSet(); |
---|
| 173 | vista2DoverlayStateSet->setRenderBinDetails( 99, "RenderBin"); |
---|
| 174 | |
---|
| 175 | // Add the HUD projection matrix as a child of the root node |
---|
| 176 | // and the HUD model view matrix as a child of the projection matrix |
---|
| 177 | // Anything under this node will be viewed using this projection matrix |
---|
| 178 | // and positioned with this model view matrix. |
---|
| 179 | vista2DProjectionMatrix->addChild(vista2DModelViewMatrix); |
---|
| 180 | |
---|
| 181 | // Add the Geometry node to contain HUD geometry as a child of the |
---|
| 182 | // HUD model view matrix. |
---|
| 183 | vista2DModelViewMatrix->addChild( vista2Dgeode ); |
---|
| 184 | |
---|
| 185 | // Add Payload: Vista2D |
---|
| 186 | osg::ref_ptr<visual_vista2D> vista2D = new visual_vista2D(); |
---|
[155] | 187 | vista2D->startVista2D(vistaProjectfile, paintBackground, position_x, position_y, zoom); |
---|
[31] | 188 | |
---|
| 189 | vista2D.get()->setUseDisplayList( false ); |
---|
| 190 | vista2Dgeode->addDrawable( vista2D.get() ); |
---|
| 191 | |
---|
| 192 | sceneGraphRoot_->addChild( vista2DProjectionMatrix ); |
---|
| 193 | return true; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | void visual_vista2D::drawImplementation(osg::RenderInfo& renderInfo) const |
---|
| 197 | { |
---|
| 198 | // save OSG-State |
---|
| 199 | osg::ref_ptr<osg::StateSet> clonedStateSet = reinterpret_cast<osg::StateSet*>( renderInfo.getCurrentCamera()->getOrCreateStateSet()->clone(osg::CopyOp::DEEP_COPY_ALL) ); |
---|
| 200 | |
---|
| 201 | // Draw View |
---|
| 202 | view->draw(); |
---|
| 203 | |
---|
| 204 | // restore OSG-State |
---|
| 205 | renderInfo.getCurrentCamera()->setStateSet( clonedStateSet ); |
---|
| 206 | |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | osg::Object* visual_vista2D::cloneType() const |
---|
| 210 | { |
---|
| 211 | return NULL; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | osg::Object* visual_vista2D::clone(const osg::CopyOp& copyop) const |
---|
| 215 | { |
---|
| 216 | return NULL; |
---|
| 217 | } |
---|