// -*-c++-*- #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include osg::ImageStream* s_imageStream = 0; class MovieEventHandler : public osgGA::GUIEventHandler { public: MovieEventHandler() {} void set(osg::Node* node); virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv); virtual void getUsage(osg::ApplicationUsage& usage) const; typedef std::vector< osg::ref_ptr > ImageStreamList; protected: virtual ~MovieEventHandler() {} class FindImageStreamsVisitor : public osg::NodeVisitor { public: FindImageStreamsVisitor(ImageStreamList& imageStreamList): _imageStreamList(imageStreamList) {} virtual void apply(osg::Geode& geode) { apply(geode.getStateSet()); for(unsigned int i=0;igetStateSet()); } traverse(geode); } virtual void apply(osg::Node& node) { apply(node.getStateSet()); traverse(node); } inline void apply(osg::StateSet* stateset) { if (!stateset) return; osg::StateAttribute* attr = stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE); if (attr) { osg::Texture2D* texture2D = dynamic_cast(attr); if (texture2D) apply(dynamic_cast(texture2D->getImage())); osg::TextureRectangle* textureRec = dynamic_cast(attr); if (textureRec) apply(dynamic_cast(textureRec->getImage())); } } inline void apply(osg::ImageStream* imagestream) { if (imagestream) { _imageStreamList.push_back(imagestream); s_imageStream = imagestream; } } ImageStreamList& _imageStreamList; }; ImageStreamList _imageStreamList; }; void MovieEventHandler::set(osg::Node* node) { _imageStreamList.clear(); if (node) { FindImageStreamsVisitor fisv(_imageStreamList); node->accept(fisv); } } bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv) { switch(ea.getEventType()) { case(osgGA::GUIEventAdapter::MOVE): case(osgGA::GUIEventAdapter::PUSH): case(osgGA::GUIEventAdapter::RELEASE): { osgViewer::View* view = dynamic_cast(&aa); osg::notify(osg::NOTICE)<<"osgmovie - view = "<computeIntersections(ea.getX(), ea.getY(), nv->getNodePath().back(), intersections)) { #if 1 osg::notify(osg::NOTICE)<<"osgmovie - Vertex interpolation not implemented yet"<asGeometry() : 0; osg::Vec3Array* vertices = geometry ? dynamic_cast(geometry->getVertexArray()) : 0; if (vertices) { // get the vertex indices. const osgUtil::LineSegmentIntersector::Intersection::IndexList& vil = intersection.indexList; if (vil.size()==3) { int i1 = vil[0]; int i2 = vil[1]; int i3 = vil[2]; osg::Vec3 v1 = (*vertices)[i1]; osg::Vec3 v2 = (*vertices)[i2]; osg::Vec3 v3 = (*vertices)[i3]; osg::Vec3 v = intersection.localIntersectionPoint; osg::Vec3 p1 = intersection.getLocalLineSegment()->start(); osg::Vec3 p2 = intersection.getLocalLineSegment()->end(); osg::Vec3 p12 = p1-p2; osg::Vec3 v13 = v1-v3; osg::Vec3 v23 = v2-v3; osg::Vec3 p1v3 = p1-v3; osg::Matrix matrix(p12.x(), v13.x(), v23.x(), 0.0, p12.y(), v13.y(), v23.y(), 0.0, p12.z(), v13.z(), v23.z(), 0.0, 0.0, 0.0, 0.0, 1.0); osg::Matrix inverse; inverse.invert(matrix); osg::Vec3 ratio = inverse*p1v3; // extract the baricentric coordinates. float r1 = ratio.y(); float r2 = ratio.z(); float r3 = 1.0f-r1-r2; osg::Array* texcoords = (geometry->getNumTexCoordArrays()>0) ? geometry->getTexCoordArray(0) : 0; osg::Vec2Array* texcoords_Vec2Array = dynamic_cast(texcoords); if (texcoords_Vec2Array) { // we have tex coord array so now we can compute the final tex coord at the point of intersection. osg::Vec2 tc1 = (*texcoords_Vec2Array)[i1]; osg::Vec2 tc2 = (*texcoords_Vec2Array)[i2]; osg::Vec2 tc3 = (*texcoords_Vec2Array)[i3]; osg::Vec2 tc = tc1*r1 + tc2*r2 + tc3*r3; osg::notify(osg::NOTICE)<<"We hit tex coords "<play(); } return true; } else if (ea.getKey()=='p') { for(ImageStreamList::iterator itr=_imageStreamList.begin(); itr!=_imageStreamList.end(); ++itr) { std::cout<<"Pause"<pause(); } return true; } else if (ea.getKey()=='r') { for(ImageStreamList::iterator itr=_imageStreamList.begin(); itr!=_imageStreamList.end(); ++itr) { std::cout<<"Restart"<rewind(); (*itr)->play(); } return true; } else if (ea.getKey()=='l') { for(ImageStreamList::iterator itr=_imageStreamList.begin(); itr!=_imageStreamList.end(); ++itr) { if ( (*itr)->getLoopingMode() == osg::ImageStream::LOOPING) { std::cout<<"Toggle Looping Off"<setLoopingMode( osg::ImageStream::NO_LOOPING ); } else { std::cout<<"Toggle Looping On"<setLoopingMode( osg::ImageStream::LOOPING ); } } return true; } return false; } default: return false; } return false; } void MovieEventHandler::getUsage(osg::ApplicationUsage& usage) const { usage.addKeyboardMouseBinding("p","Pause movie"); usage.addKeyboardMouseBinding("s","Play movie"); usage.addKeyboardMouseBinding("r","Restart movie"); usage.addKeyboardMouseBinding("l","Toggle looping of movie"); } osg::Geometry* myCreateTexturedQuadGeometry(const osg::Vec3& pos,float width,float height, osg::Image* image, bool useTextureRectangle) { if (useTextureRectangle) { osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(pos, osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,0.0f,height), 0.0f,image->t(), image->s(),0.0f); pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::TextureRectangle(image), osg::StateAttribute::ON); return pictureQuad; } else { osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(pos, osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,0.0f,height), 0.0f,1.0f, 1.0f,0.0f); osg::Texture2D* texture = new osg::Texture2D(image); texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); return pictureQuad; } } int main(int argc, char** argv) { // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); // set up the usage document, in case we need to print out how to use this program. arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example demonstrates the use of ImageStream for rendering movies as textures."); arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); arguments.getApplicationUsage()->addCommandLineOption("--texture2D","Use Texture2D rather than TextureRectangle."); arguments.getApplicationUsage()->addCommandLineOption("--shader","Use shaders to post process the video."); bool useTextureRectangle = true; bool useShader = false; // construct the viewer. osgViewer::Viewer viewer; while (arguments.read("--texture2D")) useTextureRectangle=false; while (arguments.read("--shader")) useShader=true; // if user request help write it out to cout. if (arguments.read("-h") || arguments.read("--help")) { arguments.getApplicationUsage()->write(std::cout); return 1; } osg::ref_ptr geode = new osg::Geode; osg::Vec3 pos(0.0f,0.0f,0.0f); osg::StateSet* stateset = geode->getOrCreateStateSet(); stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); if (useShader) { //useTextureRectangle = false; static const char *shaderSourceTextureRec = { "uniform vec4 cutoff_color;\n" "uniform samplerRect movie_texture;\n" "void main(void)\n" "{\n" " vec4 texture_color = textureRect(movie_texture, gl_TexCoord[0]); \n" " if (all(lessThanEqual(texture_color,cutoff_color))) discard; \n" " gl_FragColor = texture_color;\n" "}\n" }; static const char *shaderSourceTexture2D = { "uniform vec4 cutoff_color;\n" "uniform sampler2D movie_texture;\n" "void main(void)\n" "{\n" " vec4 texture_color = texture2D(movie_texture, gl_TexCoord[0]); \n" " if (all(lessThanEqual(texture_color,cutoff_color))) discard; \n" " gl_FragColor = texture_color;\n" "}\n" }; osg::Program* program = new osg::Program; program->addShader(new osg::Shader(osg::Shader::FRAGMENT, useTextureRectangle ? shaderSourceTextureRec : shaderSourceTexture2D)); stateset->addUniform(new osg::Uniform("cutoff_color",osg::Vec4(0.1f,0.1f,0.1f,1.0f))); stateset->addUniform(new osg::Uniform("movie_texture",0)); stateset->setAttribute(program); } for(int i=1;i(image); if (imagestream) imagestream->play(); if (image) { geode->addDrawable(myCreateTexturedQuadGeometry(pos,image->s(),image->t(),image, useTextureRectangle)); pos.z() += image->t()*1.5f; } else { std::cout<<"Unable to read file "<getNumDrawables()==0) { // nothing loaded. arguments.getApplicationUsage()->write(std::cout); return 1; } // pass the model to the MovieEventHandler so it can pick out ImageStream's to manipulate. MovieEventHandler* meh = new MovieEventHandler(); geode->setEventCallback(meh); meh->set(geode.get()); // report any errors if they have occured when parsing the program aguments. if (arguments.errors()) { arguments.writeErrorMessages(std::cout); return 1; } if (arguments.argc()<=1) { arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); return 1; } // set the scene to render viewer.setSceneData(geode.get()); // create the windows and run the threads. return viewer.run(); }