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