| 1 | #include <osg/Notify> |
|---|
| 2 | #include <osg/MatrixTransform> |
|---|
| 3 | #include <osg/PositionAttitudeTransform> |
|---|
| 4 | #include <osg/Geometry> |
|---|
| 5 | #include <osg/Geode> |
|---|
| 6 | |
|---|
| 7 | #include <osgUtil/Optimizer> |
|---|
| 8 | |
|---|
| 9 | #include <osgDB/Registry> |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | |
|---|
| 12 | #include <osgGA/TrackballManipulator> |
|---|
| 13 | #include <osgGA/FlightManipulator> |
|---|
| 14 | #include <osgGA/DriveManipulator> |
|---|
| 15 | |
|---|
| 16 | #include <osgProducer/Viewer> |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | osg::AnimationPath* animationPath = new osg::AnimationPath; |
|---|
| 23 | animationPath->setLoopMode(osg::AnimationPath::LOOP); |
|---|
| 24 | |
|---|
| 25 | int numSamples = 40; |
|---|
| 26 | float yaw = 0.0f; |
|---|
| 27 | float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f); |
|---|
| 28 | float roll = osg::inDegrees(30.0f); |
|---|
| 29 | |
|---|
| 30 | double time=0.0f; |
|---|
| 31 | double time_delta = looptime/(double)numSamples; |
|---|
| 32 | for(int i=0;i<numSamples;++i) |
|---|
| 33 | { |
|---|
| 34 | osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); |
|---|
| 35 | osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0))); |
|---|
| 36 | |
|---|
| 37 | animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); |
|---|
| 38 | |
|---|
| 39 | yaw += yaw_delta; |
|---|
| 40 | time += time_delta; |
|---|
| 41 | |
|---|
| 42 | } |
|---|
| 43 | return animationPath; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | osg::Node* createBase(const osg::Vec3& center,float radius) |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | int numTilesX = 10; |
|---|
| 52 | int numTilesY = 10; |
|---|
| 53 | |
|---|
| 54 | float width = 2*radius; |
|---|
| 55 | float height = 2*radius; |
|---|
| 56 | |
|---|
| 57 | osg::Vec3 v000(center - osg::Vec3(width*0.5f,height*0.5f,0.0f)); |
|---|
| 58 | osg::Vec3 dx(osg::Vec3(width/((float)numTilesX),0.0,0.0f)); |
|---|
| 59 | osg::Vec3 dy(osg::Vec3(0.0f,height/((float)numTilesY),0.0f)); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | osg::Vec3Array* coords = new osg::Vec3Array; |
|---|
| 63 | int iy; |
|---|
| 64 | for(iy=0;iy<=numTilesY;++iy) |
|---|
| 65 | { |
|---|
| 66 | for(int ix=0;ix<=numTilesX;++ix) |
|---|
| 67 | { |
|---|
| 68 | coords->push_back(v000+dx*(float)ix+dy*(float)iy); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 74 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 75 | colors->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 76 | int numColors=colors->size(); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | int numIndicesPerRow=numTilesX+1; |
|---|
| 80 | osg::UByteArray* coordIndices = new osg::UByteArray; |
|---|
| 81 | osg::UByteArray* colorIndices = new osg::UByteArray; |
|---|
| 82 | for(iy=0;iy<numTilesY;++iy) |
|---|
| 83 | { |
|---|
| 84 | for(int ix=0;ix<numTilesX;++ix) |
|---|
| 85 | { |
|---|
| 86 | |
|---|
| 87 | coordIndices->push_back(ix +(iy+1)*numIndicesPerRow); |
|---|
| 88 | coordIndices->push_back(ix +iy*numIndicesPerRow); |
|---|
| 89 | coordIndices->push_back((ix+1)+iy*numIndicesPerRow); |
|---|
| 90 | coordIndices->push_back((ix+1)+(iy+1)*numIndicesPerRow); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | colorIndices->push_back((ix+iy)%numColors); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 100 | normals->push_back(osg::Vec3(0.0f,0.0f,1.0f)); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 104 | geom->setVertexArray(coords); |
|---|
| 105 | geom->setVertexIndices(coordIndices); |
|---|
| 106 | |
|---|
| 107 | geom->setColorArray(colors); |
|---|
| 108 | geom->setColorIndices(colorIndices); |
|---|
| 109 | geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); |
|---|
| 110 | |
|---|
| 111 | geom->setNormalArray(normals); |
|---|
| 112 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 113 | |
|---|
| 114 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coordIndices->size())); |
|---|
| 115 | |
|---|
| 116 | osg::Geode* geode = new osg::Geode; |
|---|
| 117 | geode->addDrawable(geom); |
|---|
| 118 | |
|---|
| 119 | return geode; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | osg::Node* createMovingModel(const osg::Vec3& center, float radius) |
|---|
| 123 | { |
|---|
| 124 | float animationLength = 10.0f; |
|---|
| 125 | |
|---|
| 126 | osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength); |
|---|
| 127 | |
|---|
| 128 | osg::Group* model = new osg::Group; |
|---|
| 129 | |
|---|
| 130 | osg::Node* glider = osgDB::readNodeFile("glider.osg"); |
|---|
| 131 | if (glider) |
|---|
| 132 | { |
|---|
| 133 | const osg::BoundingSphere& bs = glider->getBound(); |
|---|
| 134 | |
|---|
| 135 | float size = radius/bs.radius()*0.3f; |
|---|
| 136 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 137 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 138 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 139 | osg::Matrix::scale(size,size,size)* |
|---|
| 140 | osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f)); |
|---|
| 141 | |
|---|
| 142 | positioned->addChild(glider); |
|---|
| 143 | |
|---|
| 144 | osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform; |
|---|
| 145 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0)); |
|---|
| 146 | xform->addChild(positioned); |
|---|
| 147 | |
|---|
| 148 | model->addChild(xform); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); |
|---|
| 152 | if (cessna) |
|---|
| 153 | { |
|---|
| 154 | const osg::BoundingSphere& bs = cessna->getBound(); |
|---|
| 155 | |
|---|
| 156 | float size = radius/bs.radius()*0.3f; |
|---|
| 157 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 158 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 159 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 160 | osg::Matrix::scale(size,size,size)* |
|---|
| 161 | osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f)); |
|---|
| 162 | |
|---|
| 163 | positioned->addChild(cessna); |
|---|
| 164 | |
|---|
| 165 | osg::MatrixTransform* xform = new osg::MatrixTransform; |
|---|
| 166 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0)); |
|---|
| 167 | xform->addChild(positioned); |
|---|
| 168 | |
|---|
| 169 | model->addChild(xform); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | return model; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | osg::Node* createModel() |
|---|
| 176 | { |
|---|
| 177 | osg::Vec3 center(0.0f,0.0f,0.0f); |
|---|
| 178 | float radius = 100.0f; |
|---|
| 179 | |
|---|
| 180 | osg::Group* root = new osg::Group; |
|---|
| 181 | |
|---|
| 182 | root->addChild(createMovingModel(center,radius*0.8f)); |
|---|
| 183 | |
|---|
| 184 | root->addChild(createBase(center-osg::Vec3(0.0f,0.0f,radius*0.5),radius)); |
|---|
| 185 | |
|---|
| 186 | return root; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | int main( int argc, char **argv ) |
|---|
| 191 | { |
|---|
| 192 | |
|---|
| 193 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes."); |
|---|
| 197 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 198 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | osgProducer::Viewer viewer(arguments); |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 211 | { |
|---|
| 212 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 213 | return 1; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | if (arguments.errors()) |
|---|
| 221 | { |
|---|
| 222 | arguments.writeErrorMessages(std::cout); |
|---|
| 223 | return 1; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | osg::Node* model = createModel(); |
|---|
| 228 | if (!model) |
|---|
| 229 | { |
|---|
| 230 | return 1; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | osg::MatrixTransform* rootnode = new osg::MatrixTransform; |
|---|
| 235 | rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f)); |
|---|
| 236 | rootnode->addChild(model); |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | osgUtil::Optimizer optimzer; |
|---|
| 240 | optimzer.optimize(rootnode); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | viewer.setSceneData(rootnode); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | viewer.realize(); |
|---|
| 247 | |
|---|
| 248 | while( !viewer.done() ) |
|---|
| 249 | { |
|---|
| 250 | |
|---|
| 251 | viewer.sync(); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | viewer.update(); |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | viewer.frame(); |
|---|
| 259 | |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | viewer.sync(); |
|---|
| 264 | |
|---|
| 265 | return 0; |
|---|
| 266 | } |
|---|