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