日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

osg fbo(一),生成颜色缓冲区图片

發(fā)布時間:2024/1/1 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 osg fbo(一),生成颜色缓冲区图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

由于工作需要,重新?lián)炝讼聅hader。很明顯,fbo是重中之重。好記性不如爛筆頭,先記錄下

1,生成一個顏色紋理(為了省事,可以將紋理寬高=屏幕寬高)

osg::ref_ptr<osg::Texture2D> tex = createFloatRectangleTexture(texWidth, texHeight);

2,采樣攝像機添加場景根,并把場景根的顏色緩沖區(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)的紋理,由于是在攝像機坐標系下,所以要規(guī)格化坐標系,位置【-1,1】
紋理坐標【0,1】

osg::ref_ptr<osg::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));osg::ref_ptr<osg::Vec2Array> texCoord = new osg::Vec2Array; texCoord->push_back(osg::Vec2(0.0, 0.0)); texCoord->push_back(osg::Vec2(1.0, 0.0)); texCoord->push_back(osg::Vec2(1.0, 1.0)); texCoord->push_back(osg::Vec2(0.0, 1.0));

4,三維轉(zhuǎn)二維,就是貼圖片的過程,FBO中的圖片一般還要進行一個圖像處理,所以要設(shè)置一個passRoot,用于顯示。這個passroot要同時包含采樣攝像機和面片才行。

passRoot->addChild(sampleCamera); //將攝像機加入場景 passRoot->addChild(panelGeode); viewer->setSceneData(passRoot);

運行結(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));

osg::ref_ptr<osg::Vec2Array> texCoord = new osg::Vec2Array; texCoord->push_back(osg::Vec2(0.0, 0.0)); texCoord->push_back(osg::Vec2(1.0, 0.0)); texCoord->push_back(osg::Vec2(1.0, 1.0)); texCoord->push_back(osg::Vec2(0.0, 1.0));osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; geom->setVertexArray(vertices); geom->setTexCoordArray(0, texCoord); geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));osg::ref_ptr<osg::Geode> geode = new osg::Geode; geode->addDrawable(geom); osg::ref_ptr<osg::StateSet> set1 = geode->getOrCreateStateSet(); set1->setMode(GL_LIGHTING, osg::StateAttribute::OFF); //設(shè)置不受光照影響,不然太暗了就看不清楚 return geode;

}

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);

//獲取系統(tǒng)分辨率 unsigned int screenWidth, screenHeight; osg::GraphicsContext::WindowingSystemInterface * wsInterface = osg::GraphicsContext::getWindowingSystemInterface(); if (!wsInterface) {return -1; } wsInterface->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), screenWidth, screenHeight); int texWidth = screenWidth; int texHeight = screenHeight; osg::ref_ptr<osg::Texture2D> tex = createFloatRectangleTexture(texWidth, texHeight); //綁定采樣攝像機 osg::ref_ptr<osg::Camera> sampleCamera = new osg::Camera; {sampleCamera->addChild(sceneRoot);sampleCamera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));sampleCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); //這句話使內(nèi)容不渲染到屏幕上sampleCamera->attach(osg::Camera::COLOR_BUFFER0, tex); //關(guān)聯(lián)顏色貼圖//攝像機關(guān)聯(lián)視口sampleCamera->setViewport(0, 0, screenWidth, screenHeight);} osg::ref_ptr<osg::Geode> panelGeode = createTexturePanelGeode(); osg::ref_ptr<osg::StateSet> ss = panelGeode->getOrCreateStateSet(); ss->setTextureAttributeAndModes(0, tex);

;
passRoot->addChild(sampleCamera); //將攝像機加入場景
passRoot->addChild(panelGeode);
viewer->setSceneData(passRoot);
viewer->run();
return 0;
}

總結(jié)

以上是生活随笔為你收集整理的osg fbo(一),生成颜色缓冲区图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。