osg设置透明

来源:互联网 发布:粤语搞笑网名 知乎 编辑:程序博客网 时间:2024/06/10 21:17

两种方法,不同内容:


//自定义绘制模型可以使用透明的颜色,材质的混合光中中采用透明的颜色,在绘制模型是开启透明模式。#if 0#include <Windows.h>#include <osgViewer/Viewer>#include <osgDB/ReadFile>#include <osg/Node>#include <osg/Geode>#include <osg/Geometry>#include <osgGA/GUIEventAdapter>#include <osgViewer/ViewerEventHandlers>#include <osg/StateSet>#include <osg/ShapeDrawable>#include <osg/Material>#include <osg/Image>#include <osg/Texture2D>#include <osg/LineWidth>#include <iostream>osg::ref_ptr<osg::Geode> CreatBox(){osg::ref_ptr<osg::Geode> geode = new osg::Geode();osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0,0,0),1,10,10),hints);osg::ref_ptr<osg::Material> material = new osg::Material;osg::ref_ptr<osg::Texture2D> text =new osg::Texture2D;osg::ref_ptr<osg::Image> image;//设置颜色(设置一个alpha通道,当覆盖纹理后,该值不重要,没有纹理时颜色受该值影响)shape->setColor(osg::Vec4(1.0,1.0,0.0,0.3));//设置精度,精度越大越精细hints->setDetailRatio(0.5);//设置材质的光照颜色//material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,0.0,0.0,1.0));//设置材质的混合光颜色material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,0.0,0.3));//设置镜面反射光颜色//material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,0.0,0.0,1.0));//设置影像点的大小material->setShininess(osg::Material::FRONT_AND_BACK,6.0);//读取图片设置为纹理,文件需放在:当前工程目录(即debug文件夹下)的上一个目录下image = osgDB::readImageFile("C:\\Texture\\texture\\wall_cizhuan.bmp");if(image->valid()){text->setImage(image);}//设置材质将透明打开geode->getOrCreateStateSet()->setAttributeAndModes(material,osg::StateAttribute::ON);//设置透明效果geode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);//geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,text,osg::StateAttribute::ON);//关灯geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON);geode->addDrawable(shape);return geode;}int main(int argc,char** argv){osgViewer::Viewer view;osg::Group* root = new osg::Group();root->addChild(osgDB::readNodeFile("glider.osg"));root->addChild(CreatBox());view.setSceneData(root);view.run();}#endif


//从外部导入的模型,部分模型可以像自定义一样设置透明,即通过设置材质来实现,部分可以通过下例中方法进行透明设置#if 1#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"));//方法1osg::StateSet* state = root->getOrCreateStateSet();state->setMode(GL_BLEND,osg::StateAttribute::ON);osg::ref_ptr<osg::Material> mat = new osg::Material;//漫发射光osg::Vec4 destDiffuse;destDiffuse.set(1.0,1.0,1.0,0.5);mat->setDiffuse(osg::Material::FRONT_AND_BACK,destDiffuse);//环境光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);//方法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));*/view.setSceneData(root);view.run();}#endif





0 0
原创粉丝点击