| 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 | #include <osg/LightSource> |
|---|
| 27 | #include <osg/TexGenNode> |
|---|
| 28 | |
|---|
| 29 | #include <osgUtil/Optimizer> |
|---|
| 30 | |
|---|
| 31 | #include <osgDB/Registry> |
|---|
| 32 | #include <osgDB/ReadFile> |
|---|
| 33 | |
|---|
| 34 | #include <osgViewer/Viewer> |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | #include "../osghangglide/terrain_coords.h" |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | osg::Image* createSpotLightImage(const osg::Vec4& centerColour, const osg::Vec4& backgroudColour, unsigned int size, float power) |
|---|
| 42 | { |
|---|
| 43 | osg::Image* image = new osg::Image; |
|---|
| 44 | image->allocateImage(size,size,1, |
|---|
| 45 | GL_RGBA,GL_UNSIGNED_BYTE); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | float mid = (float(size)-1)*0.5f; |
|---|
| 49 | float div = 2.0f/float(size); |
|---|
| 50 | for(unsigned int r=0;r<size;++r) |
|---|
| 51 | { |
|---|
| 52 | unsigned char* ptr = image->data(0,r,0); |
|---|
| 53 | for(unsigned int c=0;c<size;++c) |
|---|
| 54 | { |
|---|
| 55 | float dx = (float(c) - mid)*div; |
|---|
| 56 | float dy = (float(r) - mid)*div; |
|---|
| 57 | float r = powf(1.0f-sqrtf(dx*dx+dy*dy),power); |
|---|
| 58 | if (r<0.0f) r=0.0f; |
|---|
| 59 | osg::Vec4 color = centerColour*r+backgroudColour*(1.0f-r); |
|---|
| 60 | *ptr++ = (unsigned char)((color[0])*255.0f); |
|---|
| 61 | *ptr++ = (unsigned char)((color[1])*255.0f); |
|---|
| 62 | *ptr++ = (unsigned char)((color[2])*255.0f); |
|---|
| 63 | *ptr++ = (unsigned char)((color[3])*255.0f); |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | return image; |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | osg::StateSet* createSpotLightDecoratorState(unsigned int lightNum, unsigned int textureUnit) |
|---|
| 72 | { |
|---|
| 73 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 74 | |
|---|
| 75 | stateset->setMode(GL_LIGHT0+lightNum, osg::StateAttribute::ON); |
|---|
| 76 | |
|---|
| 77 | osg::Vec4 centerColour(1.0f,1.0f,1.0f,1.0f); |
|---|
| 78 | osg::Vec4 ambientColour(0.05f,0.05f,0.05f,1.0f); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | osg::Texture2D* texture = new osg::Texture2D(); |
|---|
| 82 | texture->setImage(createSpotLightImage(centerColour, ambientColour, 64, 1.0)); |
|---|
| 83 | texture->setBorderColor(osg::Vec4(ambientColour)); |
|---|
| 84 | texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_BORDER); |
|---|
| 85 | texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_BORDER); |
|---|
| 86 | texture->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_BORDER); |
|---|
| 87 | |
|---|
| 88 | stateset->setTextureAttributeAndModes(textureUnit, texture, osg::StateAttribute::ON); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | stateset->setTextureMode(textureUnit, GL_TEXTURE_GEN_S, osg::StateAttribute::ON); |
|---|
| 92 | stateset->setTextureMode(textureUnit, GL_TEXTURE_GEN_T, osg::StateAttribute::ON); |
|---|
| 93 | stateset->setTextureMode(textureUnit, GL_TEXTURE_GEN_R, osg::StateAttribute::ON); |
|---|
| 94 | stateset->setTextureMode(textureUnit, GL_TEXTURE_GEN_Q, osg::StateAttribute::ON); |
|---|
| 95 | |
|---|
| 96 | return stateset; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | osg::Node* createSpotLightNode(const osg::Vec3& position, const osg::Vec3& direction, float angle, unsigned int lightNum, unsigned int textureUnit) |
|---|
| 101 | { |
|---|
| 102 | osg::Group* group = new osg::Group; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | osg::LightSource* lightsource = new osg::LightSource; |
|---|
| 106 | osg::Light* light = lightsource->getLight(); |
|---|
| 107 | light->setLightNum(lightNum); |
|---|
| 108 | light->setPosition(osg::Vec4(position,1.0f)); |
|---|
| 109 | light->setAmbient(osg::Vec4(0.00f,0.00f,0.05f,1.0f)); |
|---|
| 110 | light->setDiffuse(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 111 | group->addChild(lightsource); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | osg::Vec3 up(0.0f,0.0f,1.0f); |
|---|
| 116 | up = (direction ^ up) ^ direction; |
|---|
| 117 | up.normalize(); |
|---|
| 118 | |
|---|
| 119 | osg::TexGenNode* texgenNode = new osg::TexGenNode; |
|---|
| 120 | texgenNode->setTextureUnit(textureUnit); |
|---|
| 121 | osg::TexGen* texgen = texgenNode->getTexGen(); |
|---|
| 122 | texgen->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 123 | texgen->setPlanesFromMatrix(osg::Matrixd::lookAt(position, position+direction, up)* |
|---|
| 124 | osg::Matrixd::perspective(angle,1.0,0.1,100)); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | group->addChild(texgenNode); |
|---|
| 128 | |
|---|
| 129 | return group; |
|---|
| 130 | |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 135 | { |
|---|
| 136 | |
|---|
| 137 | osg::AnimationPath* animationPath = new osg::AnimationPath; |
|---|
| 138 | animationPath->setLoopMode(osg::AnimationPath::LOOP); |
|---|
| 139 | |
|---|
| 140 | int numSamples = 40; |
|---|
| 141 | float yaw = 0.0f; |
|---|
| 142 | float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f); |
|---|
| 143 | float roll = osg::inDegrees(30.0f); |
|---|
| 144 | |
|---|
| 145 | double time=0.0f; |
|---|
| 146 | double time_delta = looptime/(double)numSamples; |
|---|
| 147 | for(int i=0;i<numSamples;++i) |
|---|
| 148 | { |
|---|
| 149 | osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); |
|---|
| 150 | 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))); |
|---|
| 151 | |
|---|
| 152 | animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); |
|---|
| 153 | |
|---|
| 154 | yaw += yaw_delta; |
|---|
| 155 | time += time_delta; |
|---|
| 156 | |
|---|
| 157 | } |
|---|
| 158 | return animationPath; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | osg::Node* createBase(const osg::Vec3& center,float radius) |
|---|
| 162 | { |
|---|
| 163 | |
|---|
| 164 | osg::Geode* geode = new osg::Geode; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 168 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 169 | if (image) |
|---|
| 170 | { |
|---|
| 171 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 172 | texture->setImage(image); |
|---|
| 173 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | geode->setStateSet( stateset ); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | osg::HeightField* grid = new osg::HeightField; |
|---|
| 180 | grid->allocate(38,39); |
|---|
| 181 | grid->setOrigin(center+osg::Vec3(-radius,-radius,0.0f)); |
|---|
| 182 | grid->setXInterval(radius*2.0f/(float)(38-1)); |
|---|
| 183 | grid->setYInterval(radius*2.0f/(float)(39-1)); |
|---|
| 184 | |
|---|
| 185 | float minHeight = FLT_MAX; |
|---|
| 186 | float maxHeight = -FLT_MAX; |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | unsigned int r; |
|---|
| 190 | for(r=0;r<39;++r) |
|---|
| 191 | { |
|---|
| 192 | for(unsigned int c=0;c<38;++c) |
|---|
| 193 | { |
|---|
| 194 | float h = vertex[r+c*39][2]; |
|---|
| 195 | if (h>maxHeight) maxHeight=h; |
|---|
| 196 | if (h<minHeight) minHeight=h; |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | float hieghtScale = radius*0.5f/(maxHeight-minHeight); |
|---|
| 201 | float hieghtOffset = -(minHeight+maxHeight)*0.5f; |
|---|
| 202 | |
|---|
| 203 | for(r=0;r<39;++r) |
|---|
| 204 | { |
|---|
| 205 | for(unsigned int c=0;c<38;++c) |
|---|
| 206 | { |
|---|
| 207 | float h = vertex[r+c*39][2]; |
|---|
| 208 | grid->setHeight(c,r,(h+hieghtOffset)*hieghtScale); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | geode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 213 | |
|---|
| 214 | osg::Group* group = new osg::Group; |
|---|
| 215 | group->addChild(geode); |
|---|
| 216 | |
|---|
| 217 | return group; |
|---|
| 218 | |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | osg::Node* createMovingModel(const osg::Vec3& center, float radius) |
|---|
| 222 | { |
|---|
| 223 | float animationLength = 10.0f; |
|---|
| 224 | |
|---|
| 225 | osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength); |
|---|
| 226 | |
|---|
| 227 | osg::Group* model = new osg::Group; |
|---|
| 228 | |
|---|
| 229 | osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); |
|---|
| 230 | if (cessna) |
|---|
| 231 | { |
|---|
| 232 | const osg::BoundingSphere& bs = cessna->getBound(); |
|---|
| 233 | |
|---|
| 234 | float size = radius/bs.radius()*0.3f; |
|---|
| 235 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 236 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 237 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 238 | osg::Matrix::scale(size,size,size)* |
|---|
| 239 | osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,2.0f)); |
|---|
| 240 | |
|---|
| 241 | positioned->addChild(cessna); |
|---|
| 242 | |
|---|
| 243 | osg::MatrixTransform* xform = new osg::MatrixTransform; |
|---|
| 244 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0)); |
|---|
| 245 | xform->addChild(positioned); |
|---|
| 246 | |
|---|
| 247 | xform->addChild(createSpotLightNode(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(0.0f,1.0f,-1.0f), 60.0f, 0, 1)); |
|---|
| 248 | |
|---|
| 249 | model->addChild(xform); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | return model; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | osg::Node* createModel() |
|---|
| 259 | { |
|---|
| 260 | osg::Vec3 center(0.0f,0.0f,0.0f); |
|---|
| 261 | float radius = 100.0f; |
|---|
| 262 | osg::Vec3 lightPosition(center+osg::Vec3(0.0f,0.0f,radius)); |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | osg::Node* shadower = createMovingModel(center,radius*0.5f); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | osg::Node* shadowed = createBase(center-osg::Vec3(0.0f,0.0f,radius*0.1),radius); |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | osg::Group* root = new osg::Group; |
|---|
| 272 | |
|---|
| 273 | root->setStateSet(createSpotLightDecoratorState(0,1)); |
|---|
| 274 | |
|---|
| 275 | root->addChild(shadower); |
|---|
| 276 | root->addChild(shadowed); |
|---|
| 277 | |
|---|
| 278 | return root; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | int main(int, char **) |
|---|
| 283 | { |
|---|
| 284 | |
|---|
| 285 | osgViewer::Viewer viewer; |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | viewer.setSceneData( createModel() ); |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | return viewer.run(); |
|---|
| 292 | } |
|---|