| 31 | | osg::Geode* geode = new osg::Geode(); |
| 32 | | |
| 33 | | std::string timesFont("fonts/arial.ttf"); |
| 34 | | |
| 35 | | // turn lighting off for the text and disable depth test to ensure its always ontop. |
| 36 | | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
| 37 | | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
| 38 | | |
| 39 | | osg::Vec3 position(150.0f,800.0f,0.0f); |
| 40 | | osg::Vec3 delta(0.0f,-120.0f,0.0f); |
| 41 | | |
| 42 | | { |
| 43 | | osgText::Text* text = new osgText::Text; |
| 44 | | geode->addDrawable( text ); |
| 45 | | |
| 46 | | text->setFont(timesFont); |
| 47 | | text->setPosition(position); |
| 48 | | text->setText("Head Up Displays are simple :-)"); |
| 49 | | |
| 50 | | position += delta; |
| 51 | | } |
| 52 | | |
| 53 | | |
| 54 | | { |
| 55 | | osgText::Text* text = new osgText::Text; |
| 56 | | geode->addDrawable( text ); |
| 57 | | |
| 58 | | text->setFont(timesFont); |
| 59 | | text->setPosition(position); |
| 60 | | text->setText("All you need to do is create your text in a subgraph."); |
| 61 | | |
| 62 | | position += delta; |
| 63 | | } |
| 64 | | |
| 65 | | |
| 66 | | { |
| 67 | | osgText::Text* text = new osgText::Text; |
| 68 | | geode->addDrawable( text ); |
| 69 | | |
| 70 | | text->setFont(timesFont); |
| 71 | | text->setPosition(position); |
| 72 | | text->setText("Then place an osg::Camera above the subgraph\n" |
| 73 | | "to create an orthographic projection.\n"); |
| 74 | | |
| 75 | | position += delta; |
| 76 | | } |
| 77 | | |
| 78 | | { |
| 79 | | osgText::Text* text = new osgText::Text; |
| 80 | | geode->addDrawable( text ); |
| 81 | | |
| 82 | | text->setFont(timesFont); |
| 83 | | text->setPosition(position); |
| 84 | | text->setText("Set the Camera's ReferenceFrame to ABSOLUTE_RF to ensure\n" |
| 85 | | "it remains independent from any external model view matrices."); |
| 86 | | |
| 87 | | position += delta; |
| 88 | | } |
| 89 | | |
| 90 | | { |
| 91 | | osgText::Text* text = new osgText::Text; |
| 92 | | geode->addDrawable( text ); |
| 93 | | |
| 94 | | text->setFont(timesFont); |
| 95 | | text->setPosition(position); |
| 96 | | text->setText("And set the Camera's clear mask to just clear the depth buffer."); |
| 97 | | |
| 98 | | position += delta; |
| 99 | | } |
| 100 | | |
| 101 | | { |
| 102 | | osgText::Text* text = new osgText::Text; |
| 103 | | geode->addDrawable( text ); |
| 104 | | |
| 105 | | text->setFont(timesFont); |
| 106 | | text->setPosition(position); |
| 107 | | text->setText("And finally set the Camera's RenderOrder to POST_RENDER\n" |
| 108 | | "to make sure its drawn last."); |
| 109 | | |
| 110 | | position += delta; |
| 111 | | } |
| 112 | | |
| 113 | | |
| 114 | | { |
| 115 | | osg::BoundingBox bb; |
| 116 | | for(unsigned int i=0;i<geode->getNumDrawables();++i) |
| 117 | | { |
| 118 | | bb.expandBy(geode->getDrawable(i)->getBound()); |
| 119 | | } |
| 120 | | |
| 121 | | osg::Geometry* geom = new osg::Geometry; |
| 122 | | |
| 123 | | osg::Vec3Array* vertices = new osg::Vec3Array; |
| 124 | | float depth = bb.zMin()-0.1; |
| 125 | | vertices->push_back(osg::Vec3(bb.xMin(),bb.yMax(),depth)); |
| 126 | | vertices->push_back(osg::Vec3(bb.xMin(),bb.yMin(),depth)); |
| 127 | | vertices->push_back(osg::Vec3(bb.xMax(),bb.yMin(),depth)); |
| 128 | | vertices->push_back(osg::Vec3(bb.xMax(),bb.yMax(),depth)); |
| 129 | | geom->setVertexArray(vertices); |
| 130 | | |
| 131 | | osg::Vec3Array* normals = new osg::Vec3Array; |
| 132 | | normals->push_back(osg::Vec3(0.0f,0.0f,1.0f)); |
| 133 | | geom->setNormalArray(normals); |
| 134 | | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
| 135 | | |
| 136 | | osg::Vec4Array* colors = new osg::Vec4Array; |
| 137 | | colors->push_back(osg::Vec4(1.0f,1.0,0.8f,0.2f)); |
| 138 | | geom->setColorArray(colors); |
| 139 | | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
| 140 | | |
| 141 | | geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4)); |
| 142 | | |
| 143 | | osg::StateSet* stateset = geom->getOrCreateStateSet(); |
| 144 | | stateset->setMode(GL_BLEND,osg::StateAttribute::ON); |
| 145 | | //stateset->setAttribute(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON); |
| 146 | | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
| 147 | | |
| 148 | | geode->addDrawable(geom); |
| 149 | | } |
| 150 | | |
| | 35 | // create a camera to set up the projection and model view matrices, and the subgraph to drawn in the HUD |
| | 54 | |
| | 55 | |
| | 56 | // add to this camera a subgraph to render |
| | 57 | { |
| | 58 | |
| | 59 | osg::Geode* geode = new osg::Geode(); |
| | 60 | |
| | 61 | std::string timesFont("fonts/arial.ttf"); |
| | 62 | |
| | 63 | // turn lighting off for the text and disable depth test to ensure its always ontop. |
| | 64 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
| | 65 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
| | 66 | |
| | 67 | osg::Vec3 position(150.0f,800.0f,0.0f); |
| | 68 | osg::Vec3 delta(0.0f,-120.0f,0.0f); |
| | 69 | |
| | 70 | { |
| | 71 | osgText::Text* text = new osgText::Text; |
| | 72 | geode->addDrawable( text ); |
| | 73 | |
| | 74 | text->setFont(timesFont); |
| | 75 | text->setPosition(position); |
| | 76 | text->setText("Head Up Displays are simple :-)"); |
| | 77 | |
| | 78 | position += delta; |
| | 79 | } |
| | 80 | |
| | 81 | |
| | 82 | { |
| | 83 | osgText::Text* text = new osgText::Text; |
| | 84 | geode->addDrawable( text ); |
| | 85 | |
| | 86 | text->setFont(timesFont); |
| | 87 | text->setPosition(position); |
| | 88 | text->setText("All you need to do is create your text in a subgraph."); |
| | 89 | |
| | 90 | position += delta; |
| | 91 | } |
| | 92 | |
| | 93 | |
| | 94 | { |
| | 95 | osgText::Text* text = new osgText::Text; |
| | 96 | geode->addDrawable( text ); |
| | 97 | |
| | 98 | text->setFont(timesFont); |
| | 99 | text->setPosition(position); |
| | 100 | text->setText("Then place an osg::Camera above the subgraph\n" |
| | 101 | "to create an orthographic projection.\n"); |
| | 102 | |
| | 103 | position += delta; |
| | 104 | } |
| | 105 | |
| | 106 | { |
| | 107 | osgText::Text* text = new osgText::Text; |
| | 108 | geode->addDrawable( text ); |
| | 109 | |
| | 110 | text->setFont(timesFont); |
| | 111 | text->setPosition(position); |
| | 112 | text->setText("Set the Camera's ReferenceFrame to ABSOLUTE_RF to ensure\n" |
| | 113 | "it remains independent from any external model view matrices."); |
| | 114 | |
| | 115 | position += delta; |
| | 116 | } |
| | 117 | |
| | 118 | { |
| | 119 | osgText::Text* text = new osgText::Text; |
| | 120 | geode->addDrawable( text ); |
| | 121 | |
| | 122 | text->setFont(timesFont); |
| | 123 | text->setPosition(position); |
| | 124 | text->setText("And set the Camera's clear mask to just clear the depth buffer."); |
| | 125 | |
| | 126 | position += delta; |
| | 127 | } |
| | 128 | |
| | 129 | { |
| | 130 | osgText::Text* text = new osgText::Text; |
| | 131 | geode->addDrawable( text ); |
| | 132 | |
| | 133 | text->setFont(timesFont); |
| | 134 | text->setPosition(position); |
| | 135 | text->setText("And finally set the Camera's RenderOrder to POST_RENDER\n" |
| | 136 | "to make sure its drawn last."); |
| | 137 | |
| | 138 | position += delta; |
| | 139 | } |
| | 140 | |
| | 141 | |
| | 142 | { |
| | 143 | osg::BoundingBox bb; |
| | 144 | for(unsigned int i=0;i<geode->getNumDrawables();++i) |
| | 145 | { |
| | 146 | bb.expandBy(geode->getDrawable(i)->getBound()); |
| | 147 | } |
| | 148 | |
| | 149 | osg::Geometry* geom = new osg::Geometry; |
| | 150 | |
| | 151 | osg::Vec3Array* vertices = new osg::Vec3Array; |
| | 152 | float depth = bb.zMin()-0.1; |
| | 153 | vertices->push_back(osg::Vec3(bb.xMin(),bb.yMax(),depth)); |
| | 154 | vertices->push_back(osg::Vec3(bb.xMin(),bb.yMin(),depth)); |
| | 155 | vertices->push_back(osg::Vec3(bb.xMax(),bb.yMin(),depth)); |
| | 156 | vertices->push_back(osg::Vec3(bb.xMax(),bb.yMax(),depth)); |
| | 157 | geom->setVertexArray(vertices); |
| | 158 | |
| | 159 | osg::Vec3Array* normals = new osg::Vec3Array; |
| | 160 | normals->push_back(osg::Vec3(0.0f,0.0f,1.0f)); |
| | 161 | geom->setNormalArray(normals); |
| | 162 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
| | 163 | |
| | 164 | osg::Vec4Array* colors = new osg::Vec4Array; |
| | 165 | colors->push_back(osg::Vec4(1.0f,1.0,0.8f,0.2f)); |
| | 166 | geom->setColorArray(colors); |
| | 167 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
| | 168 | |
| | 169 | geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4)); |
| | 170 | |
| | 171 | osg::StateSet* stateset = geom->getOrCreateStateSet(); |
| | 172 | stateset->setMode(GL_BLEND,osg::StateAttribute::ON); |
| | 173 | //stateset->setAttribute(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON); |
| | 174 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
| | 175 | |
| | 176 | geode->addDrawable(geom); |
| | 177 | } |
| | 178 | |
| | 179 | camera->addChild(geode); |
| | 180 | } |
| | 181 | |
| 183 | | // add the HUD subgraph. |
| 184 | | if (scene.valid()) group->addChild(scene.get()); |
| 185 | | group->addChild(createHUD()); |
| 186 | | |
| 187 | | // construct the viewer. |
| 188 | | osgViewer::Viewer viewer; |
| 189 | | |
| 190 | | // set the scene to render |
| 191 | | viewer.setSceneData(group.get()); |
| 192 | | |
| 193 | | return viewer.run(); |
| | 194 | if (!scene) |
| | 195 | { |
| | 196 | osg::notify(osg::NOTICE)<<"No model loaded"<<std::endl; |
| | 197 | return 1; |
| | 198 | } |
| | 199 | |
| | 200 | |
| | 201 | if (arguments.read("--Viewer")) |
| | 202 | { |
| | 203 | // construct the viewer. |
| | 204 | osgViewer::Viewer viewer; |
| | 205 | |
| | 206 | // create a HUD as slave camera attached to the master view. |
| | 207 | |
| | 208 | viewer.setUpViewAcrossAllScreens(); |
| | 209 | |
| | 210 | osgViewer::Viewer::Windows windows; |
| | 211 | viewer.getWindows(windows); |
| | 212 | |
| | 213 | if (windows.empty()) return 1; |
| | 214 | |
| | 215 | osg::Camera* hudCamera = createHUD(); |
| | 216 | |
| | 217 | // set up cameras to rendering on the first window available. |
| | 218 | hudCamera->setGraphicsContext(windows[0]); |
| | 219 | hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height); |
| | 220 | |
| | 221 | viewer.addSlave(hudCamera, false); |
| | 222 | |
| | 223 | // set the scene to render |
| | 224 | viewer.setSceneData(scene.get()); |
| | 225 | |
| | 226 | return viewer.run(); |
| | 227 | |
| | 228 | } |
| | 229 | if (arguments.read("--CompositeViewer")) |
| | 230 | { |
| | 231 | // construct the viewer. |
| | 232 | osgViewer::CompositeViewer viewer; |
| | 233 | |
| | 234 | // create the main 3D view |
| | 235 | osgViewer::View* view = new osgViewer::View; |
| | 236 | viewer.addView(view); |
| | 237 | |
| | 238 | view->setSceneData(scene.get()); |
| | 239 | view->setUpViewAcrossAllScreens();; |
| | 240 | view->setCameraManipulator(new osgGA::TrackballManipulator); |
| | 241 | |
| | 242 | // now create the HUD camera's view |
| | 243 | |
| | 244 | osgViewer::Viewer::Windows windows; |
| | 245 | viewer.getWindows(windows); |
| | 246 | |
| | 247 | if (windows.empty()) return 1; |
| | 248 | |
| | 249 | osg::Camera* hudCamera = createHUD(); |
| | 250 | |
| | 251 | // set up cameras to rendering on the first window available. |
| | 252 | hudCamera->setGraphicsContext(windows[0]); |
| | 253 | hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height); |
| | 254 | |
| | 255 | osgViewer::View* hudView = new osgViewer::View; |
| | 256 | hudView->setCamera(hudCamera); |
| | 257 | |
| | 258 | viewer.addView(hudView); |
| | 259 | |
| | 260 | return viewer.run(); |
| | 261 | |
| | 262 | } |
| | 263 | else |
| | 264 | { |
| | 265 | // construct the viewer. |
| | 266 | osgViewer::Viewer viewer; |
| | 267 | |
| | 268 | osg::ref_ptr<osg::Group> group = new osg::Group; |
| | 269 | |
| | 270 | // add the HUD subgraph. |
| | 271 | if (scene.valid()) group->addChild(scene.get()); |
| | 272 | group->addChild(createHUD()); |
| | 273 | |
| | 274 | // set the scene to render |
| | 275 | viewer.setSceneData(group.get()); |
| | 276 | |
| | 277 | return viewer.run(); |
| | 278 | } |
| | 279 | |