[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_draw2D.h> |
---|
| 18 | |
---|
| 19 | using namespace osgVisual; |
---|
| 20 | |
---|
| 21 | visual_draw2D::visual_draw2D(void) |
---|
| 22 | { |
---|
| 23 | OSG_NOTIFY( osg::ALWAYS ) << "visual_draw2D constructed" << std::endl; |
---|
| 24 | initialized = false; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | visual_draw2D::~visual_draw2D(void) |
---|
| 28 | { |
---|
| 29 | OSG_NOTIFY( osg::ALWAYS ) << "visual_draw2D destroyed: " << std::endl; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | visual_draw2D* visual_draw2D::getInstance() |
---|
| 33 | { |
---|
| 34 | static visual_draw2D instance; |
---|
| 35 | return &instance; |
---|
| 36 | }; |
---|
| 37 | |
---|
| 38 | bool visual_draw2D::init( osg::CoordinateSystemNode *sceneGraphRoot_, osgViewer::Viewer *viewer_ ) |
---|
| 39 | { |
---|
| 40 | screen_width = viewer_->getCamera()->getViewport()->width(); |
---|
| 41 | screen_height = viewer_->getCamera()->getViewport()->height(); |
---|
| 42 | |
---|
| 43 | // Projection node for defining view frustrum for the HUD: |
---|
| 44 | draw2DProjectionMatrix = new osg::Projection; |
---|
| 45 | |
---|
| 46 | // Initialize the projection matrix for viewing everything we |
---|
| 47 | // will add as descendants of this node. Use screen coordinates |
---|
| 48 | // to define the horizontal and vertical extent of the projection |
---|
| 49 | // matrix. Positions described under this node will equate to |
---|
| 50 | // pixel coordinates. |
---|
| 51 | draw2DProjectionMatrix->setMatrix(osg::Matrix::ortho2D(0,screen_width,0,screen_height)); |
---|
| 52 | |
---|
| 53 | // For the HUD model view matrix use an identity matrix: |
---|
| 54 | draw2DModelViewMatrix = new osg::MatrixTransform; |
---|
| 55 | draw2DModelViewMatrix->setMatrix(osg::Matrix::identity()); |
---|
| 56 | |
---|
| 57 | // Make sure the model view matrix is not affected by any transforms |
---|
| 58 | // above it in the scene graph: |
---|
| 59 | draw2DModelViewMatrix->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | // Add the HUD projection matrix as a child of the root node |
---|
| 63 | // and the HUD model view matrix as a child of the projection matrix |
---|
| 64 | // Anything under this node will be viewed using this projection matrix |
---|
| 65 | // and positioned with this model view matrix. |
---|
| 66 | draw2DProjectionMatrix->addChild(draw2DModelViewMatrix); |
---|
| 67 | sceneGraphRoot_->addChild( draw2DProjectionMatrix ); |
---|
| 68 | |
---|
| 69 | initialized = true; |
---|
| 70 | return true; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void visual_draw2D::shutdown() |
---|
| 74 | { |
---|
| 75 | if (!initialized) |
---|
| 76 | return; |
---|
| 77 | |
---|
| 78 | // Remove all Drawcontens |
---|
| 79 | removeAllDrawContents(); |
---|
| 80 | |
---|
| 81 | // Remove Projectionmatrix |
---|
| 82 | draw2DProjectionMatrix->removeChild( draw2DModelViewMatrix ); |
---|
| 83 | draw2DProjectionMatrix = NULL; |
---|
| 84 | |
---|
| 85 | initialized = false; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | bool visual_draw2D::addDrawContent( osg::Geode* content_, std::string name_,int renderBinDetail_ ) |
---|
| 89 | { |
---|
| 90 | if (!initialized) |
---|
| 91 | return false; |
---|
| 92 | |
---|
| 93 | // Add the Geometry node to contain HUD geometry as a child of the |
---|
| 94 | // HUD model view matrix. |
---|
| 95 | draw2DModelViewMatrix->addChild( content_ ); |
---|
| 96 | content_->setName( name_ ); |
---|
| 97 | |
---|
| 98 | osg::StateSet* stateSet = content_->getOrCreateStateSet(); |
---|
| 99 | stateSet->setRenderBinDetails( renderBinDetail_, "RenderBin"); |
---|
| 100 | // disable depth test to ensure that it is always drawn. |
---|
| 101 | stateSet->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
---|
| 102 | |
---|
| 103 | return true; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | osg::Geode* visual_draw2D::getDrawContent( std::string name_ ) |
---|
| 107 | { |
---|
| 108 | osg::Node* tmp = util::findNamedNode(name_, draw2DModelViewMatrix); |
---|
| 109 | if(tmp) |
---|
| 110 | { |
---|
| 111 | osg::Geode* tmp2 = tmp->asGeode(); |
---|
| 112 | if (tmp2) |
---|
| 113 | return tmp2; |
---|
| 114 | else |
---|
| 115 | return NULL; |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | return NULL; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | bool visual_draw2D::removeDrawContent( std::string name_ ) |
---|
| 122 | { |
---|
| 123 | osg::Node* tmp = util::findNamedNode(name_, draw2DModelViewMatrix); |
---|
| 124 | if(tmp) |
---|
| 125 | { |
---|
| 126 | draw2DModelViewMatrix->removeChild( tmp ); |
---|
| 127 | return true; |
---|
| 128 | } |
---|
| 129 | else |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void visual_draw2D::removeAllDrawContents() |
---|
| 134 | { |
---|
| 135 | if (getDrawContentNum() > 0) |
---|
| 136 | draw2DModelViewMatrix->removeChildren(0, getDrawContentNum() ); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | int visual_draw2D::getDrawContentNum() |
---|
| 140 | { |
---|
| 141 | return draw2DModelViewMatrix->getNumChildren(); |
---|
| 142 | } |
---|