| 1 | #include <osg/Notify> |
|---|
| 2 | #include <osg/MatrixTransform> |
|---|
| 3 | #include <osg/ShapeDrawable> |
|---|
| 4 | #include <osg/PositionAttitudeTransform> |
|---|
| 5 | #include <osg/Geometry> |
|---|
| 6 | #include <osg/Texture2D> |
|---|
| 7 | #include <osg/Geode> |
|---|
| 8 | |
|---|
| 9 | #include <osgUtil/Optimizer> |
|---|
| 10 | |
|---|
| 11 | #include <osgDB/Registry> |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | |
|---|
| 14 | #include <osgViewer/Viewer> |
|---|
| 15 | |
|---|
| 16 | #include <osgShadow/ShadowedScene> |
|---|
| 17 | #include <osgShadow/ShadowVolume> |
|---|
| 18 | #include <osgShadow/ShadowTexture> |
|---|
| 19 | #include <osgShadow/ShadowMap> |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #include "CreateShadowedScene.h" |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | #include "../osghangglide/terrain_coords.h" |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | osg::AnimationPath* animationPath = new osg::AnimationPath; |
|---|
| 34 | animationPath->setLoopMode(osg::AnimationPath::LOOP); |
|---|
| 35 | |
|---|
| 36 | int numSamples = 40; |
|---|
| 37 | float yaw = 0.0f; |
|---|
| 38 | float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f); |
|---|
| 39 | float roll = osg::inDegrees(30.0f); |
|---|
| 40 | |
|---|
| 41 | double time=0.0f; |
|---|
| 42 | double time_delta = looptime/(double)numSamples; |
|---|
| 43 | for(int i=0;i<numSamples;++i) |
|---|
| 44 | { |
|---|
| 45 | osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); |
|---|
| 46 | 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))); |
|---|
| 47 | |
|---|
| 48 | animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); |
|---|
| 49 | |
|---|
| 50 | yaw += yaw_delta; |
|---|
| 51 | time += time_delta; |
|---|
| 52 | |
|---|
| 53 | } |
|---|
| 54 | return animationPath; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | osg::Node* createBase(const osg::Vec3& center,float radius) |
|---|
| 58 | { |
|---|
| 59 | |
|---|
| 60 | osg::Geode* geode = new osg::Geode; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 64 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 65 | if (image) |
|---|
| 66 | { |
|---|
| 67 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 68 | texture->setImage(image); |
|---|
| 69 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | geode->setStateSet( stateset ); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | osg::HeightField* grid = new osg::HeightField; |
|---|
| 76 | grid->allocate(38,39); |
|---|
| 77 | grid->setOrigin(center+osg::Vec3(-radius,-radius,0.0f)); |
|---|
| 78 | grid->setXInterval(radius*2.0f/(float)(38-1)); |
|---|
| 79 | grid->setYInterval(radius*2.0f/(float)(39-1)); |
|---|
| 80 | |
|---|
| 81 | float minHeight = FLT_MAX; |
|---|
| 82 | float maxHeight = -FLT_MAX; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | unsigned int r; |
|---|
| 86 | for(r=0;r<39;++r) |
|---|
| 87 | { |
|---|
| 88 | for(unsigned int c=0;c<38;++c) |
|---|
| 89 | { |
|---|
| 90 | float h = vertex[r+c*39][2]; |
|---|
| 91 | if (h>maxHeight) maxHeight=h; |
|---|
| 92 | if (h<minHeight) minHeight=h; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | float hieghtScale = radius*0.5f/(maxHeight-minHeight); |
|---|
| 97 | float hieghtOffset = -(minHeight+maxHeight)*0.5f; |
|---|
| 98 | |
|---|
| 99 | for(r=0;r<39;++r) |
|---|
| 100 | { |
|---|
| 101 | for(unsigned int c=0;c<38;++c) |
|---|
| 102 | { |
|---|
| 103 | float h = vertex[r+c*39][2]; |
|---|
| 104 | grid->setHeight(c,r,(h+hieghtOffset)*hieghtScale); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | geode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 109 | |
|---|
| 110 | osg::Group* group = new osg::Group; |
|---|
| 111 | group->addChild(geode); |
|---|
| 112 | |
|---|
| 113 | return group; |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | osg::Node* createMovingModel(const osg::Vec3& center, float radius) |
|---|
| 118 | { |
|---|
| 119 | float animationLength = 10.0f; |
|---|
| 120 | |
|---|
| 121 | osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength); |
|---|
| 122 | |
|---|
| 123 | osg::Group* model = new osg::Group; |
|---|
| 124 | |
|---|
| 125 | osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); |
|---|
| 126 | if (cessna) |
|---|
| 127 | { |
|---|
| 128 | const osg::BoundingSphere& bs = cessna->getBound(); |
|---|
| 129 | |
|---|
| 130 | float size = radius/bs.radius()*0.3f; |
|---|
| 131 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 132 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 133 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 134 | osg::Matrix::scale(size,size,size)* |
|---|
| 135 | osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,2.0f)); |
|---|
| 136 | |
|---|
| 137 | positioned->addChild(cessna); |
|---|
| 138 | |
|---|
| 139 | osg::MatrixTransform* xform = new osg::MatrixTransform; |
|---|
| 140 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0)); |
|---|
| 141 | xform->addChild(positioned); |
|---|
| 142 | |
|---|
| 143 | model->addChild(xform); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | return model; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | osg::Node* createModel(osg::ArgumentParser& arguments) |
|---|
| 153 | { |
|---|
| 154 | osg::Vec3 center(0.0f,0.0f,0.0f); |
|---|
| 155 | float radius = 100.0f; |
|---|
| 156 | osg::Vec3 lightPosition(center+osg::Vec3(0.0f,0.0f,radius)); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | osg::Node* shadower = createMovingModel(center,radius*0.5f); |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | osg::Node* shadowed = createBase(center-osg::Vec3(0.0f,0.0f,radius*0.25),radius); |
|---|
| 163 | |
|---|
| 164 | if (arguments.read("--sv")) |
|---|
| 165 | { |
|---|
| 166 | |
|---|
| 167 | osg::DisplaySettings::instance()->setMinimumNumStencilBits(8); |
|---|
| 168 | |
|---|
| 169 | osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene; |
|---|
| 170 | |
|---|
| 171 | osg::ref_ptr<osgShadow::ShadowVolume> shadowVolume = new osgShadow::ShadowVolume; |
|---|
| 172 | shadowedScene->setShadowTechnique(shadowVolume.get()); |
|---|
| 173 | shadowVolume->setDynamicShadowVolumes(true); |
|---|
| 174 | |
|---|
| 175 | osg::ref_ptr<osg::LightSource> ls = new osg::LightSource; |
|---|
| 176 | ls->getLight()->setPosition(osg::Vec4(lightPosition,1.0)); |
|---|
| 177 | |
|---|
| 178 | shadowedScene->addChild(shadower); |
|---|
| 179 | shadowedScene->addChild(shadowed); |
|---|
| 180 | shadowedScene->addChild(ls.get()); |
|---|
| 181 | |
|---|
| 182 | return shadowedScene; |
|---|
| 183 | |
|---|
| 184 | } |
|---|
| 185 | else if (arguments.read("--st")) |
|---|
| 186 | { |
|---|
| 187 | |
|---|
| 188 | osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene; |
|---|
| 189 | |
|---|
| 190 | osg::ref_ptr<osgShadow::ShadowTexture> shadowTexture = new osgShadow::ShadowTexture; |
|---|
| 191 | shadowedScene->setShadowTechnique(shadowTexture.get()); |
|---|
| 192 | |
|---|
| 193 | osg::ref_ptr<osg::LightSource> ls = new osg::LightSource; |
|---|
| 194 | ls->getLight()->setPosition(osg::Vec4(lightPosition,1.0)); |
|---|
| 195 | |
|---|
| 196 | shadowedScene->setReceivesShadowTraversalMask(0x1); |
|---|
| 197 | shadowed->setNodeMask(shadowedScene->getReceivesShadowTraversalMask()); |
|---|
| 198 | |
|---|
| 199 | shadowedScene->setCastsShadowTraversalMask(0x2); |
|---|
| 200 | shadower->setNodeMask(shadowedScene->getCastsShadowTraversalMask()); |
|---|
| 201 | |
|---|
| 202 | shadowedScene->addChild(shadower); |
|---|
| 203 | shadowedScene->addChild(shadowed); |
|---|
| 204 | shadowedScene->addChild(ls.get()); |
|---|
| 205 | |
|---|
| 206 | return shadowedScene; |
|---|
| 207 | |
|---|
| 208 | } |
|---|
| 209 | else if (arguments.read("--sm")) |
|---|
| 210 | { |
|---|
| 211 | |
|---|
| 212 | osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene; |
|---|
| 213 | |
|---|
| 214 | osg::ref_ptr<osgShadow::ShadowMap> shadowMap = new osgShadow::ShadowMap; |
|---|
| 215 | shadowedScene->setShadowTechnique(shadowMap.get()); |
|---|
| 216 | |
|---|
| 217 | osg::ref_ptr<osg::LightSource> ls = new osg::LightSource; |
|---|
| 218 | ls->getLight()->setPosition(osg::Vec4(lightPosition,1.0)); |
|---|
| 219 | |
|---|
| 220 | shadowedScene->setReceivesShadowTraversalMask(0x1); |
|---|
| 221 | shadowed->setNodeMask(shadowedScene->getReceivesShadowTraversalMask()); |
|---|
| 222 | |
|---|
| 223 | shadowedScene->setCastsShadowTraversalMask(0x2); |
|---|
| 224 | shadower->setNodeMask(shadowedScene->getCastsShadowTraversalMask()); |
|---|
| 225 | |
|---|
| 226 | shadowedScene->addChild(shadower); |
|---|
| 227 | shadowedScene->addChild(shadowed); |
|---|
| 228 | shadowedScene->addChild(ls.get()); |
|---|
| 229 | |
|---|
| 230 | return shadowedScene; |
|---|
| 231 | |
|---|
| 232 | } |
|---|
| 233 | else |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | osg::Group* root = createShadowedScene(shadower,shadowed,lightPosition,radius/100.0f,1); |
|---|
| 237 | return root; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | int main(int argc, char ** argv) |
|---|
| 244 | { |
|---|
| 245 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | osgViewer::Viewer viewer; |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | viewer.setSceneData( createModel(arguments) ); |
|---|
| 252 | |
|---|
| 253 | viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 254 | |
|---|
| 255 | return viewer.run(); |
|---|
| 256 | } |
|---|