bool OsgMovie::loadFile(std::string filename) { osgDB::Registry::instance()->loadLibrary("osgdb_ffmpeg.so"); //create shaders for left and right eye (use uniform to set mode, and also set which eye) // int eye 0 right eye, 1 left eye // mode 0 mono, 1 top down, 2 left right, 3 interlaced (need to complete 3 interlaced) /* static const char *fragShaderTop = { "#extension GL_ARB_texture_rectangle : enable\n" "uniform sampler2DRect movie_texture;\n" "void main(void)\n" "{\n" " vec2 coord = gl_TexCoord[0].st;\n" " ivec2 size = textureSize(movie_texture, 0);\n" " coord.t = (size.t * 0.5) + (coord.t * 0.5); \n" " gl_FragColor = texture2DRect(movie_texture, coord);\n" "}\n" }; static const char *fragShaderBottom = { "#extension GL_ARB_texture_rectangle : enable\n" "uniform sampler2DRect movie_texture;\n" "void main(void)\n" "{\n" " vec2 coord = gl_TexCoord[0].st;\n" " coord.t = (coord.t * 0.5); \n" " gl_FragColor = texture2DRect(movie_texture, coord);\n" "}\n" }; static const char *fragShaderLeft = { "#extension GL_ARB_texture_rectangle : enable\n" "uniform sampler2DRect movie_texture;\n" "void main(void)\n" "{\n" " vec2 coord = gl_TexCoord[0].st;\n" " coord.s = (size.s * 0.5) \n" " gl_FragColor = texture2DRect(movie_texture, coord);\n" "}\n" }; static const char *fragShaderRight = { "#extension GL_ARB_texture_rectangle : enable\n" "uniform sampler2DRect movie_texture;\n" "void main(void)\n" "{\n" " vec2 coord = gl_TexCoord[0].st;\n" " ivec2 size = textureSize(movie_texture, 0);\n" " coord.s = (size.s * 0.5) + (coord.s * 0.5); \n" " gl_FragColor = texture2DRect(movie_texture, coord);\n" "}\n" }; static const char *fragShaderEven = { "#extension GL_ARB_texture_rectangle : enable\n" "uniform sampler2DRect movie_texture;\n" "void main(void)\n" "{\n" " vec2 coord = gl_TexCoord[0].st;\n" " ivec2 size = textureSize(movie_texture, 0); \n" " if( findLSB(coord.y) ) \n" " coord.s = (size.s * 0.5) + (coord.s * 0.5); \n" " gl_FragColor = texture2DRect(movie_texture, coord);\n" "}\n" }; */ static const char *fragShader = { "#extension GL_ARB_texture_rectangle : enable \n" "uniform sampler2DRect movie_texture; \n" "uniform int split;\n" "uniform int mode;\n" "uniform int type;\n" "uniform int eye;\n" "void main(void)\n" "{\n" " vec2 coord = gl_TexCoord[0].st; \n" " ivec2 size = textureSize(movie_texture, 0);\n" " if( (mode == 0) && split ) \n" " { \n" " if( type == 1 ) \n" " coord.y = (coord.y * 0.5); \n" " else \n" " coord.x = (coord.x * 0.5); \n" " } \n" " else if( mode == 1) \n" " { \n" " if( type == 1 ) \n" " coord.y = (coord.y * 0.5) + (0.5 * size.y * eye); \n" " else \n" " coord.x = (coord.x * 0.5) + (0.5 * size.x * eye); \n" " } \n" " gl_FragColor = texture2DRect(movie_texture, coord); \n" "}\n" }; osg::ref_ptr<osg::MatrixTransform> group = new osg::MatrixTransform; osg::ref_ptr<osg::Geode> geodeL = new osg::Geode; osg::ref_ptr<osg::Geode> geodeR = new osg::Geode; //get state for left geode osg::StateSet* stateset = geodeL->getOrCreateStateSet(); geodeL->setNodeMask(geodeL->getNodeMask() & ~(CULL_MASK_RIGHT)); stateset->addUniform(new osg::Uniform("eye",0)); //get state for right geode stateset = geodeR->getOrCreateStateSet(); geodeR->setNodeMask(geodeR->getNodeMask() & ~(CULL_MASK_LEFT)); geodeR->setNodeMask(geodeR->getNodeMask() & ~(CULL_MASK)); stateset->addUniform(new osg::Uniform("eye",1)); group->addChild(geodeR); group->addChild(geodeL); // add shader to group node osg::Program* program = new osg::Program; program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragShader)); // get name of file std::string name(filename); size_t found = filename.find_last_of("//"); if(found != filename.npos) { name = filename.substr(found + 1,filename.npos); } // create object to hold movie data struct VideoObject * currentobject = new struct VideoObject; currentobject->name = name; currentobject->stream = NULL; currentobject->scene = NULL; currentobject->firstPlay = false; currentobject->modeUniform = new osg::Uniform("mode",0); currentobject->typeUniform = new osg::Uniform("type",0); currentobject->splitUniform = new osg::Uniform("split",0); // set state parameters stateset = group->getOrCreateStateSet(); stateset->setAttribute(program); stateset->addUniform(new osg::Uniform("movie_texture",0)); stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); stateset->addUniform(currentobject->modeUniform); stateset->addUniform(currentobject->typeUniform); stateset->addUniform(currentobject->splitUniform); osg::Image* image = osgDB::readImageFile(filename.c_str()); osg::ImageStream* imagestream = dynamic_cast<osg::ImageStream*>(image); if (imagestream) { currentobject->stream = imagestream; currentobject->stream->pause(); osg::ImageStream::AudioStreams& audioStreams = currentobject->stream->getAudioStreams(); if ( !audioStreams.empty() ) { #ifdef FMOD_FOUND osg::AudioStream* audioStream = audioStreams[0].get(); audioStream->setAudioSink(new FmodAudioSink(audioStream)); currentobject->stream->setVolume(1.0); #endif } } if (image) { float width = image->s() * image->getPixelAspectRatio(); float height = image->t(); //check the ratio of the image float widthFactor = 1.0; if( (width / height) > 2.5 ) { widthFactor = 0.5; currentobject->splitUniform->set(1); // indicate double image } osg::ref_ptr<osg::Drawable> drawable = myCreateTexturedQuadGeometry(osg::Vec3(0.0,0.0,0.0), width * widthFactor, height,image); if (image->isImageTranslucent()) { drawable->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); drawable->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); } geodeR->addDrawable(drawable.get()); geodeL->addDrawable(drawable.get()); // set bound group->dirtyBound(); } else { printf("Unable to read file\n"); return false; } // add stream to the scene SceneObject * so = new SceneObject(name,false,false,false,true,true); PluginHelper::registerSceneObject(so,"OsgMovie"); so->addChild(group); so->attachToScene(); so->setNavigationOn(true); so->addMoveMenuItem(); so->addNavigationMenuItem(); currentobject->scene = so; // simple default menu MenuCheckbox * mcb = new MenuCheckbox("Play", false); // make toggle button mcb->setCallback(this); so->addMenuItem(mcb); _playMap[currentobject] = mcb; MenuButton * mrb = new MenuButton("Restart"); mrb->setCallback(this); so->addMenuItem(mrb); _restartMap[currentobject] = mrb; MenuCheckbox * scb = new MenuCheckbox("Stereo", false); // make toggle button scb->setCallback(this); so->addMenuItem(scb); _stereoMap[currentobject] = scb; MenuCheckbox * tbcb = new MenuCheckbox("TopBottom", false); // make toggle button tbcb->setCallback(this); so->addMenuItem(tbcb); _stereoTypeMap[currentobject] = tbcb; MenuRangeValue * ms = new MenuRangeValue("Scale", 0.0, 10.0, 1.0); // make video scale ms->setCallback(this); so->addMenuItem(ms); _scaleMap[currentobject] = ms; MenuButton * msab = new MenuButton("Save"); // make position and scale of video msab->setCallback(this); so->addMenuItem(msab); _saveMap[currentobject] = msab; MenuButton * mscb = new MenuButton("Load"); // load position of video mscb->setCallback(this); so->addMenuItem(mscb); _loadMap[currentobject] = mscb; MenuButton * mb = new MenuButton("Delete"); mb->setCallback(this); so->addMenuItem(mb); _deleteMap[currentobject] = mb; _loadedVideos.push_back(currentobject); return true; }
bool PointsOOC::loadFile(std::string filename) { osg::ref_ptr<Node> points = osgDB::readNodeFile(filename); if( points.valid() ) { osg::StateSet* state = points->getOrCreateStateSet(); // check to make sure that the osga is a point cloud (check for uniform) osg::Uniform* pointUniform = state->getUniform("point_size"); if( pointUniform != NULL ) { // check if see if this is the first set to be loaded if( _loadedPoints.size() == 0 ) { _startTime = CVRViewer::instance()->getFrameStamp()->getReferenceTime(); _currentFrame = CVRViewer::instance()->getFrameStamp()->getFrameNumber(); } // get fileName std::string name = osgDB::getStrippedName(filename); // add stream to the scene SceneObject * so = new SceneObject(name,false,false,false,true,true); PluginHelper::registerSceneObject(so,"PointsOOC"); so->addChild(points.get()); so->attachToScene(); so->setNavigationOn(true); so->addMoveMenuItem(); so->addNavigationMenuItem(); so->setShowBounds(false); // add submenu to control point size options SubMenu * sm = new SubMenu("Point Size Options"); so->addMenuItem(sm); _subMenuMap[so] = sm; // add menu items for controlling the points MenuCheckbox * cb = new MenuCheckbox("Show Bounds", false); cb->setCallback(this); so->addMenuItem(cb); _boundsMap[so] = cb; cb = new MenuCheckbox("Enable Shader", true); cb->setCallback(this); so->addMenuItem(cb); _shaderMap[so] = cb; // get point size float shaderSize; pointUniform->get(shaderSize); // access the alpha osg::Uniform* alphaUniform = state->getUniform("global_alpha"); // doesnt exist in table create a new entry if( _locInit.find(name) == _locInit.end() ) { Loc l; l.pos = osg::Matrix::identity(); l.pointSize = 1.0; l.shaderSize = shaderSize; l.shaderEnabled = true; l.pointAlpha = 1.0; l.pointFunc[0] = 1.0; l.pointFunc[1] = 0.05; l.pointFunc[2] = 0.0; _locInit[name] = l; } _locInit[name].shaderStateset = state; // create point stateset _locInit[name].pointStateset = new osg::StateSet(); _locInit[name].pointStateset->setMode(GL_POINT_SMOOTH, osg::StateAttribute::ON); _locInit[name].pointStateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF); _locInit[name].pointStateset->setMode(GL_BLEND, osg::StateAttribute::ON); _locInit[name].pointStateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); osg::Point* point = new osg::Point; point->setDistanceAttenuation(osg::Vec3(_locInit[name].pointFunc[0], _locInit[name].pointFunc[1], _locInit[name].pointFunc[2])); point->setSize(_locInit[name].pointSize); point->setMinSize(1.0); point->setMaxSize(100.0); _locInit[name].pointStateset->setAttribute(point); osg::PolygonMode *pm = new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::POINT ); _locInit[name].pointStateset->setAttribute(pm); // read it from the saved file pointUniform->set(_locInit[name].shaderSize); alphaUniform->set(_locInit[name].pointAlpha); // point variation float variation = _locInit[name].shaderSize * 0.75; MenuRangeValue * mrv = new MenuRangeValue("Point Size Shader", _locInit[name].shaderSize - variation, _locInit[name].shaderSize + variation, _locInit[name].shaderSize); mrv->setCallback(this); _sliderShaderSizeMap[so] = mrv; mrv = new MenuRangeValue("Point Size", 0.01, 50.0, _locInit[name].pointSize); mrv->setCallback(this); _sliderPointSizeMap[so] = mrv; mrv = new MenuRangeValue("Point Func", 0.0, 0.9, _locInit[name].pointFunc[1]); mrv->setCallback(this); _sliderPointFuncMap[so] = mrv; mrv = new MenuRangeValue("Alpha", 0.0, 1.0, _locInit[name].pointAlpha); mrv->setCallback(this); sm->addItem(mrv); _sliderAlphaMap[so] = mrv; // check if shader enabled if( !_locInit[name].shaderEnabled ) { _shaderMap[so]->setValue(false); points->setStateSet(_locInit[name].pointStateset); sm->addItem(_sliderPointSizeMap[so]); sm->addItem(_sliderPointFuncMap[so]); } else { sm->addItem(_sliderShaderSizeMap[so]); } // set default lighting and blend modes points->getStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); points->getStateSet()->setMode(GL_BLEND,osg::StateAttribute::OFF); //check pointAlpha if( _locInit[name].pointAlpha != 1.0 ) { points->getStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON); points->getStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON); points->getStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); } bool nav; nav = so->getNavigationOn(); so->setNavigationOn(false); so->setTransform(_locInit[name].pos); so->setNavigationOn(nav); // add default buttons MenuButton* mb = new MenuButton("Save"); mb->setCallback(this); so->addMenuItem(mb); _saveMap[so] = mb; mb = new MenuButton("Delete"); mb->setCallback(this); so->addMenuItem(mb); _deleteMap[so] = mb; // to list of objects _loadedPoints.push_back(so); return true; } } return false; }