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