osg loads video to specified location

Keywords: Mobile

osg loads video to specified location


Principle: Read the video stream using the ffmpeg plug-in and paste it on the graphical geometry as a texture, which sets the shape and coordinates.

1. Compile ffmpeg plug-ins

Reference resources: http://www.cnblogs.com/coolbear/p/8548440.html
Here I recompile osg as a triplet library, and if osgdb_ffmpeg d.dll appears, it compiles successfully.(in osgPlugins)
After successful compilation, place both ffmepg's DLL and osgdb_ffmpegd.dll in the osg source compiled bin directory.


You can also download my compiled ffmpeg plugin directly~
Compiled in VS2013 & osg3.4 environment, do not know if other environment can be used.
https://download.csdn.net/download/lemon_haha/11062017

2. Code

  • Test if videos can be loaded
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/ImageStream>
#include <osg/Texture2D>

int main()
{
	//Register Plugin
	osgDB::Registry::instance()->addFileExtensionAlias("avi", "ffmpeg");
	osg::ref_ptr<osg::Image> image;

	//Local video (choose your own video path)
	image = osgDB::readImageFile("aaa.avi");
	osg::ImageStream* imageStream = dynamic_cast<osg::ImageStream*>(image.get());
	if (imageStream)
		imageStream->play();

	//Add to Quadrilateral
	osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
	texture->setImage(image.get());
	osg::ref_ptr<osg::Drawable> quad = osg::createTexturedQuadGeometry(
		osg::Vec3(), osg::Vec3(-1.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 1.0f));//
	quad->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture.get());

	//Add to geode
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	geode->addDrawable(quad.get());
	geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);

	//Add to Scene
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	viewer->setSceneData(geode.get());
	viewer->run();
}

  • Load video to specified location
    Modify the Add to Quadrilateral section of the above code to the following code.Then set the vertex coordinates yourself.
    Here I select points in the scene to save coordinates and read them, or I can set vertex coordinates directly:)
    The order in which vertices are selected is counterclockwise, with the first selected from the upper left corner (0, 1).
    (The vertex correspondence between video and texture is questionable here, if you don't want to try to modify the vertex order yourself.)
    //Add to Quadrilateral
	//=============================Set vertex coordinates here====================================
	osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
	ifstream inFile;
	inFile.open("cameraVideoPoint.txt");
	for (int i = 0; i < 4; i++)
	{
		double x, y, z;
		inFile >> x >> y >> z;
		v->push_back(osg::Vec3(x, y, z+0.3));
	}
	//=============================Set vertex coordinates here====================================
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
	geom->setVertexArray(v.get());
	geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON, 0, 4));

	//Setting texture coordinates
	osg::ref_ptr<osg::Vec2Array> tcoords = new osg::Vec2Array();
	//Loading out doesn't know why it's the opposite, so I change to clockwise
	tcoords->push_back(osg::Vec2(0.0f, 0.0f));
	tcoords->push_back(osg::Vec2(0.0f, 1.0f));
	tcoords->push_back(osg::Vec2(1.0f, 1.0f));
	tcoords->push_back(osg::Vec2(1.0f, 0.0f));

	geom->setTexCoordArray(0, tcoords);
	geom->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture.get());
	
	geode->addDrawable(geom);//Gede's drawable changes

Finally, it may be inconvenient for me to post the picture. ~Welcome to the discussion on the correspondence between vertices and textures:)

Posted by burhankhan on Tue, 14 May 2019 12:30:30 -0700