|
#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();
}
|
|