osg fbo(一),生成颜色缓冲区图片
由于工作需要,重新?lián)炝讼聅hader。很明顯,fbo是重中之重。好記性不如爛筆頭,先記錄下
1,生成一個顏色紋理(為了省事,可以將紋理寬高=屏幕寬高)
osg::ref_ptr<osg::Texture2D> tex = createFloatRectangleTexture(texWidth, texHeight);2,采樣攝像機(jī)添加場景根,并把場景根的顏色緩沖區(qū)與紋理關(guān)聯(lián),
sampleCamera->addChild(sceneRoot);sampleCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); //這句話使內(nèi)容不渲染到屏幕上sampleCamera->attach(osg::Camera::COLOR_BUFFER0, tex); //關(guān)聯(lián)顏色貼圖3,將紋理關(guān)聯(lián)到面片上,以方便加載使用
osg::ref_ptr<osg::Geode> panelGeode = createTexturePanelGeode(); osg::ref_ptr<osg::StateSet> ss = panelGeode->getOrCreateStateSet(); ss->setTextureAttributeAndModes(0, tex);4,這個面片關(guān)聯(lián)的紋理,由于是在攝像機(jī)坐標(biāo)系下,所以要規(guī)格化坐標(biāo)系,位置【-1,1】
紋理坐標(biāo)【0,1】
4,三維轉(zhuǎn)二維,就是貼圖片的過程,FBO中的圖片一般還要進(jìn)行一個圖像處理,所以要設(shè)置一個passRoot,用于顯示。這個passroot要同時包含采樣攝像機(jī)和面片才行。
passRoot->addChild(sampleCamera); //將攝像機(jī)加入場景 passRoot->addChild(panelGeode); viewer->setSceneData(passRoot);運(yùn)行結(jié)果如下:
代碼如下:
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>
#include <osgGA/Device>
#include <osg/Shader>
osg::ref_ptrosg::Texture2D createFloatRectangleTexture(int width, int height)
{
osg::ref_ptrosg::Texture2D tex2D = new osg::Texture2D;
tex2D->setTextureSize(width, height);
tex2D->setInternalFormat(GL_RGBA16F_ARB);
tex2D->setSourceFormat(GL_RGBA);
tex2D->setSourceType(GL_FLOAT);
return tex2D.release();
}
osg::ref_ptrosg::Geode createTexturePanelGeode()
{
osg::ref_ptrosg::Vec3Array vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(-1.0f, -1.0f, 0.0f));
vertices->push_back(osg::Vec3(1.0f, -1.0f, 0.0f));
vertices->push_back(osg::Vec3(1.0f, 1.0f, 0.0f));
vertices->push_back(osg::Vec3(-1.0f, 1.0f, 0.0f));
}
int main()
{
osg::ref_ptrosgViewer::Viewer viewer = new osgViewer::Viewer;
std::string strFileName = “D:/OpenSceneGraph-master/OpenSceneGraph-Data-master/cow.osg”;
//pass1的根
osg::ref_ptrosg::Group passRoot = new osg::Group();
//場景根
osg::ref_ptrosg::Group sceneRoot = new osg::Group();
osg::ref_ptrosg::Node node = osgDB::readNodeFile(strFileName);
sceneRoot->addChild(node);
;
passRoot->addChild(sampleCamera); //將攝像機(jī)加入場景
passRoot->addChild(panelGeode);
viewer->setSceneData(passRoot);
viewer->run();
return 0;
}
總結(jié)
以上是生活随笔為你收集整理的osg fbo(一),生成颜色缓冲区图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【干货:Excel中插入图片的两种方式】
- 下一篇: 视频教程-扣丁学堂Python基础视频教