| 1 | #include <osgProducer/Viewer> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Group> |
|---|
| 4 | #include <osg/Geode> |
|---|
| 5 | #include <osg/ShapeDrawable> |
|---|
| 6 | #include <osg/Texture2D> |
|---|
| 7 | #include <osg/PositionAttitudeTransform> |
|---|
| 8 | #include <osg/MatrixTransform> |
|---|
| 9 | #include <osg/CoordinateSystemNode> |
|---|
| 10 | |
|---|
| 11 | #include <osgDB/FileUtils> |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | |
|---|
| 14 | #include <osgText/Text> |
|---|
| 15 | |
|---|
| 16 | #include <osgTerrain/DataSet> |
|---|
| 17 | |
|---|
| 18 | #include <osgGA/NodeTrackerManipulator> |
|---|
| 19 | |
|---|
| 20 | class GraphicsContext { |
|---|
| 21 | public: |
|---|
| 22 | GraphicsContext() |
|---|
| 23 | { |
|---|
| 24 | rs = new Producer::RenderSurface; |
|---|
| 25 | rs->setWindowRectangle(0,0,1,1); |
|---|
| 26 | rs->useBorder(false); |
|---|
| 27 | rs->useConfigEventThread(false); |
|---|
| 28 | rs->realize(); |
|---|
| 29 | std::cout<<"Realized window"<<std::endl; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | virtual ~GraphicsContext() |
|---|
| 33 | { |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | private: |
|---|
| 37 | Producer::ref_ptr<Producer::RenderSurface> rs; |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | osg::Node* createEarth() |
|---|
| 41 | { |
|---|
| 42 | osg::ref_ptr<osg::Node> scene; |
|---|
| 43 | |
|---|
| 44 | { |
|---|
| 45 | std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg"); |
|---|
| 46 | |
|---|
| 47 | osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | { |
|---|
| 51 | osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE, filename); |
|---|
| 52 | |
|---|
| 53 | source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS); |
|---|
| 54 | source->setCoordinateSystem(osgTerrain::DataSet::coordinateSystemStringToWTK("WGS84")); |
|---|
| 55 | |
|---|
| 56 | source->setGeoTransformPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION); |
|---|
| 57 | source->setGeoTransformFromRange(-180.0, 180.0, -90.0, 90.0); |
|---|
| 58 | |
|---|
| 59 | dataSet->addSource(source); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE); |
|---|
| 64 | dataSet->setConvertFromGeographicToGeocentric(true); |
|---|
| 65 | dataSet->setDestinationName("test.osg"); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | dataSet->loadSources(); |
|---|
| 69 | |
|---|
| 70 | GraphicsContext context; |
|---|
| 71 | dataSet->createDestination(30); |
|---|
| 72 | |
|---|
| 73 | if (dataSet->getDatabaseType()==osgTerrain::DataSet::LOD_DATABASE) dataSet->buildDestination(); |
|---|
| 74 | else dataSet->writeDestination(); |
|---|
| 75 | |
|---|
| 76 | scene = dataSet->getDestinationRootNode(); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | return scene.release(); |
|---|
| 80 | |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | class ModelPositionCallback : public osg::NodeCallback |
|---|
| 85 | { |
|---|
| 86 | public: |
|---|
| 87 | |
|---|
| 88 | ModelPositionCallback(): |
|---|
| 89 | _latitude(0.0), |
|---|
| 90 | _longitude(0.0), |
|---|
| 91 | _height(1000.0) |
|---|
| 92 | {} |
|---|
| 93 | |
|---|
| 94 | void updateParameters() |
|---|
| 95 | { |
|---|
| 96 | _longitude += (2.0*osg::PI)/360.0; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 101 | { |
|---|
| 102 | updateParameters(); |
|---|
| 103 | |
|---|
| 104 | osg::NodePath nodePath = nv->getNodePath(); |
|---|
| 105 | |
|---|
| 106 | osg::MatrixTransform* mt = nodePath.empty() ? 0 : dynamic_cast<osg::MatrixTransform*>(nodePath.back()); |
|---|
| 107 | if (mt) |
|---|
| 108 | { |
|---|
| 109 | osg::CoordinateSystemNode* csn = 0; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | unsigned int i; |
|---|
| 113 | for(i=0; i<nodePath.size() && csn==0; ++i) |
|---|
| 114 | { |
|---|
| 115 | csn = dynamic_cast<osg::CoordinateSystemNode*>(nodePath[i]); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | if (csn) |
|---|
| 119 | { |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
|---|
| 123 | if (ellipsoid) |
|---|
| 124 | { |
|---|
| 125 | osg::Matrixd matrix; |
|---|
| 126 | for(i+=1; i<nodePath.size()-1; ++i) |
|---|
| 127 | { |
|---|
| 128 | osg::Transform* transform = nodePath[i]->asTransform(); |
|---|
| 129 | if (transform) transform->computeLocalToWorldMatrix(matrix, nv); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | ellipsoid->computeLocalToWorldTransformFromLatLongHeight(_latitude,_longitude,_height,matrix); |
|---|
| 134 | matrix.preMult(osg::Matrixd::rotate(_rotation)); |
|---|
| 135 | |
|---|
| 136 | mt->setMatrix(matrix); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | traverse(node,nv); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | double _latitude; |
|---|
| 146 | double _longitude; |
|---|
| 147 | double _height; |
|---|
| 148 | osg::Quat _rotation; |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | class FindNamedNodeVisitor : public osg::NodeVisitor |
|---|
| 153 | { |
|---|
| 154 | public: |
|---|
| 155 | FindNamedNodeVisitor(const std::string& name): |
|---|
| 156 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 157 | _name(name) {} |
|---|
| 158 | |
|---|
| 159 | virtual void apply(osg::Node& node) |
|---|
| 160 | { |
|---|
| 161 | if (node.getName()==_name) |
|---|
| 162 | { |
|---|
| 163 | _foundNodes.push_back(&node); |
|---|
| 164 | } |
|---|
| 165 | traverse(node); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | typedef std::vector< osg::ref_ptr<osg::Node> > NodeList; |
|---|
| 169 | |
|---|
| 170 | std::string _name; |
|---|
| 171 | NodeList _foundNodes; |
|---|
| 172 | }; |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | void addModel(osgProducer::Viewer* viewer,osg::Node* model) |
|---|
| 176 | { |
|---|
| 177 | |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | int main(int argc, char **argv) |
|---|
| 182 | { |
|---|
| 183 | |
|---|
| 184 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems."); |
|---|
| 188 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye"); |
|---|
| 189 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | osgProducer::Viewer viewer(arguments); |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 205 | { |
|---|
| 206 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 207 | return 1; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | if (arguments.errors()) |
|---|
| 215 | { |
|---|
| 216 | arguments.writeErrorMessages(std::cout); |
|---|
| 217 | return 1; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | osg::Node *root = createEarth(); |
|---|
| 221 | |
|---|
| 222 | if (!root) return 0; |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | viewer.setSceneData(root); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | FindNamedNodeVisitor fnnv("cessna"); |
|---|
| 229 | root->accept(fnnv); |
|---|
| 230 | |
|---|
| 231 | if (!fnnv._foundNodes.empty()) |
|---|
| 232 | { |
|---|
| 233 | osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator; |
|---|
| 234 | tm->setTrackNode(fnnv._foundNodes[0].get()); |
|---|
| 235 | |
|---|
| 236 | std::cout<<"Found "<<std::endl; |
|---|
| 237 | |
|---|
| 238 | unsigned int num = viewer.addCameraManipulator(tm); |
|---|
| 239 | viewer.selectCameraManipulator(num); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | viewer.realize(); |
|---|
| 246 | |
|---|
| 247 | while( !viewer.done() ) |
|---|
| 248 | { |
|---|
| 249 | |
|---|
| 250 | viewer.sync(); |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | viewer.update(); |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | viewer.frame(); |
|---|
| 258 | |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | viewer.sync(); |
|---|
| 263 | |
|---|
| 264 | return 0; |
|---|
| 265 | } |
|---|