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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

OSG对象设置透明

發布時間:2023/12/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OSG对象设置透明 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

osg 打開透明度

#include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osgViewer/ViewerEventHandlers> #include <osg/StateSet> #include <osg/ShapeDrawable> #include <osg/Material> #include <osg/BlendColor> #include <osg/BlendFunc> #include <osg/Node> #include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osg/ShapeDrawable> osg::ref_ptr<osg::Node>createBoxA() {osg::ref_ptr<osg::Geode>gnode = new osg::Geode;osg::ref_ptr<osg::ShapeDrawable>sd = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, -10, 0), 15, 2, 14));gnode->addDrawable(sd.get());sd->setColor(osg::Vec4(0, 0., 0.5, 0.3f));gnode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);gnode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);return gnode; } osg::ref_ptr<osg::Node>createBoxB() {osg::ref_ptr<osg::Geode>geode = new osg::Geode;osg::ref_ptr<osg::ShapeDrawable>sd = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, 10, 0), 10, 2, 15));geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);geode->addDrawable(sd);sd->setColor(osg::Vec4(1, 0, 1.0, 0.3));return geode; }int main(int argc, char *argv[]) {osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer;osg::ref_ptr<osg::Group>root = new osg::Group;root->addChild(osgDB::readNodeFile("cow.osg"));root->addChild(createBoxB());root->addChild(createBoxA());viewer->setSceneData(root.get());return viewer->run(); }

效果如下:

從外部導入的模型,有兩種方法來設置透明,一種是材質,一種是混合

#include <Windows.h> #include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osgViewer/ViewerEventHandlers> #include <osg/StateSet> #include <osg/ShapeDrawable> #include <osg/Material> #include <osg/BlendColor> #include <osg/BlendFunc>int main(int argc,char** argv) { osgViewer::Viewer view; osg::Group* root = new osg::Group(); root->addChild(osgDB::readNodeFile("cow.osg")); //方法1 osg::StateSet* state = root->getOrCreateStateSet(); state->setMode(GL_BLEND,osg::StateAttribute::ON); osg::ref_ptr<osg::Material> mat = new osg::Material; //漫發射光 mat->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,0.5)); //環境光 mat->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,0.5)); //設置材質的混合光顏色 mat->setTransparency(osg::Material::FRONT_AND_BACK,0.5); state->setAttributeAndModes(mat,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); view.setSceneData(root); view.run(); }

//方法2,使用混合函數來設置透明?

int main(int argc,char** argv) { //方法2 osg::StateSet* state = root->getOrCreateStateSet(); //關閉燈光 state->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED); //打開混合融合模式 state->setMode(GL_BLEND,osg::StateAttribute::ON); state->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON); state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); //使用BlendFunc實現透明效果 osg::BlendColor* bc =new osg::BlendColor(osg::Vec4(1.0,1.0,1.0,0.0)); osg::BlendFunc*bf = new osg::BlendFunc(); state->setAttributeAndModes(bf,osg::StateAttribute::ON); state->setAttributeAndModes(bc,osg::StateAttribute::ON); bf->setSource(osg::BlendFunc::CONSTANT_ALPHA); bf->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA); bc->setConstantColor(osg::Vec4(1,1,1,0.5)); }

效果如下:

網格對象的透明設置:

//方法1

osg::ref_ptr<osg::Material> mat = new osg::Material;

mat->setDiffuse(osg::Material::Front_AND_BACK,osg::Vec4f(1,1,1,0.5));

mat->setAmbient(osg::Material::Front_AND_BACK,osg::Vec4f(1,1,1,0.5));
state->setAttributeAndModes(mat,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); ?//一定要加上這句話,否則網格內部對象看不見


//方法2,這種方法對于網格效果不好,不能關閉光照
osg::StateSet* state = root->getOrCreateStateSet();
state->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED);
//打開混合融合模式
state->setMode(GL_BLEND,osg::StateAttribute::ON);
state->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);
state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
//使用BlendFunc實現透明效果
osg::BlendColor* bc =new osg::BlendColor(osg::Vec4(1.0,1.0,1.0,0.0));
osg::BlendFunc*bf = new osg::BlendFunc();
state->setAttributeAndModes(bf,osg::StateAttribute::ON);
state->setAttributeAndModes(bc,osg::StateAttribute::ON);
bf->setSource(osg::BlendFunc::CONSTANT_ALPHA);
bf->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
bc->setConstantColor(osg::Vec4(1,1,1,0.5));

?

?

基本幾何體的透明度設置:?

使用OSG中自定義的基本幾何體,并設置其透明的效果和網格模型,以圓錐為例。

?

首先創建圓錐:? ??

osg::ref_ptr<osg::Geode> geode=new osg::Geode;//生成圓錐m_pCone=new osg::Cone;m_pCone->setHeight(30);m_pCone->setRadius(30);osg::ref_ptr<osg::ShapeDrawable> shap=new osg::ShapeDrawable(m_pCone);//第四個參數0.25表示不透明度,0表示完全透明,1表示完全不透明shap->setColor(osg::Vec4(0.4,0.8,0.4,0.25));geode->addDrawable(shap);</span>

?接下來設置透明效果和網格模型:?

osg::ref_ptr<osg::StateSet> stateset=geode->getOrCreateStateSet();stateset->setMode(GL_BLEND,osg::StateAttribute::ON);stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);//設置網格模型osg::ref_ptr<osg::PolygonMode> polyMode=new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);stateset->setAttribute(polyMode);

? 然后就可以使用geode這個節點了。

? ? ? ?需要注意的是 從這個例子中可以看出OSG中各個節點的屬性設置是在與這個節點相關聯的osg::StateSet對象中定義的,之前想設置線框模型時一直在osg::Cone和osg::ShapeDrawable中尋找相關的函數,但是一直沒找到。這也加深了對OSG中場景樹和渲染樹的理解。

? ? ? ? 還有一點需要注意的就是透明效果不能只在osg::Shape的setColor中設置不透明度,這樣好像也不能看到透明效果,還需要在osg::StateSet中設置相關的模式,這是由于OpenGL狀態機模型決定的,不要忘了這個地方的設置。

?

總結

以上是生活随笔為你收集整理的OSG对象设置透明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。