#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();
}
OSG 屏幕截图
最新推荐文章于 2024-06-22 21:03:04 发布