| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgUtil/Optimizer> |
|---|
| 15 | #include <osgDB/ReadFile> |
|---|
| 16 | |
|---|
| 17 | #include <osgViewer/Viewer> |
|---|
| 18 | #include <osgViewer/CompositeViewer> |
|---|
| 19 | |
|---|
| 20 | #include <osgGA/TrackballManipulator> |
|---|
| 21 | |
|---|
| 22 | #include <osg/Material> |
|---|
| 23 | #include <osg/Geode> |
|---|
| 24 | #include <osg/BlendFunc> |
|---|
| 25 | #include <osg/Depth> |
|---|
| 26 | #include <osg/PolygonOffset> |
|---|
| 27 | #include <osg/MatrixTransform> |
|---|
| 28 | #include <osg/Camera> |
|---|
| 29 | |
|---|
| 30 | #include <osgText/Text> |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | osg::Camera* createHUD() |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | osg::Camera* camera = new osg::Camera; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 43 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | camera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | camera->setRenderOrder(osg::Camera::POST_RENDER); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | camera->setAllowEventFocus(false); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | { |
|---|
| 58 | |
|---|
| 59 | osg::Geode* geode = new osg::Geode(); |
|---|
| 60 | |
|---|
| 61 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 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 | |
|---|
| 174 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 175 | |
|---|
| 176 | geode->addDrawable(geom); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | camera->addChild(geode); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | return camera; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | int main( int argc, char **argv ) |
|---|
| 186 | { |
|---|
| 187 | |
|---|
| 188 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments); |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | if (!scene) scene = osgDB::readNodeFile("dumptruck.osg"); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | if (!scene) |
|---|
| 199 | { |
|---|
| 200 | osg::notify(osg::NOTICE)<<"No model loaded"<<std::endl; |
|---|
| 201 | return 1; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | if (arguments.read("--Viewer")) |
|---|
| 206 | { |
|---|
| 207 | |
|---|
| 208 | osgViewer::Viewer viewer; |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | viewer.setUpViewAcrossAllScreens(); |
|---|
| 213 | |
|---|
| 214 | osgViewer::Viewer::Windows windows; |
|---|
| 215 | viewer.getWindows(windows); |
|---|
| 216 | |
|---|
| 217 | if (windows.empty()) return 1; |
|---|
| 218 | |
|---|
| 219 | osg::Camera* hudCamera = createHUD(); |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | hudCamera->setGraphicsContext(windows[0]); |
|---|
| 223 | hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height); |
|---|
| 224 | |
|---|
| 225 | viewer.addSlave(hudCamera, false); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | viewer.setSceneData(scene.get()); |
|---|
| 229 | |
|---|
| 230 | return viewer.run(); |
|---|
| 231 | |
|---|
| 232 | } |
|---|
| 233 | if (arguments.read("--CompositeViewer")) |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | osgViewer::CompositeViewer viewer; |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | osgViewer::View* view = new osgViewer::View; |
|---|
| 240 | viewer.addView(view); |
|---|
| 241 | |
|---|
| 242 | view->setSceneData(scene.get()); |
|---|
| 243 | view->setUpViewAcrossAllScreens();; |
|---|
| 244 | view->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | osgViewer::Viewer::Windows windows; |
|---|
| 249 | viewer.getWindows(windows); |
|---|
| 250 | |
|---|
| 251 | if (windows.empty()) return 1; |
|---|
| 252 | |
|---|
| 253 | osg::Camera* hudCamera = createHUD(); |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | hudCamera->setGraphicsContext(windows[0]); |
|---|
| 257 | hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height); |
|---|
| 258 | |
|---|
| 259 | osgViewer::View* hudView = new osgViewer::View; |
|---|
| 260 | hudView->setCamera(hudCamera); |
|---|
| 261 | |
|---|
| 262 | viewer.addView(hudView); |
|---|
| 263 | |
|---|
| 264 | return viewer.run(); |
|---|
| 265 | |
|---|
| 266 | } |
|---|
| 267 | else |
|---|
| 268 | { |
|---|
| 269 | |
|---|
| 270 | osgViewer::Viewer viewer; |
|---|
| 271 | |
|---|
| 272 | osg::ref_ptr<osg::Group> group = new osg::Group; |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | if (scene.valid()) group->addChild(scene.get()); |
|---|
| 276 | group->addChild(createHUD()); |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | viewer.setSceneData(group.get()); |
|---|
| 280 | |
|---|
| 281 | return viewer.run(); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | } |
|---|