OSG 屏幕截图

#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osg/MatrixTransform>
#include <QImage>

osg::ref_ptr <osg::MatrixTransform> getAxisNode()
{
	//坐标轴
	int scale = 10;
	osg::ref_ptr<osg::Node> m_rpAxisNode = osgDB::readNodeFile("D:/workspace/osg_test/osg_test/axes.osgt");
	osg::ref_ptr<osg::MatrixTransform> axisMatTrans = new osg::MatrixTransform;
	axisMatTrans->addChild(m_rpAxisNode);
	axisMatTrans->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);
	axisMatTrans->setMatrix(osg::Matrix::scale(scale, scale, scale) * osg::Matrix::translate(0.0, 0.0, 0.0));
	return axisMatTrans.release();
}

osg::ref_ptr<osg::Geode> getVertexGeometryNode()
{
	osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
	osg::ref_ptr<osg::Vec3Array> vertexArray = new osg::Vec3Array;
	osg::ref_ptr<osg::Vec4Array> colorArray = new osg::Vec4Array;
	osg::ref_ptr<osg::Vec3Array> normalArray = new osg::Vec3Array;
	vertexArray->push_back(osg::Vec3(1, 1, 0));
	vertexArray->push_back(osg::Vec3(0.5, 1, 0));
	vertexArray->push_back(osg::Vec3(0.5, 0.5, 0));
	vertexArray->push_back(osg::Vec3(0.75, 0.5, 0));

	for (int i = 0; i < vertexArray->size(); ++i)
	{
		if (i == 3)
			colorArray->push_back(osg::Vec4(0.0, 1.0, 0.0, 1.0));
		else
			colorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
		normalArray->push_back(osg::Vec3(0.0, 0.0, 1.0));
	}

	geometry->setVertexArray(vertexArray);
	geometry->setColorArray(colorArray, osg::Array::BIND_PER_VERTEX);
	geometry->addPrimitiveSet(new osg::DrawArrays(GL_QUAD_STRIP, 0, vertexArray->size()));

	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	geode->addChild(geometry);
	return geode.release();
}

osg::ref_ptr<osg::Image> image_c = new osg::Image;
struct CaptureDrawCallback : public osg::Camera::DrawCallback
{
	CaptureDrawCallback(osg::ref_ptr<osg::Image> image)
	{
		_image = image;
	}
	~CaptureDrawCallback() {}

	virtual void operator()(const osg::Camera& camera)const
	{
		//得到窗口系统接口
		osg::ref_ptr<osg::GraphicsContext::WindowingSystemInterface> wsi = osg::GraphicsContext::getWindowingSystemInterface();
		unsigned int width = 800, height = 500;
		//得到分辨率
		wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);

		//分配一个image
		_image->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE);
		//读取像素信息抓图
		_image->readPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE);
	}
	osg::ref_ptr<osg::Image> _image;
};

class ImageHandler :public osgGA::GUIEventHandler
{
public:
	ImageHandler() {}
	~ImageHandler() {}

	bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
	{
		osg::ref_ptr<osgViewer::Viewer> viewer = dynamic_cast<osgViewer::Viewer *>(&aa);
		if (viewer == NULL)
			return false;
		static int _screenCaptureSequence = 0;
		switch (ea.getEventType())
		{
		case osgGA::GUIEventAdapter::KEYDOWN:
		{
			if (ea.getKey() == 'c' || ea.getKey() == 'C')
			{
				char filename[128];
				sprintf(filename, "ScreenShot%4d.png", _screenCaptureSequence);
				_screenCaptureSequence++;
				osgDB::writeImageFile(*(image_c.get()), filename);

				QImage image(filename);
				QString path = QString("C:/Users/99544/Desktop/img/%1.png").arg(_screenCaptureSequence);
				bool f = image.save(path, "PNG");
			}
		}
		break;
		default:

			return false;
		}
		return true;
	}
};


int main(int argc, char *argv[])
{
	osg::ref_ptr<osgViewer::Viewer> rpViewer = new osgViewer::Viewer;

	osg::ref_ptr<osg::Group> rpGroup = new osg::Group;
	rpGroup->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
	//rpGroup->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);

	rpGroup->addChild(getAxisNode());
	rpGroup->addChild(getVertexGeometryNode());

	rpViewer->setSceneData(rpGroup.get());
	rpViewer->getCamera()->setPostDrawCallback(new CaptureDrawCallback(image_c.get()));
	rpViewer->addEventHandler(new ImageHandler());//截图  快捷键 c
	return rpViewer->run();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值