OSG物件設定透明

2021-07-06 05:40:15 字數 3078 閱讀 2163

osg 開啟透明度

[cpp]view plain

copy

print?

#include 

#include 

#include 

#include 

osg::ref_ptrcreateboxa()  

osg::ref_ptrcreateboxb()  

intmain(

intargc, 

char

*argv)    

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

#include #include #include #include #include #include #include #include #include int main(int argc,char** argv)

//方法2,使用混合函式來設定透明

int main(int argc,char** argv)

網格物件的透明設定:

//方法1

osg::ref_ptrmat = 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_ptrgeode=new osg::geode;

//生成圓錐

m_pcone=new osg::cone;

m_pcone->setheight(30);

m_pcone->setradius(30);

osg::ref_ptrshap=new osg::shapedrawable(m_pcone);

//第四個引數0.25表示不透明度,0表示完全透明,1表示完全不透明

shap->setcolor(osg::vec4(0.4,0.8,0.4,0.25));

geode->adddrawable(shap);

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

//設定幾何體透明效果

osg::ref_ptrstateset=geode->getorcreatestateset();

stateset->setmode(gl_blend,osg::stateattribute::on);

stateset->setrenderinghint(osg::stateset::transparent_bin);

//設定網格模型

osg::ref_ptrpolymode=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實現透明效果 正解

最近某個專案中要用osg畫部分半透明效果的形狀,繪製可參考osg自帶例子 examples osggeometry 但是半透明效果一直出不來,google一番之後 有位熱心提供的方案是 osg stateset stateset geometryfeild getorcreatestateset a...

osg中實現半透明效果

在為球貼圖的基礎上,實現模擬地球的透明展示。是先前效果如下 實現後效果如下 實現 如下,主要是在狀態集中進行設定 osg ref ptrblendfunc new osg blendfunc blendfunc setsource osg blendfunc src alpha blendfunc ...

gvim透明設定

週末學弟學妹過來比賽,發現清一色的用透明vim,感覺非常6,回來試了一下win下將gvim設定成透明。首先在然後在 vimrc中加入下面的 就ok了 if executable vimtweak.dll autocmd guienter call libcallnr vimtweak setalph...