osg::Sphere,osg::Texture2D,osg::StateSet,osg::TexEnv的使用

//By smells2 at Lab 2012-02-21
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
int main()
{
	osg::ref_ptr root = new osg::Group;
	osgViewer::Viewer myViewer;

	//Declear Sphere instance,the constructor takes an osg::vec3 to define
	//center and a float to define the radius.
	//Then we declear the ShapDrawable instance that derive from Drawable .We should 
	//initalize it with the shape we create above .
	osg::ref_ptr unitSphere = new osg::Geode;
	osg::ref_ptr sphere = new osg::Sphere(osg::Vec3(0,0,0), 1.0f);
	osg::ref_ptr shapeDrawable = new osg::ShapeDrawable(sphere.get());
	unitSphere->addDrawable(shapeDrawable.get());
	osg::ref_ptr sphereForm = new osg::PositionAttitudeTransform;
	sphereForm->setPosition(osg::Vec3(2.5,0.0,0.0));
	sphereForm->addChild(unitSphere.get());
	root->addChild(sphereForm.get());
	
	//load image from the file
	osg::ref_ptr earthTexture = new osg::Texture2D;
	earthTexture->setDataVariance(osg::Object::DYNAMIC);
	osg::ref_ptr earthImage = osgDB::readImageFile("D:\\OSG\\osg2.9\\OpenSceneGraph2.9.5\\data\\Images\\land_shallow_topo_2048.jpg");
	if (!earthImage.get())
	{
		std::cout<<"load texture failed !"<setImage(earthImage.get());

	//set the stateset for decal texture
	osg::ref_ptr texenv = new osg::TexEnv;
	osg::ref_ptr stateset = new osg::StateSet;
	texenv->setMode(osg::TexEnv::DECAL);
	stateset->setTextureAttributeAndModes(0,earthTexture.get(),osg::StateAttribute::ON);
	stateset->setTextureAttribute(0,texenv.get());

	//realize
	root->setStateSet(stateset.get());
	myViewer.setSceneData(root.get());
	myViewer.realize();
	myViewer.run();
}

你可能感兴趣的:(osg学习)