| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgViewer/Viewer> |
|---|
| 20 | |
|---|
| 21 | #include <osgDB/ReadFile> |
|---|
| 22 | |
|---|
| 23 | #include <osg/Geode> |
|---|
| 24 | #include <osg/Geometry> |
|---|
| 25 | #include <osg/StateSet> |
|---|
| 26 | #include <osg/Material> |
|---|
| 27 | #include <osg/Texture2D> |
|---|
| 28 | #include <osg/TextureRectangle> |
|---|
| 29 | #include <osg/TextureCubeMap> |
|---|
| 30 | #include <osg/TexMat> |
|---|
| 31 | #include <osg/CullFace> |
|---|
| 32 | #include <osg/ImageStream> |
|---|
| 33 | #include <osg/io_utils> |
|---|
| 34 | |
|---|
| 35 | #include <osgGA/TrackballManipulator> |
|---|
| 36 | #include <osgGA/EventVisitor> |
|---|
| 37 | |
|---|
| 38 | #include <iostream> |
|---|
| 39 | |
|---|
| 40 | osg::ImageStream* s_imageStream = 0; |
|---|
| 41 | class MovieEventHandler : public osgGA::GUIEventHandler |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | |
|---|
| 45 | MovieEventHandler():_trackMouse(false) {} |
|---|
| 46 | |
|---|
| 47 | void setMouseTracking(bool track) { _trackMouse = track; } |
|---|
| 48 | bool getMouseTracking() const { return _trackMouse; } |
|---|
| 49 | |
|---|
| 50 | void set(osg::Node* node); |
|---|
| 51 | |
|---|
| 52 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv); |
|---|
| 53 | |
|---|
| 54 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 55 | |
|---|
| 56 | typedef std::vector< osg::ref_ptr<osg::ImageStream> > ImageStreamList; |
|---|
| 57 | |
|---|
| 58 | protected: |
|---|
| 59 | |
|---|
| 60 | virtual ~MovieEventHandler() {} |
|---|
| 61 | |
|---|
| 62 | class FindImageStreamsVisitor : public osg::NodeVisitor |
|---|
| 63 | { |
|---|
| 64 | public: |
|---|
| 65 | FindImageStreamsVisitor(ImageStreamList& imageStreamList): |
|---|
| 66 | _imageStreamList(imageStreamList) {} |
|---|
| 67 | |
|---|
| 68 | virtual void apply(osg::Geode& geode) |
|---|
| 69 | { |
|---|
| 70 | apply(geode.getStateSet()); |
|---|
| 71 | |
|---|
| 72 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
|---|
| 73 | { |
|---|
| 74 | apply(geode.getDrawable(i)->getStateSet()); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | traverse(geode); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | virtual void apply(osg::Node& node) |
|---|
| 81 | { |
|---|
| 82 | apply(node.getStateSet()); |
|---|
| 83 | traverse(node); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | inline void apply(osg::StateSet* stateset) |
|---|
| 87 | { |
|---|
| 88 | if (!stateset) return; |
|---|
| 89 | |
|---|
| 90 | osg::StateAttribute* attr = stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE); |
|---|
| 91 | if (attr) |
|---|
| 92 | { |
|---|
| 93 | osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr); |
|---|
| 94 | if (texture2D) apply(dynamic_cast<osg::ImageStream*>(texture2D->getImage())); |
|---|
| 95 | |
|---|
| 96 | osg::TextureRectangle* textureRec = dynamic_cast<osg::TextureRectangle*>(attr); |
|---|
| 97 | if (textureRec) apply(dynamic_cast<osg::ImageStream*>(textureRec->getImage())); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | inline void apply(osg::ImageStream* imagestream) |
|---|
| 102 | { |
|---|
| 103 | if (imagestream) |
|---|
| 104 | { |
|---|
| 105 | _imageStreamList.push_back(imagestream); |
|---|
| 106 | s_imageStream = imagestream; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | ImageStreamList& _imageStreamList; |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | bool _trackMouse; |
|---|
| 115 | ImageStreamList _imageStreamList; |
|---|
| 116 | |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | void MovieEventHandler::set(osg::Node* node) |
|---|
| 122 | { |
|---|
| 123 | _imageStreamList.clear(); |
|---|
| 124 | if (node) |
|---|
| 125 | { |
|---|
| 126 | FindImageStreamsVisitor fisv(_imageStreamList); |
|---|
| 127 | node->accept(fisv); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv) |
|---|
| 133 | { |
|---|
| 134 | switch(ea.getEventType()) |
|---|
| 135 | { |
|---|
| 136 | case(osgGA::GUIEventAdapter::MOVE): |
|---|
| 137 | case(osgGA::GUIEventAdapter::PUSH): |
|---|
| 138 | case(osgGA::GUIEventAdapter::RELEASE): |
|---|
| 139 | { |
|---|
| 140 | if (_trackMouse) |
|---|
| 141 | { |
|---|
| 142 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa); |
|---|
| 143 | osgUtil::LineSegmentIntersector::Intersections intersections; |
|---|
| 144 | if (view && view->computeIntersections(ea.getX(), ea.getY(), nv->getNodePath(), intersections)) |
|---|
| 145 | { |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | const osgUtil::LineSegmentIntersector::Intersection& intersection = *(intersections.begin()); |
|---|
| 149 | osg::Drawable* drawable = intersection.drawable.get(); |
|---|
| 150 | osg::Geometry* geometry = drawable ? drawable->asGeometry() : 0; |
|---|
| 151 | osg::Vec3Array* vertices = geometry ? dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()) : 0; |
|---|
| 152 | if (vertices) |
|---|
| 153 | { |
|---|
| 154 | |
|---|
| 155 | const osgUtil::LineSegmentIntersector::Intersection::IndexList& indices = intersection.indexList; |
|---|
| 156 | const osgUtil::LineSegmentIntersector::Intersection::RatioList& ratios = intersection.ratioList; |
|---|
| 157 | |
|---|
| 158 | if (indices.size()==3 && ratios.size()==3) |
|---|
| 159 | { |
|---|
| 160 | unsigned int i1 = indices[0]; |
|---|
| 161 | unsigned int i2 = indices[1]; |
|---|
| 162 | unsigned int i3 = indices[2]; |
|---|
| 163 | |
|---|
| 164 | float r1 = ratios[0]; |
|---|
| 165 | float r2 = ratios[1]; |
|---|
| 166 | float r3 = ratios[2]; |
|---|
| 167 | |
|---|
| 168 | osg::Array* texcoords = (geometry->getNumTexCoordArrays()>0) ? geometry->getTexCoordArray(0) : 0; |
|---|
| 169 | osg::Vec2Array* texcoords_Vec2Array = dynamic_cast<osg::Vec2Array*>(texcoords); |
|---|
| 170 | if (texcoords_Vec2Array) |
|---|
| 171 | { |
|---|
| 172 | |
|---|
| 173 | osg::Vec2 tc1 = (*texcoords_Vec2Array)[i1]; |
|---|
| 174 | osg::Vec2 tc2 = (*texcoords_Vec2Array)[i2]; |
|---|
| 175 | osg::Vec2 tc3 = (*texcoords_Vec2Array)[i3]; |
|---|
| 176 | osg::Vec2 tc = tc1*r1 + tc2*r2 + tc3*r3; |
|---|
| 177 | |
|---|
| 178 | osg::notify(osg::NOTICE)<<"We hit tex coords "<<tc<<std::endl; |
|---|
| 179 | |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | else |
|---|
| 183 | { |
|---|
| 184 | osg::notify(osg::NOTICE)<<"Intersection has insufficient indices to work with"; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | else |
|---|
| 190 | { |
|---|
| 191 | osg::notify(osg::NOTICE)<<"No intersection"<<std::endl; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | break; |
|---|
| 195 | } |
|---|
| 196 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 197 | { |
|---|
| 198 | if (ea.getKey()=='s') |
|---|
| 199 | { |
|---|
| 200 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 201 | itr!=_imageStreamList.end(); |
|---|
| 202 | ++itr) |
|---|
| 203 | { |
|---|
| 204 | std::cout<<"Play"<<std::endl; |
|---|
| 205 | (*itr)->play(); |
|---|
| 206 | } |
|---|
| 207 | return true; |
|---|
| 208 | } |
|---|
| 209 | else if (ea.getKey()=='p') |
|---|
| 210 | { |
|---|
| 211 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 212 | itr!=_imageStreamList.end(); |
|---|
| 213 | ++itr) |
|---|
| 214 | { |
|---|
| 215 | std::cout<<"Pause"<<std::endl; |
|---|
| 216 | (*itr)->pause(); |
|---|
| 217 | } |
|---|
| 218 | return true; |
|---|
| 219 | } |
|---|
| 220 | else if (ea.getKey()=='r') |
|---|
| 221 | { |
|---|
| 222 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 223 | itr!=_imageStreamList.end(); |
|---|
| 224 | ++itr) |
|---|
| 225 | { |
|---|
| 226 | std::cout<<"Restart"<<std::endl; |
|---|
| 227 | (*itr)->rewind(); |
|---|
| 228 | (*itr)->play(); |
|---|
| 229 | } |
|---|
| 230 | return true; |
|---|
| 231 | } |
|---|
| 232 | else if (ea.getKey()=='l') |
|---|
| 233 | { |
|---|
| 234 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 235 | itr!=_imageStreamList.end(); |
|---|
| 236 | ++itr) |
|---|
| 237 | { |
|---|
| 238 | if ( (*itr)->getLoopingMode() == osg::ImageStream::LOOPING) |
|---|
| 239 | { |
|---|
| 240 | std::cout<<"Toggle Looping Off"<<std::endl; |
|---|
| 241 | (*itr)->setLoopingMode( osg::ImageStream::NO_LOOPING ); |
|---|
| 242 | } |
|---|
| 243 | else |
|---|
| 244 | { |
|---|
| 245 | std::cout<<"Toggle Looping On"<<std::endl; |
|---|
| 246 | (*itr)->setLoopingMode( osg::ImageStream::LOOPING ); |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | return true; |
|---|
| 250 | } |
|---|
| 251 | return false; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | default: |
|---|
| 255 | return false; |
|---|
| 256 | } |
|---|
| 257 | return false; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | void MovieEventHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 261 | { |
|---|
| 262 | usage.addKeyboardMouseBinding("p","Pause movie"); |
|---|
| 263 | usage.addKeyboardMouseBinding("s","Play movie"); |
|---|
| 264 | usage.addKeyboardMouseBinding("r","Restart movie"); |
|---|
| 265 | usage.addKeyboardMouseBinding("l","Toggle looping of movie"); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | osg::Geometry* myCreateTexturedQuadGeometry(const osg::Vec3& pos,float width,float height, osg::Image* image, bool useTextureRectangle) |
|---|
| 270 | { |
|---|
| 271 | bool flip = image->getOrigin()==osg::Image::TOP_LEFT; |
|---|
| 272 | if (useTextureRectangle) |
|---|
| 273 | { |
|---|
| 274 | osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(pos, |
|---|
| 275 | osg::Vec3(width,0.0f,0.0f), |
|---|
| 276 | osg::Vec3(0.0f,0.0f,height), |
|---|
| 277 | 0.0f, flip ? image->t() : 0.0, image->s(), flip ? 0.0 : image->t()); |
|---|
| 278 | |
|---|
| 279 | osg::TextureRectangle* texture = new osg::TextureRectangle(image); |
|---|
| 280 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 281 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, |
|---|
| 285 | texture, |
|---|
| 286 | osg::StateAttribute::ON); |
|---|
| 287 | |
|---|
| 288 | return pictureQuad; |
|---|
| 289 | } |
|---|
| 290 | else |
|---|
| 291 | { |
|---|
| 292 | osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(pos, |
|---|
| 293 | osg::Vec3(width,0.0f,0.0f), |
|---|
| 294 | osg::Vec3(0.0f,0.0f,height), |
|---|
| 295 | 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); |
|---|
| 296 | |
|---|
| 297 | osg::Texture2D* texture = new osg::Texture2D(image); |
|---|
| 298 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 299 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 300 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, |
|---|
| 304 | texture, |
|---|
| 305 | osg::StateAttribute::ON); |
|---|
| 306 | |
|---|
| 307 | return pictureQuad; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | osg::Geometry* createDomeDistortionMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, |
|---|
| 312 | osg::ArgumentParser& arguments) |
|---|
| 313 | { |
|---|
| 314 | double sphere_radius = 1.0; |
|---|
| 315 | if (arguments.read("--radius", sphere_radius)) {} |
|---|
| 316 | |
|---|
| 317 | double collar_radius = 0.45; |
|---|
| 318 | if (arguments.read("--collar", collar_radius)) {} |
|---|
| 319 | |
|---|
| 320 | double rotationDegrees = 180.0; |
|---|
| 321 | if (arguments.read("--rotation", rotationDegrees)) {} |
|---|
| 322 | |
|---|
| 323 | osg::Vec3d center(0.0,0.0,0.0); |
|---|
| 324 | osg::Vec3d eye(0.0,0.0,0.0); |
|---|
| 325 | |
|---|
| 326 | double distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius); |
|---|
| 327 | if (arguments.read("--distance", distance)) {} |
|---|
| 328 | |
|---|
| 329 | bool centerProjection = false; |
|---|
| 330 | |
|---|
| 331 | osg::Vec3d projector = eye - osg::Vec3d(0.0,0.0, distance); |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | osg::notify(osg::NOTICE)<<"Projector position = "<<projector<<std::endl; |
|---|
| 335 | osg::notify(osg::NOTICE)<<"distance = "<<distance<<std::endl; |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | osg::Geometry* geometry = new osg::Geometry(); |
|---|
| 340 | |
|---|
| 341 | geometry->setSupportsDisplayList(false); |
|---|
| 342 | |
|---|
| 343 | osg::Vec3 xAxis(widthVector); |
|---|
| 344 | float width = widthVector.length(); |
|---|
| 345 | xAxis /= width; |
|---|
| 346 | |
|---|
| 347 | osg::Vec3 yAxis(heightVector); |
|---|
| 348 | float height = heightVector.length(); |
|---|
| 349 | yAxis /= height; |
|---|
| 350 | |
|---|
| 351 | int noSteps = 160; |
|---|
| 352 | |
|---|
| 353 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 354 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
|---|
| 355 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 356 | |
|---|
| 357 | osg::Vec3 bottom = origin; |
|---|
| 358 | osg::Vec3 dx = xAxis*(width/((float)(noSteps-2))); |
|---|
| 359 | osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); |
|---|
| 360 | |
|---|
| 361 | osg::Vec3 top = origin + yAxis*height; |
|---|
| 362 | |
|---|
| 363 | osg::Vec3d screenCenter = origin + widthVector*0.5f + heightVector*0.5f; |
|---|
| 364 | float screenRadius = heightVector.length() * 0.5f; |
|---|
| 365 | |
|---|
| 366 | double rotation = osg::DegreesToRadians(rotationDegrees); |
|---|
| 367 | |
|---|
| 368 | osg::Vec3 cursor = bottom; |
|---|
| 369 | int i,j; |
|---|
| 370 | |
|---|
| 371 | int midSteps = noSteps/2; |
|---|
| 372 | |
|---|
| 373 | bool flip = false; |
|---|
| 374 | if (arguments.read("--flip")) { flip = true; } |
|---|
| 375 | |
|---|
| 376 | for(i=0;i<midSteps;++i) |
|---|
| 377 | { |
|---|
| 378 | osg::Vec3 cursor = bottom+dy*(float)i; |
|---|
| 379 | for(j=0;j<midSteps;++j) |
|---|
| 380 | { |
|---|
| 381 | osg::Vec2 delta(cursor.x() - screenCenter.x(), cursor.y() - screenCenter.y()); |
|---|
| 382 | double theta = atan2(delta.x(), -delta.y()); |
|---|
| 383 | theta += 2*osg::PI; |
|---|
| 384 | |
|---|
| 385 | double phi = osg::PI_2 * delta.length() / screenRadius; |
|---|
| 386 | if (phi > osg::PI_2) phi = osg::PI_2; |
|---|
| 387 | |
|---|
| 388 | double f = distance * sin(phi); |
|---|
| 389 | double e = distance * cos(phi) + sqrt( sphere_radius*sphere_radius - f*f); |
|---|
| 390 | double l = e * cos(phi); |
|---|
| 391 | double h = e * sin(phi); |
|---|
| 392 | double gamma = atan2(h, l-distance); |
|---|
| 393 | |
|---|
| 394 | osg::Vec2 texcoord(theta/(2.0*osg::PI), 1.0-gamma/osg::PI); |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | if (flip) |
|---|
| 399 | vertices->push_back(osg::Vec3(cursor.x(), top.y()-(cursor.y()-origin.y()),cursor.z())); |
|---|
| 400 | else |
|---|
| 401 | vertices->push_back(cursor); |
|---|
| 402 | |
|---|
| 403 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 404 | texcoords->push_back(texcoord); |
|---|
| 405 | |
|---|
| 406 | if (j+1<midSteps) cursor += dx; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | for(;j<noSteps;++j) |
|---|
| 410 | { |
|---|
| 411 | osg::Vec2 delta(cursor.x() - screenCenter.x(), cursor.y() - screenCenter.y()); |
|---|
| 412 | double theta = atan2(delta.x(), -delta.y()); |
|---|
| 413 | double phi = osg::PI_2 * delta.length() / screenRadius; |
|---|
| 414 | if (phi > osg::PI_2) phi = osg::PI_2; |
|---|
| 415 | |
|---|
| 416 | double f = distance * sin(phi); |
|---|
| 417 | double e = distance * cos(phi) + sqrt( sphere_radius*sphere_radius - f*f); |
|---|
| 418 | double l = e * cos(phi); |
|---|
| 419 | double h = e * sin(phi); |
|---|
| 420 | double gamma = atan2(h, l-distance); |
|---|
| 421 | |
|---|
| 422 | osg::Vec2 texcoord(theta/(2.0*osg::PI), 1.0-gamma/osg::PI); |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | if (flip) |
|---|
| 427 | vertices->push_back(osg::Vec3(cursor.x(), top.y()-(cursor.y()-origin.y()),cursor.z())); |
|---|
| 428 | else |
|---|
| 429 | vertices->push_back(cursor); |
|---|
| 430 | |
|---|
| 431 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 432 | texcoords->push_back(texcoord); |
|---|
| 433 | |
|---|
| 434 | cursor += dx; |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | for(;i<noSteps;++i) |
|---|
| 440 | { |
|---|
| 441 | osg::Vec3 cursor = bottom+dy*(float)i; |
|---|
| 442 | for(j=0;j<noSteps;++j) |
|---|
| 443 | { |
|---|
| 444 | osg::Vec2 delta(cursor.x() - screenCenter.x(), cursor.y() - screenCenter.y()); |
|---|
| 445 | double theta = atan2(delta.x(), -delta.y()); |
|---|
| 446 | if (theta<0.0) theta += 2*osg::PI; |
|---|
| 447 | double phi = osg::PI_2 * delta.length() / screenRadius; |
|---|
| 448 | if (phi > osg::PI_2) phi = osg::PI_2; |
|---|
| 449 | |
|---|
| 450 | double f = distance * sin(phi); |
|---|
| 451 | double e = distance * cos(phi) + sqrt( sphere_radius*sphere_radius - f*f); |
|---|
| 452 | double l = e * cos(phi); |
|---|
| 453 | double h = e * sin(phi); |
|---|
| 454 | double gamma = atan2(h, l-distance); |
|---|
| 455 | |
|---|
| 456 | osg::Vec2 texcoord(theta/(2.0*osg::PI), 1.0-gamma/osg::PI); |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | if (flip) |
|---|
| 461 | vertices->push_back(osg::Vec3(cursor.x(), top.y()-(cursor.y()-origin.y()),cursor.z())); |
|---|
| 462 | else |
|---|
| 463 | vertices->push_back(cursor); |
|---|
| 464 | |
|---|
| 465 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 466 | texcoords->push_back(texcoord); |
|---|
| 467 | |
|---|
| 468 | cursor += dx; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | geometry->setVertexArray(vertices); |
|---|
| 476 | |
|---|
| 477 | geometry->setColorArray(colors); |
|---|
| 478 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 479 | |
|---|
| 480 | geometry->setTexCoordArray(0,texcoords); |
|---|
| 481 | |
|---|
| 482 | for(i=0;i<noSteps-1;++i) |
|---|
| 483 | { |
|---|
| 484 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); |
|---|
| 485 | for(j=0;j<noSteps;++j) |
|---|
| 486 | { |
|---|
| 487 | elements->push_back(j+(i+1)*noSteps); |
|---|
| 488 | elements->push_back(j+(i)*noSteps); |
|---|
| 489 | } |
|---|
| 490 | geometry->addPrimitiveSet(elements); |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | return geometry; |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& arguments) |
|---|
| 498 | { |
|---|
| 499 | |
|---|
| 500 | viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 501 | |
|---|
| 502 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 503 | if (!wsi) |
|---|
| 504 | { |
|---|
| 505 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 506 | return; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | unsigned int screenNum = 0; |
|---|
| 510 | while (arguments.read("--screen",screenNum)) {} |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | unsigned int width, height; |
|---|
| 514 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(screenNum), width, height); |
|---|
| 515 | |
|---|
| 516 | while (arguments.read("--width",width)) {} |
|---|
| 517 | while (arguments.read("--height",height)) {} |
|---|
| 518 | |
|---|
| 519 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 520 | traits->screenNum = screenNum; |
|---|
| 521 | traits->x = 0; |
|---|
| 522 | traits->y = 0; |
|---|
| 523 | traits->width = width; |
|---|
| 524 | traits->height = height; |
|---|
| 525 | traits->windowDecoration = false; |
|---|
| 526 | traits->doubleBuffer = true; |
|---|
| 527 | traits->sharedContext = 0; |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 532 | if (!gc) |
|---|
| 533 | { |
|---|
| 534 | osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 535 | return; |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | osg::ref_ptr<osg::Drawable> distortionCorrectionMash = createDomeDistortionMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), arguments); |
|---|
| 539 | |
|---|
| 540 | osg::Texture* texture = 0; |
|---|
| 541 | for(int i=1;i<arguments.argc() && !texture;++i) |
|---|
| 542 | { |
|---|
| 543 | if (arguments.isString(i)) |
|---|
| 544 | { |
|---|
| 545 | osg::Image* image = osgDB::readImageFile(arguments[i]); |
|---|
| 546 | osg::ImageStream* imagestream = dynamic_cast<osg::ImageStream*>(image); |
|---|
| 547 | if (imagestream) imagestream->play(); |
|---|
| 548 | |
|---|
| 549 | if (image) |
|---|
| 550 | { |
|---|
| 551 | #if 1 |
|---|
| 552 | texture = new osg::TextureRectangle(image); |
|---|
| 553 | #else |
|---|
| 554 | texture = new osg::Texture2D(image); |
|---|
| 555 | #endif |
|---|
| 556 | } |
|---|
| 557 | } |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | if (!texture) |
|---|
| 561 | { |
|---|
| 562 | return; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | { |
|---|
| 567 | osg::Geode* geode = new osg::Geode(); |
|---|
| 568 | geode->addDrawable(distortionCorrectionMash.get()); |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | |
|---|
| 572 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 573 | stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); |
|---|
| 574 | texture->setMaxAnisotropy(16.0f); |
|---|
| 575 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 576 | #if 1 |
|---|
| 577 | osg::TexMat* texmat = new osg::TexMat; |
|---|
| 578 | texmat->setScaleByTextureRectangleSize(true); |
|---|
| 579 | stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON); |
|---|
| 580 | #endif |
|---|
| 581 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 582 | camera->setGraphicsContext(gc.get()); |
|---|
| 583 | camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); |
|---|
| 584 | camera->setClearColor( osg::Vec4(0.1,0.1,1.0,1.0) ); |
|---|
| 585 | camera->setViewport(new osg::Viewport(0, 0, width, height)); |
|---|
| 586 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 587 | camera->setDrawBuffer(buffer); |
|---|
| 588 | camera->setReadBuffer(buffer); |
|---|
| 589 | camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); |
|---|
| 590 | camera->setAllowEventFocus(false); |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | camera->setProjectionMatrixAsOrtho2D(0,width,0,height); |
|---|
| 595 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | camera->setName("DistortionCorrectionCamera"); |
|---|
| 601 | |
|---|
| 602 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd(), true); |
|---|
| 603 | |
|---|
| 604 | viewer.setSceneData(geode); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | viewer.getCamera()->setNearFarRatio(0.0001f); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | int main(int argc, char** argv) |
|---|
| 612 | { |
|---|
| 613 | |
|---|
| 614 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 618 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example demonstrates the use of ImageStream for rendering movies as textures."); |
|---|
| 619 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 620 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 621 | arguments.getApplicationUsage()->addCommandLineOption("--texture2D","Use Texture2D rather than TextureRectangle."); |
|---|
| 622 | arguments.getApplicationUsage()->addCommandLineOption("--shader","Use shaders to post process the video."); |
|---|
| 623 | arguments.getApplicationUsage()->addCommandLineOption("--dome","Use full dome distortion correction."); |
|---|
| 624 | |
|---|
| 625 | bool useTextureRectangle = true; |
|---|
| 626 | bool useShader = false; |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | osgViewer::Viewer viewer; |
|---|
| 630 | |
|---|
| 631 | if (arguments.argc()<=1) |
|---|
| 632 | { |
|---|
| 633 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 634 | return 1; |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | while (arguments.read("--texture2D")) useTextureRectangle=false; |
|---|
| 638 | while (arguments.read("--shader")) useShader=true; |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 642 | { |
|---|
| 643 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 644 | return 1; |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | if (arguments.read("--dome") || arguments.read("--puffer") ) |
|---|
| 650 | { |
|---|
| 651 | setDomeCorrection(viewer, arguments); |
|---|
| 652 | } |
|---|
| 653 | else |
|---|
| 654 | { |
|---|
| 655 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 656 | osg::Vec3 pos(0.0f,0.0f,0.0f); |
|---|
| 657 | |
|---|
| 658 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 659 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 660 | |
|---|
| 661 | if (useShader) |
|---|
| 662 | { |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | static const char *shaderSourceTextureRec = { |
|---|
| 666 | "uniform vec4 cutoff_color;\n" |
|---|
| 667 | "uniform samplerRect movie_texture;\n" |
|---|
| 668 | "void main(void)\n" |
|---|
| 669 | "{\n" |
|---|
| 670 | " vec4 texture_color = textureRect(movie_texture, gl_TexCoord[0]); \n" |
|---|
| 671 | " if (all(lessThanEqual(texture_color,cutoff_color))) discard; \n" |
|---|
| 672 | " gl_FragColor = texture_color;\n" |
|---|
| 673 | "}\n" |
|---|
| 674 | }; |
|---|
| 675 | |
|---|
| 676 | static const char *shaderSourceTexture2D = { |
|---|
| 677 | "uniform vec4 cutoff_color;\n" |
|---|
| 678 | "uniform sampler2D movie_texture;\n" |
|---|
| 679 | "void main(void)\n" |
|---|
| 680 | "{\n" |
|---|
| 681 | " vec4 texture_color = texture2D(movie_texture, gl_TexCoord[0]); \n" |
|---|
| 682 | " if (all(lessThanEqual(texture_color,cutoff_color))) discard; \n" |
|---|
| 683 | " gl_FragColor = texture_color;\n" |
|---|
| 684 | "}\n" |
|---|
| 685 | }; |
|---|
| 686 | |
|---|
| 687 | osg::Program* program = new osg::Program; |
|---|
| 688 | |
|---|
| 689 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, |
|---|
| 690 | useTextureRectangle ? shaderSourceTextureRec : shaderSourceTexture2D)); |
|---|
| 691 | |
|---|
| 692 | stateset->addUniform(new osg::Uniform("cutoff_color",osg::Vec4(0.1f,0.1f,0.1f,1.0f))); |
|---|
| 693 | stateset->addUniform(new osg::Uniform("movie_texture",0)); |
|---|
| 694 | |
|---|
| 695 | stateset->setAttribute(program); |
|---|
| 696 | |
|---|
| 697 | } |
|---|
| 698 | |
|---|
| 699 | for(int i=1;i<arguments.argc();++i) |
|---|
| 700 | { |
|---|
| 701 | if (arguments.isString(i)) |
|---|
| 702 | { |
|---|
| 703 | osg::Image* image = osgDB::readImageFile(arguments[i]); |
|---|
| 704 | osg::ImageStream* imagestream = dynamic_cast<osg::ImageStream*>(image); |
|---|
| 705 | if (imagestream) imagestream->play(); |
|---|
| 706 | |
|---|
| 707 | if (image) |
|---|
| 708 | { |
|---|
| 709 | geode->addDrawable(myCreateTexturedQuadGeometry(pos,image->s(),image->t(),image, useTextureRectangle)); |
|---|
| 710 | |
|---|
| 711 | pos.z() += image->t()*1.5f; |
|---|
| 712 | } |
|---|
| 713 | else |
|---|
| 714 | { |
|---|
| 715 | std::cout<<"Unable to read file "<<arguments[i]<<std::endl; |
|---|
| 716 | } |
|---|
| 717 | } |
|---|
| 718 | } |
|---|
| 719 | |
|---|
| 720 | |
|---|
| 721 | viewer.setSceneData(geode.get()); |
|---|
| 722 | |
|---|
| 723 | if (viewer.getSceneData()==0) |
|---|
| 724 | { |
|---|
| 725 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 726 | return 1; |
|---|
| 727 | } |
|---|
| 728 | } |
|---|
| 729 | |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | MovieEventHandler* meh = new MovieEventHandler(); |
|---|
| 733 | meh->set(viewer.getSceneData()); |
|---|
| 734 | viewer.addEventHandler(meh); |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | if (arguments.errors()) |
|---|
| 738 | { |
|---|
| 739 | arguments.writeErrorMessages(std::cout); |
|---|
| 740 | return 1; |
|---|
| 741 | } |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | return viewer.run(); |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | } |
|---|