查看: 1320|回复: 1

绘制透明纹理

[复制链接]

该用户从未签到

发表于 2016-3-17 19:45:27 | 显示全部楼层 |阅读模式
#if def_DEBUG
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgDBb.lib")
#pragma comment(lib,"OpenThreadsd.lib")
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgGAd.lib")
#else
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgGA.lib")
#endif
#include<osgViewer/Viewer>
#include<osgDB/ReadFile>
#include<osgGA/GUIEventAdapter>
#include<osgViewer/ViewerEventHandlers>
#include<osg/StateSet>
#include<osg/Geode>
#include<osg/ShapeDrawable>
#include<osg/Material>
#include<osg/Image>
#include<osg/Texture2D>
#include<osg/LineWidth>
#include<iostream>
osg::ref_ptr<osg::Geode>CreateBox()
{
        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.0,0.0),1.0,10.0,10.0),hints.get());
        osg::ref_ptr<osg::Material>  material=new osg::Material;
        osg::ref_ptr<osg::Texture2D> texture2D=new osg::Texture2D;
        osg::ref_ptr<osg::Image> image;

        //设置颜色
        shape->setColor(osg::Vec4(0.9,0.5,0.5,0.1));
        //设置精准度
    hints->setDetailRatio(0.5);
        //设置材质
        material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4f(1.0,1.0,1.0,1.0));
        material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4f(2.0,2.0,2.0,1.0));
        material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4f(1.0,0.0,0.0,1.0));
        material->setShininess(osg::Material::FRONT_AND_BACK,20.0);
        //设置纹理
        image=osgDB::readImageFile("Images/whitemetal_diffuse.jpg");
        if(image.valid())
        {
                texture2D->setImage(image.get());
        }
        //set state
        geode->getOrCreateStateSet()->setAttributeAndModes(material.get(),osg::StateAttribute::ON);
        geode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
        geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);
        geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture2D.get(),osg::StateAttribute::ON);
        geode->addDrawable(shape.get());
        return geode;
}
int main ()
{
        osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
        osg::ref_ptr<osg::Group> group=new osg::Group;
        group->addChild(osgDB::readNodeFile("glider.osg"));
        group->addChild( CreateBox());
        viewer->setSceneData(group.get());
        return viewer->run();
}

该用户从未签到

发表于 2017-8-14 17:16:48 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表